|
| 1 | +import { Dropoff } from "./Dropoff"; |
| 2 | +import { GameMap } from "./GameMap"; |
| 3 | +import { Logging } from "./Logging"; |
| 4 | +import { Player } from "./Player"; |
| 5 | +import { Position } from "./Position"; |
| 6 | +import { ServerCommunication } from "./ServerCommunicaion"; |
| 7 | +import { Ship } from "./Ship"; |
| 8 | +import { Shipyard } from "./Shipyard"; |
| 9 | + |
| 10 | +export class Game { |
| 11 | + public turnNumber = 0; |
| 12 | + public server: ServerCommunication = new ServerCommunication(); |
| 13 | + |
| 14 | + public myId = 0; |
| 15 | + public players = new Map<number, Player>(); |
| 16 | + public me?: Player; |
| 17 | + public gameMap?: GameMap; |
| 18 | + |
| 19 | + /** |
| 20 | + * Initialize a game object collecting all the start-state |
| 21 | + * instances for pre-game. Also sets up a log file in |
| 22 | + * "bot-<bot_id>.log". |
| 23 | + * @returns The initialized gameMap and me so we don't have to check if undefined. |
| 24 | + */ |
| 25 | + public async initialize(): Promise<[GameMap, Player]> { |
| 26 | + const serverData = await this.server.getInitialData(); |
| 27 | + this.myId = serverData.myId; |
| 28 | + |
| 29 | + Logging.setup(`bot-${this.myId}.log`); |
| 30 | + |
| 31 | + serverData.players.forEach((playerData) => { |
| 32 | + const player = new Player(playerData.id, |
| 33 | + new Shipyard(playerData.id, -1, new Position(playerData.x, playerData.y))); |
| 34 | + this.players.set(player.id, player); |
| 35 | + }); |
| 36 | + this.me = this.players.get(this.myId); |
| 37 | + |
| 38 | + this.gameMap = new GameMap(serverData.cells); |
| 39 | + |
| 40 | + return [this.gameMap as GameMap, this.me as Player]; // We cast here because we have just initialized |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Updates the game object's state. |
| 45 | + */ |
| 46 | + public async updateFrame() { |
| 47 | + const data = await this.server.getUpdateData(this.players.size); |
| 48 | + this.turnNumber = data.turn; |
| 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; |
| 52 | + player.haliteAmount = playerData.halite; |
| 53 | + |
| 54 | + // Process ships |
| 55 | + const newShipsData = playerData.ships |
| 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))); |
| 60 | + |
| 61 | + const lostShips = player.getShips() |
| 62 | + .filter((ship) => !playerData.ships.some((shipData) => shipData.id === ship.id)); |
| 63 | + lostShips.forEach((ship) => player.loseShip(ship.id)); |
| 64 | + |
| 65 | + player.getShips().forEach((ship) => { |
| 66 | + const updatedShipData = playerData.ships |
| 67 | + .find((shipData) => ship.id === shipData.id); |
| 68 | + if (updatedShipData) { |
| 69 | + [ship.haliteAmount, ship.position.x, ship.position.y] = |
| 70 | + [updatedShipData.halite, updatedShipData.x, updatedShipData.y]; |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + // Process dropoffs |
| 75 | + const newDropoffsData = playerData.dropoffs |
| 76 | + .filter((dropoffData) => !player.dropoffs.has(dropoffData.id)); |
| 77 | + newDropoffsData.forEach((newDropoffData: { id: number, x: number, y: number }) => |
| 78 | + player.dropoffs.set(newDropoffData.id, |
| 79 | + new Dropoff(player.id, newDropoffData.id, new Position(newDropoffData.x, newDropoffData.y)))); |
| 80 | + |
| 81 | + const lostDropoffs = Array.from(player.dropoffs.values()) |
| 82 | + .filter((dropoff) => !playerData.dropoffs.some((dropoffData) => dropoffData.id === dropoff.id)); |
| 83 | + lostDropoffs.forEach((lostDropoff) => { |
| 84 | + player.dropoffs.delete(lostDropoff.id); |
| 85 | + player.lostDropoffs.set(lostDropoff.id, lostDropoff); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + const gameMap = this.gameMap as GameMap; |
| 90 | + // Mark all cells as safe |
| 91 | + gameMap.cells.forEach((row) => row.forEach((cell) => cell.markSafe())); |
| 92 | + // Update cells |
| 93 | + data.cells.forEach((cell) => gameMap.get(new Position(cell.x, cell.y)).haliteAmount = cell.halite); |
| 94 | + // Mark cells with ships as unsafe for navigation, mark sturctures |
| 95 | + for (const player of this.players.values()) { |
| 96 | + player.getShips() |
| 97 | + .forEach((ship) => gameMap.get(ship.position).markUnsafe(ship)); |
| 98 | + player.getDropoffs() |
| 99 | + .forEach((dropoff) => gameMap.get(dropoff.position).structure = dropoff); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Indicate that your bot is ready to play by sending the bot name. |
| 105 | + */ |
| 106 | + public async ready(botName: string) { |
| 107 | + await this.server.sendCommands([botName]); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Send all commands to the game engine, effectively ending your |
| 112 | + * turn. |
| 113 | + */ |
| 114 | + public async endTurn(commands: string[]) { |
| 115 | + await this.server.sendCommands(commands); |
| 116 | + } |
| 117 | +} |
0 commit comments