From 994bff094da6887b2eca2fe115019d96b62da939 Mon Sep 17 00:00:00 2001 From: Takumi Hara <69781798+takumihara@users.noreply.github.com> Date: Tue, 5 Mar 2024 23:38:26 -0600 Subject: [PATCH] Handle when a game is already started for viewers (#302) --- backend/src/events/events.gateway.ts | 20 +++++++++++++------- frontend/app/lib/hooks/game/useGameSocket.ts | 5 ++++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/backend/src/events/events.gateway.ts b/backend/src/events/events.gateway.ts index 8d15b462..258012da 100644 --- a/backend/src/events/events.gateway.ts +++ b/backend/src/events/events.gateway.ts @@ -26,7 +26,8 @@ type Status = | 'friend-left' | 'won' | 'lost' - | 'finish'; + | 'finish' + | 'game-already-started'; type Scores = { [key: string]: number; @@ -112,14 +113,19 @@ export class EventsGateway implements OnGatewayDisconnect { client.join(gameId); if (!isPlayer) { + const players = Object.keys(this.players[gameId] || {}).map( + (socketId, playerNumber) => ({ + playerNumber, + user: this.users[socketId], + lostPoint: this.lostPoints[socketId], + }), + ); this.emitUpdateStatus(client, 'joined-as-viewer', { - players: Object.keys(this.players[gameId] || {}).map( - (socketId, playerNumber) => ({ - playerNumber, - user: this.users[socketId], - }), - ), + players: players, }); + if (players.some((p) => p.lostPoint == 0)) { + this.emitUpdateStatus(client, 'game-already-started'); + } return; } diff --git a/frontend/app/lib/hooks/game/useGameSocket.ts b/frontend/app/lib/hooks/game/useGameSocket.ts index aadd3366..a0950666 100644 --- a/frontend/app/lib/hooks/game/useGameSocket.ts +++ b/frontend/app/lib/hooks/game/useGameSocket.ts @@ -16,7 +16,8 @@ type Status = | "friend-left" | "won" | "lost" - | "finish"; + | "finish" + | "game-already-started"; interface HandleActionProps { playerNumber: number; @@ -44,6 +45,8 @@ const getLogFromStatus = (status: Status): string => { return "You lost!"; case "finish": return "The game has finished"; + case "game-already-started": + return "The game is already started. Wait for the next round"; } };