src/Controller/PublicController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use App\Entity\World;
  8. use App\Service\WsServerHttpClient;
  9. class PublicController extends AbstractController
  10. {
  11.     public function __construct(
  12.         private readonly EntityManagerInterface $entityManager,
  13.         private readonly WsServerHttpClient $httpClient,
  14.     ) {}
  15.     #[Route('/'name'public_game'methods: ['GET'])]
  16.     public function game(): Response
  17.     {
  18.         return $this->render('public/game.html.twig', [
  19.             'name' => 'Чат',
  20.             'gameServerUrl' => $_ENV['GAME_WSSERVER_URL'],
  21.         ]);
  22.     }
  23.     #[Route('/invite/{roomId}'name'public_invite'methods: ['GET'])]
  24.     public function invite(?string $roomId): Response
  25.     {
  26.         try {
  27.             // Делаем запрос к серверу для получения информации о комнатах
  28.             $response $this->httpClient->request('GET''room/get');
  29.             $rooms $response->toArray();
  30.             
  31.             // Ищем комнату по roomId
  32.             $foundRoom null;
  33.             foreach ($rooms as $room) {
  34.                 if ($room['id'] === $roomId) {
  35.                     $foundRoom $room;
  36.                     break;
  37.                 }
  38.             }
  39.             
  40.             if (!$foundRoom) {
  41.                 throw $this->createNotFoundException('Room not found');
  42.             }
  43.             
  44.             // Получаем worldId из найденной комнаты
  45.             $worldId $foundRoom['worldId'] ?? null;
  46.             if (!$worldId) {
  47.                 throw $this->createNotFoundException('World ID not found in room');
  48.             }
  49.             
  50.             // Ищем мир в репозитории
  51.             $worldRepository $this->entityManager->getRepository(World::class);
  52.             $world $worldRepository->find($worldId);
  53.             if (!$world) {
  54.                 throw $this->createNotFoundException('World not found');
  55.             }
  56.             return $this->render('public/invite.html.twig', [
  57.                 'worldId' => $worldId,
  58.                 'worldName' => $world->getName(),
  59.                 'worldStyle' => $world->getPromts()['style'],
  60.                 'wsServerUrl' => $_ENV['STATUS_WSSERVER_URL'],
  61.             ]);
  62.         } catch (\Exception $e) {
  63.             throw $this->createNotFoundException('Room not found: ' $e->getMessage());
  64.         }
  65.     }
  66. }