-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
133 lines (103 loc) · 2.86 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var express = require('express'),
Game = require('./lib/game.js'),
Map = require('./lib/map.js');
_ = require('underscore'),
Channel = require('./lib/channel.js'),
base64 = require('./lib/base64.js');
var app = express.createServer();
Channel.listen(app);
app.use(express.bodyParser()); // for parsing incoming json + www-encoded-forms into req.body
app.use(express.cookieParser()); // supports sessions
app.use(express.session({ secret: 'pacman' }));
app.use(express.favicon());
app.set('view engine', 'ejs');
/*
* Menu-type screens:
*/
app.get('/', function(req, res){
Map.list(function(err, maps) {
if (!err) {
res.render("index", {
maps: maps
, _:_
});
}
else res.render('error');
});
});
app.get('/help', function(req, res){
res.render("help");
});
app.get('/map/create', function(req, res){
res.render("create_map");
});
/*
* Game on!
*/
app.get('/game/:id/', function(req, res, next) {
if (!req.params.id) next()
Game.get(req.params.id, function(err, game) {
if( err ){ res.render('error', {error:err}); return;}
Map.get(game.map_id, function(err, map) {
if( err ){ res.render('error', {error:err}); return; }
res.setHeader('Content-Type', 'text/html');
res.render("game", {
map: map,
game: game,
production: process.env['NODE_ENV'] === 'production',
_:_
});
});
});
});
/*
* Game API
*/
app.post('/api/game/create', function(req, res, next) {
var params = {
map_id: req.body.map_id,
avaliable_characters:[],
characters:{},
users:{},
open:true,
score: 0,
mode: 'normal',
timestamp: Date.now()
};
Game.create(base64.encode('' + Date.now()), params, function(err, game) {
res.end(JSON.stringify({error: err, game: game}));
});
});
app.get('/api/game/list', function(req, res, next) {
Game.list(function(err, games) {
res.end(JSON.stringify({error:err, games:games}));
});
});
app.get('/api/game/:id/', function(req, res, next) {
Game.get(req.params.id, function(err, game) {
res.end(JSON.stringify({error:err, game:game}));
});
});
/*
* Map API
*/
app.post('/api/map/create', function(req, res, next) {
req.body.id = req.body.id.replace(/\s+/g, '+');
Map.create(req.body.id, req.body, function(err, map) {
res.end(JSON.stringify({error:err, map:map}));
});
});
app.get('/api/map/list', function(req, res, next) {
Map.list(function(err, maps) {
res.end(JSON.stringify({error:err, maps:maps}));
});
});
app.get('/api/map/:id/', function(req, res, next) {
Map.get(req.params.id, function(err, map) {
res.end(JSON.stringify({error:err, map:map}));
});
});
// Jim: moved this to after the app.get()s because it was taking over from ejs with index etc
app.use('/static', express.static(__dirname + '/public', { maxAge: 0 }));
console.log('started server');
app.listen(80);