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;
|
|
|
|
|
|
2026-04-11 22:20:21 +02:00
|
|
|
use App\Entity\PlayedGame;
|
|
|
|
|
use App\Repository\PlayedGameRepository;
|
2026-04-19 18:04:01 +02:00
|
|
|
use App\Service\ResolveUserNamesService;
|
2026-04-09 22:00:53 +02:00
|
|
|
use App\Util\RpcManager;
|
|
|
|
|
use App\Util\TopicManager;
|
2026-04-20 11:10:00 +02:00
|
|
|
use DateTimeInterface;
|
|
|
|
|
use Exception;
|
2026-04-09 22:00:53 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2026-04-12 08:01:46 +02:00
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController;
|
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.
|
|
|
|
|
*/
|
2026-04-12 08:01:46 +02:00
|
|
|
#[AsController]
|
2026-04-09 22:00:53 +02:00
|
|
|
class MercureController extends AbstractController
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
2026-04-19 18:04:01 +02:00
|
|
|
private readonly TopicManager $topicManager,
|
|
|
|
|
private readonly RpcManager $rpcManager,
|
|
|
|
|
private readonly ResolveUserNamesService $userNamesService,
|
2026-04-09 22:00:53 +02:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
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-20 11:10:00 +02:00
|
|
|
try {
|
|
|
|
|
$data = $request->toArray();
|
|
|
|
|
$result = $this->rpcManager->saveGrid($data['gameAssoc']);
|
|
|
|
|
|
|
|
|
|
return $this->json(['success' => $result]);
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
return $this->json(
|
|
|
|
|
['success' => false, 'error' => 'Failed to start game: ' . $e->getMessage()],
|
|
|
|
|
Response::HTTP_INTERNAL_SERVER_ERROR
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-09 22:00:53 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2026-04-19 18:04:01 +02:00
|
|
|
try {
|
|
|
|
|
$payload = $this->rpcManager->getConnectInformation($gameAssoc);
|
|
|
|
|
return new Response($payload, Response::HTTP_OK, ['Content-Type' => 'text/plain']);
|
2026-04-20 11:10:00 +02:00
|
|
|
} catch (Exception $e) {
|
2026-04-19 18:04:01 +02:00
|
|
|
return new Response('', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
|
}
|
2026-04-09 22:00:53 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-10 12:33:38 +02:00
|
|
|
#[Route('/api/game/join/{gameAssoc}', name: 'MineSeekerBundle_api_game_join', methods: ['POST'])]
|
2026-04-19 18:04:01 +02:00
|
|
|
public function join(string $gameAssoc): JsonResponse
|
2026-04-09 22:00:53 +02:00
|
|
|
{
|
2026-04-19 18:04:01 +02:00
|
|
|
$this->topicManager->subscribe($gameAssoc, $this->userNamesService->resolveUserName());
|
2026-04-09 22:00:53 +02:00
|
|
|
|
|
|
|
|
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-19 18:04:01 +02:00
|
|
|
$result = $this->topicManager->publish($gameAssoc, $this->userNamesService->resolveUserName(), $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-19 18:04:01 +02:00
|
|
|
public function leave(string $gameAssoc): JsonResponse
|
2026-04-09 22:00:53 +02:00
|
|
|
{
|
2026-04-19 18:04:01 +02:00
|
|
|
$this->topicManager->unSubscribe($gameAssoc, $this->userNamesService->resolveUserName());
|
2026-04-09 22:00:53 +02:00
|
|
|
|
|
|
|
|
return $this->json(['success' => true]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 08:01:46 +02:00
|
|
|
#[Route('/api/game/challenge/{targetGameAssoc}', name: 'MineSeekerBundle_api_game_challenge', methods: ['POST'])]
|
|
|
|
|
public function challenge(string $targetGameAssoc, Request $request): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$data = $request->toArray();
|
|
|
|
|
$challengerGameAssoc = $data['challengerGameAssoc'] ?? '';
|
|
|
|
|
$this->topicManager->publishChallenge($targetGameAssoc, $challengerGameAssoc);
|
|
|
|
|
|
|
|
|
|
return $this->json(['success' => true]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 18:04:01 +02:00
|
|
|
#[Route(
|
|
|
|
|
'/api/game/challenge/respond/{challengerGameAssoc}',
|
|
|
|
|
name: 'MineSeekerBundle_api_game_challenge_respond',
|
|
|
|
|
methods: ['POST'],
|
|
|
|
|
)]
|
2026-04-12 08:01:46 +02:00
|
|
|
public function challengeRespond(string $challengerGameAssoc, Request $request): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$data = $request->toArray();
|
|
|
|
|
$accepted = (bool)($data['accepted'] ?? false);
|
|
|
|
|
$targetGameAssoc = $data['targetGameAssoc'] ?? '';
|
|
|
|
|
$this->topicManager->publishChallengeResponse($challengerGameAssoc, $accepted, $targetGameAssoc);
|
|
|
|
|
|
|
|
|
|
return $this->json(['success' => true]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 18:04:01 +02:00
|
|
|
#[Route('/api/game/heartbeat/{gameAssoc}', name: 'MineSeekerBundle_api_game_heartbeat', methods: ['POST'])]
|
|
|
|
|
public function heartbeat(string $gameAssoc, Request $request): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$data = $request->toArray();
|
|
|
|
|
$color = $data['color'] ?? '';
|
2026-04-19 20:56:51 +02:00
|
|
|
|
2026-04-19 18:04:01 +02:00
|
|
|
if ('red' !== $color && 'blue' !== $color) {
|
|
|
|
|
return $this->json(['success' => false], Response::HTTP_BAD_REQUEST);
|
|
|
|
|
}
|
2026-04-19 20:56:51 +02:00
|
|
|
|
2026-04-19 18:04:01 +02:00
|
|
|
$this->topicManager->publishHeartbeat($gameAssoc, $color);
|
|
|
|
|
|
|
|
|
|
return $this->json(['success' => true]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 22:20:21 +02:00
|
|
|
#[Route('/api/game/waiting', name: 'MineSeekerBundle_api_game_waiting', methods: ['GET'])]
|
|
|
|
|
public function waiting(PlayedGameRepository $repo): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$games = $repo->findWaitingGames();
|
|
|
|
|
|
|
|
|
|
$result = array_map(static function (PlayedGame $g): array {
|
|
|
|
|
$name = match (true) {
|
2026-04-20 09:05:36 +02:00
|
|
|
null !== $g->red => $g->red->getUsername(),
|
|
|
|
|
null !== $g->redAnon => $g->redAnon->userName,
|
|
|
|
|
null !== $g->blue => $g->blue->getUsername(),
|
|
|
|
|
default => $g->blueAnon?->userName ?? 'Unknown',
|
2026-04-11 22:20:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return [
|
2026-04-20 09:05:36 +02:00
|
|
|
'gameAssoc' => $g->gameAssoc,
|
2026-04-11 22:20:21 +02:00
|
|
|
'name' => $name,
|
2026-04-20 11:10:00 +02:00
|
|
|
'since' => $g->created?->format(DateTimeInterface::ATOM) ?? '',
|
2026-04-11 22:20:21 +02:00
|
|
|
];
|
|
|
|
|
}, $games);
|
|
|
|
|
|
|
|
|
|
return $this->json($result);
|
|
|
|
|
}
|
2026-04-09 22:00:53 +02:00
|
|
|
}
|