]> git.cworth.org Git - empires-server/commitdiff
tictactoe: Use a "game-state" event instead of a series of "move" events
authorCarl Worth <cworth@cworth.org>
Mon, 1 Jun 2020 14:38:45 +0000 (07:38 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 1 Jun 2020 14:38:45 +0000 (07:38 -0700)
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

index 74cf3436334f214123981d8698b17b0cacf8ae21..7b91a9bc34461158d41fd1d1495cdc84d33600c0 100644 (file)
@@ -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`);
   }
 }