From 4a3856158395b0f3d2020f51af13dbf9d2895d1a Mon Sep 17 00:00:00 2001 From: Aleksey Orekhovsky Date: Sat, 26 Jul 2025 11:23:29 +0700 Subject: [PATCH] Track gold obtained through eliminations in player stats (#1566) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description: Currently, the game only recorded war gold statistics when a defender was killed as part of a territory‑annexation sequence. Regular attack eliminations did not trigger the war gold stat update. This PR: - Invokes `stats.goldWar(...)` in the standard elimination code path of `AttackExecution`, so that any kill - whether by full annexation or direct attack - correctly increments the attacker's war gold stat. - A new integration test suite `AttackStats.test.ts` covering both scenarios ## Please complete the following: - [X] I have added screenshots for all UI updates - [X] I process any text displayed to the user through translateText() and I've added it to the en.json file - [X] I have added relevant tests to the test directory - [X] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [X] I have read and accepted the CLA aggreement (only required once). ## Please put your Discord username so you can be contacted if a bug or regression is found: aaa4xu --- src/core/execution/AttackExecution.ts | 1 + tests/AttackStats.test.ts | 85 +++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 tests/AttackStats.test.ts diff --git a/src/core/execution/AttackExecution.ts b/src/core/execution/AttackExecution.ts index 62d692956..2e23fbb63 100644 --- a/src/core/execution/AttackExecution.ts +++ b/src/core/execution/AttackExecution.ts @@ -345,6 +345,7 @@ export class AttackExecution implements Execution { ); this.target.removeGold(gold); this._owner.addGold(gold); + this.mg.stats().goldWar(this._owner, this.target, gold); for (let i = 0; i < 10; i++) { for (const tile of this.target.tiles()) { diff --git a/tests/AttackStats.test.ts b/tests/AttackStats.test.ts new file mode 100644 index 000000000..1955aec11 --- /dev/null +++ b/tests/AttackStats.test.ts @@ -0,0 +1,85 @@ +import { AttackExecution } from "../src/core/execution/AttackExecution"; +import { SpawnExecution } from "../src/core/execution/SpawnExecution"; +import { Game, Player, PlayerInfo, PlayerType } from "../src/core/game/Game"; +import { GOLD_INDEX_WAR, GOLD_INDEX_WORK } from "../src/core/StatsSchemas"; +import { setup } from "./util/Setup"; + +let game: Game; +let player1: Player; +let player2: Player; + +describe("AttackStats", () => { + beforeEach(async () => { + game = await setup("plains", { infiniteTroops: true }, [ + new PlayerInfo("player1", PlayerType.Human, "player1", "player1"), + new PlayerInfo("player2", PlayerType.Human, "player2", "player2"), + ]); + + player1 = game.player("player1"); + player2 = game.player("player2"); + + game.addExecution(new SpawnExecution(player1.info(), game.ref(50, 50))); + game.addExecution(new SpawnExecution(player2.info(), game.ref(50, 55))); + + while (game.inSpawnPhase()) { + game.executeNextTick(); + } + }); + + test("should increase war gold stat when a player is eliminated", () => { + expect(player1.sharesBorderWith(player2)).toBeTruthy(); + performAttack(game, player1, player2); + expectWarGoldStatIsIncreasedAfterKill(game, player1, player2); + }); + + test("should increase war gold stat when elimination occurs via territory annexation", () => { + // Mark every tile on the map as owned by player1 + for (let x = 0; x < game.map().width(); x++) { + for (let y = 0; y < game.map().height(); y++) { + player1.conquer(game.ref(x, y)); + } + } + // Place tiles of player2 in the center of the map + const centerX = Math.round(game.map().width() / 2); + const centerY = Math.round(game.map().height() / 2); + for (let x = -20; x < 20; x++) { + for (let y = -20; y < 20; y++) { + player2.conquer(game.ref(centerX + x, centerY + y)); + } + } + + performAttack(game, player1, player2); + expectWarGoldStatIsIncreasedAfterKill(game, player1, player2); + }); +}); + +function expectWarGoldStatIsIncreasedAfterKill( + game: Game, + attacker: Player, + defender: Player, +) { + // Verify that the defender was killed as a result of the attack + expect(attacker.isAlive()).toBeTruthy(); + expect(defender.isAlive()).toBeFalsy(); + + const attackerStats = game.stats().stats()[attacker.clientID()!]; + const defenderStats = game.stats().stats()[defender.clientID()!]; + + // Verify that all defender's gold was recorded as war gold in the attacker's stats + expect(attackerStats?.gold?.[GOLD_INDEX_WAR]).toBeDefined(); + expect(defenderStats?.gold?.[GOLD_INDEX_WORK]).toBeDefined(); + expect(attackerStats?.gold?.[GOLD_INDEX_WAR]).toBe( + defenderStats?.gold?.reduce((acc, g) => acc + g, 0n), + ); +} + +function performAttack(game: Game, attacker: Player, defender: Player) { + // Execute the attack + game.addExecution( + new AttackExecution(attacker.troops(), attacker, defender.id()), + ); + // Wait for the attack to complete + do { + game.executeNextTick(); + } while (attacker.outgoingAttacks().length > 0); +}