-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
95 lines (92 loc) · 1.98 KB
/
game.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
85
86
87
88
89
90
91
92
93
94
95
var config=require('./config.js');
var gameGroups=new Map();
function parseJSON(data){
if(data.type!=='utf8') return null;
try{
data=JSON.parse(data.utf8Data);
}catch(e){
return null;
}
return data;
}
function Game(code,viewSocket){
var _=this;
this.code=code;
this.viewSocket=viewSocket;
this.controls=new Map();
this.sendViewer({
'action': 'code',
'code': code
});
viewSocket
.on('message',function(data){
data=parseJSON(data);
if(data===null) viewSocket.close(4002,'format error');
var player=data.player;
if(player){
data.player=undefined;
_.sendOneControl(player,data)
}else
_.sendControl(data);
})
.on('close',function(){
_.end(4100,'lose viewer socket');
})
;
}
Game.prototype.join=function(socket){
var _=this;
var code=Math.floor(Math.random()*config.randRange).toString(36);
this.controls.set(code,socket);
this.sendViewer({
'action': 'join',
'player': code
});
socket
.on('message',function(data){
data=parseJSON(data);
if(data===null) socket.close(4002,'format error');
data.player=code;
_.sendViewer(data);
})
.on('close',function(){
_.sendViewer({
'action': 'close',
'player': code
});
})
;
}
Game.prototype.sendOneControl=function(player,data){
var control=this.controls.get(player);
if(control) control.sendUTF(JSON.stringify(data));
}
Game.prototype.sendControl=function(data){
data=JSON.stringify(data);
this.controls.forEach(function(control){
control.sendUTF(data);
});
}
Game.prototype.sendViewer=function(data){
this.viewSocket.sendUTF(JSON.stringify(data));
}
Game.prototype.end=function(code){
this.viewSocket.close(code);
this.controls.forEach(function(control){
control.close(code);
});
gameGroups.delete(this.code);
}
module.exports={
'create': function(code,viewSocket){
var group=new Game(code,viewSocket);
gameGroups.set(code,group);
return group;
},
'get': function(code){
return gameGroups.get(code);
},
'exists': function(code){
return gameGroups.has(code);
}
}