-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfleet.js
More file actions
305 lines (274 loc) · 11.1 KB
/
fleet.js
File metadata and controls
305 lines (274 loc) · 11.1 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
var simpleserver = require('./simpleserver').simpleserver;
var simpleclient = require('./simpleclient').simpleclient;
function fleet() {
this.colony = false; // no colony yet
this.port = 0; // port
this.host = ""; // Hostname
this.max = 2000; // Max number of users per server. if more users connect, they will be sent to another serer instance.
this.serverChangeRequests = {}; // used to track server change requests (when a server is full)
this.users = {};
this.perlevel = {};
}
// Receiving data from the Colony
fleet.prototype.onReceive = function(data) {
if (data.available && data.available === true && !this.serverChangeRequests[data.uuid]) {
this.log("Found a server:", data);
// Save the fact that we just received an available server (so that other servers who aare available will be ignored, to not send the same user to more than one server. First to answer get the user.)
this.serverChangeRequests[data.uuid] = data;
this.server.send(data.uuid, {
serverchange: {
host: data.host,
port: data.port
}
});
}
};
fleet.prototype.init = function() {
var scope = this;
var i;
if (!this.colony) {
this.log("You are not part of a colony yet");
return false;
}
// Tell the colony what is the max number of users we can accept
this.colony.max = this.max;
this.log("Starting server on port "+this.port);
// Share the number of online users
this.onlineCount = this.colony.share("online", 0);
if (this.onlineCount === false) {
this.onlineCount = this.colony.shared["online"];
this.log("We already have this var in memory: ",this.onlineCount.value);
}
// Share the number of users on each level
for (i=0;i<this.games.length;i++) {
this.perlevel[i] = this.colony.share("level_"+i, 0);
if (this.perlevel[i] === false) {
this.perlevel[i] = this.colony.shared["level_"+i];
this.log("We already have this var in memory: ",this.perlevel[i].value);
}
}
// Share the user list
this.users = this.colony.share("users", {});
if (this.users === false) {
this.users = this.colony.shared["users"];
this.log("We already have this var in memory: ",this.users.value);
}
this.server = new simpleserver(this.port, {
alias: "SERVER@FLEET:"+this.port, // To display on the console
onConnect: function(client) {
scope.log("Connected.","Client ID:" + client.uid, "Online on this instance:" + scope.server.ocount);
// Save the current number of online user.
// The Entity needs to be able to know how many users are online, to know if it can volunteer to receive more users.
scope.colony.ocount = scope.server.ocount;
// Update the shared memory (total number of online users accross the colony)
scope.onlineCount.add(1);
// Check if there is still place on this server
if (scope.server.ocount > scope.max) {
scope.log("Max number of users reached. Sending user to another server...");
// Tell the rest of the colony we are full. Any available entity will volunteer to receive the user.
scope.colony.broadcast({
full: true,
uuid: client.uid // sending the user's UUIDv4, to know who is available for him
});
return true;
}
// Send auth request: Auth and update on the number of users online
scope.server.send(client.uid, [
"auth",
{
online: scope.onlineCount.value
}
]);
},
onReceive: function(client, data) {
//scope.log("Message from #"+client.uid+":", data);
switch (data) {
case "ping":
scope.server.send(client.uid, "pong");
break;
default:
if (data.auth) {
// If the user provides an authtoken
if (data.auth.demo) {
scope.demomode = true;
} else {
scope.demomode = false;
}
if (data.auth.authtoken) {
// Save the authtoken
client.authtoken = data.auth.authtoken;
// Check Auth Token validity in the database
scope.sql.query("select * from authtokens where token='"+client.authtoken+"' and validity > "+(new Date().getTime()/1000), function(err, rows, fields) {
if (err) throw err;
if (rows.length > 0 && rows[0].id > 0) {
scope.info("Auth Token validated: ","UID: \t\t"+rows[0].uid,"Token: \t"+rows[0].token,"Validity: \t"+new Date(rows[0].validity*1000).toISOString());
scope.info(new Date(),new Date(scope.raceData.start_time*1000));
// register the user
var tmpObject = {};
tmpObject[client.uid] = {
token: client.uid,
uid: rows[0].uid,
authtoken: rows[0].token,
validity: rows[0].validity,
level: 0
};
scope.users.push(tmpObject);
// Check if the user is registered to the race
scope.sql.query("select * from races_registrations where uid='"+rows[0].uid+"' and rid="+scope.raceData.id, function(err, rows, fields) {
if (err) throw err;
if (rows.length > 0 && rows[0].id > 0) {
// register the user on the race
// Is the user already in the database, meaning he already played?
scope.sql.query("select * from races_scores where rid='"+scope.raceData.id+"' and uid='"+scope.users.value[client.uid].uid+"'", function(err, rows, fields) {
if (rows.length == 0) {
// register the user's participation
scope.sql.query("insert into races_scores (rid,uid,start_time) values ('"+scope.raceData.id+"','"+scope.users.value[client.uid].uid+"','"+Math.round(new Date().getTime()/1000)+"')", function(err, rows, fields) {
// Send to the client his UID (UUIDv4)
scope.server.send(client.uid, {
uid: client.uid,
start: scope.raceData.start_time,
seconds: Math.floor((new Date(scope.raceData.start_time*1000)-new Date())/1000),
mseconds: (new Date(scope.raceData.start_time*1000)-new Date())
});
// Broadcast the updated number of online users
scope.server.broadcast({
online: scope.onlineCount.value
}, [client.uid]);
});
} else {
// Check if the user is just reconnecting from a lost connection
scope.sql.query("select * from races_scores where rid='"+scope.raceData.id+"' and uid='"+scope.users.value[client.uid].uid+"' and score=0", function(err, rows, fields) {
scope.log("######################################################","select * from races_scores where rid='"+scope.raceData.id+"' and uid='"+scope.users.value[client.uid].uid+"' and score=0",rows);
if (rows.length == 0 && !data.auth.demo) {
// User was registered and already has a score for this race
scope.server.send(client.uid, {
played: true
});
scope.server.close(client.uid);
} else {
// user is reconnecting, we need to let him play
scope.server.send(client.uid, {
welcomeback:true,
uid: client.uid,
start: scope.raceData.start_time,
seconds: Math.floor((new Date(scope.raceData.start_time*1000)-new Date())/1000),
mseconds: (new Date(scope.raceData.start_time*1000)-new Date())
});
// Broadcast the updated number of online users
scope.server.broadcast({
online: scope.onlineCount.value
}, [client.uid]);
}
});
}
});
} else {
scope.server.send(client.uid, {
notregistered: true
});
scope.server.close(client.uid);
}
});
} else {
// Send the 'invalid_token' error to the user
scope.server.send(client.uid, {
invalid_token: client.authtoken
});
}
});
}
}
if (data.available && data.available === true) {
if (data.uuid = client.uid) {
// this is for us
}
}
if (data.setlevel != undefined) {
var perlevel_update = {}; // buffer for the broadcast
// Update the user count on each level
if (data.setlevel > 0) {
scope.perlevel[data.setlevel-1].substract(1);
perlevel_update[data.setlevel-1] = scope.perlevel[data.setlevel-1].value;
}
scope.perlevel[data.setlevel].add(1);
perlevel_update[data.setlevel] = scope.perlevel[data.setlevel].value;
scope.users.setsub({
label: client.uid,
prop: "level",
value: data.setlevel
});
// Braodcast the new user count
scope.server.broadcast({
perlevel: perlevel_update
});
}
if (data.scoredump) {
var perlevel_update = {}; // buffer for the broadcast
var totalscore = 0;
for (i in data.scoredump) {
if (!isNaN(data.scoredump[i].score)) {
totalscore += data.scoredump[i].score;
}
}
// unregister from this level
if (scope.perlevel[data.quitlevel] && scope.perlevel[data.quitlevel].substract) {
scope.perlevel[data.quitlevel].substract(1);
perlevel_update[data.quitlevel] = scope.perlevel[data.quitlevel].value;
scope.server.broadcast({
perlevel: perlevel_update
});
}
scope.info("SCORE SENT: ", totalscore);
// Register the score
if (!scope.demomode) {
scope.sql.query("update races_scores set score="+totalscore+", log='"+JSON.stringify(data.scoredump)+"', end_time='"+Math.round(new Date().getTime()/1000)+"' where uid="+scope.users.value[client.uid].uid+" and rid="+scope.raceData.id, function(err, rows, fields) {
});
} else {
scope.info("User in demo mode. Score won't be saved.");
}
}
break;
}
},
onQuit: function(client) {
scope.log("Client Quit:", client.uid);
// remove the user from the list
scope.log("**********************************************************************");
scope.log("**********************************************************************");
if (scope.users.value[client.uid].level && scope.perlevel[scope.users.value[client.uid].level]) {
scope.perlevel[scope.users.value[client.uid].level].substract(1);
}
scope.users.remove(client.uid);
scope.onlineCount.substract(1);
scope.server.broadcast({
online: scope.onlineCount.value
});
// Save the current number of online user.
// The Entity needs to be able to know how many users are online, to know if it can volunteer to receive more users.
scope.colony.ocount = scope.server.ocount;
}
});
};
fleet.prototype.log = function(){
var red, blue, reset;
red = '\u001b[31m';
blue = '\u001b[34m';
green = '\u001b[32m';
reset = '\u001b[0m';
console.log(green+"<FLEET>");
for (i in arguments) {
console.log(reset, arguments[i],reset);
}
};
fleet.prototype.info = function(){
var red, blue, reset;
red = '\u001b[31m';
blue = '\u001b[34m';
green = '\u001b[32m';
reset = '\u001b[0m';
console.log(green+"<FLEET>");
for (i in arguments) {
console.log(blue, arguments[i],reset);
}
};
exports.fleet = fleet;