-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlobby-connection.js
83 lines (65 loc) · 2.59 KB
/
lobby-connection.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
var io = require('./server.js').io;
function LobbyConnection(socket, user, game) {
var game_room = game.room(), user_room = game_room + ':user:'+user.id;
function gameHasStarted() {
if( game.status != 'lobby' ) {
socket.emit('join', game.id);
return true;
}
return false;
}
var Rooms = [game_room, user_room];
var Events = {
'Lobby.sit': function onSit(i) {
if( gameHasStarted() )
return;
if( game.players.indexOf(user) != -1 )
return;
if( game.players[i] !== null )
return;
game.players[i] = user;
io.to(user_room).emit('Lobby.setIsSeated', true);
io.to('index').emit('Index.update', [game.serializeToLobby()] );
io.to(game_room).emit('Lobby.update', game.serializeToPregame() );
},
'Lobby.stand': function onStand() {
if( gameHasStarted() )
return;
var i = game.players.indexOf(user);
if( i == -1 )
return;
game.players[i] = null;
io.to(user_room).emit('Lobby.setIsSeated', false);
io.to('index').emit('Index.update', [game.serializeToLobby()] );
io.to(game_room).emit('Lobby.update', game.serializeToPregame() );
},
'Lobby.startGame': function() {
if( gameHasStarted() )
return;
if( user !== game.owner || game.players.indexOf(null) != -1 )
return;
game.logic = new (game.game_type().Logic)(game);
game.changeStatus('ongoing');
io.to(game_room).emit('Room.join', game.id );
}
};
socket.emit('Lobby.setGameTypeInfo', game.game_type().info );
socket.emit('Lobby.setSettings', game.settings );
// Warning, order of emits is important: owner -> player_list(update) -> seated
socket.emit('Lobby.setIsOwner', game.owner === user );
socket.emit('Lobby.update', game.serializeToPregame() );
socket.emit('Lobby.setIsSeated', game.players.indexOf(user) != -1 );
for( var i in Rooms )
socket.join(Rooms[i]);
for( var e in Events )
socket.on(e, Events[e]);
return function() {
for( var i in Rooms )
socket.leave(Rooms[i]);
for( var e in Events )
socket.removeListener(e, Events[e]);
};
}
module.exports = {
LobbyConnection: LobbyConnection
};