Files
OpenFrontIO/src/core/execution/DonateTroopExecution.ts
T
bijx 9512e480d2 Fix: Players don't auto-send emoji replies when donated to, unlike nations (#2808)
## Description:

The new (awesome) nation emoji updates had a small bug in them when I
was playtesting with a friend where donating troops to them (a human
player) would result in the player automatically sending an emoji reply.
Sometimes these replies were negative-connotations like  and 🥱, which
could impact how other players perceive their donation attempt. This PR
fixes that issue.

### Example of player nation sending emojis automatically


https://github.com/user-attachments/assets/99689966-b784-4c3f-b43b-953a4a102e2d

### Donating to player after fix


https://github.com/user-attachments/assets/ace0c1ee-3eb8-4240-9c78-167dd773cfb2



## 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

## Please put your Discord username so you can be contacted if a bug or
regression is found:

bijx
2026-01-08 15:08:11 +00:00

126 lines
3.4 KiB
TypeScript

import {
Difficulty,
Execution,
Game,
Player,
PlayerID,
PlayerType,
} from "../game/Game";
import { PseudoRandom } from "../PseudoRandom";
import { assertNever } from "../Util";
import { EmojiExecution } from "./EmojiExecution";
import {
EMOJI_DONATION_TOO_SMALL,
EMOJI_LOVE,
} from "./nation/NationEmojiBehavior";
export class DonateTroopsExecution implements Execution {
private recipient: Player;
private random: PseudoRandom;
private mg: Game;
private active = true;
constructor(
private sender: Player,
private recipientID: PlayerID,
private troops: number | null,
) {}
init(mg: Game, ticks: number): void {
this.mg = mg;
this.random = new PseudoRandom(mg.ticks());
if (!mg.hasPlayer(this.recipientID)) {
console.warn(
`DonateTroopExecution recipient ${this.recipientID} not found`,
);
this.active = false;
return;
}
this.recipient = mg.player(this.recipientID);
this.troops ??= mg.config().defaultDonationAmount(this.sender);
const maxDonation =
mg.config().maxTroops(this.recipient) - this.recipient.troops();
this.troops = Math.min(this.troops, maxDonation);
}
tick(ticks: number): void {
if (this.troops === null) throw new Error("not initialized");
const minTroops = this.getMinTroopsForRelationUpdate();
if (
this.sender.canDonateTroops(this.recipient) &&
this.sender.donateTroops(this.recipient, this.troops)
) {
// Prevent players from just buying a good relation by sending 1% troops. Instead, a minimum is needed, and it's random.
if (this.troops >= minTroops) {
this.recipient.updateRelation(this.sender, 50);
}
// Only AI nations auto-respond with emojis, human players should not
if (this.recipient.type() === PlayerType.Nation) {
this.mg.addExecution(
new EmojiExecution(
this.recipient,
this.sender.id(),
this.random.randElement(
this.troops >= minTroops ? EMOJI_LOVE : EMOJI_DONATION_TOO_SMALL,
),
),
);
}
} else {
console.warn(
`cannot send troops from ${this.sender} to ${this.recipient}`,
);
}
this.active = false;
}
private getMinTroopsForRelationUpdate(): number {
const { difficulty } = this.mg.config().gameConfig();
const recipientMaxTroops = this.mg.config().maxTroops(this.recipient);
switch (difficulty) {
// ~7.7k - ~9.1k troops (for 100k troops)
case Difficulty.Easy:
return this.random.nextInt(
recipientMaxTroops / 13,
recipientMaxTroops / 11,
);
// ~9.1k - ~11.1k troops (for 100k troops)
case Difficulty.Medium:
return this.random.nextInt(
recipientMaxTroops / 11,
recipientMaxTroops / 9,
);
// ~11.1k - ~14.3k troops (for 100k troops)
case Difficulty.Hard:
return this.random.nextInt(
recipientMaxTroops / 9,
recipientMaxTroops / 7,
);
// ~14.3k - ~20k troops (for 100k troops)
case Difficulty.Impossible:
return this.random.nextInt(
recipientMaxTroops / 7,
recipientMaxTroops / 5,
);
default:
assertNever(difficulty);
}
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}