From 443a21300369b2bfe01a6e62eaad91953541d9d8 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 24 Jun 2020 18:29:45 -0700 Subject: [PATCH] Mark players as active:false when they drop all connections Prior to this commit, the intent had been to delete players entirely when they had no remaining connections. But this code was broken for the same reason as the bug fixed in the previous commit (filter returns a new array). But instead of fixing that bug, here we're actually changing the semantics so that once a player has so remaining connections they are simply marked as active:false. This is still broadcast out to all active players as a "player-exit" event but it means the server is holding onto the data so that a player can reclaim their spot (and their score) by rejoining later. --- game.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/game.js b/game.js index 4649b56..7ca1507 100644 --- a/game.js +++ b/game.js @@ -9,6 +9,7 @@ class Player { this.name = name; this.connections = [connection]; this.team = no_team; + this.active = true; } add_connection(connection) { @@ -214,9 +215,8 @@ class Game { remove_player_connection(player, connection) { const remaining = player.remove_connection(connection); if (remaining === 0) { + player.active = false; const player_data = JSON.stringify({ id: player.id }); - this.players.filter(p => p !== player); - delete this.players_by_session[player.session_id]; this.broadcast_event("player-exit", player_data); } } @@ -266,8 +266,8 @@ class Game { /* And the player-info event. */ response.write(`event: player-info\ndata: ${player.info_json()}\n\n`); - /* As well as player-enter events for all existing players. */ - this.players.filter(p => p !== player).forEach(p => { + /* As well as player-enter events for all existing, active players. */ + this.players.filter(p => p !== player && p.active).forEach(p => { response.write(`event: player-enter\ndata: ${p.info_json()}\n\n`); }); -- 2.43.0