-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
463 lines (385 loc) · 12.7 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/******************************************
* KRIEGSPIEL
* Copyright 2013, Max Irwin
* MIT License
*
* HTTP and WebSocket Router
*
*******************************************/
var express = require('express')
, http = require('http')
, path = require('path')
, redis = require('redis')
, redstore = require('connect-redis')(express)
, sessionstore = new redstore()
, cookies = require('express/node_modules/cookie')
, connectutils = require('express/node_modules/connect/lib/utils')
, secrets = require('./secrets')
, db = require('./lib/db')
, security = require('./lib/security')
, spiel = require('./lib/spiel')
, variants = require('./lib/variants').load()
, cache = require('./lib/cache')
, lobby = require('./lib/lobby');
var app = express();
app.configure(function() {
// all environments
app.set('port', process.env.PORT || 80);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({secret:secrets.redis,store:sessionstore,cookie:{maxAge:4E9}}));
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
//Setup rethinkdb connection
db.setup();
//Setup spieler lobby
lobby.setup();
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
});
var okId = function(id) {
var reId = /^\w+$/i;
return reId.test(id);
}
var badId = function(res) {
res.redirect('/join?error=badid');
}
var newGame = function(req,res) {
var gameid = Math.floor(Math.random()*100000).toString(16);
res.redirect('/games/'+gameid);
};
var joinGame = function(req,res,gameid) {
res.redirect('/games/'+gameid);
};
//Shortcut file sender
var file = function(filename) { return function(req,res){ res.sendfile(filename); }};
//Sends the result of a merge or db response
var sender = function(res) {
return function(err,records){
if(err) res.send(500,err); else res.send(200,records);
};
}
//Merges the two color results
var merger = function(total,res) {
var output = [], count = 0;
return function(err,records) {
output = output.concat(records);
if (++count===total) {
sender(res)(null,output);
}
};
}
//Formats a player record for listing
var formatspieler = function(rec) {
rec.rating = Math.floor(rec.rating);
rec.joined = (new Date(rec.joined||'10/10/2013')).toDateString().substr(3);
return rec;
}
//Formats a game record for listing (hide secret opponent stuff)
var formatgame = function(rec) {
var fin = (typeof rec.result === 'object' && rec.result.type) ? (rec.result.white + '-' + rec.result.black) : "";
var out = {
gameid:rec.gameid,
white:rec.whiteusername,
black:rec.blackusername,
state:spiel.state(rec.state),
turn:rec.turn,
moves:rec.moves,
result:fin,
rated:rec.rated?'Rated':'Unrated',
startdate:rec.startdate,
enddate:(rec.enddate&&rec.enddate.toDateString)?rec.enddate.toDateString():''
};
if (rec.state===spiel.state('inactive')) {
out.player=out.white||out.black;
out.pcolor=out.white?'white':'black';
out.ocolor=out.black?'white':'black';
}
return out;
};
//-------------------------------------------
// Routes
app.get('/', file('public/index.html'));
app.get('/join/?', file('public/join.html'));
app.get('/about/?', file('public/about.html'));
app.get('/privacy/?', file('public/privacy.html'));
app.get('/robots.txt', file('public/robots.txt'));
app.get('/google70e3e038531249c6.html', file('public/google70e3e038531249c6.html'));
//Logs in or creates a new user
app.post('/join/?', function(req,res) {
security.loginOrCreateUser(req,res);
});
//Forgot Password Flow
app.get('/forgot/:code/?', file('public/forgot.html'));
app.post('/forgot/:code/?', function(req,res){
security.resetPassword(req,res);
});
app.post('/forgot/?', function(req,res) {
security.forgotPassword(req,res,function(err,msg) {
if(!err) res.sendfile('public/forgotsent.html');
else res.redirect('/join?error='+err);
});
});
//Starts a new Game:
app.post('/start/?', security.authenticateUser, function(req,res) {
var username = req.session.username;
var inactive = spiel.state('inactive');
db.findGamesByPlayerAndState(username,inactive,formatgame,function(err,records) {
if(!err && records.length) {
//Inactive game found
res.redirect('/games/'+records[0].gameid);
} else {
//No inactive games found - create a new one!
var gameid = Math.floor(Math.random()*100000).toString(16);
var variant = 'lovenheim'; //req.body.variant;
var color = req.body.startcolor;
var player = req.session.username;
var rated = true; //req.body.ratedgame === 'rated' ? true : false;
if (color === 'random') color = Math.floor(Math.random()*100)%2?'white':'black';
spiel.add(gameid,variant,color,player,rated,function(game){
res.redirect('/games/'+gameid);
});
}
});
});
app.get('/challenges/:gameid', security.authenticateUser, function(req,res) {
var gameid = req.params.gameid;
var username = req.session.username;
if (okId(gameid)) {
spiel.exists(gameid,function(dbgameid){
if(gameid) {
res.redirect('/games/'+gameid);
} else {
var variant = 'lovenheim';
var color = Math.floor(Math.random()*100)%2?'white':'black';
var player = req.session.username;
var rated = true;
spiel.add(gameid,variant,color,player,rated,function(game){
res.redirect('/games/'+gameid);
});
}
});
} else {
badId(res);
}
});
//Gets a list of games
app.get('/games/?', security.authenticateUser, function(req,res) {
var state = spiel.state(req.query.state||'active');
if (state===spiel.state('inactive')) {
//Get all the inactive games
db.findGamesByFilter({state:state},formatgame,function(err,results){
var send = sender(res);
if (err) {
send(err,results);
} else {
var outgoing = [];
for(var i=0,l=results.length;i<l;i++) {
if (lobby.isonline(results[i].player)) {
//only add online players to the waiting list!
outgoing.push(results[i]);
}
}
send(err,outgoing);
}
});
} else if (req && req.session && req.session.username) {
//Get the user's games (both as black and white)
var username = req.session.username;
db.findGamesByPlayerAndState(username,state,formatgame,sender(res));
} else {
//No session, No data for you!
req.send(204,false);
}
});
//Checks the database for an existing username
app.get('/usernames/:username',function(req,res){
security.existingUser(req.params.username,function(isExists){
res.send(200,isExists);
});
});
//Gets the user's session data
app.get('/session',function(req,res){
if(req.session && req.session.username) {
var data = {};
//Send user session information, but not cookie stuff
for(var s in req.session) { if(req.session.hasOwnProperty(s) && s!=='cookie') data[s] = req.session[s]; }
res.send(200,data);
} else {
//No session, No data for you!
res.send(204,false);
}
});
//Gets all the users that are online
app.get('/online',function(req,res){
res.send(200,{online:lobby.isonline()});
});
//Gets a game html file
app.get('/games/:gameid',security.authenticateUser,function(req,res){
var gameid = req.params.gameid;
var username = req.session.username;
if (okId(gameid)) {
spiel.find(gameid,function(game){
if(game.state === spiel.state('finished')) {
res.redirect('/replays/'+gameid);
} else if (game.white.username && game.white.username!==username && game.black.username && game.black.username!==username) {
res.redirect('/join?error=gameactive');
} else {
res.sendfile('public/game.html');
}
});
} else {
badId(res);
}
});
//Gets game replays
app.get('/replays/?',function(req,res){
if (req.query.username) {
//Only get finished games for a specific username
var username = req.query.username;
var finished = spiel.state('finished');
db.findGamesByPlayerAndState(username,finished,formatgame,sender(res));
} else {
//Gets all the finished games:
db.findGamesByFilter({state:spiel.state('finished')},formatgame,sender(res));
}
});
//Gets all game replays
app.get('/spielers/:username',file('public/spieler.html'));
//Gets player info
app.get('/spielers/?',function(req,res){
var filter = {};
if(req.query.username) filter.username=req.query.username;
db.findUsersByFilter(filter,formatspieler,sender(res));
});
//Gets a game replay
app.get('/replays/:gameid',function(req,res){
var gameid = req.params.gameid;
if (okId(gameid)) {
spiel.find(gameid,function(game){
console.log(game.state,spiel.state('finished'));
if(game.state === spiel.state('finished')) {
res.sendfile('public/replay.html');
} else if (game.white.username || game.black.username) {
res.redirect('/join?error=gameactive');
} else {
res.redirect('/join?error=gamenotfound');
}
});
} else {
badId(res);
}
});
//Gets game data for a finished game
app.get('/data/:gameid',security.authenticateUser,function(req,res){
var gameid = req.params.gameid;
if(okId(gameid)) {
spiel.find(gameid,function(game){
if (game.state===spiel.state('finished')) {
var data = game.serialize();
data.orientation = (req.session.username === data.blackusername) ? 'black' : 'white';
res.send(data);
} else {
//No peeking!
res.send(403);
}
});
} else {
badId(res);
}
});
//Logs out the user
app.get('/logout',security.authenticateUser,function(req,res){
req.session.destroy(function(){res.redirect('/join')});
});
//-------------------------------------------
// Express
var server = http.createServer(app).listen(app.get('port'), function(){
console.log('KRIEGSPIEL :: server listening on port ' + app.get('port'));
});
//-------------------------------------------
// WebSockets:
//Gets the connect-redis signed session cookie for the socket
var parseSessionCookie = function(cookie, callback) {
var parsed = cookies.parse(cookie);
var signed = connectutils.parseSignedCookies(parsed,secrets.redis);
if (signed && signed['connect.sid']) {
sessionstore.get(signed['connect.sid'],callback);
} else {
callback(null,null);
}
}
var io = require('socket.io').listen(server);
io.set('log level', 1); //not so loud!
io.configure(function(){
io.set('authorization',function(handshake,callback){
parseSessionCookie(handshake.headers.cookie, function(err,session) {
if (!err && session && session.username) {
callback(null,true);
} else if (!err) {
callback(null,false);
} else {
callback(err,false);
}
});
});
});
io.sockets.on('connection', function (socket) {
parseSessionCookie(socket.handshake.headers.cookie, function(err,session) {
if(!err && session && session.username) {
socket.set('username',session.username,function(){
socket.on('join', function (data) {
if(data.gameid) spiel.join(data.gameid, {session:session, socket:socket}, function(game) {
if (game.state===spiel.state('inactive')) {
var out = { gameid:game.gameid,white:game.white.username,black:game.black.username,state:'inactive',rated:game.rated?'Rated':'Unrated' };
out.player=out.white||out.black;
out.pcolor=out.white?'white':'black';
out.ocolor=out.black?'white':'black';
socket.broadcast.emit("joinadd",out);
} else {
socket.broadcast.emit("joinremove",{ gameid:game.gameid });
}
});
});
socket.on('move', function (data) {
if(data.gameid) spiel.move(data.gameid, data.source, data.target, data.scratch, data.promotion, {session:session, socket:socket});
});
socket.on('chat', function (data) {
if(data.gameid) spiel.chat(data.gameid, data.text, {session:session, socket:socket});
});
socket.on('pawncaptures', function (data) {
if(data.gameid) spiel.pawncaptures(data.gameid, {session:session, socket:socket});
});
socket.on('occupies', function (data) {
if(data.gameid) spiel.occupies(data.gameid, data.target, {session:session, socket:socket});
});
socket.on('resign', function (data) {
if(data.gameid) spiel.resign(data.gameid, {session:session, socket:socket});
});
socket.on('offerdraw', function (data) {
if(data.gameid) spiel.offerdraw(data.gameid, {session:session, socket:socket});
});
socket.on('acceptdraw', function (data) {
if(data.gameid) spiel.acceptdraw(data.gameid, {session:session, socket:socket});
});
socket.on('declinedraw', function (data) {
if(data.gameid) spiel.declinedraw(data.gameid, {session:session, socket:socket});
});
socket.on('ping', function (data) {
if(data.gameid) spiel.ping(data.gameid, {session:session, socket:socket});
});
socket.on('disconnect', function(data){
lobby.disconnect(socket,session.username);
});
lobby.connect(socket,session.username);
});
}
});
});