Files
Cameron Clark 1ecd6c4ee1 Private lobby toggle donation (#1752)
Resolve #1652

1. Add the ability to toggle **gold donations** and **troop donations**
for private lobbies
~2. Add relevant translations.~
3. Refactor `canDonate` to be specific to gold and troop donations
4. Add placeholders for singleplayer mode if this is to be extended to
support that too.
5. Add Tests for Donate logic
<img width="1643" height="1788" alt="image"
src="https://github.com/user-attachments/assets/82b93400-a1f0-45f0-8b2b-a7f78dc0c3e9"
/>

_Private Lobby_

![donatetroopsprivatelobby](https://github.com/user-attachments/assets/c6690bbc-958e-48a1-9cf1-e2b361dfb1b2)
_Testing Troop Send In Private Lobby_

![donatetroopsprivatelobby2](https://github.com/user-attachments/assets/698c7603-6b4b-4da7-91ab-7bdc38bb49a5)

_Troop Send Complete In Private Lobby_

![testtradepublicteams](https://github.com/user-attachments/assets/1010332c-3f38-4644-9218-46aa7141f578)
Confirming that public teams still works

- [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 agreement (only required once).
regression is found:

DISCORD_USERNAME: cool_clarky

---------

Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com>
Co-authored-by: Drills Kibo <59177241+drillskibo@users.noreply.github.com>
2025-09-03 16:19:13 -07:00

253 lines
6.7 KiB
TypeScript

import { DonateGoldExecution } from "../src/core/execution/DonateGoldExecution";
import { DonateTroopsExecution } from "../src/core/execution/DonateTroopExecution";
import { SpawnExecution } from "../src/core/execution/SpawnExecution";
import { PlayerInfo, PlayerType } from "../src/core/game/Game";
import { setup } from "./util/Setup";
describe("Donate troops to an ally", () => {
it("Troops should be successfully donated", async () => {
const game = await setup("ocean_and_land", {
infiniteTroops: false,
donateTroops: true,
});
const donorInfo = new PlayerInfo(
"donor",
PlayerType.Human,
null,
"donor_id",
);
const recipientInfo = new PlayerInfo(
"recipient",
PlayerType.Human,
null,
"recipient_id",
);
game.addPlayer(donorInfo);
game.addPlayer(recipientInfo);
const donor = game.player(donorInfo.id);
const recipient = game.player(recipientInfo.id);
// Spawn both players
const spawnA = game.ref(0, 10);
const spawnB = game.ref(0, 15);
game.addExecution(
new SpawnExecution(donorInfo, spawnA),
new SpawnExecution(recipientInfo, spawnB),
);
while (game.inSpawnPhase()) {
game.executeNextTick();
}
// donor sends alliance request to recipient
const allianceRequest = donor.createAllianceRequest(recipient);
expect(allianceRequest).not.toBeNull();
// recipient accepts the alliance request
if (allianceRequest) {
allianceRequest.accept();
}
// Ensure donor can actually donate the requested amount
donor.addTroops(6000);
const donorTroopsBefore = donor.troops();
const recipientTroopsBefore = recipient.troops();
game.addExecution(new DonateTroopsExecution(donor, recipientInfo.id, 5000));
for (let i = 0; i < 5; i++) {
game.executeNextTick();
}
expect(donor.troops() < donorTroopsBefore).toBe(true);
expect(recipient.troops() > recipientTroopsBefore).toBe(true);
});
});
describe("Donate gold to an ally", () => {
it("Gold should be successfully donated", async () => {
const game = await setup("ocean_and_land", {
infiniteGold: false,
donateGold: true,
});
const donorInfo = new PlayerInfo(
"donor",
PlayerType.Human,
null,
"donor_id",
);
const recipientInfo = new PlayerInfo(
"recipient",
PlayerType.Human,
null,
"recipient_id",
);
game.addPlayer(donorInfo);
game.addPlayer(recipientInfo);
const donor = game.player(donorInfo.id);
const recipient = game.player(recipientInfo.id);
// Spawn both players
const spawnA = game.ref(0, 10);
const spawnB = game.ref(0, 15);
game.addExecution(
new SpawnExecution(donorInfo, spawnA),
new SpawnExecution(recipientInfo, spawnB),
);
while (game.inSpawnPhase()) {
game.executeNextTick();
}
// donor sends alliance request to recipient
const allianceRequest = donor.createAllianceRequest(recipient);
expect(allianceRequest).not.toBeNull();
// recipient accepts the alliance request
if (allianceRequest) {
allianceRequest.accept();
}
game.executeNextTick();
// Ensure donor can actually donate the requested amount
donor.addGold(6000n);
const donorGoldBefore = donor.gold();
const recipientGoldBefore = recipient.gold();
game.addExecution(new DonateGoldExecution(donor, recipientInfo.id, 5000n));
for (let i = 0; i < 5; i++) {
game.executeNextTick();
}
expect(donor.gold() < donorGoldBefore).toBe(true);
expect(recipient.gold() > recipientGoldBefore).toBe(true);
});
});
describe("Donate troops to a non ally", () => {
it("Troops should not be donated", async () => {
const game = await setup("ocean_and_land", {
infiniteTroops: false,
donateTroops: true,
});
const donorInfo = new PlayerInfo(
"donor",
PlayerType.Human,
null,
"donor_id",
);
const recipientInfo = new PlayerInfo(
"recipient",
PlayerType.Human,
null,
"recipient_id",
);
game.addPlayer(donorInfo);
game.addPlayer(recipientInfo);
const donor = game.player(donorInfo.id);
const recipient = game.player(recipientInfo.id);
// Spawn both players
const spawnA = game.ref(0, 10);
const spawnB = game.ref(0, 15);
game.addExecution(
new SpawnExecution(donorInfo, spawnA),
new SpawnExecution(recipientInfo, spawnB),
);
while (game.inSpawnPhase()) {
game.executeNextTick();
}
// Donor sends alliance request to Recipient
const allianceRequest = donor.createAllianceRequest(recipient);
expect(allianceRequest).not.toBeNull();
// Donor rejects the Recipient
if (allianceRequest) {
allianceRequest.reject();
}
const donorTroopsBefore = donor.troops();
const recipientTroopsBefore = recipient.troops();
game.addExecution(new DonateTroopsExecution(donor, recipientInfo.id, 5000));
game.executeNextTick();
// Troops should not be donated since they are not allies
expect(donor.troops() >= donorTroopsBefore).toBe(true);
expect(recipient.troops() >= recipientTroopsBefore).toBe(true);
});
});
describe("Donate Gold to a non ally", () => {
it("Gold should not be donated", async () => {
const game = await setup("ocean_and_land", {
infiniteGold: false,
donateGold: true,
});
const donorInfo = new PlayerInfo(
"donor",
PlayerType.Human,
null,
"donor_id",
);
const recipientInfo = new PlayerInfo(
"recipient",
PlayerType.Human,
null,
"recipient_id",
);
game.addPlayer(donorInfo);
game.addPlayer(recipientInfo);
const donor = game.player(donorInfo.id);
const recipient = game.player(recipientInfo.id);
// Spawn both players
const spawnA = game.ref(0, 10);
const spawnB = game.ref(0, 15);
game.addExecution(
new SpawnExecution(donorInfo, spawnA),
new SpawnExecution(recipientInfo, spawnB),
);
while (game.inSpawnPhase()) {
game.executeNextTick();
}
// Donor sends alliance request to Recipient
const allianceRequest = donor.createAllianceRequest(recipient);
expect(allianceRequest).not.toBeNull();
// Donor rejects the Recipient
if (allianceRequest) {
allianceRequest.reject();
}
const donorGoldBefore = donor.gold();
const recipientGoldBefore = donor.gold();
game.addExecution(new DonateGoldExecution(donor, recipientInfo.id, 5000n));
game.executeNextTick();
// Gold should not be donated since they are not allies
expect(donor.gold() >= donorGoldBefore).toBe(true);
expect(recipient.gold() >= recipientGoldBefore).toBe(true);
});
});