X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=empires.js;h=f95bba9f48da600bb68ae46aec66e436ff4fed6b;hb=212b5a93209741c091d2f504f3d2edac8ae98262;hp=7907ce0c80a7711d4c92044f7391298156c2a664;hpb=eec2fd5c7db6f39a9994fa620619ebf005d26064;p=empires-server diff --git a/empires.js b/empires.js index 7907ce0..f95bba9 100644 --- a/empires.js +++ b/empires.js @@ -1,4 +1,7 @@ const express = require("express"); +const Game = require("./game.js"); + +const engine_name = "empires"; const router = express.Router(); @@ -30,15 +33,14 @@ function shuffle(a) { } } -class Game { +class Empires extends Game { constructor() { + super(engine_name); this._spectators = []; this.next_spectator_id = 1; this._players = []; this.next_player_id = 1; this.characters_to_reveal = null; - this.clients = []; - this.next_client_id = 1; this.state = GameState.JOIN; /* Send a comment to every connected client every 15 seconds. */ @@ -183,33 +185,6 @@ class Game { return this._players.map(player => ({id: player.id, name: player.name })); } - add_client(response) { - const id = this.next_client_id; - this.clients.push({id: id, - response: response}); - this.next_client_id++; - - return id; - } - - remove_client(id) { - this.clients = this.clients.filter(client => client.id !== id); - } - - /* Send a string to all clients */ - broadcast_string(str) { - this.clients.forEach(client => client.response.write(str + '\n')); - } - - /* Send an event to all clients. - * - * An event has both a declared type and a separate data block. - * It also ends with two newlines (to mark the end of the event). - */ - broadcast_event(type, data) { - this.broadcast_string(`event: ${type}\ndata: ${data}\n`); - } - game_state_event_data(old_state, new_state) { var old_state_name; if (old_state) @@ -238,51 +213,38 @@ class 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) => { @@ -384,8 +346,11 @@ 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 = "empires"; -exports.Game = Game; +exports.name = engine_name; +exports.Game = Empires;