]> git.cworth.org Git - empires-server/commitdiff
lmno: Generalize the support for multiple game engines
authorCarl Worth <cworth@cworth.org>
Wed, 27 May 2020 03:57:27 +0000 (20:57 -0700)
committerCarl Worth <cworth@cworth.org>
Wed, 27 May 2020 16:47:29 +0000 (09:47 -0700)
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).

empires.js
lmno.js

index 1387cd23ab18b2fbf381615242e38b05edcfe838..034ba424a55031829c07ab921603c3dbd2a9ecd4 100644 (file)
@@ -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 8a3210e5229c14cd750d5bbd4408c2536eab6c59..d818cd12fb16de02af766b02b995d5eac3f43886 100644 (file)
--- 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');