From 39c8f9481dcf546bd5768c45bb0fedfbdda10225 Mon Sep 17 00:00:00 2001 From: evanpelle Date: Mon, 6 Jan 2025 19:56:52 -0800 Subject: [PATCH] attack working --- src/client/ClientGameRunner.ts | 20 ++++++++++++++------ src/core/GameView.ts | 15 ++++++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/client/ClientGameRunner.ts b/src/client/ClientGameRunner.ts index 57d49df90..40f433781 100644 --- a/src/client/ClientGameRunner.ts +++ b/src/client/ClientGameRunner.ts @@ -14,7 +14,7 @@ import { DisplayMessageEvent } from '../core/game/Game'; import { WorkerClient } from "../core/worker/WorkerClient"; import { consolex, initRemoteSender } from "../core/Consolex"; import { getConfig, getServerConfig } from "../core/configuration/Config"; -import { GameUpdateViewData, GameView } from "../core/GameView"; +import { GameUpdateViewData, GameView, PlayerView } from "../core/GameView"; export interface LobbyConfig { playerName: () => string @@ -99,7 +99,7 @@ export async function createClientGame(lobbyConfig: LobbyConfig, gameConfig: Gam } export class ClientGameRunner { - private myPlayer: Player + private myPlayer: PlayerView private isActive = false private turnsSeen = 0 @@ -124,7 +124,7 @@ export class ClientGameRunner { this.input.initialize() this.worker.start((gu: GameUpdateViewData) => { const size = gu.packedTileUpdates.length * 4 / 1000 - console.log(`game update size: ${size}kb`) + // console.log(`game update size: ${size}kb`) this.gameView.update(gu) this.renderer.tick() }) @@ -204,9 +204,17 @@ export class ClientGameRunner { if (tile.terrain().isLand()) { if (tile.hasOwner()) { - if (this.myPlayer.sharesBorderWith(tile.owner())) { - this.eventBus.emit(new SendAttackIntentEvent(targetID, this.myPlayer.troops() * this.renderer.uiState.attackRatio)) - } + this.myPlayer.sharesBorderWithAsync(tile.owner()).then(sharesBorder => { + if (sharesBorder) { + this.eventBus.emit( + + new SendAttackIntentEvent( + targetID, + this.myPlayer.troops() * this.renderer.uiState.attackRatio + ) + ) + } + }) } else { outer_loop: for (const t of bfs(tile, and(t => !t.hasOwner() && t.terrain().isLand(), dist(tile, 200)))) { for (const n of t.neighbors()) { diff --git a/src/core/GameView.ts b/src/core/GameView.ts index b0c501d48..2a3fb3c37 100644 --- a/src/core/GameView.ts +++ b/src/core/GameView.ts @@ -29,7 +29,7 @@ export class TileView { type(): TerrainType { return this._terrain.type() } - owner(): Player | TerraNullius { + owner(): PlayerView | TerraNullius { if (!this.hasOwner()) { return new TerraNulliusImpl() } @@ -195,6 +195,11 @@ export class PlayerView implements Player { sharesBorderWith(other: Player | TerraNullius): boolean { return false } + + async sharesBorderWithAsync(other: Player | TerraNullius): Promise { + return this.game.worker.sharesBorderWith(this.id(), other.id()) + } + incomingAllianceRequests(): AllianceRequest[] { return [] } @@ -255,7 +260,7 @@ export class GameView { private tiles: TileView[][] = [] private smallIDToID = new Map() - constructor(private worker: WorkerClient, private _config: Config, private _terrainMap: TerrainMap) { + constructor(public worker: WorkerClient, private _config: Config, private _terrainMap: TerrainMap) { // Initialize the 2D array this.tiles = Array(_terrainMap.width()).fill(null).map(() => Array(_terrainMap.height()).fill(null)); @@ -289,21 +294,21 @@ export class GameView { return this.data.tileUpdates.filter(d => true).map(tu => new TileView(this, tu, this._terrainMap.terrain(new Cell(tu.x, tu.y)))) } - player(id: PlayerID): Player { + player(id: PlayerID): PlayerView { if (id in this.data.players) { return new PlayerView(this, this.data.players[id]) } throw Error(`player id ${id} not found`) } - playerBySmallID(id: number): Player { + playerBySmallID(id: number): PlayerView { if (!this.smallIDToID.has(id)) { throw new Error(`small id ${id} not found`) } return this.player(this.smallIDToID.get(id)) } - playerByClientID(id: ClientID): Player | null { + playerByClientID(id: ClientID): PlayerView | null { const pd = Object.values(this.data.players).filter(p => p.clientID == id)[0] ?? null if (pd == null) { return null