This repository has been archived by the owner on Jul 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathserver.js
133 lines (120 loc) · 3.87 KB
/
server.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var connect = require('connect');
var WebSocketServer = require('ws').Server;
var Serializer = require('./lib/serializer.js');
var Config = require('./lib/config.js');
var Player = new require('./lib/player.js');
var server = connect.createServer();
server.use(connect.static(__dirname));
server.use(require('browserify')({
require: ['bison', __dirname + '/lib/serializer', __dirname + '/lib/config'],
mount : '/browserify.js',
}));
server.listen(Config.port);
var players = [];
var wss = new WebSocketServer({server: server});
WebSocketServer.prototype.broadcast = function(data, sender) {
for(var i in this.clients){
var receiver = this.clients[i];
if(sender === undefined || sender.sessionId != receiver.sessionId){
receiver.send(data);
}
}
};
wss.on('connection', function(client){
addPlayer(client);
sendPing(client);
initializeRestart();
client.on('message', function(data){
var msg = Serializer.deserialize(data);
// TODO: check if valid before sending data to clients
if (msg.t) {
switch (msg.t) {
case Serializer.MSG_PLAYER_POSITION:
msg["i"] = getPlayerId(client.sessionId);
wss.broadcast(Serializer.serialize(msg.t, msg), client);
break;
case Serializer.MSG_NEW_SHELL:
msg.tankId = getPlayerId(client.sessionId);
wss.broadcast(Serializer.serialize(msg.t, msg), client);
break;
case Serializer.MSG_PING:
var now = Date.now();
console.log("Ping[" + getPlayerId(client.sessionId) + "]: " + (now - msg.time) + "ms");
sendPing(client);
break;
default:
console.log("unknown message:" + msg);
}
}
});
client.on('close',function(){
console.log('Client disconnected');
removePlayer(client.sessionId);
});
});
// removes player id so server does not send it for new players
var removePlayer = function(sid){
var msg = Serializer.serialize(Serializer.MSG_GONE_PLAYER, {i: getPlayerId(sid)});
wss.broadcast(msg);
for (var i=0; i < players.length; i++) {
if(players[i].sid == sid){
players.splice(i, 1);
break;
}
}
console.log(players.length + " players left");
};
// let new player know about existing players
// TODO: not efficient, send in one message
var introduceExistingSprites = function(client){
for (var x=0; x < players.length; x++) {
var sp = Serializer.serialize(Serializer.MSG_NEW_PLAYER, {i: players[x].id});
client.send(sp);
}
};
var getPlayerBySid = function(sid){
for (var i=0; i < players.length; i++) {
if(players[i].sid == sid){
return players[i];
}
}
console.log("Player does not exist!" + sid);
return null;
};
// return "internal" player id given client's sessionId
var getPlayerId = function(sid){
var player = getPlayerBySid(sid);
if (player) {
return player.id;
}
return NaN;
};
var addPlayer = function(client){
client.sessionId = Math.floor((1 + Math.random()) * 0x1000000).toString(16);
// notify everyone about new player, pass player id
var p = new Player(client.sessionId);
var pser = Serializer.serialize(Serializer.MSG_NEW_PLAYER, {i: p.id});
// notify everyone except new player
wss.broadcast(pser, client.sessionId);
// notify new player about existing game objects and positions
introduceExistingSprites(client);
players.push(p);
console.log("Player added " + p.id);
};
var sendPing = function(client) {
setTimeout(function() {
if (getPlayerBySid(client.sessionId)){
var timestamp = String(Date.now());
client.send(Serializer.serialize(Serializer.MSG_PING, {time: timestamp}));
}
}, 3000);
};
var initializeRestart = function(){
var after = 1;
// if (players.length > 2) {
// after = 30;
// };
var msg = Serializer.serialize(Serializer.MSG_RESTART_GAME, {ta: after});
wss.broadcast(msg);
};
console.log('Server is running on http://'+Config.ip+':'+Config.port);