Add pregame lobby with live chat

Players used to get bounced back to the dashboard when a session
wasn't full yet. Now they land on a lobby page showing who has
joined, and can chat with each other while waiting - messages are
broadcast live over the existing Mercure hub, and the session
auto-starts (and the lobby notifies everyone) once it fills up.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Frank
2026-08-10 15:32:23 +02:00
co-authored by Claude Sonnet 5
parent 98cf48b29d
commit 444f54b6c6
7 changed files with 301 additions and 4 deletions
+63 -1
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Game\Service;
use App\Game\Entity\Game;
use App\Game\Entity\LobbyMessage;
use App\Game\Entity\Player;
use App\Game\Entity\Session;
use App\Game\Entity\SessionSetting;
@@ -11,6 +12,7 @@ use App\Game\Enum\GameStatus;
use App\Game\Enum\SessionSettingType;
use App\Game\Enum\SessionStatus;
use App\Game\Repository\GameRepository;
use App\Game\Repository\LobbyMessageRepository;
use App\Game\Repository\SessionRepository;
use App\Tech\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
@@ -21,10 +23,12 @@ use Symfony\Component\Mercure\Update;
final class GameDashboardService
{
private const READY_TIMEOUT_SECONDS = 60;
private const LOBBY_MESSAGE_MAX_LENGTH = 500;
public function __construct(
private readonly GameRepository $gameRepository,
private readonly SessionRepository $sessionRepository,
private readonly LobbyMessageRepository $lobbyMessageRepository,
private readonly EntityManagerInterface $entityManager,
private readonly HubInterface $hub,
) {
@@ -122,6 +126,8 @@ final class GameDashboardService
$this->entityManager->flush();
$this->publishLobbyEvent($session, 'player_joined');
if (count($session->getPlayers()) === $session->getGame()->getNumberOfPlayers()) {
$this->startSession($session);
}
@@ -283,9 +289,60 @@ final class GameDashboardService
$this->entityManager->persist($session);
$this->entityManager->flush();
$this->publishLobbyEvent($session, 'session_started');
return true;
}
/**
* @return LobbyMessage[]
*/
public function getLobbyMessages(Session $session): array
{
return $this->lobbyMessageRepository->findForSession($session);
}
public function postLobbyMessage(Session $session, User $user, string $content): ?LobbyMessage
{
if ($session->getStatus() !== SessionStatus::CREATED) {
return null;
}
$player = null;
foreach ($session->getPlayers() as $sessionPlayer) {
if ($sessionPlayer->getUser() === $user) {
$player = $sessionPlayer;
break;
}
}
if (!$player) {
return null;
}
$content = trim($content);
if ($content === '') {
return null;
}
$content = mb_substr($content, 0, self::LOBBY_MESSAGE_MAX_LENGTH);
$message = new LobbyMessage();
$message->setSession($session);
$message->setPlayer($player);
$message->setContent($content);
$this->entityManager->persist($message);
$this->entityManager->flush();
$this->publishLobbyEvent($session, 'lobby_message', [
'username' => $user->getUserIdentifier(),
'content' => $content,
'createdAt' => $message->getCreatedAt()->format(DATE_ATOM),
]);
return $message;
}
public function toggleReady(Session $session, User $user): bool
{
if ($session->getStatus() !== SessionStatus::READY) {
@@ -393,10 +450,15 @@ final class GameDashboardService
}
private function publishPlayerReady(Session $session, int $screen, bool $ready): void
{
$this->publishLobbyEvent($session, 'player_ready', ['player' => $screen, 'ready' => $ready]);
}
private function publishLobbyEvent(Session $session, string $type, array $extra = []): void
{
try {
$topic = '/game/hub/' . $session->getId();
$this->hub->publish(new Update($topic, json_encode(['type' => 'player_ready', 'player' => $screen, 'ready' => $ready])));
$this->hub->publish(new Update($topic, json_encode(array_merge(['type' => $type], $extra))));
} catch (\Exception $e) {
// Mercure might be down, but we don't want to crash the game
}