@@ -4,7 +4,7 @@ import WebSocket from "ws";
4
4
import Client from "./Client" ;
5
5
import Player from "./Player" ;
6
6
import * as lowDb from 'lowdb' ;
7
- import { randomPos } from "./util" ;
7
+ import { randomPos , chunk } from "./util" ;
8
8
import msgpack from "msgpack-lite" ;
9
9
import GameState from "./GameState" ;
10
10
import * as Physics from "./Physics" ;
@@ -15,7 +15,7 @@ import GameObject from "../gameobjects/GameObject";
15
15
import { PacketType } from "../packets/PacketType" ;
16
16
import FileAsync from 'lowdb/adapters/FileAsync' ;
17
17
import { PacketFactory } from "../packets/PacketFactory" ;
18
- import { getWeaponDamage , getWeaponAttackDetails } from "../items/items" ;
18
+ import { getWeaponDamage , getWeaponAttackDetails , getItemCost , getPlaceable } from "../items/items" ;
19
19
import { gameObjectSizes , GameObjectType } from "../gameobjects/gameobjects" ;
20
20
21
21
let currentGame : Game | null = null ;
@@ -58,6 +58,12 @@ export default class Game {
58
58
process . nextTick ( this . update ) ;
59
59
}
60
60
61
+ getNextGameObjectID ( ) {
62
+ return this . state . gameObjects . length > 0
63
+ ? Math . max ( ...this . state . gameObjects . map ( ( gameObj ) => gameObj . id ) ) + 1
64
+ : 0 ;
65
+ }
66
+
61
67
generateStructures ( ) {
62
68
const gameObjectTypes = [ GameObjectType . Tree ] ;
63
69
@@ -68,13 +74,11 @@ export default class Game {
68
74
69
75
if ( sizes ) {
70
76
let newGameObject = new GameObject (
71
- this . state . gameObjects . length > 0
72
- ? Math . max ( ...this . state . gameObjects . map ( ( gameObj ) => gameObj . id ) ) + 1
73
- : 0 ,
77
+ this . getNextGameObjectID ( ) ,
74
78
randomPos ( 12e3 , 12e3 ) ,
75
- 0 ,
76
- sizes [ Math . floor ( Math . random ( ) * sizes . length ) ] ,
77
- gameObjectType
79
+ 0 ,
80
+ sizes [ Math . floor ( Math . random ( ) * sizes . length ) ] ,
81
+ gameObjectType
78
82
) ;
79
83
80
84
for ( let gameObject of this . state . gameObjects ) {
@@ -166,8 +170,8 @@ export default class Game {
166
170
}
167
171
168
172
kickClient ( client : Client , reason : string = "kicked" ) {
169
- console . log ( `Kicked ${ client . id } : ${ reason } ` ) ;
170
173
this . clients . splice ( this . clients . indexOf ( client ) , 1 ) ;
174
+ console . log ( `Kicked ${ client . id } : ${ reason } ` ) ;
171
175
172
176
// nothing sketchy, just keeps the reason there using a glitch that allows script execution
173
177
client . socket . send ( msgpack . encode ( [ "d" , [
@@ -176,7 +180,7 @@ export default class Game {
176
180
177
181
setTimeout ( ( ) => {
178
182
client . socket . close ( ) ;
179
- } , 10 ) ;
183
+ } , 1 ) ;
180
184
}
181
185
182
186
async banClient ( client : Client ) {
@@ -185,6 +189,7 @@ export default class Game {
185
189
await ( await ( await this . db . get ( "bannedIPs" ) ) . push ( client . ip ) ) . write ( ) ;
186
190
}
187
191
192
+ console . log ( `Banned ${ client . id } with ip ${ client . ip } ` ) ;
188
193
this . kickClient ( client , "Banned by a Moderator" ) ;
189
194
}
190
195
}
@@ -350,7 +355,7 @@ export default class Game {
350
355
this . state . players . forEach ( ( player ) => {
351
356
Physics . movePlayer ( player , 33 ) ;
352
357
353
- if ( player . isAttacking ) {
358
+ if ( player . isAttacking && player . buildItem == - 1 ) {
354
359
if ( Date . now ( ) - player . lastHitTime >= player . getWeaponHitTime ( ) ) {
355
360
let nearbyPlayers = player . getNearbyPlayers ( this . state ) ;
356
361
@@ -441,11 +446,35 @@ export default class Game {
441
446
* @param player the player doing the attacking
442
447
*/
443
448
normalAttack ( player : Player ) {
444
- player . isAttacking = true ;
445
-
446
449
if ( player . buildItem != - 1 ) {
447
- // TODO: use the item
450
+ let item = player . buildItem ;
451
+ if ( player . useItem ( item , this . state , this . getNextGameObjectID ( ) ) ) {
452
+ if ( getPlaceable ( item ) ) {
453
+ player . getNearbyPlayers ( this . state ) . forEach ( nearbyPlayer => this . sendGameObjects ( nearbyPlayer ) )
454
+ this . sendGameObjects ( player ) ;
455
+ }
456
+
457
+ let itemCost = getItemCost ( item ) ;
458
+ let costs = chunk ( itemCost , 2 ) ;
459
+
460
+ for ( let cost of costs ) {
461
+ switch ( cost [ 0 ] ) {
462
+ case "food" :
463
+ player . food -= cost [ 1 ] as number ;
464
+ break ;
465
+ case "wood" :
466
+ player . wood -= cost [ 1 ] as number ;
467
+ break ;
468
+ case "stone" :
469
+ player . stone -= cost [ 1 ] as number ;
470
+ break ;
471
+ }
472
+ }
473
+
474
+ player . buildItem = - 1 ;
475
+ }
448
476
} else {
477
+ player . isAttacking = true ;
449
478
}
450
479
}
451
480
@@ -530,6 +559,11 @@ export default class Game {
530
559
newPlayer . dead = false ;
531
560
newPlayer . health = 100 ;
532
561
562
+ newPlayer . food = packet . data [ 0 ] . moofoll ? 100 : 0 ;
563
+ newPlayer . points = packet . data [ 0 ] . moofoll ? 100 : 0 ;
564
+ newPlayer . stone = packet . data [ 0 ] . moofoll ? 100 : 0 ;
565
+ newPlayer . wood = packet . data [ 0 ] . moofoll ? 100 : 0 ;
566
+
533
567
client . socket . send (
534
568
packetFactory . serializePacket (
535
569
new Packet ( PacketType . PLAYER_START , [ newPlayer . id ] )
@@ -672,6 +706,46 @@ export default class Game {
672
706
}
673
707
}
674
708
break ;
709
+ case PacketType . SELECT_ITEM :
710
+ if ( client . player ) {
711
+ let isWeapon = packet . data [ 1 ] ;
712
+
713
+ if ( isWeapon ) {
714
+ client . player . buildItem = - 1 ;
715
+ if ( client . player . weapon == packet . data [ 0 ] ) {
716
+ client . player . selectedWeapon = client . player . weapon ;
717
+ } else if ( client . player . secondaryWeapon == packet . data [ 0 ] ) {
718
+ client . player . selectedWeapon = client . player . secondaryWeapon ;
719
+ }
720
+ } else {
721
+ let itemCost = getItemCost ( packet . data [ 0 ] ) ;
722
+ let costs = chunk ( itemCost , 2 ) ;
723
+
724
+ for ( let cost of costs ) {
725
+ switch ( cost [ 0 ] ) {
726
+ case "food" :
727
+ if ( client . player . food < cost [ 1 ] )
728
+ return ;
729
+ break ;
730
+ case "wood" :
731
+ if ( client . player . wood < cost [ 1 ] )
732
+ return ;
733
+ break ;
734
+ case "stone" :
735
+ if ( client . player . stone < cost [ 1 ] )
736
+ return ;
737
+ break ;
738
+ }
739
+ }
740
+
741
+ if ( client . player . buildItem == packet . data [ 0 ] ) {
742
+ client . player . buildItem = - 1 ;
743
+ } else {
744
+ client . player . buildItem = packet . data [ 0 ] ;
745
+ }
746
+ }
747
+ }
748
+ break ;
675
749
}
676
750
}
677
751
}
0 commit comments