X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=empires.js;h=cb7571d67ef3ff81934593cf76c8b5c407c65ce6;hb=493acb2b3b21f592d0ab958ba2875bb412eb8468;hp=46c57c5d414c977bf83abecf2f20b1334b44b4df;hpb=76e5097689002f03c721c388e2ca71c52abf3354;p=empires-server diff --git a/empires.js b/empires.js index 46c57c5..cb7571d 100644 --- a/empires.js +++ b/empires.js @@ -1,8 +1,6 @@ const express = require("express"); const Game = require("./game.js"); -const engine_name = "empires"; - const router = express.Router(); const GameState = { @@ -34,8 +32,8 @@ function shuffle(a) { } class Empires extends Game { - constructor() { - super(engine_name); + constructor(id) { + super(id); this._spectators = []; this.next_spectator_id = 1; this._players = []; @@ -85,6 +83,8 @@ class Empires extends Game { want. */ const player_data = JSON.stringify((({id, name}) => ({id, name}))(new_player)); this.broadcast_event("player-join", player_data); + + return new_player; } remove_player(id) { @@ -213,59 +213,39 @@ 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 queue up cleanup to be triggered on client close. */ - request.on('close', () => { - game.remove_client(id); - }); -} + /* 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; + } + } -router.get('/', (request, response) => { - if (! request.session.nickname) - response.render('choose-nickname.html', { game_name: "Empires" }); - else - response.render('empires-game.html'); -}); +} router.post('/spectator', (request, response) => { const game = request.game; @@ -293,8 +273,8 @@ router.post('/register', (request, response) => { if (request.body.name) name = request.body.name; - game.add_player(name, request.body.character); - response.send(); + const player = game.add_player(name, request.body.character); + response.send(JSON.stringify(player.id)); }); router.post('/deregister/:id', (request, response) => { @@ -359,8 +339,17 @@ 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; exports.Game = Empires; + +Empires.meta = { + name: "Empires", + identifier: "empires" +}; + +exports.meta = Empires.meta;