mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 18:30:53 +00:00
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import { consolex } from "../Consolex";
|
|
import { Execution, Game, Player, PlayerID } from "../game/Game";
|
|
|
|
export class DonateExecution implements Execution {
|
|
private sender: Player;
|
|
private recipient: Player;
|
|
|
|
private active = true;
|
|
|
|
constructor(
|
|
private senderID: PlayerID,
|
|
private recipientID: PlayerID,
|
|
private troops: number | null,
|
|
) {}
|
|
|
|
init(mg: Game, ticks: number): void {
|
|
if (!mg.hasPlayer(this.senderID)) {
|
|
console.warn(`DonateExecution: sender ${this.senderID} not found`);
|
|
this.active = false;
|
|
return;
|
|
}
|
|
if (!mg.hasPlayer(this.recipientID)) {
|
|
console.warn(`DonateExecution recipient ${this.recipientID} not found`);
|
|
this.active = false;
|
|
return;
|
|
}
|
|
|
|
this.sender = mg.player(this.senderID);
|
|
this.recipient = mg.player(this.recipientID);
|
|
if (this.troops == null) {
|
|
this.troops = mg.config().defaultDonationAmount(this.sender);
|
|
}
|
|
}
|
|
|
|
tick(ticks: number): void {
|
|
if (this.sender.canDonate(this.recipient)) {
|
|
this.sender.donate(this.recipient, this.troops);
|
|
this.recipient.updateRelation(this.sender, 50);
|
|
} else {
|
|
consolex.warn(
|
|
`cannot send tropps from ${this.sender} to ${this.recipient}`,
|
|
);
|
|
}
|
|
this.active = false;
|
|
}
|
|
|
|
owner(): Player {
|
|
return null;
|
|
}
|
|
|
|
isActive(): boolean {
|
|
return this.active;
|
|
}
|
|
|
|
activeDuringSpawnPhase(): boolean {
|
|
return false;
|
|
}
|
|
}
|