Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/execution/DonateGoldExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions tests/Donate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Loading