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:
@@ -141,12 +141,21 @@ final class GameController extends AbstractController
|
||||
return $this->redirectToRoute('game', ['session' => $session->getId()]);
|
||||
}
|
||||
|
||||
if ($request->isMethod('POST') && $request->request->has('send_message')) {
|
||||
$dashboardService->postLobbyMessage($session, $user, (string) $request->request->get('content', ''));
|
||||
return $this->redirectToRoute('game', ['session' => $session->getId()]);
|
||||
}
|
||||
|
||||
// Lazily pick up readiness changes from other players since our last request
|
||||
$dashboardService->checkAllPlayersReady($session);
|
||||
|
||||
if ($session->getStatus() === SessionStatus::CREATED) {
|
||||
$this->addFlash('info', 'This session is still waiting for more players to join.');
|
||||
return $this->redirectToRoute('game_dashboard');
|
||||
return $this->render('game/lobby.html.twig', [
|
||||
'session' => $session,
|
||||
'messages' => $dashboardService->getLobbyMessages($session),
|
||||
'player' => $player,
|
||||
'mercure_public_url' => $this->mercurePublicUrl,
|
||||
]);
|
||||
}
|
||||
|
||||
if ($session->getStatus() === SessionStatus::WON) {
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace App\Game\Entity;
|
||||
|
||||
use App\Game\Repository\LobbyMessageRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: LobbyMessageRepository::class)]
|
||||
#[ORM\Table(name: 'lobby_message')]
|
||||
class LobbyMessage
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: Session::class)]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
private ?Session $session = null;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: Player::class)]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
private ?Player $player = null;
|
||||
|
||||
#[ORM\Column(length: 500)]
|
||||
private ?string $content = null;
|
||||
|
||||
#[ORM\Column(type: 'datetime')]
|
||||
private ?\DateTimeInterface $createdAt = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->createdAt = new \DateTime();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getSession(): ?Session
|
||||
{
|
||||
return $this->session;
|
||||
}
|
||||
|
||||
public function setSession(?Session $session): static
|
||||
{
|
||||
$this->session = $session;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPlayer(): ?Player
|
||||
{
|
||||
return $this->player;
|
||||
}
|
||||
|
||||
public function setPlayer(?Player $player): static
|
||||
{
|
||||
$this->player = $player;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getContent(): ?string
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function setContent(string $content): static
|
||||
{
|
||||
$this->content = $content;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function setCreatedAt(\DateTimeInterface $createdAt): static
|
||||
{
|
||||
$this->createdAt = $createdAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Game\Repository;
|
||||
|
||||
use App\Game\Entity\LobbyMessage;
|
||||
use App\Game\Entity\Session;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<LobbyMessage>
|
||||
*/
|
||||
class LobbyMessageRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, LobbyMessage::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LobbyMessage[]
|
||||
*/
|
||||
public function findForSession(Session $session, int $limit = 100): array
|
||||
{
|
||||
return $this->createQueryBuilder('m')
|
||||
->andWhere('m.session = :session')
|
||||
->setParameter('session', $session)
|
||||
->orderBy('m.createdAt', 'ASC')
|
||||
->setMaxResults($limit)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user