]> git.cworth.org Git - empires-server/commitdiff
Add a new server: lmno.js
authorCarl Worth <cworth@cworth.org>
Sun, 17 May 2020 19:19:53 +0000 (12:19 -0700)
committerCarl Worth <cworth@cworth.org>
Sun, 17 May 2020 19:19:53 +0000 (12:19 -0700)
This is the new top-level server for all dynamic content and games
that we plan to host at https://lmno.games. The previous server
implementation (in server.js) will likely be subsumed as a
sub-application within this new server soon.

lmno.js [new file with mode: 0644]

diff --git a/lmno.js b/lmno.js
new file mode 100644 (file)
index 0000000..dc23fc2
--- /dev/null
+++ b/lmno.js
@@ -0,0 +1,53 @@
+const express = require("express");
+const cors = require("cors");
+const body_parser = require("body-parser");
+
+const app = express();
+app.use(cors());
+
+class LMNO {
+  constructor() {
+    this.ids = {};
+  }
+
+  generate_id() {
+    return [null,null,null,null].map(() => LMNO.letters.charAt(Math.floor(Math.random() * LMNO.letters.length))).join('');
+  }
+
+  create_game(engine) {
+    do {
+      var id = this.generate_id();
+    } while (id in this.ids);
+
+    const game = {
+       id: id,
+       engine: engine,
+    };
+
+    this.ids[id] = game;
+
+    return game;
+  }
+}
+
+/* Some letters we don't use in our IDs:
+ *
+ * 1. Vowels (AEIOU) to avoid accidentally spelling an unfortunate word
+ * 2. Lowercase letters (replace with corresponding capital on input)
+ * 3. N (replace with M on input)
+ * 4. P (replace with B on input)
+ * 5. S (replace with F on input)
+ */
+LMNO.letters = "BCDFGHJKLMQRTVWXYZ";
+
+const lmno = new LMNO();
+
+app.post('/new/:game_engine', (request, response) =>  {
+  const game_engine = request.params.game_engine;
+  const game = lmno.create_game(game_engine);
+  response.send(JSON.stringify(game.id));
+});
+
+app.listen(4000, function () {
+  console.log('LMNO server listening on localhost:4000');
+});