From 2c93bc13fb20e23084671ca3dc92ce9ee3e33494 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Mon, 1 Jun 2020 07:38:45 -0700 Subject: [PATCH] tictactoe: Use a "game-state" event instead of a series of "move" events With this, when a client joins they will get a snapshot of the current game state as well as a history of all previous moves. --- tictactoe.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tictactoe.js b/tictactoe.js index 74cf343..7b91a9b 100644 --- a/tictactoe.js +++ b/tictactoe.js @@ -37,10 +37,10 @@ class TicTacToe extends Game { handle_events(request, response) { super.handle_events(request, response); - /* When a new client joins, replay all previous moves to it. */ - for (let move of this.state.moves) { - response.write(`event: move\ndata: ${move}\n\n`); - } + /* When a new client joins, give them the current game state, + * (which includes the history of moves). */ + const state_json = JSON.stringify(this.state); + response.write(`event: game-state\ndata: ${state_json}\n\n`); } } -- 2.43.0