feat(PlayerExecution): downgrade defense posts on capture (#1957)

## Description:

Closes https://github.com/openfrontio/OpenFrontIO/issues/1619.

On capture, defense posts will be downgraded.

On the live version this means defense posts will be destroyed, as
defense posts can only be level 1.

Misc. changes:
- added `decreaserLevel` helper
- cleaned up if/else in tick unit loop for clarity to avoid yet another
nested layer

Continuation of the stale PR,
https://github.com/openfrontio/OpenFrontIO/pull/1622.

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

Discord username: `seekerreturns`
This commit is contained in:
Jeff
2025-10-08 20:31:56 -04:00
committed by GitHub
parent e895e53a1e
commit 187ef1f2dd
4 changed files with 115 additions and 2 deletions
+7 -2
View File
@@ -30,9 +30,14 @@ export class PlayerExecution implements Execution {
this.player.units().forEach((u) => {
const tileOwner = this.mg!.owner(u.tile());
if (u.info().territoryBound) {
if (tileOwner.isPlayer()) {
if (tileOwner?.isPlayer()) {
if (tileOwner !== this.player) {
this.mg!.player(tileOwner.id()).captureUnit(u);
if (u.type() === UnitType.DefensePost) {
this.mg!.player(tileOwner.id()).captureUnit(u);
u.decreaseLevel(this.mg!.player(tileOwner.id()));
} else {
this.mg!.player(tileOwner.id()).captureUnit(u);
}
}
} else {
u.delete();
+1
View File
@@ -494,6 +494,7 @@ export interface Unit {
// Upgradable Structures
level(): number;
increaseLevel(): void;
decreaseLevel(destroyer?: Player): void;
// Warships
setPatrolTile(tile: TileRef): void;
+12
View File
@@ -397,6 +397,18 @@ export class UnitImpl implements Unit {
this.mg.addUpdate(this.toUpdate());
}
decreaseLevel(destroyer?: Player): void {
this._level--;
if ([UnitType.MissileSilo, UnitType.SAMLauncher].includes(this.type())) {
this._missileTimerQueue.pop();
}
if (this._level <= 0) {
this.delete(true, destroyer);
return;
}
this.mg.addUpdate(this.toUpdate());
}
trainType(): TrainType | undefined {
return this._trainType;
}
@@ -0,0 +1,95 @@
import { PlayerExecution } from "../../../src/core/execution/PlayerExecution";
import {
Game,
Player,
PlayerInfo,
PlayerType,
UnitType,
} from "../../../src/core/game/Game";
import { setup } from "../../util/Setup";
import { executeTicks } from "../../util/utils";
let game: Game;
let player: Player;
let otherPlayer: Player;
describe("PlayerExecution", () => {
beforeEach(async () => {
game = await setup(
"big_plains",
{
infiniteGold: true,
instantBuild: true,
},
[
new PlayerInfo("player", PlayerType.Human, "client_id1", "player_id"),
new PlayerInfo("other", PlayerType.Human, "client_id2", "other_id"),
],
);
while (game.inSpawnPhase()) {
game.executeNextTick();
}
player = game.player("player_id");
otherPlayer = game.player("other_id");
game.addExecution(new PlayerExecution(player));
game.addExecution(new PlayerExecution(otherPlayer));
});
test("DefensePost lv. 1 is destroyed when tile owner changes", () => {
const tile = game.ref(50, 50);
player.conquer(tile);
const defensePost = player.buildUnit(UnitType.DefensePost, tile, {});
game.executeNextTick();
expect(game.unitCount(UnitType.DefensePost)).toBe(1);
expect(defensePost.level()).toBe(1);
otherPlayer.conquer(tile);
executeTicks(game, 2);
expect(game.unitCount(UnitType.DefensePost)).toBe(0);
});
test("DefensePost lv. 2+ is downgraded when tile owner changes", () => {
const tile = game.ref(50, 50);
player.conquer(tile);
const defensePost = player.buildUnit(UnitType.DefensePost, tile, {});
defensePost.increaseLevel();
expect(defensePost.level()).toBe(2);
expect(game.unitCount(UnitType.DefensePost)).toBe(2); // unitCount sums levels
expect(player.units(UnitType.DefensePost)).toHaveLength(1);
expect(defensePost.isActive()).toBe(true);
otherPlayer.conquer(tile);
executeTicks(game, 2);
expect(defensePost.level()).toBe(1);
expect(game.unitCount(UnitType.DefensePost)).toBe(1);
expect(otherPlayer.units(UnitType.DefensePost)).toHaveLength(1);
expect(defensePost.owner()).toBe(otherPlayer);
expect(defensePost.isActive()).toBe(true);
});
test("Non-DefensePost structures are transferred (not downgraded) when tile owner changes", () => {
const tile = game.ref(50, 50);
player.conquer(tile);
const city = player.buildUnit(UnitType.City, tile, {});
expect(game.unitCount(UnitType.City)).toBe(1);
expect(city.level()).toBe(1);
expect(city.owner()).toBe(player);
expect(city.isActive()).toBe(true);
otherPlayer.conquer(tile);
executeTicks(game, 2);
expect(game.unitCount(UnitType.City)).toBe(1);
expect(city.level()).toBe(1);
expect(city.owner()).toBe(otherPlayer);
expect(city.isActive()).toBe(true);
});
});