* @category Class * @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License * @link www.splendidbear.org * @since 2026. 04. 09. */ class MercureController extends AbstractController { public function __construct( private readonly TopicManager $topicManager, private readonly RpcManager $rpcManager, ) { } #[Route('/api/game/start', name: 'MineSeekerBundle_api_game_start', methods: ['POST'])] public function start(Request $request): JsonResponse { $data = $request->toArray(); $result = $this->rpcManager->saveGrid($data['gameAssoc']); return $this->json(['success' => $result]); } #[Route('/api/game/connect/{gameAssoc}', name: 'MineSeekerBundle_api_game_connect', methods: ['GET'])] public function connect(string $gameAssoc): Response { $payload = $this->rpcManager->getConnectInformation($gameAssoc); return new Response($payload, Response::HTTP_OK, ['Content-Type' => 'text/plain']); } #[Route('/api/game/join/{gameAssoc}', name: 'MineSeekerBundle_api_game_join', methods: ['POST'])] public function join(string $gameAssoc, Request $request): JsonResponse { $this->topicManager->subscribe($gameAssoc, $this->resolveUserName($request), $this->getUser()); return $this->json(['success' => true]); } #[Route('/api/game/step/{gameAssoc}', name: 'MineSeekerBundle_api_game_step', methods: ['POST'])] public function step(string $gameAssoc, Request $request): JsonResponse { $result = $this->topicManager->publish($gameAssoc, $this->resolveUserName($request), $request->toArray()); return $this->json($result); } #[Route('/api/game/leave/{gameAssoc}', name: 'MineSeekerBundle_api_game_leave', methods: ['POST'])] public function leave(string $gameAssoc, Request $request): JsonResponse { $this->topicManager->unSubscribe($gameAssoc, $this->resolveUserName($request)); return $this->json(['success' => true]); } private function resolveUserName(Request $request): string { $user = $this->getUser(); if (null !== $user) { return $user->getUserIdentifier(); } $sessionId = $request->getSession()->getId(); if (empty($sessionId)) { $sessionId = bin2hex(random_bytes(16)); } return 'anon_' . $sessionId; } }