-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (30 loc) · 1.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// This is expected to run with CWD set to the app root directory.
process.chdir(__dirname);
const express = require('express');
const bodyParser = require('body-parser');
const uuid = require('uuid');
const { Broker } = require('./broker');
const { Manager } = require('./manager');
const port = process.env.PORT || 8080
const app = express();
const expressWs = require('express-ws')(app);
app.use(express.static('static')); // Client content is statically served
app.use(bodyParser.json());
const mgr = new Manager();
const brk = new Broker(mgr);
app.post('/create', (req, res) => {
mgr.NewGame(req.body.characters)
.then((gameID) => {
res.send({ gameId: gameID });
})
.catch((err) => {
console.error(err);
res.sendStatus(400);
});
// This is an ajax handler, so redirect is not useful.
})
app.ws('/ws/player/:gameID', (ws, req) => { try { brk.AddPlayerSocket(ws, req.params.gameID); } catch (err) { console.error(err); } });
app.ws('/ws/admin/:gameID', (ws, req) => { try { brk.AddAdminSocket(ws, req.params.gameID); } catch (err) { console.error(err); } });
app.listen(port, () => {
console.log(`Initiative backend listening at http://localhost:${port}`)
});