From: Carl Worth Date: Mon, 1 Jun 2020 14:44:37 +0000 (-0700) Subject: Standardize transmission of game "state" when a client joins X-Git-Url: https://git.cworth.org/git?p=empires-server;a=commitdiff_plain;h=143c01e643c36e87c6bfc377ad1cf4edcc79e62c Standardize transmission of game "state" when a client joins With this addition to the Game class, the TicTacToe class drops its custom implementation of the handle_events method. Similarly, future game implementations can simply have a "state" property and not need to do anything further for new clients to be provided with the current state when they join. --- diff --git a/game.js b/game.js index 706bfc7..faef437 100644 --- a/game.js +++ b/game.js @@ -69,6 +69,13 @@ class Game { request.on('close', () => { this.remove_client(id); }); + + /* Finally, if this game class has a "state" property, stream that + * current state to the client. */ + if (this.state) { + const state_json = JSON.stringify(this.state); + response.write(`event: game-state\ndata: ${state_json}\n\n`); + } } } diff --git a/tictactoe.js b/tictactoe.js index 7b91a9b..9140b89 100644 --- a/tictactoe.js +++ b/tictactoe.js @@ -33,15 +33,6 @@ 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, 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`); - } } router.post('/move', (request, response) => {