mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 14:00:48 +00:00
70745faac4
## Description: Improve type safety and runtime correctness by: 1. Enabling TypeScript's [strictNullChecks](https://www.typescriptlang.org/tsconfig/#strictNullChecks) compiler option. 2. Replacing all loose equality operators (`==` and `!=`) with strict equality operators (`===` and `!==`). 3. Cleaning up of type declarations, null handling logic, and equality expressions throughout the project. Currently, the code allows implicit assumptions that `null` and `undefined` are interchangeable, and relies on type-coercing equality checks that can introduce subtle bugs. These practices make it difficult to reason about when values may be absent and hinder the effectiveness of static analysis. Migrating to strict null checks and enforcing strict equality comparisons will clarify intent, reduce bugs, and make the codebase safer and easier to maintain. Fixes #466 ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors --------- Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com> Co-authored-by: evanpelle <openfrontio@gmail.com>
56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import { consolex } from "../Consolex";
|
|
import { Execution, Game, Player, PlayerID } from "../game/Game";
|
|
|
|
export class DonateTroopsExecution 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.troops === null) throw new Error("not initialized");
|
|
if (this.sender.canDonate(this.recipient)) {
|
|
this.sender.donateTroops(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;
|
|
}
|
|
|
|
isActive(): boolean {
|
|
return this.active;
|
|
}
|
|
|
|
activeDuringSpawnPhase(): boolean {
|
|
return false;
|
|
}
|
|
}
|