]> git.cworth.org Git - empires-server/commitdiff
Include inactive players (if they have any points) when sending to clients
authorCarl Worth <cworth@cworth.org>
Tue, 30 Jun 2020 00:02:39 +0000 (17:02 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 6 Jul 2020 01:43:58 +0000 (18:43 -0700)
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
test

diff --git a/game.js b/game.js
index fa290681f2fc85117500a0c883683c9fb230ead7..f078c354d44164b4f5ad449b372c3f7cb258e056 100644 (file)
--- 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 c6c87508d50dab970b6ff7298f05152eb0f36a49..65af6c2b3199b5636cec5231116fe32d09d6fc1f 100755 (executable)
--- 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"