From: Carl Worth Date: Wed, 27 May 2020 03:57:27 +0000 (-0700) Subject: lmno: Generalize the support for multiple game engines X-Git-Url: https://git.cworth.org/git?p=empires-server;a=commitdiff_plain;h=ab53a3a530ad30db6cf52b8507fa1cbddb28eecf lmno: Generalize the support for multiple game engines Rather than having repeated lists of the engines, we have one list at the top of the file and then use that list to iterate over all engines to mount their sub-apps at a path that is named by engine.name. We also fix the construction of the Game class to use an imported constructor (so that each game engine has objects of its own class instead of only having games constructed via empires.Game() exclusively). --- diff --git a/empires.js b/empires.js index 1387cd2..034ba42 100644 --- a/empires.js +++ b/empires.js @@ -399,4 +399,5 @@ app.get('/players', (request, response) => { app.get('/events', handle_events); exports.app = app; +exports.name = "empires"; exports.Game = Game; diff --git a/lmno.js b/lmno.js index 8a3210e..d818cd1 100644 --- a/lmno.js +++ b/lmno.js @@ -47,8 +47,10 @@ nunjucks.configure("templates", { }); /* Load each of our game mini-apps. */ -var empires = require("./empires"); -var tictactoe = require("./tictactoe"); +const engines = { + empires: require("./empires"), + tictactoe: require("./tictactoe") +}; class LMNO { constructor() { @@ -59,17 +61,19 @@ class LMNO { return Array(4).fill(null).map(() => LMNO.letters.charAt(Math.floor(Math.random() * LMNO.letters.length))).join(''); } - create_game(engine) { + create_game(engine_name) { do { var id = this.generate_id(); } while (id in this.ids); - const game = new empires.Game(); + const engine = engines[engine_name]; + + const game = new engine.Game(); this.ids[id] = { - id: id, - engine: engine, - game: game + id: id, + engine: engine.name, + game: game }; return id; @@ -246,8 +250,10 @@ app.get('/admin/', auth_admin, (request, response) => { /* Mount sub apps. only _after_ we have done all the middleware we need. */ -app.use('/empires/[a-zA-Z0-9]{4}/', empires.app); -app.use('/tictactoe/[a-zA-Z0-9]{4}/', tictactoe.app); +for (let key in engines) { + const engine = engines[key]; + app.use(`/${engine.name}/[a-zA-Z0-9]{4}/`, engine.app); +} app.listen(4000, function () { console.log('LMNO server listening on localhost:4000');