Files
Jeff 187ef1f2dd 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`
2025-10-08 17:31:56 -07:00

96 lines
2.8 KiB
TypeScript

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);
});
});