-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
276 lines (229 loc) · 6.8 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
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
var express = require('express');
var mongo = require('mongodb');
var ObjectID = require('mongodb').ObjectID;
//var DBRef = require('mongodb').DBRef;
var app = express()
,server = require('http').createServer(app);
var config = require('./config');
var db = new mongo.Db(
config.DB_NAME || 'drawing',
new mongo.Server(
config.DB_HOST || 'localhost',
config.DB_PORT || 27017,
{ auto_reconnect: true }
),
{ safe : true}
);
var gameData = require('./lib/gameData')(db);
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public'));
app.use(app.router);
var io = require('socket.io').listen(server);
io.set('log level', 1);
io.sockets.on('connection', function(socket) {
socket.on('draw', function(data) {
socket.broadcast.emit('draw', data);
});
socket.on('join', function(game) {
console.log("joining" , game);
socket.join(game + '/player');
socket.join(game);
});
socket.on('host', function(game) {
socket.join(game + '/host');
socket.join(game);
});
socket.on('drawCommand', function(cmd) {
io.sockets.in(cmd.game + '/host').emit('drawCommand', cmd);
});
});
function nextTurn(gameId, next) {
gameData.createTurn(gameId, function(err, turn) {
if (err) return next(err);
pushStateNotificationForGameId(gameId, next);
});
}
function generateStateMessage(game) {
if(!game || !game.turns || !game.turns.length) return {
state : 'waiting',
//turnId: null,
playerId :null,
turn :null
};
var turn = game.turns.pop();
var result = {
turn : turn,
//turnId : turn.identifier,
playerId : turn.playerIdentifier
};
if (!turn.word) {
result.state= 'word';
} else if (!turn.completed) {
result.state = 'drawing';
} else if (turn.completed) {
result.state = 'results';
}
return result;
}
function pushStateNotification(game, next) {
var msg = generateStateMessage(game);
io.sockets.in(game._id.toString()).emit('stateChange', msg);
if (next) {
next(null);
}
}
function pushStateNotificationForGameId(gameId, next) {
gameData.findGame(gameId, function(err, game) {
if (err) return next(err);
pushStateNotification(game, next);
//if (next) return next(game);
});
}
function pushScoreUpdate(gameId, next) {
gameData.findGame(gameId, function(err, game) {
if (err) return next(err);
io.sockets.in(game._id.toString() + '/host')
.emit('scoreUpdate', game.players);
console.log("PLAYER", game._id.toString() + '/host', game.players);
next();
});
}
app.post('/data/game/:id/join', function(req, res, next) {
console.log(req.body);
if (!req.params.id || !req.body.playerName) {
return res.send(400);
}
gameData.joinGame(req.params.id, req.body.playerName, function(err, identifier) {
if (err) return next(err);
res.send(identifier);
io.sockets.in(req.params.id + '/host').emit('playerJoined', {
name : req.body.playerName,
identifier: identifier,
score: 0
});
pushStateNotificationForGameId(req.params.id);
});
});
app.post('/data/game/:game/start', function(req, res, next) {
if (!req.params.game) return res.send(400);
nextTurn(req.params.game, function(err, turn) {
if (err) return next(err);
res.send(turn);
});
});
app.post('/data/game/:game/beginTurn', function(req, res, next) {
if (!req.body.word ) {
return res.send(400);
}
gameData.findTurn(req.params.game, function(err, turn) {
if (err) return next(err);
if (!turn) return res.send(404);
console.log(turn);
//validate word was actually one they had a choice of
if (turn.choices.indexOf(req.body.word) === -1) {
return res.send(400);
}
gameData.beginTurn(req.params.game, turn.identifier.toString(), req.body.word, function(err) {
if (err) return next(err);
res.send(200);
pushStateNotificationForGameId(req.params.game);
//Set turn to expire in 30s
var activeTurn = turn;
setTimeout(function() {
console.log("timer");
gameData.findTurn(req.params.game, function(err, turn) {
console.log(err,turn);
console.log(activeTurn);
if (!err && turn) {
if (turn.identifier.equals(activeTurn.identifier)) {
console.log('Expire');
passTurn(req.params.game, activeTurn.playerIdentifier.toString(), function(err) {
if (err) return next(err);
res.send({ });
});
}
}
});
}, 90 * 1000);
});
});
});
app.post('/data/game/:game/guessWord', function(req, res, next) {
if (!req.body.word || !req.body.playerIdentifier) return res.send(400);
gameData.findTurn(req.params.game, function(err, turn) {
console.log("cb findTurn", err, turn);
if (err) return next(err);
if (!turn || !turn.word) return res.send(404);
if (turn.word.toLowerCase() !== req.body.word.toLowerCase()) {
//todo: store guess
return res.send({
correct: false
});
}
//guessed the right word
gameData.completeTurn(req.params.game, turn.identifier.toString(), req.body.playerIdentifier, function(err) {
if (err) return next(err);
pushScoreUpdate(req.params.game, function(err) {
if (err) return next(err);
nextTurn(req.params.game, function(err, turn) {
if (err) return next(err);
res.send({ correct: true });
});
});
})
});
});
function passTurn(gameId, playerId, next) {
gameData.findTurn(gameId, function(err, turn) {
if (err) return next(err);
if (!turn) return res.send(404);
gameData.passTurn(gameId, turn.identifier.toString(), playerId, function(err) {
if (err) return next(err);
nextTurn(gameId, next);
})
});
}
app.post('/data/game/:game/pass', function(req, res, next) {
if (!req.params.game || !req.body.playerIdentifier) return res.send(400);
passTurn(req.params.game, req.body.playerIdentifier, function(err) {
if (err) return next(err);
res.send({ });
});
});
app.get('/data/game/:id/players', function(req, res, next) {
if (!req.params.id) return res.send(400);
gameData.findGame(req.params.id, function(err, game) {
if(err) return next(err);
if(!game) return res.send([]);
res.send(game.players || []);
});
});
app.get('/data/game/:id/state', function(req, res, next) {
if (!req.params.id) return res.send(400);
gameData.findGame(req.params.id, function(err, game) {
if(err) return next(err);
res.send(generateStateMessage(game));
});
});
///Pages
app.get('/game/:gameId', function(req, res) {
if (!/^[a-z0-9]{24}$/i.test(req.params.gameId)) return res.send(400);
res.render('host', {
gameId : req.params.gameId,
joinUrl : config.URL + '/play/' + req.params.gameId,
});
});
app.get('/play/:gameId', function(req, res) {
if (!/^[a-z0-9]{24}$/i.test(req.params.gameId)) return res.send(400);
res.render('play', {
gameId : req.params.gameId
});
});
app.get('/', function(req, res) {
res.redirect('/game/' + new ObjectID());
});
server.listen(config.PORT);