forked from elbuo8/stop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketManager.js
51 lines (42 loc) · 1.51 KB
/
socketManager.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
module.exports = function(dependencies) {
var db = dependencies.db;
var io = dependencies.io;
return function mainHandler(socket) {
socket.on('join', function(info, ack) {
if (io.sockets.adapter.rooms[info.room] && io.sockets.adapter.rooms[info.room]['in-game']) {
return ack('IN GAME ALREADY');
}
socket.join(info.room, function(err) {
if (err) {
console.log(err);
return ack('ERROR');
}
ack('OK');
socket.to(info.room).emit('new player', { name: info.name });
socket.on('disconnect', function() {
socket.to(info.room).emit('player left', { name: info.name });
});
socket.on('start', function(game_details) {
io.sockets.adapter.rooms[info.room]['in-game'] = true;
io.to(info.room).emit('start game', { game: game_details });
socket.on('end', function() {
io.to(info.room).emit('send data');
socket.on('submission', function(data, fn) {
if (!(info.room in db)) {
db[info.room] = [];
}
db[info.room].push(data);
if (db[info.room].length >= io.sockets.adapter.rooms[info.room]) {
// calculate score
// emit winners to all
console.log('finished');
delete db[info.room];
delete io.sockets.adapter.rooms[info.room]['in-game'];
}
});
});
});
});
});
};
};