-
Notifications
You must be signed in to change notification settings - Fork 23
/
app.js
871 lines (723 loc) · 20.7 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
/**
* Server entry file
*/
const EXPRESS = require('express')
const APP = EXPRESS()
const PATH = require('path')
const SHORTID = require('shortid')
const SERVER = require('http').Server(APP)
const IO = require('socket.io')(SERVER)
const UTIL = require('./common/util')
const GUTIL = require('./common/gutil')
const CONFIG = require('./common/config')
const mapJson = require('./public/src/asset/image/map.json')
const MODULE = require('./common/module')
const Vector = MODULE.Vector
const CreatureInfo = MODULE.CreatureInfo
let VTMAP = {}
const SERVER_PORT = CONFIG.serverPort
const EVENT_NAME = CONFIG.eventName
const STATIC_PATH = PATH.join(__dirname, '/public')
const COMMON_PATH = PATH.join(__dirname, '/common')
const SERVER_HEARTBEAT = 100
// const IS_DEBUG = CONFIG.isDebug
/** @type {Array.CreatureInfo} */
let ZOMBIE_INFOS = []
/** @type {Array.CreatureInfo} */
let MACHINE_INFOS = []
/** @type {Array.CreatureInfo} */
let BAT_INFOS = []
/** @type {Array.CreatureInfo} */
let PLAYER_INFOS = []
/* ================================================================ App & Init
*/
initVirtualMap()
initMonsters()
// disable bot (for now)
// initBots();
// Set static file
APP.use('/public', EXPRESS.static(STATIC_PATH))
APP.use('/common', EXPRESS.static(COMMON_PATH))
APP.get('/', function (req, res) {
res.sendFile(STATIC_PATH + '/index.html')
})
SERVER.listen(SERVER_PORT, function (err) {
if (err) {
throw err
} else {
UTIL.serverLog('Listening on port: ' + SERVER_PORT)
}
})
/* ================================================================ Socket util
*/
/**
* Get number of client connection by checking io connection
*
* @see http://stackoverflow.com/questions/10275667/socket-io-connected-user-count
*
* @returns {number}
*/
function getNumberOfConnection () {
return IO.engine.clientsCount
}
/* ================================================================ Init
*/
/**
* Initial virtual map
* 1: layer 1 - bush
* 2: layer 0 - ground
* 3: layer 1 - rock
* 4: layer 2 - tree
* 5: layer 0 - well
* 6: layer 0 - fire
*/
function initVirtualMap () {
// VTMAP
VTMAP.mapTileWidth = mapJson.tilewidth
VTMAP.mapTileHeight = mapJson.tileheight
VTMAP.nTileWidth = mapJson.width
VTMAP.nTileHeight = mapJson.height
VTMAP.data = UTIL.creature2DArray(
VTMAP.nTileWidth,
VTMAP.nTileHeight,
0
)
// row
for (let i = 0; i < VTMAP.nTileHeight; i++) {
// column
for (let j = 0; j < VTMAP.nTileWidth; j++) {
var idx = i * VTMAP.nTileWidth + j
// floor
var tmp1 = mapJson.layers[0].data[idx]
if (tmp1 === 5 || tmp1 === 6) VTMAP.data[i][j] = tmp1
// stone
var tmp2 = mapJson.layers[1].data[idx]
if (tmp2 === 1 || tmp2 === 3) VTMAP.data[i][j] = tmp2
}
}
}
/**
* Initial monster
* - zombie
* - machine
* - bat
*
* @todo n of init monster should be in config
*/
function initMonsters () {
const nZombies = 8
const nMachines = 8
const nBats = 8
for (let i = 0; i < nZombies; i++) {
let creatureInfo = getNewZombieInfo()
ZOMBIE_INFOS.push(creatureInfo)
}
for (let i = 0; i < nMachines; i++) {
let creatureInfo = getNewMachineInfo()
MACHINE_INFOS.push(creatureInfo)
}
for (let i = 0; i < nBats; i++) {
let creatureInfo = getNewBatInfo()
BAT_INFOS.push(creatureInfo)
}
}
/**
* Initial bot player
* - stay
*
* @todo move nBots to config
*/
function initBots () { // eslint-disable-line
const nBots = 4
for (let i = 0; i < nBots; i++) {
var heroInfo = getNewPlayerInfo()
PLAYER_INFOS.push(heroInfo)
}
}
/* ================================================================ Game
*/
/**
* Get new CreatureInfo of zombie
*
* @returns {CreatureInfo}
*/
function getNewZombieInfo () {
return getNewCreatureInfo('zombie', 100, 4, 6)
}
/**
* Get new CreatureInfo of machine
*
* @returns {CreatureInfo}
*/
function getNewMachineInfo () {
return getNewCreatureInfo('machine', 0, 5, 5)
}
/**
* Get new CreatureInfo of bat
*
* @returns {CreatureInfo}
*/
function getNewBatInfo () {
return getNewCreatureInfo('bat', 120, 3, 3)
}
/**
* Get new CreatureInfo of hero
*
* @returns {CreatureInfo}
*/
function getNewPlayerInfo () {
return getNewCreatureInfo('hero', 200, 3, 8)
}
/**
* Get new creature info
*
* @param {string} type
* @param {number} life
* @param {number} maxLife
* @returns {CreatureInfo}
*/
function getNewCreatureInfo (type, velocitySpeed, life, maxLife) {
const creatureId = getUniqueCreatureId()
const startVector = getRandomStartCreatureVector()
const creatureInfo = new CreatureInfo(creatureId, type, startVector, velocitySpeed, life, maxLife)
return creatureInfo
}
/**
* Get random walkable creature position
*
* @param {Position|Vector} currentPos
* @param {number} minimumDistance
* @returns {Position}
*/
function getRandomWalkableCreaturePosition (currentPos, minimumDistance) { // eslint-disable-line
if (typeof minimumDistance === 'undefined') minimumDistance = 0
let targetPos = {}
let distance = 0
let isNotOk = true
while (isNotOk) {
targetPos = getCreaturePositionByExclusion([1, 3, 5, 6])
distance = UTIL.getDistanceBetween(currentPos, targetPos)
if (distance >= minimumDistance) {
isNotOk = false
}
}
return targetPos
}
/**
* Get random start creature position
* not be allowed at
* - fire
* - bush
* - well
* - stone
*
* @returns {Position} start position
*/
function getRandomStartCreaturePosition () {
return getCreaturePositionByExclusion([1, 3, 5, 6])
}
/**
* Get random start creature vector
* not be allowed at
* - fire
* - bush
* - well
* - stone
*
* @returns {Vector} start vector
*/
function getRandomStartCreatureVector () {
const startPosition = getRandomStartCreaturePosition()
const startRotation = GUTIL.getRandomRotation()
const startVector = new Vector(startPosition.x, startPosition.y, startRotation)
return startVector
}
/**
* Get random creature position (real x, y in map)
* by excluding given `arr`
*
* note
* it's work, if all creature sprites is not over than
* mapTileWidth and mapTileHeight
*
* @param {Array.number} arr - Array of tile index that you do not want
* @returns {Position} return position (middle of tile)
*/
function getCreaturePositionByExclusion (arr) {
const nTileWidth = VTMAP.nTileWidth
const nTileHeight = VTMAP.nTileHeight
const tileWidth = VTMAP.mapTileWidth
const tileHeight = VTMAP.mapTileHeight
let tileIndexX
let tileIndexY
let isNotOk = true
while (isNotOk) {
tileIndexX = UTIL.getRandomInt(0, nTileWidth - 1)
tileIndexY = UTIL.getRandomInt(0, nTileHeight - 1)
// if the tile value is not be contained in
if (arr.indexOf(VTMAP.data[tileIndexY][tileIndexX]) === -1) {
isNotOk = false
}
}
const middlePos = GUTIL.convertTileIndexToPoint(
tileIndexX,
tileIndexY,
tileWidth,
tileHeight,
true
)
return middlePos
}
/**
* Reset creatureInfo
* used for respawning only (server side only)
*
* @param {CreatureInfo} creatureInfo
* @param {Vector} startVector
* @returns {CreatureInfo} creatureInfo
*/
function resetCreatureInfo (creatureInfo, startVector) {
creatureInfo.life = creatureInfo.initialLife
creatureInfo.startVector = startVector
creatureInfo.lastVector = startVector
creatureInfo.autoMove = {}
return creatureInfo
}
/**
* Check the `creatureId` is already existing in the `creatureInfos`
*
* @param {string} creatureId
* @param {Array.CreatureInfo} creatureInfos
* @returns {boolean}
*/
function isDuplicateCreatureId (creatureId, creatureInfos) {
let isDuplicated = false
const n = creatureInfos.length
for (let i = 0; i < n; i++) {
var creatureInfo = creatureInfos[i]
if (creatureInfo.id === creatureId) {
isDuplicated = true
break
}
}
return isDuplicated
}
/**
* Get unique creature id
*
* @returns {string}
*/
function getUniqueCreatureId () {
let creatureId
let isDuplicated = true
while (isDuplicated) {
creatureId = SHORTID.generate()
if (!isDuplicateCreatureId(creatureId, ZOMBIE_INFOS) &&
!isDuplicateCreatureId(creatureId, MACHINE_INFOS) &&
!isDuplicateCreatureId(creatureId, BAT_INFOS) &&
!isDuplicateCreatureId(creatureId, PLAYER_INFOS)) {
isDuplicated = false
}
}
return creatureId
}
/**
* Get playerInfo index by id
*
* @param {string} playerId
* @returns {number} return integer number when it's found (return -1, if not found)
*/
function getPlayerInfoIndexById (playerId) {
const nPlayers = PLAYER_INFOS.length
for (let i = 0; i < nPlayers; i++) {
if (PLAYER_INFOS[i].id === playerId) {
return i
}
}
UTIL.serverBugLog('getPlayerInfoIndexById', 'Not found playerId', playerId)
return -1
}
/**
* Get monsterInfo index
*
* @param {string} monsterId
* @param {Array} monsterInfos
* @returns {number} return integer number when it's found (return -1, if not found)
*/
function getMonsterInfoIndex (monsterId, monsterInfos) {
const nMonsters = monsterInfos.length
for (let i = 0; i < nMonsters; i++) {
if (monsterInfos[i].id === monsterId) {
return i
}
}
UTIL.serverBugLog('getMonsterInfoIndex', 'Not found monsterId', monsterId)
return -1
}
/**
* Check this player is already
* exists in the server
*
* @param {string} playerId
* @returns {boolean}
*/
function isExistingPlayer (playerId) { // eslint-disable-line
return (getPlayerInfoIndexById(playerId) > -1)
}
/**
* Remove player out of PLAYER_INFOS
*
* @param {string} playerId
* @returns
*/
function removePlayer (playerId) {
const playerIdx = getPlayerInfoIndexById(playerId)
if (playerIdx > -1) {
PLAYER_INFOS.splice(playerIdx, 1)
return true
}
return false
}
/* ================================================================ Socket
*/
IO.on('connection', function (socket) {
const socketId = socket.id
let playerInfo = getNewPlayerInfo()
UTIL.serverLog(playerInfo.id + ' is connect')
// disconnect
socket.on('disconnect', function () {
UTIL.serverLog(playerInfo.id + ' is disconnect')
// remove player
removePlayer(playerInfo.id)
// send disconnected player
const data = {
playerInfo: playerInfo
}
socket.broadcast.emit(EVENT_NAME.server.disconnectedPlayer, data)
})
// ready
socket.on(EVENT_NAME.player.ready, function () {
// data for new player
const data1 = {
VTMap: VTMAP,
playerInfo: playerInfo,
existingPlayerInfos: PLAYER_INFOS,
zombieInfos: ZOMBIE_INFOS,
machineInfos: MACHINE_INFOS,
batInfos: BAT_INFOS
}
IO.sockets.connected[socketId].emit(EVENT_NAME.player.ready, data1)
// add new player
PLAYER_INFOS.push(playerInfo)
// broadcast new player data
// to existing players
const data2 = {
playerInfo: playerInfo
}
socket.broadcast.emit(EVENT_NAME.server.newPlayer, data2)
})
// ping
socket.on(EVENT_NAME.player.ping, function () {
IO.sockets.connected[socketId].emit(EVENT_NAME.player.ping)
})
// message
// @todo refactor cause it has the same behavior
// @todo define object structure somewhere
socket.on(EVENT_NAME.player.message, function (data) {
const { id, lastMessage, lastMessageTimestamp } = data
const playerIdx = getPlayerInfoIndexById(id)
if (playerIdx > -1) {
PLAYER_INFOS[playerIdx].lastMessage = lastMessage
PLAYER_INFOS[playerIdx].lastMessageTimestamp = lastMessageTimestamp
socket.broadcast.emit(EVENT_NAME.player.message, data)
}
})
// move
// @todo refactor cause it has the same behavior
// @todo define object structure somewhere
socket.on(EVENT_NAME.player.move, function (data) {
const { id, lastVector } = data
const playerIdx = getPlayerInfoIndexById(id)
if (playerIdx > -1) {
PLAYER_INFOS[playerIdx].lastVector = lastVector
socket.broadcast.emit(EVENT_NAME.player.move, data)
}
})
// rotate
// @todo refactor cause it has the same behavior
// @todo define object structure somewhere
socket.on(EVENT_NAME.player.rotate, function (data) {
const { id, lastVector } = data
const playerIdx = getPlayerInfoIndexById(id)
if (playerIdx > -1) {
PLAYER_INFOS[playerIdx].lastVector = lastVector
socket.broadcast.emit(EVENT_NAME.player.rotate, data)
}
})
// fire
// @todo refactor cause it has the same behavior
// @todo define object structure somewhere
socket.on(EVENT_NAME.player.fire, function (data) {
const { id } = data.playerInfo
const playerIdx = getPlayerInfoIndexById(id)
if (playerIdx > -1) {
socket.broadcast.emit(EVENT_NAME.player.fire, data)
}
})
// is damaged
// @todo refactor cause it has the same behavior
socket.on(EVENT_NAME.player.isDamaged, function (data) {
const playerInfo = data.playerInfo
const playerIdx = getPlayerInfoIndexById(playerInfo.id)
if (playerIdx > -1) {
PLAYER_INFOS[playerIdx] = playerInfo
IO.sockets.connected[socketId].emit(EVENT_NAME.player.isDamagedItSelf, data)
socket.broadcast.emit(EVENT_NAME.player.isDamaged, data)
}
})
// is recovered
// @todo refactor cause it has the same behavior
socket.on(EVENT_NAME.player.isRecovered, function (data) {
const { id, life } = data.playerInfo
const playerIdx = getPlayerInfoIndexById(id)
if (playerIdx > -1) {
PLAYER_INFOS[playerIdx].life = life
IO.sockets.connected[socketId].emit(EVENT_NAME.player.isRecoveredItSelf, data)
socket.broadcast.emit(EVENT_NAME.player.isRecovered, data)
}
})
// is died
// @todo refactor cause it has the same behavior
socket.on(EVENT_NAME.player.isDied, function (data) {
let playerInfo = data.playerInfo
const playerIdx = getPlayerInfoIndexById(playerInfo.id)
if (playerIdx > -1) {
// die event
IO.sockets.connected[socketId].emit(EVENT_NAME.player.isDiedItSelf, data)
socket.broadcast.emit(EVENT_NAME.player.isDied, data)
// reset player info
const newStartVector = getRandomStartCreatureVector()
playerInfo = resetCreatureInfo(playerInfo, newStartVector)
// update server data
PLAYER_INFOS[playerIdx] = playerInfo
// send data
const newData = {
playerInfo: playerInfo
}
IO.sockets.connected[socketId].emit(EVENT_NAME.player.isRespawnItSelf, newData)
socket.broadcast.emit(EVENT_NAME.player.isRespawn, newData)
}
})
// attack - zombie
// @todo refactor
socket.on(EVENT_NAME.player.attackZombie, function (data) {
const { id, life } = data.monsterInfo
const monsterIdx = getMonsterInfoIndex(id, ZOMBIE_INFOS)
if (monsterIdx > -1) {
ZOMBIE_INFOS[monsterIdx].life = life
IO.emit(EVENT_NAME.player.attackZombie, data)
}
})
// attack - machine
// @todo refactor
socket.on(EVENT_NAME.player.attackMachine, function (data) {
const { id, life } = data.monsterInfo
const monsterIdx = getMonsterInfoIndex(id, MACHINE_INFOS)
if (monsterIdx > -1) {
MACHINE_INFOS[monsterIdx].life = life
IO.emit(EVENT_NAME.player.attackMachine, data)
}
})
// attack - bat
// @todo refactor
socket.on(EVENT_NAME.player.attackBat, function (data) {
const { id, life } = data.monsterInfo
const monsterIdx = getMonsterInfoIndex(id, BAT_INFOS)
if (monsterIdx > -1) {
BAT_INFOS[monsterIdx].life = life
IO.emit(EVENT_NAME.player.attackBat, data)
}
})
// kill - zombie
// @todo refactor
socket.on(EVENT_NAME.player.killZombie, function (data) {
let monsterInfo = data.monsterInfo
const monsterIdx = getMonsterInfoIndex(monsterInfo.id, ZOMBIE_INFOS)
if (monsterIdx > -1) {
// die event
IO.emit(EVENT_NAME.player.killZombie, data)
// reset monster info
var newStartVector = getRandomStartCreatureVector()
monsterInfo = resetCreatureInfo(monsterInfo, newStartVector)
// update server data
ZOMBIE_INFOS[monsterIdx] = monsterInfo
// send data
// @todo check, we need this object ?
const newData = { // eslint-disable-line
monsterInfo: monsterInfo
}
IO.emit(EVENT_NAME.player.respawnZombie, data)
}
})
// kill - machine
// @todo refactor
socket.on(EVENT_NAME.player.killMachine, function (data) {
let monsterInfo = data.monsterInfo
const monsterIdx = getMonsterInfoIndex(monsterInfo.id, MACHINE_INFOS)
if (monsterIdx > -1) {
// die event
IO.emit(EVENT_NAME.player.killMachine, data)
// reset monster info
const newStartVector = getRandomStartCreatureVector()
monsterInfo = resetCreatureInfo(monsterInfo, newStartVector)
// update server data
MACHINE_INFOS[monsterIdx] = monsterInfo
// send data
// @todo check, we need this object ?
const newData = { // eslint-disable-line
monsterInfo: monsterInfo
}
IO.emit(EVENT_NAME.player.respawnMachine, data)
}
})
// kill - bat
// @todo refactor
socket.on(EVENT_NAME.player.killBat, function (data) {
let monsterInfo = data.monsterInfo
const monsterIdx = getMonsterInfoIndex(monsterInfo.id, BAT_INFOS)
if (monsterIdx > -1) {
// die event
IO.emit(EVENT_NAME.player.killBat, data)
// reset monster info
const newStartVector = getRandomStartCreatureVector()
monsterInfo = resetCreatureInfo(monsterInfo, newStartVector)
// update server data
BAT_INFOS[monsterIdx] = monsterInfo
// send data
// @todo check, we need this object ?
const newData = { // eslint-disable-line
monsterInfo: monsterInfo
}
IO.emit(EVENT_NAME.player.respawnBat, data)
}
})
// attack - enemy
socket.on(EVENT_NAME.player.attackEnemy, function (data) {
const playerInfo = data.playerInfo
const playerIdx = getPlayerInfoIndexById(playerInfo.id)
if (playerIdx > -1) {
PLAYER_INFOS[playerIdx] = playerInfo
IO.emit(EVENT_NAME.player.attackEnemy, data)
}
})
// kill - enemy
// is died
socket.on(EVENT_NAME.player.killEnemy, function (data) {
let playerInfo = data.playerInfo
const playerIdx = getPlayerInfoIndexById(playerInfo.id)
if (playerIdx > -1) {
// die event
IO.emit(EVENT_NAME.player.killEnemy, data)
// reset player info
var newStartVector = getRandomStartCreatureVector()
playerInfo = resetCreatureInfo(playerInfo, newStartVector)
// update server data
PLAYER_INFOS[playerIdx] = playerInfo
// send data
// @todo check, we need this object ?
const newData = { // eslint-disable-line
playerInfo: playerInfo
}
IO.emit(EVENT_NAME.player.respawnEnemy, data)
}
})
})
/* ================================================================ Log / Report
*/
/**
* Report number of current connection
*/
function reportNumberOfConnections () { // eslint-disable-line
var n = getNumberOfConnection()
UTIL.serverLog('Players (n)', n)
}
/* ================================================================ Update
*/
/**
* Get nearest player
*
* @todo update function name
* @param {CreatureInfo} monsterInfo
* @param {number} visibleRange
* @returns {Object}
*/
function getNearestPlayer (monsterInfo, visibleRange) {
const nPlayers = PLAYER_INFOS.length
let nearestPlayerDistance = 9000 // hack (must to bigger more than map size)
let nearestPlayerVector = {}
let data = {}
let playerInfo
for (let i = 0; i < nPlayers; i++) {
playerInfo = PLAYER_INFOS[i]
const distance = UTIL.getDistanceBetween(monsterInfo.lastVector, playerInfo.lastVector)
if (distance <= visibleRange && distance < nearestPlayerDistance) {
nearestPlayerDistance = distance
nearestPlayerVector = playerInfo.lastVector
}
}
if (!UTIL.isEmptyObject(nearestPlayerVector)) {
data = {
monsterInfo: {
id: monsterInfo.id
},
targetCreatureId: playerInfo.id, // unused
targetVector: nearestPlayerVector
}
}
return data
}
/**
* Update zombie position
* @todo complete it
*/
function updateZombie () {
}
/**
* Update machine monster event
* due to machine cannot move
* so, it will fire `fire` event only
*/
function updateMachine () {
const visibleRange = 336
const nMachines = MACHINE_INFOS.length
const nPlayers = PLAYER_INFOS.length
let dataArr = []
if (nPlayers > 0) {
// @todo refactor
for (let i = 0; i < nMachines; i++) {
const monsterInfo = MACHINE_INFOS[i]
const data = getNearestPlayer(monsterInfo, visibleRange)
if (!UTIL.isEmptyObject(data)) {
dataArr.push(data)
}
}
}
if (!UTIL.isEmpty(dataArr)) {
IO.emit(EVENT_NAME.server.machineFire, dataArr)
}
}
/**
* Update bat
* based on `updateZombie`
* @todo complete it
*/
function updateBat () {
}
/* ================================================================ Interval
*/
setInterval(function () {
// reportNumberOfConnections();
updateZombie()
updateMachine()
updateBat()
}, SERVER_HEARTBEAT)