2026-04-09 22:00:53 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
/**
|
|
|
|
|
* This file is part of the SplendidBear Websites' projects.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2019 @ www.splendidbear.org
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
|
|
use App\Util\RpcManager;
|
|
|
|
|
use App\Util\TopicManager;
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2026-04-10 12:33:38 +02:00
|
|
|
use Symfony\Component\Routing\Attribute\Route;
|
2026-04-09 22:00:53 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class MercureController
|
|
|
|
|
*
|
|
|
|
|
* Handles HTTP API endpoints that replace the former WebSocket RPC and Topic handlers.
|
|
|
|
|
* Client → Server communication is via HTTP POST/GET.
|
|
|
|
|
* Server → Client broadcasting is via Mercure (SSE).
|
|
|
|
|
*
|
|
|
|
|
* @package App\Controller
|
|
|
|
|
* @author Lang <https://www.splendidbear.org>
|
|
|
|
|
* @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,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-10 12:33:38 +02:00
|
|
|
#[Route('/api/game/start', name: 'MineSeekerBundle_api_game_start', methods: ['POST'])]
|
2026-04-09 22:00:53 +02:00
|
|
|
public function start(Request $request): JsonResponse
|
|
|
|
|
{
|
2026-04-10 12:23:21 +02:00
|
|
|
$data = $request->toArray();
|
|
|
|
|
$result = $this->rpcManager->saveGrid($data['gameAssoc']);
|
2026-04-09 22:00:53 +02:00
|
|
|
|
|
|
|
|
return $this->json(['success' => $result]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-10 12:33:38 +02:00
|
|
|
#[Route('/api/game/connect/{gameAssoc}', name: 'MineSeekerBundle_api_game_connect', methods: ['GET'])]
|
2026-04-09 22:00:53 +02:00
|
|
|
public function connect(string $gameAssoc): Response
|
|
|
|
|
{
|
|
|
|
|
$payload = $this->rpcManager->getConnectInformation($gameAssoc);
|
|
|
|
|
|
|
|
|
|
return new Response($payload, Response::HTTP_OK, ['Content-Type' => 'text/plain']);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-10 12:33:38 +02:00
|
|
|
#[Route('/api/game/join/{gameAssoc}', name: 'MineSeekerBundle_api_game_join', methods: ['POST'])]
|
2026-04-09 22:00:53 +02:00
|
|
|
public function join(string $gameAssoc, Request $request): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$this->topicManager->subscribe($gameAssoc, $this->resolveUserName($request), $this->getUser());
|
|
|
|
|
|
|
|
|
|
return $this->json(['success' => true]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-10 12:33:38 +02:00
|
|
|
#[Route('/api/game/step/{gameAssoc}', name: 'MineSeekerBundle_api_game_step', methods: ['POST'])]
|
2026-04-09 22:00:53 +02:00
|
|
|
public function step(string $gameAssoc, Request $request): JsonResponse
|
|
|
|
|
{
|
2026-04-10 12:23:21 +02:00
|
|
|
$result = $this->topicManager->publish($gameAssoc, $this->resolveUserName($request), $request->toArray());
|
2026-04-09 22:00:53 +02:00
|
|
|
|
2026-04-10 12:23:21 +02:00
|
|
|
return $this->json($result);
|
2026-04-09 22:00:53 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-10 12:33:38 +02:00
|
|
|
#[Route('/api/game/leave/{gameAssoc}', name: 'MineSeekerBundle_api_game_leave', methods: ['POST'])]
|
2026-04-09 22:00:53 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|