]> git.cworth.org Git - empires-server/blobdiff - empires.js
empires: Add an initial "choose nickname" step before joining a game
[empires-server] / empires.js
index 20dfa78d2102e6e1fe0d3b77bc23eb6cba729b30..a8c177109a08f409969be419951e97da59d2a96f 100644 (file)
@@ -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,9 +216,6 @@ class 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. */
@@ -249,6 +255,13 @@ function handle_events(request, response) {
   });
 }
 
+app.get('/', (request, response) => {
+  if (! request.session.nickname)
+    response.render('choose-nickname.html');
+  else
+    response.sendFile(path.join(__dirname, './game.html'));
+});
+
 app.post('/register', (request, response) => {
   const game = request.game;
   game.add_player(request.body.name, request.body.character);