]> git.cworth.org Git - empires-server/blob - lmno.js
Add a new server: lmno.js
[empires-server] / lmno.js
1 const express = require("express");
2 const cors = require("cors");
3 const body_parser = require("body-parser");
4
5 const app = express();
6 app.use(cors());
7
8 class LMNO {
9   constructor() {
10     this.ids = {};
11   }
12
13   generate_id() {
14     return [null,null,null,null].map(() => LMNO.letters.charAt(Math.floor(Math.random() * LMNO.letters.length))).join('');
15   }
16
17   create_game(engine) {
18     do {
19       var id = this.generate_id();
20     } while (id in this.ids);
21
22     const game = {
23         id: id,
24         engine: engine,
25     };
26
27     this.ids[id] = game;
28
29     return game;
30   }
31 }
32
33 /* Some letters we don't use in our IDs:
34  *
35  * 1. Vowels (AEIOU) to avoid accidentally spelling an unfortunate word
36  * 2. Lowercase letters (replace with corresponding capital on input)
37  * 3. N (replace with M on input)
38  * 4. P (replace with B on input)
39  * 5. S (replace with F on input)
40  */
41 LMNO.letters = "BCDFGHJKLMQRTVWXYZ";
42
43 const lmno = new LMNO();
44
45 app.post('/new/:game_engine', (request, response) =>  {
46   const game_engine = request.params.game_engine;
47   const game = lmno.create_game(game_engine);
48   response.send(JSON.stringify(game.id));
49 });
50
51 app.listen(4000, function () {
52   console.log('LMNO server listening on localhost:4000');
53 });