-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
73 lines (60 loc) · 1.76 KB
/
server.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
const version = require('./package.json').version;
var express = require('express');
var exphbs = require('express-handlebars');
var bodyParser = require('body-parser');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var logger = require('./lib/logger');
var controller = require('./lib/controller');
var hbs = exphbs.create({
defaultLayout: 'main',
helpers: {
section: function(name, options) {
if(!this._sections) this._sections = {};
this._sections[name] = options.fn(this);
return null;
}
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('port', (process.env.PORT || 3000));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/', function (req, res) {
var data = {
version,
games: controller.games,
};
res.render('index', data);
});
app.post('/game', function(req, res) {
var gameId = controller.newGame(req.body.gameName);
res.redirect('/game/' + gameId);
});
app.get('/game/:game', function(req, res) {
if (!controller.games[req.params.game]) {
logger.verbose('Tried to join game that does not exist');
res.redirect('/');
} else {
res.render('game', { layout: false, game: controller.games[req.params.game] });
}
});
app.get('/zombies', function(req, res) {
res.render('zombies', { version });
});
io.on('connection', function(socket) {
controller.setupSocket(socket);
});
io.on('error', function(err) {
logger.error(err);
});
server.listen(app.get('port'), function() {
logger.verbose('Example app listening at ' + app.get('port'));
});
server.on('error', function(err) {
logger.error(err);
});