From 72c2eef7c267c07ba4886904a3c74bd012e17883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berk=20Elmal=C4=B1?= Date: Thu, 6 Aug 2026 03:51:24 +0300 Subject: [PATCH] fix(core): preserve null gold in DonateGoldExecution constructor (#4092) --- src/core/execution/DonateGoldExecution.ts | 4 ++-- tests/Donate.test.ts | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/core/execution/DonateGoldExecution.ts b/src/core/execution/DonateGoldExecution.ts index c12575b187..7a9e34c181 100644 --- a/src/core/execution/DonateGoldExecution.ts +++ b/src/core/execution/DonateGoldExecution.ts @@ -18,7 +18,7 @@ import { export class DonateGoldExecution implements Execution { private recipient: Player; - private gold: Gold; + private gold: Gold | null = null; private mg: Game; private random: PseudoRandom; @@ -30,7 +30,7 @@ export class DonateGoldExecution implements Execution { private recipientID: PlayerID, goldNum: number | null, ) { - this.gold = toInt(goldNum ?? 0); + this.gold = goldNum !== null ? toInt(goldNum) : null; } init(mg: Game, ticks: number): void { diff --git a/tests/Donate.test.ts b/tests/Donate.test.ts index 107e75ac09..339d2de4e4 100644 --- a/tests/Donate.test.ts +++ b/tests/Donate.test.ts @@ -124,6 +124,27 @@ describe("Donate gold to an ally", () => { expect(donor.gold() < donorGoldBefore).toBe(true); expect(recipient.gold() > recipientGoldBefore).toBe(true); }); + + it("Gold should default to 1/3 when null is passed", async () => { + const game = await setup("ocean_and_land", { infiniteGold: false, donateGold: true }); + const dInfo = new PlayerInfo("d", PlayerType.Human, null, "d_id"); + const rInfo = new PlayerInfo("r", PlayerType.Human, null, "r_id"); + game.addPlayer(dInfo); + game.addPlayer(rInfo); + const donor = game.player(dInfo.id), recipient = game.player(rInfo.id); + game.addExecution( + new SpawnExecution("g", dInfo, game.ref(0, 10)), + new SpawnExecution("g", rInfo, game.ref(0, 15)), + ); + donor.createAllianceRequest(recipient)?.accept(); + game.executeNextTick(); + donor.addGold(9000n); + const goldBefore = donor.gold(), recBefore = recipient.gold(); + game.addExecution(new DonateGoldExecution(donor, rInfo.id, null)); + game.executeNextTick(); + game.executeNextTick(); + expect(recipient.gold() >= recBefore + goldBefore / 3n).toBe(true); + }); }); describe("Donate troops to a non ally", () => {