-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame_io.js
48 lines (41 loc) · 1.27 KB
/
game_io.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
var game = require('./game')
, uid = require('./uid')
, _ = require('underscore');
module.exports = function(io) {
// Client -> Server
io.sockets.on('connection', function(socket) {
var joined = false;
socket.on('join', function(nick, callback) {
if (_(game.getNicks()).include(nick) || nick.trim().length === 0 || joined === true) {
callback(false);
} else {
joined = true;
var id = uid();
socket.on('startGame', function(callback) {
game.playerJoined(id, nick);
callback(game.getState());
});
socket.on('startMoving', function(direction, callback) {
game.playerStartedMoving(id, direction);
if (callback) callback();
});
socket.on('stopMoving', function(callback) {
game.playerStoppedMoving(id);
if (callback) callback();
});
socket.on('fire', function(callback) {
game.playerFired(id);
if (callback) callback();
});
socket.on('disconnect', function() {
game.playerLeft(id);
});
callback(true);
}
});
});
// Server -> Client
game.on('stateDelta', function(stateDelta) {
io.sockets.emit('gameStateDelta', stateDelta);
});
};