<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\World;
use App\Service\WsServerHttpClient;
class PublicController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly WsServerHttpClient $httpClient,
) {}
#[Route('/', name: 'public_game', methods: ['GET'])]
public function game(): Response
{
return $this->render('public/game.html.twig', [
'name' => 'Чат',
'gameServerUrl' => $_ENV['GAME_WSSERVER_URL'],
]);
}
#[Route('/invite/{roomId}', name: 'public_invite', methods: ['GET'])]
public function invite(?string $roomId): Response
{
try {
// Делаем запрос к серверу для получения информации о комнатах
$response = $this->httpClient->request('GET', 'room/get');
$rooms = $response->toArray();
// Ищем комнату по roomId
$foundRoom = null;
foreach ($rooms as $room) {
if ($room['id'] === $roomId) {
$foundRoom = $room;
break;
}
}
if (!$foundRoom) {
throw $this->createNotFoundException('Room not found');
}
// Получаем worldId из найденной комнаты
$worldId = $foundRoom['worldId'] ?? null;
if (!$worldId) {
throw $this->createNotFoundException('World ID not found in room');
}
// Ищем мир в репозитории
$worldRepository = $this->entityManager->getRepository(World::class);
$world = $worldRepository->find($worldId);
if (!$world) {
throw $this->createNotFoundException('World not found');
}
return $this->render('public/invite.html.twig', [
'worldId' => $worldId,
'worldName' => $world->getName(),
'worldStyle' => $world->getPromts()['style'],
'wsServerUrl' => $_ENV['STATUS_WSSERVER_URL'],
]);
} catch (\Exception $e) {
throw $this->createNotFoundException('Room not found: ' . $e->getMessage());
}
}
}