From b965f0f085570097e85bf7739236a33c82851615 Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 8 Jan 2026 20:11:14 +0100 Subject: [PATCH] Added mercure to update when everyone is ready --- src/Game/Controller/GameController.php | 8 ++++++- src/Game/Service/GameDashboardService.php | 28 ++++++++++++++++++---- src/Game/Service/GameResponseService.php | 20 ++++++++++++---- templates/game/waiting.html.twig | 29 +++++++++++++++++++++++ tests/Game/GameDashboardServiceTest.php | 6 +++++ 5 files changed, 82 insertions(+), 9 deletions(-) diff --git a/src/Game/Controller/GameController.php b/src/Game/Controller/GameController.php index cfc9ef6..1da07e3 100644 --- a/src/Game/Controller/GameController.php +++ b/src/Game/Controller/GameController.php @@ -132,16 +132,22 @@ final class GameController extends AbstractController if ($session->getStatus() === SessionStatus::READY) { $isReady = false; + $readyAt = null; if ($player) { $settingName = SessionSettingType::tryFrom('ReadyAtForPlayer' . $player->getScreen()); if ($settingName) { - $isReady = $session->getSettings()->exists(fn($i, SessionSetting $s) => $s->getName() === $settingName && $s->getPlayer() === $player); + $setting = $session->getSettings()->filter(fn(SessionSetting $s) => $s->getName() === $settingName && $s->getPlayer() === $player)->first(); + if ($setting) { + $isReady = true; + $readyAt = (int)$setting->getValue(); + } } } return $this->render('game/waiting.html.twig', [ 'session' => $session, 'isReady' => $isReady, + 'readyAt' => $readyAt, 'mercure_public_url' => $this->mercurePublicUrl, ]); } diff --git a/src/Game/Service/GameDashboardService.php b/src/Game/Service/GameDashboardService.php index b53aa7b..db8c7b1 100644 --- a/src/Game/Service/GameDashboardService.php +++ b/src/Game/Service/GameDashboardService.php @@ -329,8 +329,12 @@ final class GameDashboardService $this->checkAllPlayersReady($session); $this->entityManager->flush(); - $topic = $this->mercureTopicBase . '/game/hub-' . $session->getId(); - $this->hub->publish(new Update($topic, json_encode(['type' => 'player_ready', 'player' => $player->getScreen(), 'ready' => !$setting]))); + try { + $topic = $this->mercureTopicBase . '/game/hub-' . $session->getId(); + $this->hub->publish(new Update($topic, json_encode(['type' => 'player_ready', 'player' => $player->getScreen(), 'ready' => !$setting]))); + } catch (\Exception $e) { + // Mercure might be down, but we don't want to crash the game + } return true; } @@ -350,6 +354,7 @@ final class GameDashboardService $readyPlayersCount = 0; $now = new \DateTime(); + $anyReset = false; /** @var \App\Game\Repository\SessionSettingRepository $settingRepo */ $settingRepo = $this->entityManager->getRepository(SessionSetting::class); @@ -367,12 +372,23 @@ final class GameDashboardService if (($now->getTimestamp() - $readyAtTimestamp) > 60) { $session->removeSetting($setting); $this->entityManager->remove($setting); + $anyReset = true; } else { $readyPlayersCount++; } } } + if ($anyReset) { + $this->entityManager->flush(); + try { + $topic = $this->mercureTopicBase . '/game/hub-' . $session->getId(); + $this->hub->publish(new Update($topic, json_encode(['type' => 'player_ready']))); + } catch (\Exception $e) { + // Mercure might be down + } + } + if ($readyPlayersCount === $numPlayers) { $session->setStatus(SessionStatus::PLAYING); $this->entityManager->persist($session); @@ -389,8 +405,12 @@ final class GameDashboardService } } - $topic = $this->mercureTopicBase . '/game/hub-' . $session->getId(); - $this->hub->publish(new Update($topic, json_encode(['type' => 'all_ready']))); + try { + $topic = $this->mercureTopicBase . '/game/hub-' . $session->getId(); + $this->hub->publish(new Update($topic, json_encode(['type' => 'all_ready']))); + } catch (\Exception $e) { + // Mercure might be down, but we don't want to crash the game + } } } diff --git a/src/Game/Service/GameResponseService.php b/src/Game/Service/GameResponseService.php index e2e746c..0e1a221 100644 --- a/src/Game/Service/GameResponseService.php +++ b/src/Game/Service/GameResponseService.php @@ -336,8 +336,12 @@ class GameResponseService if(is_null($activeGame)) return false; - $topic = $_ENV['MERCURE_TOPIC_BASE'] . '/game/hub-' . $activeGame; - $this->hub->publish(new Update($topic, json_encode([$sendTo, $message]))); + $topic = $this->mercureTopicBase . '/game/hub-' . $activeGame; + try { + $this->hub->publish(new Update($topic, json_encode([$sendTo, $message]))); + } catch (\Exception $e) { + // Mercure might be down + } $this->updateChatTracking($player, (int)$sendTo); @@ -381,7 +385,11 @@ class GameResponseService $topic = $this->mercureTopicBase . '/game/hub-' . $session->getId(); $notification = "Security Alert: One of your verify codes was shared and has been regenerated."; // We send it only to this player (screen) - $this->hub->publish(new Update($topic, json_encode([$screen, $notification]))); + try { + $this->hub->publish(new Update($topic, json_encode([$screen, $notification]))); + } catch (\Exception $e) { + // Mercure might be down + } } } @@ -667,7 +675,11 @@ class GameResponseService $topic = $this->mercureTopicBase . '/game/hub-' . $session->getId(); $message = "Mainframe Help Modus: Agents Doyle, Vega and Lennox rapports have been updated with coded messages."; - $this->hub->publish(new Update($topic, json_encode([0, $message]))); + try { + $this->hub->publish(new Update($topic, json_encode([0, $message]))); + } catch (\Exception $e) { + // Mercure might be down + } } } diff --git a/templates/game/waiting.html.twig b/templates/game/waiting.html.twig index b2c26f0..6e65de3 100644 --- a/templates/game/waiting.html.twig +++ b/templates/game/waiting.html.twig @@ -14,6 +14,15 @@

{{ session.game.name }}

Welcome to the game! Please wait for all players to join and signal they are ready to start.

+
+ Please keep the following things in mind: + +
+
Game Information: