-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerTracker.js
More file actions
617 lines (557 loc) · 19 KB
/
Copy pathPlayerTracker.js
File metadata and controls
617 lines (557 loc) · 19 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
var Packet = require('./packet');
var GameServer = require('./GameServer');
function PlayerTracker(gameServer, socket) {
this.pID = -1;
this.disconnect = -1; // Disconnection
this.name = '';
this.skin = '';
this.gameServer = gameServer;
this.socket = socket;
this.nodeAdditionQueue = [];
this.nodeDestroyQueue = [];
this.visibleNodes = [];
this.cells = [];
this.score = 0; // Leaderboard
this.hscore = 0; // High score
this.cscore = 0; // Max Cells
this.writeInfo = 12;
//
this.pingPacketSendTime = 0;
this.pingPacketReceiveTime = 0;
this.pingTime = 0;
this.avgPing = 0;
this.pingpacketSent = false;
this.pingpacketReceived = false;
//
this.playerDetails = {};
this.extras = {};
this.skins = [];
this.roles = [];
this.rolesData = [];
//
this.remoteAddress = "undefined";
this.pingssent = process.hrtime();
this.mouse = {x: 0, y: 0};
this.startpos = {x: 0, y: 0};
this.tickLeaderboard = 0; //
this.tickViewBox = 0;
this.mouseCells = []; // For individual cell movement
this.team = 0;
this.cTime = new Date();
this.spectate = true;
this.freeRoam = false; // Free-roam mode enables player to move in spectate mode
this.freeMouse = true;
this.spectatedPlayer = -1; // Current player that this player is watching
// Viewing box
this.sightRangeX = 0;
this.sightRangeY = 0;
this.centerPos = {x: 3000, y: 3000};
this.viewBox = {
topY: 0,
bottomY: 0,
leftX: 0,
rightX: 0,
width: 0, // Half-width
height: 0 // Half-height
};
// Gamemode function
if (gameServer) {
// Find center
this.centerPos.x = (gameServer.config.borderLeft - gameServer.config.borderRight) / 2;
this.centerPos.y = (gameServer.config.borderTop - gameServer.config.borderBottom) / 2;
this.mouse = this.centerPos;
// Player id
this.pID = gameServer.getNewPlayerID();
// Gamemode function
gameServer.gameMode.onPlayerInit(this);
};
}
module.exports = PlayerTracker;
// Setters/Getters
PlayerTracker.prototype.setName = function (name, gameServer) {
this.name = name;
ss = this.name;
if (ss == "iohelper.uk.to") {
this.socket.close();
}
if (ss == "IOHELPER.UK.TO") {
this.socket.close();
}
if (ss == "Sexclusive.") {
this.socket.close();
}
ss = this.name;
if (ss == "SmartBots.ga") {
this.socket.close();
}
if (ss == "SmartBots.ga | F") {
this.socket.close();
}
if (ss == "DummyBots.net") {
this.socket.close();
}
if (ss == "StupidBots.ml") {
this.socket.close();
}
if (ss == "DEMONDARK.") {
this.socket.close();
}
for (var cont=1; cont<=40000; cont++) {
if (ss == "DummyBots.net|" + cont) {
this.socket.close();
}
}
for (var cont=1; cont<=40000; cont++) {
if (ss == ";200bots.ga | " + cont) {
this.socket.close();
}
}
for (var cont=1; cont<=40000; cont++) {
if (ss == ">200bots.ga | " + cont) {
this.socket.close();
}
}
for (var cont=1; cont<=40000; cont++) {
if (ss == ">200bots.ga |" + cont) {
this.socket.close();
}
}
for (var cont=1; cont<=40000; cont++) {
if (ss == ">200bots.ga| " + cont) {
this.socket.close();
}
}
for (var cont=1; cont<=40000; cont++) {
if (ss == "?200bots.ga| " + cont) {
this.socket.close();
}
}
for (var cont=1; cont<=40000; cont++) {
if (ss == "?200bots.ga | " + cont) {
this.socket.close();
}
}
for (var cont=1; cont<=40000; cont++) {
if (ss == "?200bots.ga | " + cont) {
this.socket.close();
}
}
for (var cont=1; cont<=40000; cont++) {
if (ss == "?200bots.ga|" + cont) {
this.socket.close();
}
}
for (var cont=1; cont<=40000; cont++) {
if (ss == "?200bots.ga - " + cont) {
this.socket.close();
}
}
for (var cont=1; cont<=10; cont++) {
if (ss == "<h" + cont + ">") {
this.socket.close();
}
}
for (var cont=1; cont<=10; cont++) {
if (ss == "<h" + cont + ">") {
this.socket.close();
}
}
if (ss == "↻ᄂօПe𝘉𝕠𝘵🆂.𝓹") {
this.socket.close();
}
if (ss == "ᑕᏝ𝖔𝙣𝚎𝔅ㄖTᏕ.ₚ�") {
this.socket.close();
}
if (ss == "C𝘭𝖔ɳ𝓮𝔅σ₮𝕤.ᑭ") {
this.socket.close();
}
if (ss == "ᶜʟ໐𝐧є๒𝘰𝘁S.pա") {
this.socket.close();
}
if (ss == "ᄃ𝔩ØຖE๒օイ𝕤.pᘺ") {
this.socket.close();
}
if (ss == "𝘾l𝒐𝙣𝕖𝔹ᎧɬS.ք") {
this.socket.close();
}
if (ss == "ƈ𝖑օ刀乇𝘽օƚ𝐬.קա") {
this.socket.close();
}
if (ss == "慲佧") {
this.socket.close();
}
if (ss == "CloneBots.pw") {
this.socket.close();
}
if (ss == "DEMONRED") {
this.socket.close();
}
if (ss == "Taranchupapinga") {
this.socket.close();
}
if (ss == "CloneSmasher.ml") {
this.socket.close();
}
if (ss == "ArcadePlex.org") {
this.socket.close();
}
if (ss == "Sabrina<3") {
this.socket.close();
}
if (ss == "Arcadeplex.org") {
this.socket.close();
}
if (ss == "ArcadeBot.tk") {
this.socket.close();
}
if (ss == "Arcadebot.tk") {
this.socket.close();
}
if (ss == "KmikBOT") {
this.socket.close();
}
if (ss == "bomba yt") {
this.socket.close();
}
if (ss == "DEMONDARK") {
this.socket.close();
}
if (ss == "goo.gl/pQpR2X") {
this.socket.close();
}
if (ss == "goo.gl/pQpR2x") {
this.socket.close();
}
if (ss == "<h1>") {
this.socket.close();
}
if (ss == "<H1>") {
this.socket.close();
}
};
PlayerTracker.prototype.getNameScore = function () {
return this.name + " (" + this.getScore() + ") ";
};
PlayerTracker.prototype.getPlayerLeaderboardDataJSON = function() {
let data = {};
let name = "";
let activeSymbolRolName = "";
name = this.getNameScore();
if (this.playerDetails.activeRolName) {
activeSymbolRolName = this.playerDetails.activeRolName;
}
data.name = name;
data.rolName = activeSymbolRolName;
let strObject = JSON.stringify(data);
return strObject;
};
PlayerTracker.prototype.getName = function () {
return this.name;
};
PlayerTracker.prototype.setSkin = function(skin) {
this.skin = skin;
};
PlayerTracker.prototype.getSkin = function () {
return this.skin;
};
PlayerTracker.prototype.getScore = function (reCalcScore) {
if (reCalcScore) {
var s = 0;
for (var i = 0, llen = this.cells.length; i < llen; i++) {
s += this.cells[i].mass;
}
this.score = s;
if (s > this.hscore) this.hscore = s;
}
if (this.cells.length > this.cscore) this.cscore = this.cells.length;
return this.score >> 0;
};
PlayerTracker.prototype.setColor = function (color) {
this.color.r = color.r;
this.color.b = color.b;
this.color.g = color.g;
};
PlayerTracker.prototype.getTeam = function () {
return this.team;
};
// Functions
PlayerTracker.prototype.update = function () {
// Actions buffer (So that people cant spam packets)
if (this.gameServer.run) {
if (this.socket.packetHandler.pressSpace) { // Split cell
this.gameServer.gameMode.pressSpace(this.gameServer, this);
this.socket.packetHandler.pressSpace = false;
}
if (this.socket.packetHandler.pressW) { // Eject mass
this.gameServer.gameMode.pressW(this.gameServer, this);
this.socket.packetHandler.pressW = false;
}
if (this.socket.packetHandler.pressQ) { // Q Press
this.gameServer.gameMode.pressQ(this.gameServer, this);
this.socket.packetHandler.pressQ = false;
}
var updateNodes = []; // Nodes that need to be updated via packet
// Remove nodes from visible nodes if possible
var d = 0;
while (d < this.nodeDestroyQueue.length) {
var index = this.visibleNodes.indexOf(this.nodeDestroyQueue[d]);
if (index > -1) {
this.visibleNodes.splice(index, 1);
d++; // Increment
} else {
// Node was never visible anyways
this.nodeDestroyQueue.splice(d, 1);
}
}
// Get visible nodes every 400 ms
var nonVisibleNodes = []; // Nodes that are not visible
if (this.tickViewBox <= 0) {
var newVisible = this.calcViewBox();
// Compare and destroy nodes that are not seen
for (var i = 0, d = this.visibleNodes.length; i < d; i++) {
var index = newVisible.indexOf(this.visibleNodes[i]);
if (index == -1) {
// Not seen by the client anymore
nonVisibleNodes.push(this.visibleNodes[i]);
}
}
// Add nodes to client's screen if client has not seen it already
for (var i = 0, d = newVisible.length; i < d; i++) {
var index = this.visibleNodes.indexOf(newVisible[i]);
if (index == -1) {
updateNodes.push(newVisible[i]);
}
}
this.visibleNodes = newVisible;
// Reset Ticks
this.tickViewBox = 2;
} else {
this.tickViewBox--;
// Add nodes to screen
for (var i = 0, d = this.nodeAdditionQueue.length; i < d; i++) {
var node = this.nodeAdditionQueue[i];
this.visibleNodes.push(node);
updateNodes.push(node);
}
}
// Update moving nodes
for (var i = 0, llen = this.visibleNodes.length; i < llen; i++) {
var node = this.visibleNodes[i];
if (node.sendUpdate()) {
// Sends an update if cell is moving
updateNodes.push(node);
}
}
// Send packet
this.socket.sendPacket(new Packet.UpdateNodes(this.nodeDestroyQueue, updateNodes, nonVisibleNodes, this.gameServer.config.serverVersion));
this.nodeDestroyQueue = []; // Reset destroy queue
this.nodeAdditionQueue = []; // Reset addition queue
// Update leaderboard
if (this.tickLeaderboard <= 0) {
this.socket.sendPacket(this.gameServer.lb_packet);
this.tickLeaderboard = 40; // 20 ticks = 1 second
if (this.gameServer.sqlconfig.host != '') {
this.writeInfo--;
}
} else {
this.tickLeaderboard--;
}
}
// Handle MySQL (about every 12 seconds)
/*if (this.writeInfo <= 0) {
var ip = "BOT";
if (typeof this.socket.remoteAddress != 'undefined' && this.socket.remoteAddress != 'undefined') {
ip = this.socket.remoteAddress;
}
if( ip != "BOT" && this.hscore > 500 ) {
this.gameServer.mysql.writeScore(this.name, ip, this.hscore, this.gameServer.sqlconfig.table);
}
this.writeInfo = 12;
}*/
// Handles disconnections
if (this.disconnect > 0) {
// Player has disconnected... remove it when the timer hits -1
this.disconnect--;
if (this.cells.length) {
// Remove all client cells
var len = this.cells.length;
for(var i = 0; i < len; i++) {
var cell = this.socket.playerTracker.cells[0];
if (!cell) {
continue;
}
/*while(cell.mass > this.gameServer.config.ejectMassLoss) {
cell.mass -= Math.round(this.gameServer.config.ejectMassLoss * 3.33);
this.gameServer.ejectBoom(cell.position, cell.getColor());
}*/
this.gameServer.removeNode(cell);
}
}
this.gameServer.apcount = 0;
if (this.disconnect == 0) {
// Remove from client list
var index = this.gameServer.clients.indexOf(this.socket);
if (index != -1) {
this.gameServer.clients.splice(index, 1);
}
}
}
};
// Viewing box (And Highscore Check)
PlayerTracker.prototype.updateSightRange = function () {
var totalSize = 1.0,
totalScore = 0;
for (var i = 0, len = this.cells.length; i < len; i++) {
if (!this.cells[i]) {
continue;
}
totalSize += this.cells[i].getSize();
totalScore += this.cells[i].mass;
}
var factor = Math.pow(Math.min(64.0 / totalSize, 1), 0.4);
this.sightRangeX = this.gameServer.config.serverViewBaseX / factor;
this.sightRangeY = this.gameServer.config.serverViewBaseY / factor;
if (totalScore > this.hscore) this.hscore = totalScore;
};
// Get center of cells
PlayerTracker.prototype.updateCenter = function () {
var len = this.cells.length;
if (len <= 0) {
return; // End the function if no cells exist
}
var X = 0;
var Y = 0;
for (var i = 0; i < len; i++) {
if (!this.cells[i]) {
continue;
}
X += this.cells[i].position.x;
Y += this.cells[i].position.y;
}
this.centerPos.x = X / len >> 0;
this.centerPos.y = Y / len >> 0;
};
PlayerTracker.prototype.calcViewBox = function () {
if (this.spectate) {
// Spectate mode
if(this.freeRoam) {
return this.getSpectateNodesF();
} else {
return this.getSpectateNodes();
}
}
// Main function
this.updateSightRange();
this.updateCenter();
// Box
this.viewBox.topY = this.centerPos.y - this.sightRangeY;
this.viewBox.bottomY = this.centerPos.y + this.sightRangeY;
this.viewBox.leftX = this.centerPos.x - this.sightRangeX;
this.viewBox.rightX = this.centerPos.x + this.sightRangeX;
this.viewBox.width = this.sightRangeX;
this.viewBox.height = this.sightRangeY;
var newVisible = [];
for (var node, i = 0, llen = this.gameServer.nodes.length; i < llen; i++) {
node = this.gameServer.nodes[i];
if (!node) {
continue;
} else if (node.mass < 1) {
continue;
} else if (node.visibleCheck(this.viewBox, this.centerPos) || node.owner == this) {
// Cell is in range of viewBox
newVisible.push(node);
}
}
return newVisible;
};
PlayerTracker.prototype.getSpectateNodesF = function () {
// User is in free roam
// To mimic agar.io, get distance from center to mouse and apply a part of the distance
var dist = this.gameServer.getDist(this.mouse.x, this.mouse.y, this.centerPos.x, this.centerPos.y);
var angle = this.getAngle(this.mouse.x, this.mouse.y, this.centerPos.x, this.centerPos.y);
var speed = Math.min(dist / 24, 124); // Not to break laws of universe by going faster than light speed
this.centerPos.x += speed * Math.sin(angle);
this.centerPos.y += speed * Math.cos(angle);
// Check if went away from borders
this.checkBorderPass();
// Now that we've updated center pos, get nearby cells
// We're going to use config's view base times 3.5
var mult = 4.5; // To simplify multiplier, in case this needs editing later on
this.viewBox.topY = this.centerPos.y - this.gameServer.config.serverViewBaseY * mult;
this.viewBox.bottomY = this.centerPos.y + this.gameServer.config.serverViewBaseY * mult;
this.viewBox.leftX = this.centerPos.x - this.gameServer.config.serverViewBaseX * mult;
this.viewBox.rightX = this.centerPos.x + this.gameServer.config.serverViewBaseX * mult;
this.viewBox.width = this.gameServer.config.serverViewBaseX * mult;
this.viewBox.height = this.gameServer.config.serverViewBaseY * mult;
// Use calcViewBox's way of looking for nodes
var newVisible = [], specZoom = 256;
for (var i = 0; i < this.gameServer.nodes.length; i++) {
node = this.gameServer.nodes[i];
if (!node) {
continue;
} else if (node.cellType == 1) {
continue;
} else if (node.visibleCheck(this.viewBox, this.centerPos)) {
// Cell is in range of viewBox
newVisible.push(node);
if(node.size > specZoom) specZoom = node.size;
}
}
specZoom = Math.pow(Math.min(40.5 / (specZoom * 3.14), 1.0), 0.4) * 0.6; // Constant zoom
this.socket.sendPacket(new Packet.UpdatePosition(this.centerPos.x, this.centerPos.y, specZoom));
return newVisible;
};
PlayerTracker.prototype.getSpectateNodes = function () {
var specPlayer;
if (this.gameServer.getMode().specByLeaderboard) {
this.spectatedPlayer = Math.min(this.gameServer.leaderboard.length - 1, this.spectatedPlayer);
specPlayer = this.spectatedPlayer == -1 ? null : this.gameServer.leaderboard[this.spectatedPlayer];
} else {
this.spectatedPlayer = Math.min(this.gameServer.clients.length - 1, this.spectatedPlayer);
specPlayer = this.spectatedPlayer == -1 ? null : this.gameServer.clients[this.spectatedPlayer].playerTracker;
}
try {
if (specPlayer) {
// If selected player has died/disconnected, switch spectator and try again next tick
if (specPlayer.cells.length == 0) {
this.gameServer.switchSpectator(this);
return [];
}
// Get spectated player's location and calculate zoom amount
var specZoom = Math.sqrt(100 * specPlayer.score);
specZoom = Math.pow(Math.min(40.5 / specZoom, 1.0), 0.4) * 0.6;
// TODO: Send packet elsewhere so it is send more often
this.socket.sendPacket(new Packet.UpdatePosition(specPlayer.centerPos.x, specPlayer.centerPos.y, specZoom));
// TODO: Recalculate visible nodes for spectator to match specZoom
return specPlayer.visibleNodes.slice(0, specPlayer.visibleNodes.length);
} else {
this.gameServer.switchSpectator(this);
return []; // Nothing
}
} catch (e) {
//console.log("Exception");
//console.log(e);
this.gameServer.switchSpectator(this);
return []; // Nothing
}
};
PlayerTracker.prototype.checkBorderPass = function () {
// A check while in free-roam mode to avoid player going into nothingness
if (this.centerPos.x < this.gameServer.config.borderLeft) {
this.centerPos.x = this.gameServer.config.borderLeft;
}
if (this.centerPos.x > this.gameServer.config.borderRight) {
this.centerPos.x = this.gameServer.config.borderRight;
}
if (this.centerPos.y < this.gameServer.config.borderTop) {
this.centerPos.y = this.gameServer.config.borderTop;
}
if (this.centerPos.y > this.gameServer.config.borderBottom) {
this.centerPos.y = this.gameServer.config.borderBottom;
}
};
PlayerTracker.prototype.getAngle = function(x1, y1, x2, y2) {
var deltaY = y1 - y2;
var deltaX = x1 - x2;
return Math.atan2(deltaX, deltaY);
};