-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
52 lines (47 loc) · 1.73 KB
/
app.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
41
42
43
44
45
46
47
48
49
50
51
52
const Koa = require('koa'),
Router = require('koa-router'),
KoaJade = require('koa-jade'),
fs = require('fs-promise'),
path = require('path'),
bodyParser = require('koa-bodyparser'),
_ = require('lodash'),
gameMatterDir = "games";
var app = new Koa();
app.use(require('koa-static')('./public/'));
app.use(bodyParser({multipart: true}));
var jadeware = new KoaJade({
viewPath: "./views",
debug: true,
pretty: true,
noCache: true,
compileDebug: true,
app: app
});
fs.readdir(gameMatterDir).then((files) => files.map((file) => fs.readJson(path.join(gameMatterDir, file)).then((obj) => obj)), (err) => {
console.error(`Couldn't load game matter files:\n${err.message}`);
process.exit(1);
}).then((gamePromises) => {
Promise.all(gamePromises).then((games) => {
runServer(games);
}, (err) => {
console.error(`Couldn't load a game matter file:\n${err.message}`);
process.exit(2);
});
});
function runServer(games) {
jadeware.locals.games = games;
app.use(new Router().get('/', function*() {
this.render('index');
}).post('/generate', function*() {
var requestedGames = [].concat(this.request.body.games).map((id) => _.merge({id: id}, games[parseInt(id)]));
Object.keys((this.request.body.expansions || {})).forEach((key) => {
intKey = parseInt(key.substring(1, key.length));
requestedGames.filter((g) => g.id == intKey)[0].expansions = [];
[].concat(this.request.body.expansions[key]).forEach((expansionId) => {
requestedGames.filter((g) => g.id == intKey)[0].expansions.push(games[intKey].expansions[expansionId]);
});
})
this.render('generate', {requestedGames: requestedGames});
}).middleware());
app.listen(process.env.PORT || 3000);
}