-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-connection.js
52 lines (43 loc) · 1.58 KB
/
game-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
var io = require('./server.js').io;
var Connection = require('./server.js').Connection;
var game_module = require('./game.js');
function GameConnection(socket, user, game) {
var game_room = game.room();
var Rooms = [game_room];
var Events = {
'Game.rematch': function(data) {
var settings = game.settings;
var error = game_module.verify_settings(settings);
if( error ) {
socket.emit('Toast.show', {
message: error,
type: 'error'
});
return;
}
var new_game = new game_module.Game(settings, user);
io.to('index').emit('Index.update', [new_game.serializeToLobby()] );
socket.emit('Room.join', new_game.id );
socket.broadcast.to(game_room).emit('Game.rematch', {
id: new_game.id,
who: user.username
});
game.rematch_id = new_game.id;
}
};
if( game.status == 'ended' ) {
socket.emit('Game.ended');
if( 'rematch_id' in game )
socket.emit('Game.rematch', { id: game.rematch_id });
}
Connection.init(socket, Rooms, Events);
var internal = game.game_type().Connection(socket, user, game);
Connection.init(socket, internal.Rooms, internal.Events);
return function() {
Connection.deinit(socket, Rooms, Events);
Connection.deinit(socket, internal.Rooms, internal.Events);
};
}
module.exports = {
GameConnection: GameConnection
};