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).
app.get('/events', handle_events);
exports.app = app;
app.get('/events', handle_events);
exports.app = app;
+exports.name = "empires";
});
/* Load each of our game mini-apps. */
});
/* 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() {
class LMNO {
constructor() {
return Array(4).fill(null).map(() => LMNO.letters.charAt(Math.floor(Math.random() * LMNO.letters.length))).join('');
}
return Array(4).fill(null).map(() => LMNO.letters.charAt(Math.floor(Math.random() * LMNO.letters.length))).join('');
}
+ create_game(engine_name) {
do {
var id = this.generate_id();
} while (id in this.ids);
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();
- id: id,
- engine: engine,
- game: game
+ id: id,
+ engine: engine.name,
+ game: game
/* Mount sub apps. only _after_ we have done all the middleware we need. */
/* 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');
app.listen(4000, function () {
console.log('LMNO server listening on localhost:4000');