From a6f851d7bcba70f15c6e6804aa293a58a319bd63 Mon Sep 17 00:00:00 2001 From: Jordan Pawlett Date: Wed, 29 Apr 2020 17:58:53 +0100 Subject: [PATCH] Add error messages when the turn ends unexpectedly --- services/games-service/src/game.ts | 13 +++++++++---- services/games-service/src/turn.ts | 2 ++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/services/games-service/src/game.ts b/services/games-service/src/game.ts index 2c477b16..0cb4afa8 100644 --- a/services/games-service/src/game.ts +++ b/services/games-service/src/game.ts @@ -169,7 +169,7 @@ export default class Game extends TurnHandler { // If no users selected any cards to play, skip. if (!Object.keys(this.selectedCards).length) { - this.handleNoWinner(); + this.handleNoWinner('No one selected any cards. Everyone loses!'); return; } @@ -194,7 +194,7 @@ export default class Game extends TurnHandler { // If the czar doesn't pick a winner within 60 seconds. Move on. // should handle a "no winner selected state" this.gameInterval = setTimeout(() => { - this.handleNextTurn(); + this.handleNoWinner('The Czar did not pick a winner! He has failed us all...'); }, 60 * 1000); } @@ -221,7 +221,11 @@ export default class Game extends TurnHandler { } } - private handleNoWinner() { + private handleNoWinner(reason?: string) { + if (this.gameInterval) { + clearTimeout(this.gameInterval); + } + const initialData: TurnDataWithState = { players: Object.values(this.players).map(({ _id, score, isCzar }) => ({ _id, score, isCzar })), roomId: this.room._id, @@ -230,6 +234,7 @@ export default class Game extends TurnHandler { winner: null, winningCards: [], state: GameState.TURN_SETUP, + errorMessage: reason?.length ? reason : 'No one selected any cards. Everyone loses!' }; this.lastGameState = initialData; @@ -291,7 +296,7 @@ export default class Game extends TurnHandler { if (player.isCzar) { clearTimeout(this.gameInterval); clearTimeout(this.nextTurnTimeout); - this.handleNoWinner(); + this.handleNoWinner('The Czar left the game'); } } diff --git a/services/games-service/src/turn.ts b/services/games-service/src/turn.ts index 6f3ee148..b42479d3 100644 --- a/services/games-service/src/turn.ts +++ b/services/games-service/src/turn.ts @@ -29,6 +29,7 @@ export interface TurnDataWithState extends TurnData { selectedCards: { [id: string]: Card[] }; winner: string | string[]; winningCards: Card[]; + errorMessage?: string; } export default class TurnHandler { @@ -151,6 +152,7 @@ export default class TurnHandler { // mutate by reference. ensure we reset the czar. Object.values(players).forEach(player => player.isCzar = false); + this.selectedCards = {}; this._turnData.turn += 1; this._turnData.czar = this.pickCzar(players); this._turnData.blackCard = await this.pickBlackCard();