-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunication.js
84 lines (77 loc) · 1.96 KB
/
communication.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
module.exports = function(http){
var io = require('socket.io')(http);
var plNum = 0;
var onConnect = function(){};
var pls = {};
io.on('connection', function(socket){
plNum++;
io.emit('plNum', plNum);
var playerHandler = {};
socket.on('disconnect', function(){
plNum--;
delete pls[plId];
if(playerHandler.disconnect)
playerHandler.disconnect();
});
socket.on('move', function(move){
if(playerHandler.move)
playerHandler.move(move);
});
socket.on('shoot', function(){
if(playerHandler.shoot)
playerHandler.shoot();
});
socket.on('nextMap', function(){
if(playerHandler.map)
playerHandler.map();
});
socket.on('join', function(name){
name = name.replace(/\s/g,'');
if(name === "") name = "guest";
if(playerHandler.join)
playerHandler.join(name);
});
var plId = plNum;
pls[plId] = socket;
onConnect({
'gId': plId.toString(),
'sendPl': function(players){
socket.emit('players', players);
},
'sendJoinedCount': function(joinedCount){
socket.emit('players', joinedCount);
},
'sendBul': function(bullets){
socket.emit('bullets', bullets);
},
'changeMap': function(map){
socket.emit('map', map);
},
'sendSID': function(id){
socket.emit('sId', id);
},
'setHandler': function(handler){
playerHandler = handler;
}
});
});
this.connListener = function(callback){
onConnect = callback;
};
this.sendPl = function(players){
io.emit('players', players);
};
this.sendJoinedCount = function(joinedCount){
io.emit('joinedCount', joinedCount);
};
this.changeMap = function(map){
io.emit('map', map);
};
this.sendBul = function(bullets){
io.emit('bullets', bullets);
};
this.sendScore = function(score){
io.emit('score', score);
};
return this;
};