Skip to content

Commit

Permalink
Add error messages when the turn ends unexpectedly
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanPawlett committed Apr 29, 2020
1 parent 9e16ef3 commit a6f851d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 9 additions & 4 deletions services/games-service/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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');
}

}
Expand Down
2 changes: 2 additions & 0 deletions services/games-service/src/turn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface TurnDataWithState extends TurnData {
selectedCards: { [id: string]: Card[] };
winner: string | string[];
winningCards: Card[];
errorMessage?: string;
}

export default class TurnHandler {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit a6f851d

Please sign in to comment.