1- import { Logging } from "./Logging " ;
1+ import { Dropoff } from "./Dropoff " ;
22import { GameMap } from "./GameMap" ;
3+ import { Logging } from "./Logging" ;
34import { Player } from "./Player" ;
4- import { Ship } from "./Ship" ;
55import { Position } from "./Position" ;
6- import { Shipyard } from "./Shipyard" ;
7- import { Dropoff } from "./Dropoff" ;
86import { ServerCommunication } from "./ServerCommunicaion" ;
7+ import { Ship } from "./Ship" ;
8+ import { Shipyard } from "./Shipyard" ;
99
1010export class Game {
11- turnNumber = 0 ;
12- server : ServerCommunication = new ServerCommunication ( ) ;
11+ public turnNumber = 0 ;
12+ public server : ServerCommunication = new ServerCommunication ( ) ;
1313
1414 public myId = 0 ;
1515 public players = new Map < number , Player > ( ) ;
@@ -22,94 +22,96 @@ export class Game {
2222 * "bot-<bot_id>.log".
2323 * @returns The initialized gameMap and me so we don't have to check if undefined.
2424 */
25- async initialize ( ) : Promise < [ GameMap , Player ] > {
25+ public async initialize ( ) : Promise < [ GameMap , Player ] > {
2626 const serverData = await this . server . getInitialData ( ) ;
2727 this . myId = serverData . myId ;
2828
2929 Logging . setup ( `bot-${ this . myId } .log` ) ;
3030
31- serverData . players . forEach ( playerData => {
32- const player = new Player ( playerData . id , new Shipyard ( playerData . id , - 1 , new Position ( playerData . x , playerData . y ) ) )
31+ serverData . players . forEach ( ( playerData ) => {
32+ const player = new Player ( playerData . id ,
33+ new Shipyard ( playerData . id , - 1 , new Position ( playerData . x , playerData . y ) ) ) ;
3334 this . players . set ( player . id , player ) ;
3435 } ) ;
3536 this . me = this . players . get ( this . myId ) ;
3637
3738 this . gameMap = new GameMap ( serverData . cells ) ;
3839
39- return [ < GameMap > this . gameMap , < Player > this . me ] ; // We cast here because we have just initialized
40+ return [ this . gameMap as GameMap , this . me as Player ] ; // We cast here because we have just initialized
4041 }
4142
4243 /**
4344 * Updates the game object's state.
4445 */
45- async updateFrame ( ) {
46+ public async updateFrame ( ) {
4647 const data = await this . server . getUpdateData ( this . players . size ) ;
4748 this . turnNumber = data . turn ;
48- Logging . info ( `================ TURN ${ this . turnNumber . toString ( ) . padStart ( 3 , '0' ) } ================` ) ;
49- data . players . forEach ( playerData => {
50- const player = < Player > this . players . get ( playerData . id ) ;
49+ Logging . info ( `================ TURN ${ this . turnNumber . toString ( ) . padStart ( 3 , "0" ) } ================` ) ;
50+ data . players . forEach ( ( playerData ) => {
51+ const player = this . players . get ( playerData . id ) as Player ;
5152 player . haliteAmount = playerData . halite ;
5253
5354 // Process ships
5455 const newShipsData = playerData . ships
55- . filter ( shipData => ! player . hasShip ( shipData . id ) ) ;
56- newShipsData . forEach ( newShipData =>
57- player . addShip ( new Ship ( player . id , newShipData . id , new Position ( newShipData . x , newShipData . y ) , newShipData . halite ) ) ) ;
56+ . filter ( ( shipData ) => ! player . hasShip ( shipData . id ) ) ;
57+ newShipsData . forEach ( ( newShipData ) =>
58+ player . addShip ( new Ship ( player . id , newShipData . id ,
59+ new Position ( newShipData . x , newShipData . y ) , newShipData . halite ) ) ) ;
5860
5961 const lostShips = player . getShips ( )
60- . filter ( ship => ! playerData . ships . some ( shipData => shipData . id === ship . id ) ) ;
61- lostShips . forEach ( ship => player . loseShip ( ship . id ) ) ;
62+ . filter ( ( ship ) => ! playerData . ships . some ( ( shipData ) => shipData . id === ship . id ) ) ;
63+ lostShips . forEach ( ( ship ) => player . loseShip ( ship . id ) ) ;
6264
63- player . getShips ( ) . forEach ( ship => {
65+ player . getShips ( ) . forEach ( ( ship ) => {
6466 const updatedShipData = playerData . ships
65- . find ( shipData => ship . id === shipData . id ) ;
67+ . find ( ( shipData ) => ship . id === shipData . id ) ;
6668 if ( updatedShipData ) {
67- [ ship . haliteAmount , ship . position . x , ship . position . y ] = [ updatedShipData . halite , updatedShipData . x , updatedShipData . y ] ;
69+ [ ship . haliteAmount , ship . position . x , ship . position . y ] =
70+ [ updatedShipData . halite , updatedShipData . x , updatedShipData . y ] ;
6871 }
6972 } ) ;
7073
7174 // Process dropoffs
7275 const newDropoffsData = playerData . dropoffs
73- . filter ( dropoffData => ! player . dropoffs . has ( dropoffData . id ) ) ;
76+ . filter ( ( dropoffData ) => ! player . dropoffs . has ( dropoffData . id ) ) ;
7477 newDropoffsData . forEach ( ( newDropoffData : { id : number , x : number , y : number } ) =>
7578 player . dropoffs . set ( newDropoffData . id ,
7679 new Dropoff ( player . id , newDropoffData . id , new Position ( newDropoffData . x , newDropoffData . y ) ) ) ) ;
7780
7881 const lostDropoffs = Array . from ( player . dropoffs . values ( ) )
79- . filter ( dropoff => ! playerData . dropoffs . some ( dropoffData => dropoffData . id === dropoff . id ) ) ;
80- lostDropoffs . forEach ( lostDropoff => {
82+ . filter ( ( dropoff ) => ! playerData . dropoffs . some ( ( dropoffData ) => dropoffData . id === dropoff . id ) ) ;
83+ lostDropoffs . forEach ( ( lostDropoff ) => {
8184 player . dropoffs . delete ( lostDropoff . id ) ;
8285 player . lostDropoffs . set ( lostDropoff . id , lostDropoff ) ;
8386 } ) ;
8487 } ) ;
8588
86- const gameMap = < GameMap > this . gameMap ;
89+ const gameMap = this . gameMap as GameMap ;
8790 // Mark all cells as safe
88- gameMap . cells . forEach ( row => row . forEach ( cell => cell . markSafe ( ) ) ) ;
91+ gameMap . cells . forEach ( ( row ) => row . forEach ( ( cell ) => cell . markSafe ( ) ) ) ;
8992 // Update cells
90- data . cells . forEach ( cell => gameMap . get ( new Position ( cell . x , cell . y ) ) . haliteAmount = cell . halite ) ;
93+ data . cells . forEach ( ( cell ) => gameMap . get ( new Position ( cell . x , cell . y ) ) . haliteAmount = cell . halite ) ;
9194 // Mark cells with ships as unsafe for navigation, mark sturctures
9295 for ( const player of this . players . values ( ) ) {
9396 player . getShips ( )
94- . forEach ( ship => gameMap . get ( ship . position ) . markUnsafe ( ship ) ) ;
97+ . forEach ( ( ship ) => gameMap . get ( ship . position ) . markUnsafe ( ship ) ) ;
9598 player . getDropoffs ( )
96- . forEach ( dropoff => gameMap . get ( dropoff . position ) . structure = dropoff ) ;
99+ . forEach ( ( dropoff ) => gameMap . get ( dropoff . position ) . structure = dropoff ) ;
97100 }
98101 }
99102
100- /**
101- * Indicate that your bot is ready to play by sending the bot name.
103+ /**
104+ * Indicate that your bot is ready to play by sending the bot name.
102105 */
103- async ready ( botName : string ) {
106+ public async ready ( botName : string ) {
104107 await this . server . sendCommands ( [ botName ] ) ;
105108 }
106109
107110 /**
108111 * Send all commands to the game engine, effectively ending your
109112 * turn.
110113 */
111- async endTurn ( commands : string [ ] ) {
114+ public async endTurn ( commands : string [ ] ) {
112115 await this . server . sendCommands ( commands ) ;
113116 }
114117}
115-
0 commit comments