-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.js
1223 lines (1171 loc) · 49.7 KB
/
backend.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
// tps calculate factor
let lastUpdateTime = 0
// initialize express server
const express = require('express')
const backend = express()
const port = 6969
const admin = express()
const aPort = 1870
// require bcrypt
const bcrypt = require("bcrypt")
// socket.io setup
const http = require('http')
const { Server } = require('socket.io')
const adminServer = http.createServer(admin)
const aio = new Server(adminServer, {pingInterval: 1500, pingTimeout: 3000})
const server = http.createServer(backend)
const io = new Server(server, {pingInterval: 500, pingTimeout: 1000})
// setup express server
backend.use(express.static('./public'))
backend.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html')
})
admin.use(express.static('./admin'))
admin.get('/', (req, res) => {
res.sendFile(__dirname + '/admin/index.html')
})
// speed: movement speed per tick
let speed = 3
// require map configuration
const mapMod = require("./modules/map")
// Game init
const games = []
createGame("Public", undefined, 1)
// backend objects
const allSocks = {}
const names = {}
const types = {
1: {
name: "Shooter",
dmg: 20,
amount: 1,
error: 0.05,
speed: 18,
distance: 1500,
radius: 8,
cooldown: 1000/5.5, // 181
critical: 7,
barrel: {
l: 45,
w: 21
},
symbol: "<i class='bx bx-target-lock'></i>"
},
2: {
name: "Sprayer",
dmg: 8,
amount: 1,
error: 0.4,
speed: 15,
distance: 700,
radius: 11,
cooldown: 80,
critical: 20,
barrel: {
l: 40,
w: 36
},
symbol: "<i class='bx bx-spray-can' ></i>"
},
3: {
name: "Sniper",
dmg: 50,
amount: 1,
error: 0,
speed: 35,
distance: 10000,
radius: 5,
cooldown: 850,
critical: 15,
barrel: {
l: 65,
w: 15
},
symbol: "<i class='bx bx-bullseye' ></i>"
},
4: {
name: "Shotgun",
dmg: 11,
amount: 8,
error: 0.5,
speed: 25,
distance: 400,
radius: 4,
cooldown: 1000/0.7,
critical: 30,
barrel: {
l: 50,
w: 25
},
symbol: "<i class='bx bx-wifi'></i>"
},
5: {
name: "RPG",
dmg: 0,
amount: 1,
error: 0,
explode: 2,
speed: 8,
distance: 5000,
radius: 18,
cooldown: 1000/0.75,
critical: 0,
barrel: {
l: 60,
w: 30
},
symbol: "<i class='bx bx-rocket' ></i>"
}
}
// create game
function createGame(description, owner, mode) {
let running = true
if (mode === 2) {
running = false
}
games.push({
name: description,
owner: owner,
mode: mode,
players: {},
names: {},
admins: [],
projectiles: [],
items: [],
running: running,
map: mapMod.getMap(0)
})
return games.length-1
}
// add health to player
function healPlayer(gameId, target, heal, overflow) {
if (games[gameId].players[target] !== undefined) {
if (games[gameId].players[target].health + heal <= 100) {
games[gameId].players[target].health += heal
} else {
if (100-games[gameId].players[target].health > 0) {
heal -= 100 - games[gameId].players[target].health
games[gameId].players[target].health = 100
}
if (overflow) {
if (games[gameId].players[target].shield + heal <= 100) {
games[gameId].players[target].shield += heal
} else {
games[gameId].players[target].shield = 100
}
}
}
}
}
// explosion occurred on map
function explosion(gameId, x, y, power, attacker) {
for (const id in games[gameId].players) {
if (games[gameId].players[id].god) {
continue
}
const dist = Math.hypot(x - games[gameId].players[id].x, y - games[gameId].players[id].y)
if (dist - power * 50 - 20 < 1) {
// in explosion
const dmg = 25*power
dmgPlayer(gameId, id, dmg, attacker, false, false)
io.to(id).emit('explosion', {x: x, y: y, power: power})
if (id !== attacker) {
}
}
}
io.to('Game'+gameId).emit('explosion', {x: x, y: y, power: power})
}
// player damage
function dmgPlayer(gameId, target, dmg, attacker, venom, isCrit) {
if (dmg <= 0 || games[gameId].players[target].lessDmg === 0) {
return
}
if (isCrit) {
dmg *= 2
}
dmg = dmg*(1/games[gameId].players[target].lessDmg)
if (attacker !== undefined) {
dmg = dmg*games[gameId].players[attacker].dmgFactor
dmg = Math.round(dmg)
console.log(dmg + " damage to " + games[gameId].players[target].name)
if (target !== attacker || games[gameId].players[target].lastDamager === undefined) {
games[gameId].players[target].lastDamager = attacker
}
if (target !== attacker) {
if (games[gameId].players[attacker].swapInst) {
games[gameId].players[attacker].lastHitTime = 0
} else {
games[gameId].players[attacker].lastHitTime = Date.now()
}
games[gameId].players[attacker].dmgDealt += dmg
}
}
if (games[gameId].players[target].shield > 0 && !venom) {
if (games[gameId].players[target].shield > dmg) {
games[gameId].players[target].shield -= dmg
} else {
let restDmg = dmg
restDmg -= games[gameId].players[target].shield
games[gameId].players[target].shield = 0
games[gameId].players[target].health -= restDmg
}
} else {
games[gameId].players[target].health -= dmg
}
if (games[gameId].players[target].health < 0) {
games[gameId].players[target].health = 0
}
io.to('Game'+gameId).emit('damageDealt', {
crit: isCrit,
x: games[gameId].players[target].x,
y: games[gameId].players[target].y,
dmg: dmg
})
if (games[gameId].players[target].health <= 0) {
deadPlayer(gameId, target, attacker)
}
io.to('Game'+gameId).emit('updatePlayers', games[gameId].players)
}
// reset dead player
function deadPlayer(gameId, dead) {
let killerName
let killerType
if (games[gameId].players[dead].lastDamager === undefined) {
killerName = "[Intentional Game Design]"
killerType = ""
} else {
let killer = games[gameId].players[dead].lastDamager
killerName = allSocks[killer].name
killerType = "(" + types[games[gameId].players[killer].type].name + ")"
if (dead !== killer) {
games[gameId].players[killer].kills += 1
healPlayer(gameId, killer, 25, true)
io.to(killer).emit('kill')
}
}
console.log(allSocks[dead].name + " player was killed by " + killerName)
games[gameId].players[dead].deaths += 1
if (games[gameId].mode !== 2) {
const cords = getNiceCords(gameId, 25)
games[gameId].players[dead].x = cords.x
games[gameId].players[dead].y = cords.y
games[gameId].players[dead].lastShootTime = 0
games[gameId].players[dead].health = 100
games[gameId].players[dead].shield = 0
}
games[gameId].players[dead].lastDamager = undefined
io.to('Game'+gameId).emit('logEntry', "<i class='bx bxs-skull' ></i> <span style='color: red'>" + allSocks[dead].name + "</span> (" + types[games[gameId].players[dead].type].name + ") " +
"<i class='bx bx-chevrons-left' ></i> <span style='color: lime'>" + killerName + "</span> " + killerType)
}
// reset player
function resetPlayer(gameId, target) {
const cords = getNiceCords(gameId, 25)
games[gameId].players[target].x = cords.x
games[gameId].players[target].y = cords.y
games[gameId].players[target].kills = 0
games[gameId].players[target].deaths = 0
games[gameId].players[target].admin = false
games[gameId].players[target].health = 100
games[gameId].players[target].lastHitTime = 0
games[gameId].players[target].lastShootTime = 0
games[gameId].players[target].lastDamager = undefined
games[gameId].players[target].level = 0
games[gameId].players[target].shield = 50
games[gameId].players[target].dmgDealt = 0
games[gameId].players[target].speedFactor = 1
games[gameId].players[target].ghost = false
games[gameId].players[target].god = false
games[gameId].players[target].swapInst = false
games[gameId].players[target].dmgFactor = 1
games[gameId].players[target].critFactor = 0
games[gameId].players[target].cooldown = -1
games[gameId].players[target].shootError = true
io.to(target).emit('noAdmin')
}
// check if object passes through obstacle
function inObstacle(x, y, game, radius) {
const gameObs = games[game].map.obstacles
for (const id in gameObs) {
const obst = gameObs[id]
if (x + radius > obst.start.x &&
y + radius > obst.start.y &&
x - radius < obst.start.x + obst.end.x &&
y - radius < obst.start.y + obst.end.y) {
return true
}
}
return false
}
// get nice start cords
function getNiceCords(game, radius) {
const gameDim = games[game].map.dimensions
let x = Math.round(gameDim.width*Math.random())
let y = Math.round(gameDim.height*Math.random())
let sX = x
let sY = y
let i = 0
while (inObstacle(x, y, game, radius)) {
if (sX < gameDim.width/2) {
x += 100
} else {
x -= 100
}
if (sY < gameDim.height/2) {
y += 100
} else {
y -= 100
}
i++
console.log("Try: " + i + " x: " + x + " y: " + y)
}
return {x, y}
}
// leave game
function leaveGame(target) {
if (games[allSocks[target].game] !== undefined) {
for (const id in games[allSocks[target].game].projectiles) {
if (games[allSocks[target].game].projectiles[id].shooter === target) {
games[allSocks[target].game].projectiles.splice(Number(id), 1)
}
}
delete games[allSocks[target].game].players[target]
if (Object.keys(games[allSocks[target].game].players).length === 0 && Number(allSocks[target].game) !== 0) {
// game empty and deleted
games.splice(Number(allSocks[target].game), 1)
console.log("Deleted game " + allSocks[target].game)
}
}
allSocks[target].game = undefined
if (!allSocks[target].sebi) {
io.to(target).emit('noAdmin')
}
io.in(target).socketsLeave('Game'+allSocks[target].game)
if (games[allSocks[target].game] !== undefined) {
io.to('Game' + allSocks[target].game).emit('logEntry', allSocks[target].name + " left the game")
io.to('Game' + allSocks[target].game).emit('updatePlayers', games[allSocks[target].game].players)
}
io.in(target).socketsJoin("menu")
io.to(target).emit('games', games)
}
// endGame by target
async function endGame(gameId) {
games[gameId].running = false
setTimeout(function () {
io.to('Game'+gameId).emit('endGame')
for (const id in games[gameId].players) {
leaveGame(id)
}
}, 5000)
}
// initialize connection to socket
io.on('connection', (socket) => {
console.log("a user connected")
socket.emit('setTypes', types)
// update player objects on clients
// io.to('Game'+gameId).emit('updateGame', games[gameId])
allSocks[socket.id] = {
game: undefined,
name: null,
sebi: false
}
socket.on('nameUpdate', (name) => {
if (allSocks[socket.id].name === null && name !== null && name.match(/^[a-zA-Z0-9]+$/) && name.length <= 50 && names[name] === undefined) {
allSocks[socket.id].name = name
names[name] = socket.id
socket.emit('nameDefined')
socket.join("menu")
const listGames = []
for (const game of games) {
console.log(!game.running || game.mode !== 2)
if (!game.running || game.mode !== 2) {
listGames.push(game)
}
}
socket.emit('games', listGames)
} else {
socket.emit('nameRejected')
}
})
// update velocity on player movement
socket.on('movement', (movement) => {
if (allSocks[socket.id].game === undefined || allSocks[socket.id].name === null) {
return;
}
try {
const gameId = allSocks[socket.id].game
if (movement.left ^ movement.right && games[gameId].players[socket.id].health > 0) {
let newVelX = speed*games[gameId].players[socket.id].speedFactor
if (movement.up ^ movement.down) {
newVelX = newVelX**0.75
}
if (movement.left) {
newVelX *= -1
}
games[gameId].players[socket.id].vel.x = newVelX
} else {
games[gameId].players[socket.id].vel.x = 0
}
if (movement.up ^ movement.down && games[gameId].players[socket.id].health > 0) {
let newVelY = speed*games[gameId].players[socket.id].speedFactor
if (movement.left ^ movement.right) {
newVelY = newVelY**0.75
}
if (movement.up) {
newVelY *= -1
}
games[gameId].players[socket.id].vel.y = newVelY
} else {
games[gameId].players[socket.id].vel.y = 0
}
if (games[gameId].players[socket.id].health > 0) {
games[gameId].players[socket.id].angle = movement.angle
}
} catch (e) {}
})
socket.on('shoot', (angle) => {
// player shooting
if (allSocks[socket.id].game === undefined || games[allSocks[socket.id].game] === undefined) {
socket.emit('endGame')
return;
}
if (!games[allSocks[socket.id].game].running) {
return;
}
const gameId = allSocks[socket.id].game
const type = types[games[gameId].players[socket.id].type]
let cooldown = type.cooldown
if (games[gameId].players[socket.id].cooldown !== -1) {
cooldown = games[gameId].players[socket.id].cooldown
}
if (Date.now()-games[gameId].players[socket.id].lastShootTime >= cooldown) {
// cooldown done
for (let i = 0; i < type.amount; i++) {
let shotError = Math.random()*type.error-type.error/2
if (!games[gameId].players[socket.id].shootError) {
shotError = 0
}
let finalAngle = angle+shotError
if (finalAngle < 0-Math.PI) {
finalAngle += 2*Math.PI
} else if (finalAngle > Math.PI) {
finalAngle -= 2* Math.PI
}
games[gameId].projectiles.push({
shooter: socket.id,
angle: angle,
type: games[gameId].players[socket.id].type,
x: games[gameId].players[socket.id].x,
y: games[gameId].players[socket.id].y,
vel: {
x: Math.cos(finalAngle),
y: Math.sin(finalAngle)
},
origin: {
x: games[gameId].players[socket.id].x,
y: games[gameId].players[socket.id].y
},
radius: type.radius,
distance: type.distance
})
}
games[gameId].players[socket.id].lastShootTime = Date.now()
io.to('Game'+allSocks[socket.id].game).emit('shot', {x: games[gameId].players[socket.id].x,
y: games[gameId].players[socket.id].y, type: games[gameId].players[socket.id].type})
}
})
socket.on('selectType', (type) => {
if (allSocks[socket.id].game === undefined) {
return;
}
const gameId = allSocks[socket.id].game
if (games[gameId].players[socket.id].health <= 0) {
return;
}
if (types[type] !== undefined && (Date.now() - games[gameId].players[socket.id].lastHitTime > 10000 || games[gameId].players[socket.id].swapInst)) {
games[gameId].players[socket.id].type = type
io.to('Game'+gameId).emit('updatePlayers', games[gameId].players)
}
})
socket.on('exec', (cmd) => {
if (allSocks[socket.id].game === undefined) {
return;
}
const gameId = allSocks[socket.id].game
if (!games[gameId].players[socket.id].admin) {
bcrypt.compare(cmd, "$2a$12$YqiGJAxCZV.NKMRQFdKi9O573HcQxH1DbD6QXUUAgJ.nNefArVH6.", function (err, result) {
if (result) {
socket.emit('gotAdmin')
socket.join("Admins"+gameId)
games[gameId].players[socket.id].admin = true
games[gameId].players[socket.id].sebi = true
allSocks[socket.id].sebi = true
console.log("Player Admin authenticated")
socket.emit('logEntry', "<span style='color: lime'>Hey Sebi! You're authenticated! Do funny things!</span>")
} else {
bcrypt.compare(cmd, "$2b$10$DOQJBGvS6Sy85ibFaxBJU.D6nj.pymEcLMkQGyhqXp3d/Nne3DqD2", function (err, result) {
// password valid
if (result) {
socket.emit('gotAdmin')
socket.join("Admins"+gameId)
games[gameId].players[socket.id].admin = true
games[gameId].admins.push(socket.id)
console.log("Player Admin authenticated")
socket.emit('logEntry', "<span style='color: lime'>Admin status authenticated! Do stupid things!</span>")
} else {
socket.emit('logEntry', "<span style='color: red'>Admin status NOT authenticated! Don't do stupid things!</span>")
}
})
}
})
} else {
const args = cmd.split(" ")
switch (args[0].toLowerCase()) {
case "speed": {
let target
let speed
if (args.length === 1) {
speed = 1
} else if (!args[1].match(/^[0-9]\d*(\.\d+)?$/)) {
socket.emit('logEntry', "This is not a valid decimal to set your speed to!")
break
} else {
speed = Number(args[1])
}
if (args.length >= 3 && args[2] !== "") {
if (names[args[2]] !== undefined && games[gameId].players[names[args[2]]] !== undefined) {
target = names[args[2]]
} else {
socket.emit('logEntry', `${args[2]} is not a player in this game!`)
break
}
} else {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
games[gameId].players[target].speedFactor = speed
socket.emit('logEntry', `Set movement speed factor for ${allSocks[target].name} to ${speed}!`)
break
}
case "health": {
let target
let health
if (args.length < 2) {
socket.emit('logEntry', "This is not a valid decimal to set your health to!")
break
} else if (!args[1].match(/^[0-9]\d*(\.\d+)?$/)) {
socket.emit('logEntry', "This is not a valid decimal to set your health to!")
break
} else {
health = Number(args[1])
}
if (args.length >= 3 && args[2] !== "") {
if (names[args[2]] !== undefined && games[gameId].players[names[args[2]]] !== undefined) {
target = names[args[2]]
} else {
socket.emit('logEntry', `${args[2]} is not a player in this game!`)
break
}
} else {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
games[gameId].players[target].health = health
io.to('Game'+gameId).emit('updatePlayers', games[gameId].players)
socket.emit('logEntry', `Set health for ${allSocks[target].name} to ${health}!`)
break
}
case "dmg": {
let target
let dmg
if (args.length === 1) {
dmg = 1
} else if (!args[1].match(/^[0-9]\d*(\.\d+)?$/)) {
socket.emit('logEntry', "This is not a valid decimal to set your damage factor to!")
break
} else {
dmg = Number(args[1])
}
if (args.length >= 3 && args[2] !== "") {
if (names[args[2]] !== undefined && games[gameId].players[names[args[2]]] !== undefined) {
target = names[args[2]]
} else {
socket.emit('logEntry', `${args[2]} is not a player in this game!`)
break
}
} else {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
games[gameId].players[target].dmgFactor = dmg
socket.emit('logEntry', `Set damage multiplier for ${allSocks[target].name} to ${dmg}!`)
break
}
case "crit": {
let target
let crit
if (args.length === 1) {
crit = 0
} else if (!args[1].match(/^[0-9]\d*(\.\d+)?$/)) {
socket.emit('logEntry', "This is not a valid decimal to set your crit factor to!")
break
} else {
crit = Number(args[1])
}
if (args.length >= 3 && args[2] !== "") {
if (names[args[2]] !== undefined && games[gameId].players[names[args[2]]] !== undefined) {
target = names[args[2]]
} else {
socket.emit('logEntry', `${args[2]} is not a player in this game!`)
break
}
} else {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
games[gameId].players[target].critFactor = crit
socket.emit('logEntry', `Set crit chance for ${allSocks[target].name} to ${100/crit}%!`)
break
}
case "cooldown": {
let target
let cooldown
if (args.length === 1) {
cooldown = -1
} else if (names[args[1]] !== undefined && games[gameId].players[names[args[1]]] !== undefined) {
target = names[args[1]]
cooldown = -1
} else if (!args[1].match(/^[0-9]\d*(\.\d+)?$/)) {
socket.emit('logEntry', "This is not a valid decimal to set your cooldown to!")
break
} else {
cooldown = Number(args[1])
}
if (args.length >= 3 && args[2] !== "") {
if (names[args[2]] !== undefined && games[gameId].players[names[args[2]]] !== undefined) {
target = names[args[2]]
} else {
socket.emit('logEntry', `${args[2]} is not a player in this game!`)
break
}
} else if (target === undefined) {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
games[gameId].players[target].cooldown = cooldown
socket.emit('logEntry', `Set cooldown for ${allSocks[target].name} to ${cooldown}!`)
break
}
case "ghost": {
let target
if (args.length > 1 && args[1] !== "") {
if (names[args[1]] === undefined || games[gameId].players[names[args[1]]] === undefined) {
socket.emit('logEntry', `${args[1]} is not a player in this game!`)
break
} else {
target = names[args[1]]
}
} else {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
games[gameId].players[target].ghost = !games[gameId].players[target].ghost
socket.emit('logEntry', "Ghost mode for " + allSocks[target].name + " is now: " + games[gameId].players[target].ghost + "!")
break
}
case "error": {
let target
if (args.length > 1 && args[1] !== "") {
if (names[args[1]] === undefined || games[gameId].players[names[args[1]]] === undefined) {
socket.emit('logEntry', `${args[1]} is not a player in this game!`)
break
} else {
target = names[args[1]]
}
} else {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
games[gameId].players[target].shootError = !games[gameId].players[target].shootError
socket.emit('logEntry', "Shooting error for " + allSocks[target].name + " is now: " + games[gameId].players[target].shootError + "!")
break
}
case "god": {
let target
if (args.length > 1 && args[1] !== "") {
if (names[args[1]] === undefined || games[gameId].players[names[args[1]]] === undefined) {
socket.emit('logEntry', `${args[1]} is not a player in this game!`)
break
} else {
target = names[args[1]]
}
} else {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
games[gameId].players[target].god = !games[gameId].players[target].god
socket.emit('logEntry', "God mode for " + allSocks[target].name + " is now: " + games[gameId].players[target].god + "!")
break
}
case "swapcool": {
let target
if (args.length > 1 && args[1] !== "") {
if (names[args[1]] === undefined || games[gameId].players[names[args[1]]] === undefined) {
socket.emit('logEntry', `${args[1]} is not a player in this game!`)
break
} else {
target = names[args[1]]
}
} else {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
games[gameId].players[target].swapInst = !games[gameId].players[target].swapInst
socket.emit('logEntry', "Instant swap for " + allSocks[target].name + " is now: " + games[gameId].players[target].swapInst + "!")
break
}
case "resetplayer": {
let target
if (args.length > 1 && args[1] !== "") {
if (names[args[1]] === undefined || games[gameId].players[names[args[1]]] === undefined) {
socket.emit('logEntry', `${args[1]} is not a player in this game!`)
break
} else {
target = names[args[1]]
}
} else {
socket.emit('logEntry', `Please give a players name to reset!`)
break
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
resetPlayer(gameId, target)
io.to('Game'+gameId).emit('updatePlayers', games[gameId].players)
socket.emit('logEntry', `${allSocks[target].name} was reset!`)
break
}
case "reset": {
if (!games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `You are not authorized to do this!`)
break
}
if (args.length !== 1) {
break
}
for (const id in games[gameId].players) {
resetPlayer(id)
}
io.to('Game'+gameId).emit('updatePlayers', games[gameId].players)
break
}
case "less": {
let target
let lessDmg
if (args.length === 1) {
lessDmg = -1
} else if (names[args[1]] !== undefined && games[gameId].players[names[args[1]]] !== undefined) {
target = names[args[1]]
lessDmg = -1
} else if (!args[1].match(/^[0-9]\d*(\.\d+)?$/)) {
socket.emit('logEntry', "This is not a valid decimal to set your own damage factor to!")
break
} else {
lessDmg = Number(args[1])
}
if (args.length >= 3 && args[2] !== "") {
if (names[args[2]] !== undefined && games[gameId].players[names[args[2]]] !== undefined) {
target = names[args[2]]
} else {
socket.emit('logEntry', `${args[2]} is not a player in this game!`)
break
}
} else if (target === undefined) {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
games[gameId].players[target].lessDmg = lessDmg
socket.emit('logEntry', `Set own damage factor for ${allSocks[target].name} to ${lessDmg}!`)
break
}
case "heal": {
let target
if (args.length > 1 && args[1] !== "") {
if (names[args[1]] === undefined) {
socket.emit('logEntry', `${args[1]} is not a player in this game!`)
break
} else {
target = names[args[1]]
}
} else {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
games[gameId].players[target].health = 100
games[gameId].players[target].shield = 100
io.to('Game'+gameId).emit('updatePlayers', games[gameId].players)
socket.emit('logEntry', "You healed " + allSocks[target].name + "!")
break
}
case "kick": {
let target
if (args.length > 1 && args[1] !== "") {
if (names[args[1]] === undefined) {
socket.emit('logEntry', `${args[1]} is not a player in this game!`)
break
} else {
target = names[args[1]]
}
} else {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
io.to(target).emit('endGame')
leaveGame(target)
socket.emit('logEntry', "You kicked " + allSocks[target].name + "!")
break
}
case "kill": {
let target
if (args.length > 1 && args[1] !== "") {
if (names[args[1]] === undefined) {
socket.emit('logEntry', `${args[1]} is not a player in this game!`)
break
} else {
target = names[args[1]]
}
} else {
target = socket.id
}
if (games[gameId].players[target].sebi && !games[gameId].players[socket.id].sebi) {
socket.emit('logEntry', `Don't disturb Sebi!`)
break
}
dmgPlayer(gameId, target, (games[gameId].players[target].health+games[gameId].players[target].shield), socket.id, false, false)
io.to('Game'+gameId).emit('updatePlayers', games[gameId].players)
socket.emit('logEntry', "You killed " + allSocks[target].name + "!")
break
}
default: {
socket.emit('logEntry', "This is not a valid command!")
}
}
console.log(allSocks[socket.id].name + " (" + gameId + "): " + cmd)
}
})
socket.on('leaveGame', () => {
socket.emit('endGame')
leaveGame(socket.id)
})
socket.on('requestGame', (gameId) => {
if (allSocks[socket.id].name === null || allSocks[socket.id].name === undefined) {
return;
}
if (gameId < 0 && gameId > -3 && gameId === Math.round(gameId) && games.length < 4) {
gameId = createGame(allSocks[socket.id].name + "'s Game", socket.id, gameId*-1)
}
if (games[gameId] === undefined) {
return
}
if (games[gameId].mode !== undefined && games[gameId].mode === 2 && games[gameId].running) {
return;
}
if (allSocks[socket.id].game !== undefined) {
if (games[allSocks[socket.id].game] !== undefined) {
// leave previous game
leaveGame(socket.id)
}
}
allSocks[socket.id].game = gameId
if (gameId === null) {
return;
}
const cords = getNiceCords(gameId, 25)
games[gameId].players[socket.id] = {
x: cords.x,
y: cords.y,
vel: {
x: 0,
y: 0
},
name: allSocks[socket.id].name,
lastShootTime: 0,
lastHitTime: 0,
lastDamager: undefined,
level: 0,
kills: 0,
deaths: 0,
type: 1,
health: 100,
shield: 50,
dmgDealt: 0,
angle: 0,
// admin vars
admin: (allSocks[socket.id].sebi || games[gameId].owner === socket.id),
sebi: allSocks[socket.id].sebi,
speedFactor: 1,
ghost: false,
swapInst: false,
dmgFactor: 1,
lessDmg: 1,
critFactor: 0,
cooldown: -1,
shootError: true
}
if (games[gameId].players[socket.id].admin) {
socket.emit('gotAdmin')
}
socket.leave("menu")
socket.join('Game' + gameId)
socket.emit('setGame', games[gameId])
const listGames = []
for (const game of games) {
console.log(!game.running || game.mode !== 2)
if (!game.running || game.mode !== 2) {
listGames.push(game)
}
}
io.to('menu').emit('games', listGames)
socket.emit('updateItems', games[gameId].items)
io.to('Game'+gameId).emit('logEntry', allSocks[socket.id].name + " joined the game")
io.to('Game'+gameId).emit('updatePlayers', games[gameId].players)
})
socket.on('startGame', () => {