From 92eba6c6fa387e7ffe798f12d23bdd203dde4a29 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Mon, 29 Jun 2020 17:02:39 -0700 Subject: [PATCH] Include inactive players (if they have any points) when sending to clients This allows clients to display scores for all players in a game, (even if some players happen to not be active anymore). To make the state consistent, we also send the active bit in the JSON info for any player. This commit fixes up the Tic-tac-toe tests in the test suite to now expect this active property in the info sent from the server. --- game.js | 5 ++++- test | 12 ++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/game.js b/game.js index fa29068..f078c35 100644 --- a/game.js +++ b/game.js @@ -36,6 +36,7 @@ class Player { info_json() { return JSON.stringify({ id: this.id, + active: this.active, name: this.name, team: this.team.name, score: this.score @@ -291,7 +292,9 @@ class Game { response.write(`event: player-info\ndata: ${player.info_json()}\n\n`); /* As well as player-enter events for all existing, active players. */ - this.players.filter(p => p !== player && p.active).forEach(p => { + this.players.filter( + p => (p !== player + && (p.active || p.score))).forEach(p => { response.write(`event: player-enter\ndata: ${p.info_json()}\n\n`); }); diff --git a/test b/test index c6c8750..65af6c2 100755 --- a/test +++ b/test @@ -376,7 +376,7 @@ TEST_END TEST "Verify player-info event reports 'curl' name" result=$(tictactoe_player_info) -test "$result" = 'data: {"id":1,"name":"curl","team":""}' +test "$result" = 'data: {"id":1,"active":true,"name":"curl","team":""}' TEST_END TEST_SUBSECTION "Tic Tac Toe /player" @@ -384,31 +384,31 @@ TEST_SUBSECTION "Tic Tac Toe /player" TEST "Change name to 'newname'" tictactoe_player_name newname result=$(tictactoe_player_info) -test "$result" = 'data: {"id":1,"name":"newname","team":""}' +test "$result" = 'data: {"id":1,"active":true,"name":"newname","team":""}' TEST_END TEST "Change team to 'X'" tictactoe_player_team X result=$(tictactoe_player_info) -test "$result" = 'data: {"id":1,"name":"newname","team":"X"}' +test "$result" = 'data: {"id":1,"active":true,"name":"newname","team":"X"}' TEST_END TEST "Change team to 'O'" tictactoe_player_team O result=$(tictactoe_player_info) -test "$result" = 'data: {"id":1,"name":"newname","team":"O"}' +test "$result" = 'data: {"id":1,"active":true,"name":"newname","team":"O"}' TEST_END TEST "Verify cannot change team to 'Z'" tictactoe_player_team Z result=$(tictactoe_player_info) -test "$result" = 'data: {"id":1,"name":"newname","team":"O"}' +test "$result" = 'data: {"id":1,"active":true,"name":"newname","team":"O"}' TEST_END TEST "Leave current team" tictactoe_player_team "" result=$(tictactoe_player_info) -test "$result" = 'data: {"id":1,"name":"newname","team":""}' +test "$result" = 'data: {"id":1,"active":true,"name":"newname","team":""}' TEST_END TEST_SUBSECTION "Tic Tac Toe /move" -- 2.43.0