diff --git a/src/core/execution/TradeShipExecution.ts b/src/core/execution/TradeShipExecution.ts index 99663faad..fd7062ca5 100644 --- a/src/core/execution/TradeShipExecution.ts +++ b/src/core/execution/TradeShipExecution.ts @@ -16,9 +16,9 @@ import { distSortUnit } from "../Util"; export class TradeShipExecution implements Execution { private active = true; - private mg: Game; - private origOwner: Player; - private tradeShip: Unit; + private mg: Game | null = null; + private origOwner: Player | null = null; + private tradeShip: Unit | null = null; private index = 0; private wasCaptured = false; @@ -35,12 +35,15 @@ export class TradeShipExecution implements Execution { } tick(ticks: number): void { - if (this.tradeShip == null) { + if (this.mg === null || this.origOwner === null) { + throw new Error("Not initialized"); + } + if (this.tradeShip === null) { const spawn = this.origOwner.canBuild( UnitType.TradeShip, this.srcPort.tile(), ); - if (spawn == false) { + if (spawn === false) { consolex.warn(`cannot build trade ship`); this.active = false; return; @@ -55,14 +58,14 @@ export class TradeShipExecution implements Execution { return; } - if (this.origOwner != this.tradeShip.owner()) { + if (this.origOwner !== this.tradeShip.owner()) { // Store as vairable in case ship is recaptured by previous owner this.wasCaptured = true; } // If a player captures an other player's port while trading we should delete // the ship. - if (this._dstPort.owner().id() == this.srcPort.owner().id()) { + if (this._dstPort.owner().id() === this.srcPort.owner().id()) { this.tradeShip.delete(false); this.active = false; return; @@ -83,7 +86,7 @@ export class TradeShipExecution implements Execution { .owner() .units(UnitType.Port) .sort(distSortUnit(this.mg, this.tradeShip)); - if (ports.length == 0) { + if (ports.length === 0) { this.tradeShip.delete(false); this.active = false; return; @@ -120,6 +123,10 @@ export class TradeShipExecution implements Execution { } private complete() { + if (this.mg === null || this.origOwner === null) { + throw new Error("Not initialized"); + } + if (this.tradeShip === null) return; this.active = false; this.tradeShip.delete(false); const gold = this.mg