From: Carl Worth Date: Mon, 1 Jun 2020 14:38:45 +0000 (-0700) Subject: tictactoe: Use a "game-state" event instead of a series of "move" events X-Git-Url: https://git.cworth.org/git?p=lmno-server;a=commitdiff_plain;h=2c93bc13fb20e23084671ca3dc92ce9ee3e33494 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. --- 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`); } }