]> git.cworth.org Git - empires-server/commitdiff
tictactoe: Replay previous moves when a new client connects
authorCarl Worth <cworth@cworth.org>
Sat, 30 May 2020 03:55:30 +0000 (20:55 -0700)
committerCarl Worth <cworth@cworth.org>
Sat, 30 May 2020 03:55:30 +0000 (20:55 -0700)
This fixes the bug where a client would join a game in progress but
would see only the blank board.

tictactoe.js

index 79d7350f7c84e70473e25d6310057afbcfdbfe39..6d4b0148a4a12afa4a5e8046fd3c8447d9d6729d 100644 (file)
@@ -27,6 +27,15 @@ class TicTacToe extends Game {
   broadcast_move(square) {
     this.broadcast_event("move", square);
   }
+
+  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.moves) {
+      response.write(`event: move\ndata: ${move}\n\n`);
+    }
+  }
 }
 
 router.get('/', (request, response) => {