]> git.cworth.org Git - empires-server/commitdiff
Add common handle_events code to the Game class
authorCarl Worth <cworth@cworth.org>
Sat, 30 May 2020 03:39:56 +0000 (20:39 -0700)
committerCarl Worth <cworth@cworth.org>
Sat, 30 May 2020 03:39:56 +0000 (20:39 -0700)
Again, putting code into the parent class to reduce duplication that
is in the Empires and TicTacToe classes. In Empires, there's a little
extra work to be done when a client connects, so we shadow the parent
function, call super.handle_events, and then do the rest that's
needed.

empires.js
game.js
tictactoe.js

index 46c57c5d414c977bf83abecf2f20b1334b44b4df..f95bba9f48da600bb68ae46aec66e436ff4fed6b 100644 (file)
@@ -213,51 +213,38 @@ class Empires extends Game {
 
     this.broadcast_state_change();
   }
-}
 
-function handle_events(request, response) {
-  const game = request.game;
-  /* These headers will keep the connection open so we can stream events. */
-  const headers = {
-    "Content-type": "text/event-stream",
-    "Connection": "keep-alive",
-    "Cache-Control": "no-cache"
-  };
-  response.writeHead(200, headers);
-
-  /* Now that a client has connected, first we need to stream all of
-   * the existing spectators and players (if any). */
-  if (game._spectators.length > 0) {
-    const spectators_json = JSON.stringify(game.spectators);
-    const spectators_data = `event: spectators\ndata: ${spectators_json}\n\n`;
-    response.write(spectators_data);
-  }
+  handle_events(request, response) {
 
-  if (game._players.length > 0) {
-    const players_json = JSON.stringify(game.players);
-    const players_data = `event: players\ndata: ${players_json}\n\n`;
-    response.write(players_data);
-  }
+    super.handle_events(request, response);
 
-  /* And we need to inform the client of the current game state.
-   *
-   * In fact, we need to cycle through each state transition from the
-   * beginning so the client can see each.
-   */
-  var old_state = null;
-  for (var state = GameState.JOIN; state <= game.state; state++) {
-    var event_data = game.game_state_event_data(old_state, state);
-    response.write("event: game-state\n" + "data: " + event_data + "\n\n");
-    old_state = state;
-  }
+    /* Now that a client has connected, first we need to stream all of
+     * the existing spectators and players (if any). */
+    if (this._spectators.length > 0) {
+      const spectators_json = JSON.stringify(this.spectators);
+      const spectators_data = `event: spectators\ndata: ${spectators_json}\n\n`;
+      response.write(spectators_data);
+    }
 
-  /* Add this new client to our list of clients. */
-  const id = game.add_client(response);
+    if (this._players.length > 0) {
+      const players_json = JSON.stringify(this.players);
+      const players_data = `event: players\ndata: ${players_json}\n\n`;
+      response.write(players_data);
+    }
+
+    /* And we need to inform the client of the current game state.
+     *
+     * In fact, we need to cycle through each state transition from the
+     * beginning so the client can see each.
+     */
+    var old_state = null;
+    for (var state = GameState.JOIN; state <= this.state; state++) {
+      var event_data = this.game_state_event_data(old_state, state);
+      response.write("event: game-state\n" + "data: " + event_data + "\n\n");
+      old_state = state;
+    }
+  }
 
-  /* And queue up cleanup to be triggered on client close. */
-  request.on('close', () => {
-    game.remove_client(id);
-  });
 }
 
 router.get('/', (request, response) => {
@@ -359,7 +346,10 @@ router.get('/players', (request, response) => {
   response.send(game.players);
 });
 
-router.get('/events', handle_events);
+router.get('/events', (request, response) => {
+  const game = request.game;
+  game.handle_events(request, response);
+});
 
 exports.router = router;
 exports.name = engine_name;
diff --git a/game.js b/game.js
index f99ff234fa24c64aa9b9b362a6ad7c475902d5fb..20cd2411e2275ff8aef51e1c35a3772931815b6b 100644 (file)
--- a/game.js
+++ b/game.js
@@ -33,6 +33,24 @@ class Game {
     this.broadcast_string(`event: ${type}\ndata: ${data}\n`);
   }
 
+  handle_events(request, response) {
+    /* These headers will keep the connection open so we can stream events. */
+    const headers = {
+      "Content-type": "text/event-stream",
+      "Connection": "keep-alive",
+      "Cache-Control": "no-cache"
+    };
+    response.writeHead(200, headers);
+
+    /* Add this new client to our list of clients. */
+    const id = this.add_client(response);
+
+    /* And queue up cleanup to be triggered on client close. */
+    request.on('close', () => {
+      this.remove_client(id);
+    });
+  }
+
 }
 
 module.exports = Game;
index 4e76638a7cd3290df810eee00cd677c3af1ec0f8..79d7350f7c84e70473e25d6310057afbcfdbfe39 100644 (file)
@@ -57,21 +57,7 @@ router.post('/move', (request, response) => {
 router.get('/events', (request, response) => {
   const game = request.game;
 
-  /* These headers will keep the connection open so we can stream events. */
-  const headers = {
-    "Content-type": "text/event-stream",
-    "Connection": "keep-alive",
-    "Cache-Control": "no-cache"
-  };
-  response.writeHead(200, headers);
-
-  /* Add this new client to our list of clients. */
-  const id = game.add_client(response);
-
-  /* And queue up cleanup to be triggered on client close. */
-  request.on('close', () => {
-    game.remove_client(id);
-  });
+  game.handle_events(request, response);
 });
 
 exports.router = router;