X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=empires.js;h=d8cff6f70314623f01c06135d426e32e6749de72;hb=d1a58a5a8c2f30278dc002a38f2f540a914bc677;hp=2794c47ee41006098f3547ec685049eef7f0d520;hpb=38e0c09faeefc252754b48ea9d7b2b3c93a69fe5;p=lmno-server diff --git a/empires.js b/empires.js index 2794c47..d8cff6f 100644 --- a/empires.js +++ b/empires.js @@ -1,9 +1,18 @@ 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 GameState = { JOIN: 1, @@ -207,12 +216,8 @@ class Game { } } -const game = new Game(); - -app.use(body_parser.urlencoded({ extended: false })); -app.use(body_parser.json()); - 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", @@ -251,63 +256,82 @@ function handle_events(request, response) { } app.get('/', (request, response) => { - response.send('Hello World!'); + if (! request.session.nickname) + response.render('choose-nickname.html'); + else + response.sendFile(path.join(__dirname, './game.html')); }); app.post('/register', (request, response) => { - game.add_player(request.body.name, request.body.character); + const game = request.game; + var name = request.session.nickname;; + + /* If the request includes a name, that overrides the session nickname. */ + if (request.body.name) + name = request.body.name; + + game.add_player(name, request.body.character); response.send(); }); app.post('/deregister/:id', (request, response) => { + const game = request.game; game.remove_player(parseInt(request.params.id)); response.send(); }); app.post('/reveal', (request, response) => { + const game = request.game; game.reveal(); response.send(); }); app.post('/start', (request, response) => { + const game = request.game; game.start(); response.send(); }); app.post('/reset', (request, response) => { + const game = request.game; game.reset(); response.send(); }); app.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) => { + const game = request.game; game.liberate(parseInt(request.params.id)); response.send(); }); app.post('/restart', (request, response) => { + const game = request.game; game.restart(parseInt(request.params.id)); response.send(); }); app.get('/characters', (request, response) => { + const game = request.game; response.send(game.characters); }); app.get('/empires', (request, response) => { + const game = request.game; response.send(game.empires); }); app.get('/players', (request, response) => { + const game = request.game; response.send(game.players); }); app.get('/events', handle_events); -app.listen(3000, function () { - console.log('Empires server listening on localhost:3000'); -}); +exports.app = app; +exports.Game = Game;