From: Carl Worth Date: Sat, 23 May 2020 17:25:44 +0000 (-0700) Subject: empires: Add an initial "choose nickname" step before joining a game X-Git-Url: https://git.cworth.org/git?p=lmno-server;a=commitdiff_plain;h=70a137f52f6753491838515016489cab1347261b empires: Add an initial "choose nickname" step before joining a game This lodges the selected nickname in the current session, (using a new /profile API also added in this commit). Having the nickname stored in the session has the advantage that when playing multiple games in a row, a player won't need to keep re-typing their own name each time. --- diff --git a/empires.js b/empires.js index 69fc7c2..a8c1771 100644 --- a/empires.js +++ b/empires.js @@ -2,12 +2,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, REVEAL: 2, @@ -250,7 +256,10 @@ function handle_events(request, response) { } app.get('/', (request, response) => { - response.sendFile(path.join(__dirname, './game.html')); + if (! request.session.nickname) + response.render('choose-nickname.html'); + else + response.sendFile(path.join(__dirname, './game.html')); }); app.post('/register', (request, response) => { diff --git a/lmno.js b/lmno.js index f5de9eb..c990cac 100644 --- a/lmno.js +++ b/lmno.js @@ -212,6 +212,16 @@ app.post('/login', async (request, response) => { return; }); +/* API to set uer profile information */ +app.put('/profile', (request, response) => { + const nickname = request.body.nickname; + if (nickname) { + request.session.nickname = nickname; + request.session.save(); + } + response.send(); +}); + /* An admin page (only available to admin users, of course) */ app.get('/admin/', auth_admin, (request, response) => { let active = []; diff --git a/templates/choose-nickname.html b/templates/choose-nickname.html new file mode 100644 index 0000000..86c3413 --- /dev/null +++ b/templates/choose-nickname.html @@ -0,0 +1,45 @@ +{% extends "base.html" %} + +{% block head %} + +{% endblock %} + +{% block page %} +

+ Welcome to Empires +

+ +

+ To join the game, please set your own nickname: +

+ + +
+
+ + +
+ +
+ +
+
+{% endblock %}