X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=empires.js;h=46c57c5d414c977bf83abecf2f20b1334b44b4df;hb=76e5097689002f03c721c388e2ca71c52abf3354;hp=19c57d7dba2b3cc11d4c84c56c4898dbaea6d50f;hpb=d5b111168961ef1a05f62df26e00ec86bfd3a1c0;p=lmno-server diff --git a/empires.js b/empires.js index 19c57d7..46c57c5 100644 --- a/empires.js +++ b/empires.js @@ -1,18 +1,9 @@ const express = require("express"); -const cors = require("cors"); -const body_parser = require("body-parser"); -const path = require("path"); -const nunjucks = require("nunjucks"); - -const app = express(); -app.use(cors()); -app.use(body_parser.urlencoded({ extended: false })); -app.use(body_parser.json()); - -nunjucks.configure("templates", { - autoescape: true, - express: app -}); +const Game = require("./game.js"); + +const engine_name = "empires"; + +const router = express.Router(); const GameState = { JOIN: 1, @@ -42,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. */ @@ -195,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) @@ -297,14 +260,14 @@ function handle_events(request, response) { }); } -app.get('/', (request, response) => { +router.get('/', (request, response) => { if (! request.session.nickname) - response.render('choose-nickname.html'); + response.render('choose-nickname.html', { game_name: "Empires" }); else response.render('empires-game.html'); }); -app.post('/spectator', (request, response) => { +router.post('/spectator', (request, response) => { const game = request.game; var name = request.session.nickname; @@ -316,13 +279,13 @@ app.post('/spectator', (request, response) => { response.send(JSON.stringify(id)); }); -app.delete('/spectator/:id', (request, response) => { +router.delete('/spectator/:id', (request, response) => { const game = request.game; game.remove_spectator(parseInt(request.params.id)); response.send(); }); -app.post('/register', (request, response) => { +router.post('/register', (request, response) => { const game = request.game; var name = request.session.nickname;; @@ -334,69 +297,70 @@ app.post('/register', (request, response) => { response.send(); }); -app.post('/deregister/:id', (request, response) => { +router.post('/deregister/:id', (request, response) => { const game = request.game; game.remove_player(parseInt(request.params.id)); response.send(); }); -app.post('/reveal', (request, response) => { +router.post('/reveal', (request, response) => { const game = request.game; game.reveal(); response.send(); }); -app.post('/start', (request, response) => { +router.post('/start', (request, response) => { const game = request.game; game.start(); response.send(); }); -app.post('/reset', (request, response) => { +router.post('/reset', (request, response) => { const game = request.game; game.reset(); response.send(); }); -app.post('/capture/:captor/:captee', (request, response) => { +router.post('/capture/:captor/:captee', (request, response) => { const game = request.game; game.capture(parseInt(request.params.captor), parseInt(request.params.captee)); response.send(); }); -app.post('/liberate/:id', (request, response) => { +router.post('/liberate/:id', (request, response) => { const game = request.game; game.liberate(parseInt(request.params.id)); response.send(); }); -app.post('/restart', (request, response) => { +router.post('/restart', (request, response) => { const game = request.game; game.restart(parseInt(request.params.id)); response.send(); }); -app.get('/characters', (request, response) => { +router.get('/characters', (request, response) => { const game = request.game; response.send(game.characters); }); -app.get('/empires', (request, response) => { +router.get('/empires', (request, response) => { const game = request.game; response.send(game.empires); }); -app.get('/spectators', (request, response) => { +router.get('/spectators', (request, response) => { const game = request.game; response.send(game.spectators); }); -app.get('/players', (request, response) => { +router.get('/players', (request, response) => { const game = request.game; response.send(game.players); }); -app.get('/events', handle_events); +router.get('/events', handle_events); -exports.app = app; -exports.Game = Game; +exports.router = router; +exports.name = engine_name; +exports.Game = Empires;