@@ -255,13 +255,13 @@ class World {
255
255
// client input
256
256
// - decode packets
257
257
for ( let i = 0 ; i < this . playerIds . length ; i ++ ) {
258
+ // iterate on playerIds so pid order matters
258
259
if ( this . playerIds [ i ] === - 1 ) {
259
260
continue ;
260
261
}
261
262
262
263
const player = this . players [ this . playerIds [ i ] ] ;
263
264
if ( ! player ) {
264
- this . playerIds [ i ] = - 1 ;
265
265
continue ;
266
266
}
267
267
@@ -322,13 +322,13 @@ class World {
322
322
// - player/npc interactions
323
323
// - close interface if attempting to logout
324
324
for ( let i = 0 ; i < this . playerIds . length ; i ++ ) {
325
+ // iterate on playerIds so pid order matters
325
326
if ( this . playerIds [ i ] === - 1 ) {
326
327
continue ;
327
328
}
328
329
329
330
const player = this . players [ this . playerIds [ i ] ] ;
330
331
if ( ! player ) {
331
- this . playerIds [ i ] = - 1 ;
332
332
continue ;
333
333
}
334
334
@@ -377,10 +377,12 @@ class World {
377
377
378
378
if ( this . shutdownTick < this . currentTick ) {
379
379
if ( this . currentTick - player . lastResponse >= 25 ) {
380
+ // request logout after 15 seconds (this may be 16 ticks in osrs!)
380
381
player . logoutRequested = true ;
381
382
}
382
383
383
- if ( this . currentTick - player . lastResponse >= 100 ) {
384
+ if ( process . env . LOCAL_DEV === 'true' && this . currentTick - player . lastResponse >= 100 ) {
385
+ // remove after 60 seconds
384
386
player . queue = [ ] ;
385
387
player . weakQueue = [ ] ;
386
388
player . engineQueue = [ ] ;
@@ -484,6 +486,8 @@ class World {
484
486
this . futureUpdates . delete ( this . currentTick ) ;
485
487
}
486
488
489
+ this . computeSharedEvents ( ) ;
490
+
487
491
// client output
488
492
// - map update
489
493
// - player info
@@ -492,43 +496,9 @@ class World {
492
496
// - inv changes
493
497
// - stat changes
494
498
// - flush packets
495
- this . computeSharedEvents ( ) ;
496
-
497
- /// we're doing a pass to convert p_tele to walk/run if needed, todo refactor
498
- for ( let i = 0 ; i < this . playerIds . length ; i ++ ) {
499
- if ( this . playerIds [ i ] === - 1 ) {
500
- continue ;
501
- }
502
-
503
- const player = this . players [ this . playerIds [ i ] ] ;
504
- if ( ! player ) {
505
- this . playerIds [ i ] = - 1 ;
506
- continue ;
507
- }
508
-
509
- try {
510
- if ( player . tele && ! player . jump && Math . abs ( player . x - player . lastX ) < 2 && Math . abs ( player . z - player . lastZ ) < 2 ) {
511
- // convert teleport to a walk/run op
512
- player . walkDir = Position . face ( player . lastX , player . lastZ , player . x , player . z ) ;
513
- player . runDir = - 1 ; // TODO support run <= 2 tiles
514
- player . tele = false ;
515
- }
516
- } catch ( err ) {
517
- console . error ( err ) ;
518
- player . logout ( ) ;
519
- this . removePlayer ( player ) ;
520
- }
521
- }
522
-
523
- // client output
524
- for ( let i = 0 ; i < this . playerIds . length ; i ++ ) {
525
- if ( this . playerIds [ i ] === - 1 ) {
526
- continue ;
527
- }
528
-
529
- const player = this . players [ this . playerIds [ i ] ] ;
499
+ for ( let i = 0 ; i < this . players . length ; i ++ ) {
500
+ const player = this . players [ i ] ;
530
501
if ( ! player ) {
531
- this . playerIds [ i ] = - 1 ;
532
502
continue ;
533
503
}
534
504
@@ -552,14 +522,9 @@ class World {
552
522
}
553
523
554
524
// reset entity masks
555
- for ( let i = 0 ; i < this . playerIds . length ; i ++ ) {
556
- if ( this . playerIds [ i ] === - 1 ) {
557
- continue ;
558
- }
559
-
560
- const player = this . players [ this . playerIds [ i ] ] ;
525
+ for ( let i = 0 ; i < this . players . length ; i ++ ) {
526
+ const player = this . players [ i ] ;
561
527
if ( ! player ) {
562
- this . playerIds [ i ] = - 1 ;
563
528
continue ;
564
529
}
565
530
@@ -594,7 +559,7 @@ class World {
594
559
}
595
560
596
561
const end = Date . now ( ) ;
597
- // console.log(`tick ${this.currentTick} took ${end - start}ms`);
562
+ // console.log(`tick ${this.currentTick} took ${end - start}ms: ${this.getTotalPlayers()} players `);
598
563
599
564
this . currentTick ++ ;
600
565
@@ -944,7 +909,7 @@ class World {
944
909
stream . pos += length ;
945
910
946
911
socket . inCount [ opcode ] ++ ;
947
- if ( socket . inCount [ opcode ] > 10 ) {
912
+ if ( socket . inCount [ opcode ] > 5 ) {
948
913
continue ;
949
914
}
950
915
@@ -975,6 +940,7 @@ class World {
975
940
this . players [ index ] = player ;
976
941
this . playerIds [ pid ] = index ;
977
942
player . pid = pid ;
943
+ player . uid = ( ( Number ( player . username37 & 0x1FFFFFn ) << 11 ) | player . pid ) >>> 0 ;
978
944
979
945
this . getZone ( player . x , player . z , player . level ) . addPlayer ( player ) ;
980
946
@@ -1010,7 +976,27 @@ class World {
1010
976
1011
977
const player = this . players [ slot ] ;
1012
978
if ( ! player ) {
1013
- this . playerIds [ pid ] = - 1 ;
979
+ return null ;
980
+ }
981
+
982
+ return player ;
983
+ }
984
+
985
+ getPlayerByUid ( uid : number ) {
986
+ const pid = uid & 0x7FF ;
987
+ const name37 = ( uid >> 11 ) & 0x1FFFFF ;
988
+
989
+ const slot = this . playerIds [ pid ] ;
990
+ if ( slot === - 1 ) {
991
+ return null ;
992
+ }
993
+
994
+ const player = this . players [ slot ] ;
995
+ if ( ! player ) {
996
+ return null ;
997
+ }
998
+
999
+ if ( Number ( player . username37 & 0x1FFFFFn ) !== name37 ) {
1014
1000
return null ;
1015
1001
}
1016
1002
@@ -1023,13 +1009,25 @@ class World {
1023
1009
}
1024
1010
1025
1011
getTotalPlayers ( ) {
1026
- return this . players . length ;
1012
+ return this . players . filter ( p => p ) . length ;
1027
1013
}
1028
1014
1029
1015
getNpc ( nid : number ) {
1030
1016
return this . npcs [ nid ] ;
1031
1017
}
1032
1018
1019
+ getNpcByUid ( uid : number ) {
1020
+ const slot = uid & 0xFFFF ;
1021
+ const type = ( uid >> 16 ) & 0xFFFF ;
1022
+
1023
+ const npc = this . npcs [ slot ] ;
1024
+ if ( ! npc || npc . type !== type ) {
1025
+ return null ;
1026
+ }
1027
+
1028
+ return npc ;
1029
+ }
1030
+
1033
1031
getNextNid ( ) {
1034
1032
return this . npcs . indexOf ( null , 1 ) ;
1035
1033
}
0 commit comments