From d833f614abee21f1fce8e3ba0a0cdf25636aded9 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Sun, 31 May 2020 17:10:46 -0700 Subject: [PATCH] Eliminate code duplication for root path 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 | 9 --------- lmno.js | 12 ++++++++++++ tictactoe.js | 9 --------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/empires.js b/empires.js index f11d9cd..8c0b833 100644 --- a/empires.js +++ b/empires.js @@ -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 6c85ce8..7fe4265 100644 --- 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); } diff --git a/tictactoe.js b/tictactoe.js index 5b5270d..ca67834 100644 --- a/tictactoe.js +++ b/tictactoe.js @@ -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; -- 2.45.2