Skip to content

Commit

Permalink
Handle when a game is already started for viewers (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
takumihara authored Mar 6, 2024
1 parent 0053ebf commit 994bff0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
20 changes: 13 additions & 7 deletions backend/src/events/events.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type Status =
| 'friend-left'
| 'won'
| 'lost'
| 'finish';
| 'finish'
| 'game-already-started';

type Scores = {
[key: string]: number;
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion frontend/app/lib/hooks/game/useGameSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type Status =
| "friend-left"
| "won"
| "lost"
| "finish";
| "finish"
| "game-already-started";

interface HandleActionProps {
playerNumber: number;
Expand Down Expand Up @@ -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";
}
};

Expand Down

0 comments on commit 994bff0

Please sign in to comment.