-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.js
1226 lines (1133 loc) · 28.2 KB
/
app.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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var express = require('express');
var app = express();
var serv = require('http').Server(app);
var colors = require('colors/safe');
var middleware = require('socketio-wildcard')();
var debug = typeof v8debug === 'object' || /--debug/.test(process.execArgv.join(' '));
console.log(colors.green("[jsShooter] Starting server..."));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/client/index.html');
});
app.use('/client', express.static(__dirname + '/client'));
//---------- Server settings ----------
var MAX_SOCKET_ACTIVITY_PER_SECOND = 1000;
var fps = 30;
//-------------------------------------
var port = process.env.PORT || 80;
if (process.env.PORT == undefined) {
console.log(colors.blue("[jsShooter] No port defined using default (80)"));
}
serv.listen(port);
var io = require("socket.io")(serv, {});
io.use(middleware);
console.log(colors.green("[jsShooter] Socket started on port " + port));
var SOCKET_LIST = {};
var SOCKET_ACTIVITY = {};
var PLAYER_LIST = {};
var BULLET_LIST = {};
var BLOCK_LIST = {};
var ATTACKER_LIST = {};
var NPCSHOOTER_LIST = {};
var POWERUP_LIST = {};
// ---------- Entities ----------
// Npc shooter object
var NPCShooter = function (id, x, y) {
var self = {
id: id,
x: x,
y: y,
targetPlayer: -1,
hp: 3,
activationTimer: 100
}
if (countOPPlayers() > 0) {
self.hp = 10;
}
self.fireBullet = function () {
try {
let bID = Math.random() * 2000;
let target = PLAYER_LIST[self.targetPlayer];
BULLET_LIST[bID] = Bullet(bID, -1, self.x, self.y, Math.atan2(target.y - self.y, target.x - self.x) * 180 / Math.PI, 1);
} catch (err) {
if (debug) {
throw err;
}
}
}
self.update = function () {
if (self.activationTimer > 0) {
self.activationTimer--;
} else {
try {
let dist = {};
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (countOPPlayers() < 1) {
if (player.joinKickTimeout < 0 && player.spawnCooldown < 0) {
let d = getDistance(self.x, self.y, player.x, player.y);
dist[player.id] = d;
}
} else {
if (isOverPower(player)) {
let d = getDistance(self.x, self.y, player.x, player.y);
dist[player.id] = d;
}
}
}
let target = getSmallest(dist);
if (!(target == undefined)) {
self.targetPlayer = target;
if (getDistance(self.x, self.y, PLAYER_LIST[self.targetPlayer].x, PLAYER_LIST[self.targetPlayer].y) > 200 && countOPPlayers() < 1) {
let dir = Math.atan2(PLAYER_LIST[self.targetPlayer].y - self.y, PLAYER_LIST[self.targetPlayer].x - self.x) * 180 / Math.PI;
self.x += Math.cos(dir / 180 * Math.PI) * 0.5;
self.y += Math.sin(dir / 180 * Math.PI) * 0.5;
}
} else {
self.targetPlayer = -1;
}
} catch (err) {
if (debug) {
throw err;
}
}
}
if (self.hp <= 0) {
delete NPCSHOOTER_LIST[self.id];
}
}
return self;
}
// NPC attacker object
var NPCAttacker = function (id, x, y) {
var self = {
id: id,
x: x,
y: y,
targetPlayer: -1,
attackCooldown: -1,
hp: 5,
activationTimer: 100
}
self.update = function () {
if (self.activationTimer > 0) {
self.activationTimer--;
} else {
try {
let dist = {};
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (player.joinKickTimeout < 0 && player.spawnCooldown < 0) {
let d = getDistance(self.x, self.y, player.x, player.y);
dist[player.id] = d;
}
}
let target = getSmallest(dist);
if (!(target == undefined)) {
self.targetPlayer = target;
} else {
self.targetPlayer = -1;
}
if (!(self.targetPlayer == -1)) {
if (getDistance(self.x, self.y, PLAYER_LIST[self.targetPlayer].x, PLAYER_LIST[self.targetPlayer].y) > 8) {
let dir = Math.atan2(PLAYER_LIST[self.targetPlayer].y - self.y, PLAYER_LIST[self.targetPlayer].x - self.x) * 180 / Math.PI;
self.x += Math.cos(dir / 180 * Math.PI) * 2;
self.y += Math.sin(dir / 180 * Math.PI) * 2;
}
}
} catch (err) {
if (debug) {
throw err;
}
}
}
if (self.attackCooldown > 0) {
self.attackCooldown--;
} else {
if (!self.activationTimer > 0) {
if (countActivePlayers() > 0) {
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (getDistance(self.x, self.y, player.x, player.y) < 10 && player.powerupTime < 1) {
player.hp--;
self.attackCooldown = (1000 / fps) * 1;
}
}
}
}
}
if (self.hp <= 0) {
delete ATTACKER_LIST[self.id];
delete self;
}
}
return self;
}
// Bullet object
var Bullet = function (id, ownerID, x, y, angle, size) {
var self = {
size: size,
id: id,
lifetime: 200,
x: x,
y: y,
xvel: Math.cos(angle / 180 * Math.PI) * 10,
yvel: Math.sin(angle / 180 * Math.PI) * 10,
owner: ownerID
}
self.update = function () {
self.x += self.xvel;
self.y += self.yvel;
self.lifetime--;
let extraSize = 4 * (self.size - 1);
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (player.joinKickTimeout < 0 && player.spawnCooldown < 0) {
if (self.x >= (player.x - 9) - extraSize && self.x <= player.x + 9 + extraSize) {
if (self.y >= (player.y - 9) - extraSize && self.y <= player.y + 9) {
if (!(self.owner == player.id)) {
if (!(player.powerupTime > 0)) player.hp--;
let owner = getPlayerByID(self.owner);
if (!(owner == undefined)) {
owner.score += 10;
if (player.hp <= 0) {
owner.score += 100;
owner.score += Math.floor(player.score / 4);
if (player.doubleFireSpeed)
owner.score += 500;
if (player.quadrupleFireSpeed)
owner.score += 2000;
if (player.dualBullets)
owner.score += 1250;
if (player.quadrupleBullets)
owner.score += 2000;
if (player.doubleBulletSize)
owner.score += 2000;
if (owner.hp < owner.maxHp) {
owner.hp++;
}
}
}
self.lifetime = 0;
}
}
}
}
}
for (let b in BLOCK_LIST) {
let block = BLOCK_LIST[b];
if (self.x >= (block.x - 8) - extraSize && self.x <= block.x + 8 + extraSize) {
if (self.y >= (block.y - 8) - extraSize && self.y <= block.y + 8) {
delete BLOCK_LIST[block.id];
let owner = getPlayerByID(self.owner);
if (!(owner == undefined)) {
owner.score += 50;
}
self.lifetime = 0;
}
}
}
for (let na in ATTACKER_LIST) {
let at = ATTACKER_LIST[na];
if (self.x >= (at.x - 7) - extraSize && self.x <= at.x + 7 + extraSize) {
if (self.y >= (at.y - 7) - extraSize && self.y <= at.y + 7 + extraSize) {
at.hp--;
let owner = getPlayerByID(self.owner);
if (!(owner == undefined)) {
owner.score += 10;
if (at.hp <= 0) {
owner.score += 100;
}
}
self.lifetime = 0;
}
}
}
for (let s in NPCSHOOTER_LIST) {
let sh = NPCSHOOTER_LIST[s];
if (self.x >= (sh.x - 7) - extraSize && self.x <= sh.x + 7 + extraSize) {
if (self.y >= (sh.y - 7) - extraSize && self.y <= sh.y + 7 + extraSize) {
sh.hp--;
let owner = getPlayerByID(self.owner);
if (!(owner == undefined)) {
owner.score += 10;
if (sh.hp <= 0) {
owner.score += 250;
}
}
self.lifetime = 0;
}
}
}
if (self.x < 0 || self.x > 1200 || self.y < 0 || self.y > 600) {
self.lifetime = 0;
}
if (self.lifetime <= 0) {
delete BULLET_LIST[self.id];
delete self;
}
}
return self;
}
// NPCBlock object
var NPCBlock = function (id) {
var self = {
id: id,
x: Math.floor(Math.random() * 1180) + 10,
y: Math.floor(Math.random() * 580) + 10
}
return self;
}
// Player object
var Player = function (id) {
var self = {
x: Math.floor(Math.random() * 1160) + 20,
y: Math.floor(Math.random() * 560) + 20,
id: id,
spawnCooldown: -1,
afkKickTimeout: 100,
joinKickTimeout: 80,
pressingRight: false,
pressingLeft: false,
pressingUp: false,
pressingDown: false,
maxHp: 10,
hp: 10,
color: Math.floor(Math.random() * 360),
regen: -1,
afk: false,
mx: 0,
my: 0,
powerupTime: -1,
score: 0,
maxSpeed: 3,
name: "Unnamed",
doubleFireSpeed: false,
doubleBulletSize: false,
quadrupleFireSpeed: false,
dualBullets: false,
quadrupleBullets: false,
upgHPPrice: 500
}
self.respawn = function () {
self.x = Math.floor(Math.random() * 1160) + 20;
self.y = Math.floor(Math.random() * 560) + 20;
self.pressingRight = false;
self.pressingLeft = false;
self.pressingUp = false;
self.pressingDown = false;
self.hp = 10;
self.powerupTime = -1;
self.score = Math.round(self.score / 3);
if (self.doubleFireSpeed) {
self.score += 400;
}
if (self.quadrupleFireSpeed) {
self.score += 1600;
}
if (self.doubleBulletSize) {
self.score += 1600;
}
if (self.dualBullets) {
self.score += 1000;
}
if (self.quadrupleBullets) {
self.score += 1600;
}
self.maxHp = 10;
self.regen = -1;
self.maxSpeed = 3;
self.doubleFireSpeed = false;
self.quadrupleFireSpeed = false;
self.doubleBulletSize = false;
self.dualBullets = false;
self.quadrupleBullets = false;
self.upgHPPrice = 500;
self.spawnCooldown = 10;
}
self.fireBullet = function () {
if (self.joinKickTimeout < 0 && self.spawnCooldown < 0) {
let bsize = (self.doubleBulletSize ? 1.5 : 1);
let id = Math.random() * 2000;
BULLET_LIST[id] = Bullet(id, self.id, self.x, self.y, Math.atan2(self.my - self.y, self.mx - self.x) * 180 / Math.PI, bsize);
if (self.dualBullets) {
id = Math.random() * 2000;
BULLET_LIST[id] = Bullet(id, self.id, self.x, self.y, (Math.atan2(self.my - self.y, self.mx - self.x) * 180 / Math.PI) - 180, bsize);
if (self.quadrupleBullets) {
id = Math.random() * 2000;
BULLET_LIST[id] = Bullet(id, self.id, self.x, self.y, (Math.atan2(self.my - self.y, self.mx - self.x) * 180 / Math.PI) - 90, bsize);
id = Math.random() * 2000;
BULLET_LIST[id] = Bullet(id, self.id, self.x, self.y, (Math.atan2(self.my - self.y, self.mx - self.x) * 180 / Math.PI) - 270, bsize);
}
}
}
}
self.update = function () {
if (self.powerupTime > 0) {
self.maxSpeed = 4;
} else {
self.maxSpeed = 3;
}
if (self.hp <= 0) {
self.respawn();
return;
}
if (self.spawnCooldown < 0) {
if (self.pressingRight) {
if (self.x < (1200 - self.maxSpeed) - 10) {
self.x += self.maxSpeed;
}
}
if (self.pressingLeft) {
if (self.x > (0 + self.maxSpeed) + 10) {
self.x -= self.maxSpeed;
}
}
if (self.pressingUp) {
if (self.y > (0 + self.maxSpeed) + 10) {
self.y -= self.maxSpeed;
}
}
if (self.pressingDown) {
if (self.y < (600 - self.maxSpeed) - 10) {
self.y += self.maxSpeed;
}
}
}
}
return self;
}
//Powerup object
var PowerUp = function (x, y, id) {
var self = {
x: x,
y: y,
id: id
};
self.update = function () {
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (getDistance(self.x, self.y, player.x, player.y) < 16) {
player.powerupTime += 10;
player.score += 750;
self.destroy();
}
}
}
self.destroy = function () {
delete POWERUP_LIST[self.id];
delete self;
}
return self;
}
// ---------- Functions ----------
function getPlayerByID(id) {
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (player.id == id) {
return player;
}
}
}
function getDistance(x1, y1, x2, y2) {
let a = x1 - x2;
let b = y1 - y2;
return Math.sqrt(a * a + b * b);
}
function getSmallest(obj) {
let min, key;
for (let k in obj) {
if (typeof (min) == 'undefined') {
min = obj[k];
key = k;
continue;
}
if (obj[k] < min) {
min = obj[k];
key = k;
}
}
return key;
}
function countActivePlayers() {
let result = 0;
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (player.joinKickTimeout < 0 && player.spawnCooldown < 0) {
result++;
}
}
return result;
}
function isOverPower(player) {
if (!(player.joinKickTimeout < 0 && player.spawnCooldown < 0)) {
return false;
}
let power = 0;
if (player.doubleFireSpeed) {
power++;
}
if (player.quadrupleFireSpeed) {
power++;
}
if (player.doubleBulletSize) {
power++;
}
if (player.dualBullets) {
power++;
}
if (player.quadrupleBullets) {
power++;
}
if (power > 3) {
return true;
} else {
return false;
}
}
function countOPPlayers() {
let result = 0;
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (isOverPower(player)) {
result++;
}
}
return result;
}
function spawnBlock() {
let id = (Math.random() * 10);
BLOCK_LIST[id] = NPCBlock(id);
return id;
}
function spawnAttacker() {
let id = (Math.random() * 10);
let x = Math.floor(Math.random() * 1180) + 10;
let y = Math.floor(Math.random() * 580) + 10;
ATTACKER_LIST[id] = NPCAttacker(id, x, y);
return id;
}
function spawnShooter() {
let id = (Math.random() * 10);
let x = Math.floor(Math.random() * 1180) + 10;
let y = Math.floor(Math.random() * 580) + 10;
NPCSHOOTER_LIST[id] = NPCShooter(id, x, y);
return id;
}
function disconnectSocket(id) {
SOCKET_LIST[id].disconnect();
delete SOCKET_LIST[id];
delete SOCKET_ACTIVITY[id];
}
function getCommand(text) {
let command = "";
for (let i = 0; i < text.length; i++) {
if (text.charAt(i) == ' ') {
i = text.length;
} else {
command += text.charAt(i);
}
}
return command.toLowerCase();
}
function getArgs(text) {
let args = [];
let arg = "";
let j = false;
text += " ";
for (let i = 0; i < text.length; i++) {
if (text.charAt(i) == ' ') {
if (!j) {
j = true;
} else {
args.push(arg);
}
arg = "";
} else {
arg += text.charAt(i);
}
}
return args;
}
// ---------- Socket Connections ----------
io.sockets.on("connection", function (socket) {
socket.id = Math.random();
if (SOCKET_ACTIVITY[socket.id] == undefined) {
SOCKET_ACTIVITY[socket.id] = 0;
}
SOCKET_LIST[socket.id] = socket;
let player = Player(socket.id);
PLAYER_LIST[socket.id] = player;
console.log(colors.cyan("[jsShooter] Socket connection with id " + socket.id));
socket.emit("id", {
id: socket.id
});
socket.on("disconnect", function () {
try {
for (let b in BULLET_LIST) {
let bullet = BULLET_LIST[b];
if (bullet.owner == socket.id) {
delete BULLET_LIST[b];
}
}
delete PLAYER_LIST[socket.id];
disconnectSocket(socket.id);
console.log(colors.cyan("[jsShooter] Player with id " + socket.id + " disconnected"));
} catch (err) {
if (debug) {
throw err;
}
}
});
socket.on('keyPress', function (data) {
try {
if (data.inputId === 'left') {
player.pressingLeft = data.state;
} else if (data.inputId === 'right') {
player.pressingRight = data.state;
} else if (data.inputId === 'up') {
player.pressingUp = data.state;
} else if (data.inputId === 'down') {
player.pressingDown = data.state;
}
} catch (err) {
if (debug) {
throw err;
}
}
});
socket.on('changeName', function (data) {
try {
if (data.name.length > 64) { // Name is way too long. Kick the player for sending too much data
console.log(colors.red("[jsShooter] Player with id " + socket.id + " tried to change name to " + data.name + " but it is longer than 64 chars. Disconnecting socket"));
disconnectSocket(socket.id);
return;
}
if (data.name.length > 16 || data.name.length < 1) { // Name is too long or too short
return;
}
let player = getPlayerByID(socket.id);
if (player.name != data.name) {
console.log(colors.cyan("[jsShooter] Player with id " + socket.id + " changed name to " + data.name));
player.name = data.name;
}
} catch (err) {
if (debug) {
throw err;
}
}
});
socket.on('not afk', function (data) {
try {
let player = getPlayerByID(socket.id);
player.afkKickTimeout = 100;
} catch (err) {
if (debug) {
throw err;
}
}
});
socket.on('kthx', function (data) {
try {
let player = getPlayerByID(socket.id);
if (!(player == undefined)) {
player.joinKickTimeout = -1;
console.log(colors.cyan("[jsShooter] Player with id " + socket.id + " is now verified"));
}
} catch (err) {
if (debug) {
throw err;
}
}
});
socket.on("*", function (data) {
try {
SOCKET_ACTIVITY[socket.id]++;
//console.log(data);
} catch (err) {
if (debug) {
throw err;
}
}
});
// HP Upgrade
socket.on('upgHPClicked', function (data) {
try {
let player = getPlayerByID(socket.id);
if (!(player == undefined)) {
if (player.score >= player.upgHPPrice) {
player.maxHp++;
player.score -= player.upgHPPrice;
player.upgHPPrice += 250;
if (player.hp < player.maxHp) {
player.hp++;
}
}
}
} catch (err) {
if (debug) {
throw err;
}
}
});
// Fire speed upgrade
socket.on('upgFSpeedClicked', function (data) {
try {
let player = getPlayerByID(socket.id);
if (!(player == undefined)) {
if (!player.doubleFireSpeed) {
if (player.score >= 2000) {
player.doubleFireSpeed = true;
player.score -= 2000;
}
} else if (!player.quadrupleFireSpeed) {
if (player.score >= 8000) {
player.quadrupleFireSpeed = true;
player.score -= 8000;
}
}
}
} catch (err) {
if (debug) {
throw err;
}
}
});
// Bullet size upgrade
socket.on('upgBulletSize', function (data) {
try {
let player = getPlayerByID(socket.id);
if (!(player == undefined)) {
if (!player.doubleBulletSize) {
if (player.score >= 5000) {
player.doubleBulletSize = true;
player.score -= 5000;
}
}
}
} catch (err) {
if (debug) {
throw err;
}
}
});
// Dual bullet upgrade
socket.on('upgDualBullets', function () {
try {
let player = getPlayerByID(socket.id);
if (!(player == undefined)) {
if (!player.dualBullets) {
if (player.score >= 5000) {
player.dualBullets = true;
player.score -= 5000;
}
} else {
if (player.score >= 8000) {
player.quadrupleBullets = true;
player.score -= 8000;
}
}
}
} catch (err) {
if (debug) {
throw err;
}
}
});
socket.on('mouseMove', function (data) {
try {
let player = getPlayerByID(socket.id);
if (player != undefined && data.x != undefined && data.y != undefined) {
player.mx = data.x;
player.my = data.y;
}
} catch (err) {
if (debug) {
throw err;
}
}
});
});
// ---------- Loops ----------
// Bullet fire loop
setInterval(function () {
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
player.fireBullet();
}
setTimeout(function () {
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (player.joinKickTimeout < 0 && player.spawnCooldown < 0) {
if (player.doubleFireSpeed || player.powerupTime > 0) {
player.fireBullet();
}
}
}
}, 150);
setTimeout(function () {
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (player.joinKickTimeout < 0 && player.spawnCooldown < 0) {
if (player.quadrupleFireSpeed) {
player.fireBullet();
}
}
}
}, 50);
setTimeout(function () {
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (player.joinKickTimeout < 0 && player.spawnCooldown < 0) {
if (player.quadrupleFireSpeed) {
player.fireBullet();
}
}
}
}, 200);
}, 250);
// Spawn blocks
setInterval(function () {
if (Object.keys(BLOCK_LIST).length <= 40) {
spawnBlock();
}
}, 500);
// Spawn / despawn / afk test loop
setInterval(function () {
try {
// Overload protection
for (let sa in SOCKET_ACTIVITY) {
if (isNaN(SOCKET_ACTIVITY[sa])) {
delete SOCKET_ACTIVITY[sa];
break;
}
if (SOCKET_ACTIVITY[sa] > MAX_SOCKET_ACTIVITY_PER_SECOND) {
console.log(colors.red("[jsShooter] Kicked " + sa + " Too high network activity. " + SOCKET_ACTIVITY[sa] + " > " + MAX_SOCKET_ACTIVITY_PER_SECOND + " Messages in 1 second"));
delete PLAYER_LIST[sa];
disconnectSocket(sa);
} else {
SOCKET_ACTIVITY[sa] = 0;
}
}
// Spawn attackers
if (Object.keys(ATTACKER_LIST).length < 3 && Math.floor(Math.random() * 4) == 1) {
if (countActivePlayers() > 0) {
spawnAttacker();
}
}
// Player respawn cooldown
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (!(player.spawnCooldown < 0)) {
player.spawnCooldown--;
}
if (player.powerupTime > 0) {
player.powerupTime--;
} else {
player.powerupTime = -1;
}
}
// Spawn and despawn shooters
if (Object.keys(NPCSHOOTER_LIST).length < 5 && Math.floor(Math.random() * (countOPPlayers() > 0 ? 4 : 20)) == 1) {
if (countActivePlayers() > 0) {
spawnShooter();
}
}
if (countActivePlayers() < 1) {
for (let s in NPCSHOOTER_LIST) {
let sh = NPCSHOOTER_LIST[s];
if (Math.floor(Math.random() * 30) == 1) {
sh.hp = 0;
break;
}
}
}
// Shooter loop
for (let s in NPCSHOOTER_LIST) {
let sh = NPCSHOOTER_LIST[s];
if (sh.targetPlayer > 0) {
sh.fireBullet();
}
}
if (countOPPlayers() > 0) {
setTimeout(function () {
for (let s in NPCSHOOTER_LIST) {
let sh = NPCSHOOTER_LIST[s];
if (sh.targetPlayer > 0) {
sh.fireBullet();
}
}
}, 500);
}
// AFK Test loop
for (let i in SOCKET_LIST) {
let socket = SOCKET_LIST[i];
socket.emit("afk?", {});
}
// Powerup spawn
if (!(Object.keys(POWERUP_LIST).length > 0)) {
if (Math.floor(Math.random() * 200) == 1) {
if (countActivePlayers() > 0) {
let sID = Math.random();
let x = Math.floor(Math.random() * 1180) + 10;
let y = Math.floor(Math.random() * 580) + 10;
POWERUP_LIST[sID] = PowerUp(x, y, sID);
}
}
}
// Check players hp
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
if (player.hp > player.maxHp) {
player.hp = player.maxHp;
}
}
} catch (err) {
if (debug) {
throw err;
}
};
}, 1000);
// Regen and kick loop
setInterval(function () {
for (let p in PLAYER_LIST) {
let player = PLAYER_LIST[p];
player.afkKickTimeout--;
if (player.hp < player.maxHp) {
if (player.regen < 0) {
player.regen = 50;
}
}
if (player.regen >= 0) {
player.regen--;
}
if (player.regen == 0) {
if (player.hp < player.maxHp) {
player.hp++;
}
}
if (player.joinKickTimeout > 0) {
player.joinKickTimeout--;
}
if (player.joinKickTimeout == 0 || player.afkKickTimeout <= 0) {
delete PLAYER_LIST[player.id];
disconnectSocket(player.id);
console.log(colors.red("[jsShooter] Kicked " + player.id + " for inactivity"));
}
}
}, 100);
// Main update loop
var updatePriceCooldown = 0;