From: Carl Worth <cworth@cworth.org>
Date: Tue, 30 Jun 2020 00:02:39 +0000 (-0700)
Subject: Include inactive players (if they have any points) when sending to clients
X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=92eba6c6fa387e7ffe798f12d23bdd203dde4a29;p=lmno-server

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.
---

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"