Files
OpenFrontIO/src/core/execution/alliance/AllianceRequestExecution.ts
T
Scott Anderson de3e80154b Fix eslint failure (#1921)
## Description:

Fix an eslint failure blocking PRs from being merged.

## 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
2025-08-24 17:23:11 -04:00

74 lines
1.7 KiB
TypeScript

import {
AllianceRequest,
Execution,
Game,
Player,
PlayerID,
} from "../../game/Game";
export class AllianceRequestExecution implements Execution {
private req: AllianceRequest | null = null;
private active = true;
private mg: Game | undefined;
constructor(
private readonly requestor: Player,
private readonly recipientID: PlayerID,
) {}
init(mg: Game, ticks: number): void {
this.mg = mg;
if (!mg.hasPlayer(this.recipientID)) {
console.warn(
`AllianceRequestExecution recipient ${this.recipientID} not found`,
);
return;
}
const recipient = mg.player(this.recipientID);
if (!this.requestor.canSendAllianceRequest(recipient)) {
console.warn("cannot send alliance request");
this.active = false;
} else {
const incoming = recipient
.outgoingAllianceRequests()
.find((r) => r.recipient() === this.requestor);
if (incoming) {
// If the recipient already has pending alliance request,
// then accept it instead of creating a new one.
this.active = false;
incoming.accept();
} else {
this.req = this.requestor.createAllianceRequest(recipient);
}
}
}
tick(ticks: number): void {
if (
this.req?.status() === "accepted" ||
this.req?.status() === "rejected"
) {
this.active = false;
return;
}
if (this.mg === undefined) throw new Error("Not initialized");
if (
this.mg.ticks() - (this.req?.createdAt() ?? 0) >
this.mg.config().allianceRequestDuration()
) {
this.req?.reject();
this.active = false;
}
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}