]> git.cworth.org Git - empires-server/commitdiff
Eliminate code duplication for root path
authorCarl Worth <cworth@cworth.org>
Mon, 1 Jun 2020 00:10:46 +0000 (17:10 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 1 Jun 2020 00:10:46 +0000 (17:10 -0700)
The desired behavior for both existing games is identical for the root
path: If there's no nickname set in the session, we want to query for
one, otherwise we server the game-specific template.

And in the last few commits, we actually made the implementation
identical for this function in both games.

So in this commit we replace the two copied functions with a single
function at the top-level. Hurrah for deletion of duplicated code!
This means less boilerplate is required for all future games.

empires.js
lmno.js
tictactoe.js

index f11d9cdb396bf1007ff393f4f5c311b1c37534a2..8c0b8338f1837a3ca7d9dde42fa4c215ca339979 100644 (file)
@@ -249,15 +249,6 @@ class Empires extends Game {
 
 }
 
-router.get('/', (request, response) => {
-  const game = request.game;
-
-  if (! request.session.nickname)
-    response.render('choose-nickname.html', { game_name: game.meta.name });
-  else
-    response.render(`${game.meta.identifier}-game.html`);
-});
-
 router.post('/spectator', (request, response) => {
   const game = request.game;
   var name = request.session.nickname;
diff --git a/lmno.js b/lmno.js
index 6c85ce878eb9fccb9ca7437b3d481ad3f03cfc84..7fe42659c28d8dde86a9c734fdb83659c0923086 100644 (file)
--- a/lmno.js
+++ b/lmno.js
@@ -251,6 +251,18 @@ app.get('/admin/', auth_admin, (request, response) => {
 /* Mount sub apps. only _after_ we have done all the middleware we need. */
 for (let key in engines) {
   const engine = engines[key];
+
+  /* Add routes that are common to all games. */
+  engine.router.get('/', (request, response) => {
+    const game = request.game;
+
+    if (! request.session.nickname)
+      response.render('choose-nickname.html', { game_name: game.meta.name });
+    else
+      response.render(`${game.meta.identifier}-game.html`);
+  });
+
+  /* And mount the whole router at the path for the game. */
   app.use(`/${engine.name}/[a-zA-Z0-9]{4}/`, engine.router);
 }
 
index 5b5270d21c41a001e0df9c81ca5deda95ba4778b..ca67834ecc5790da0a1d125b9a0038d4a58a28cf 100644 (file)
@@ -38,15 +38,6 @@ class TicTacToe extends Game {
   }
 }
 
-router.get('/', (request, response) => {
-  const game = request.game;
-
-  if (! request.session.nickname)
-    response.render('choose-nickname.html', { game_name: game.meta.name });
-  else
-    response.render(`${game.meta.identifier}-game.html`);
-});
-
 router.post('/move', (request, response) => {
   const game = request.game;
   const square = request.body.square;