Files
OpenFrontIO/src/core/execution/ConstructionExecution.ts
T
CrackeRR11 8f53785a80 BUG FIX: Gold double deduction + Rmoval of UnitType.Construction (#2378)
## Description:

- Removed the temporary UnitType.Construction and embedded construction
state into real units via isUnderConstruction().
- Centralized non-structure spawning to perform a single validation
right before unit creation/launch.
- Updated UI layers to render construction state without relying on the
removed enum.
- Adjusted and created tests to match the new flow and to cover the
no-refundscenarios.

# Tests updated 
- tests/economy/ConstructionGold.test.ts: covers structure cost
deduction and income, tolerant of passive income; ensures no refunds
during construction.
- tests/nukes/HydrogenAndMirv.test.ts: accounts for single-check launch
flow; MIRV test targets a player-owned tile; ensures launch after
payment.
- tests/client/graphics/UILayer.test.ts: mocks now provide
isUnderConstruction and real type strings;

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

CrackeRR1

---------

Co-authored-by: Evan <evanpelle@gmail.com>
2025-11-26 14:45:14 -08:00

168 lines
4.8 KiB
TypeScript

import { Execution, Game, Player, Tick, Unit, UnitType } from "../game/Game";
import { TileRef } from "../game/GameMap";
import { CityExecution } from "./CityExecution";
import { DefensePostExecution } from "./DefensePostExecution";
import { FactoryExecution } from "./FactoryExecution";
import { MirvExecution } from "./MIRVExecution";
import { MissileSiloExecution } from "./MissileSiloExecution";
import { NukeExecution } from "./NukeExecution";
import { PortExecution } from "./PortExecution";
import { SAMLauncherExecution } from "./SAMLauncherExecution";
import { WarshipExecution } from "./WarshipExecution";
export class ConstructionExecution implements Execution {
private structure: Unit | null = null;
private active: boolean = true;
private mg: Game;
private ticksUntilComplete: Tick;
constructor(
private player: Player,
private constructionType: UnitType,
private tile: TileRef,
) {}
init(mg: Game, ticks: number): void {
this.mg = mg;
if (this.mg.config().isUnitDisabled(this.constructionType)) {
console.warn(
`cannot build construction ${this.constructionType} because it is disabled`,
);
this.active = false;
return;
}
if (!this.mg.isValidRef(this.tile)) {
console.warn(`cannot build construction invalid tile ${this.tile}`);
this.active = false;
return;
}
}
tick(ticks: number): void {
if (this.structure === null) {
const info = this.mg.unitInfo(this.constructionType);
// For non-structure units (nukes/warship), charge once and delegate to specialized executions.
const isStructure = this.isStructure(this.constructionType);
if (!isStructure) {
// Defer validation and gold deduction to the specific execution
this.completeConstruction();
this.active = false;
return;
}
// Structures: build real unit and mark under construction
const spawnTile = this.player.canBuild(this.constructionType, this.tile);
if (spawnTile === false) {
console.warn(`cannot build ${this.constructionType}`);
this.active = false;
return;
}
this.structure = this.player.buildUnit(
this.constructionType,
spawnTile,
{},
);
const duration = info.constructionDuration ?? 0;
if (duration > 0) {
this.structure.setUnderConstruction(true);
this.ticksUntilComplete = duration;
return;
}
// No construction time
this.completeConstruction();
this.active = false;
return;
}
if (!this.structure.isActive()) {
this.active = false;
return;
}
if (this.player !== this.structure.owner()) {
this.player = this.structure.owner();
}
if (this.ticksUntilComplete === 0) {
this.player = this.structure.owner();
this.completeConstruction();
this.active = false;
return;
}
this.ticksUntilComplete--;
}
private completeConstruction() {
if (this.structure) {
this.structure.setUnderConstruction(false);
}
const player = this.player;
switch (this.constructionType) {
case UnitType.AtomBomb:
case UnitType.HydrogenBomb:
this.mg.addExecution(
new NukeExecution(this.constructionType, player, this.tile),
);
break;
case UnitType.MIRV:
this.mg.addExecution(new MirvExecution(player, this.tile));
break;
case UnitType.Warship:
this.mg.addExecution(
new WarshipExecution({ owner: player, patrolTile: this.tile }),
);
break;
case UnitType.Port:
this.mg.addExecution(new PortExecution(this.structure!));
break;
case UnitType.MissileSilo:
this.mg.addExecution(new MissileSiloExecution(this.structure!));
break;
case UnitType.DefensePost:
this.mg.addExecution(new DefensePostExecution(this.structure!));
break;
case UnitType.SAMLauncher:
this.mg.addExecution(
new SAMLauncherExecution(player, null, this.structure!),
);
break;
case UnitType.City:
this.mg.addExecution(new CityExecution(this.structure!));
break;
case UnitType.Factory:
this.mg.addExecution(new FactoryExecution(this.structure!));
break;
default:
console.warn(
`unit type ${this.constructionType} cannot be constructed`,
);
break;
}
}
private isStructure(type: UnitType): boolean {
switch (type) {
case UnitType.Port:
case UnitType.MissileSilo:
case UnitType.DefensePost:
case UnitType.SAMLauncher:
case UnitType.City:
case UnitType.Factory:
return true;
default:
return false;
}
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}