Enforce stack limits when disembarking onto land
This commit is contained in:
parent
3253a4b701
commit
b8c072ebb9
4 changed files with 454 additions and 2 deletions
183
scripts/disembark-stack-limit.test.cjs
Normal file
183
scripts/disembark-stack-limit.test.cjs
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
// Synthetic public WASM regression tests for disembarkation stacking-limit enforcement.
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { test } = require('node:test');
|
||||
|
||||
const engine = require(process.env.CLASH_WASM || '../web-client/.engine/server.js');
|
||||
|
||||
const loadFixture = (rel) =>
|
||||
JSON.parse(
|
||||
fs.readFileSync(
|
||||
path.join(__dirname, '..', 'server', 'tests', 'test_games', rel),
|
||||
'utf8',
|
||||
),
|
||||
);
|
||||
|
||||
const queryMovement = (g, units, seat = 0) =>
|
||||
JSON.parse(
|
||||
engine.webQuery(
|
||||
JSON.stringify(g),
|
||||
seat,
|
||||
JSON.stringify({ kind: 'movement', units }),
|
||||
),
|
||||
);
|
||||
|
||||
const tryMove = (g, action, seat = 0) =>
|
||||
JSON.parse(engine.tryMove(JSON.stringify(g), JSON.stringify(action), seat));
|
||||
|
||||
const moveAction = (units, destination) => ({
|
||||
Movement: { Move: { units, destination, payment: {} } },
|
||||
});
|
||||
|
||||
function setupDisembarkFixture(carriedTypes, friendlyOnLand = []) {
|
||||
const g = loadFixture('movement/movement.json');
|
||||
g.state = 'Playing';
|
||||
g.actions_left = 3;
|
||||
g.map.tiles = [
|
||||
['D8', 'Water'],
|
||||
['C8', 'Barren'],
|
||||
];
|
||||
for (const p of g.players) {
|
||||
p.units = [];
|
||||
p.cities = [];
|
||||
}
|
||||
if (!g.players[0].advances.includes('Tactics')) {
|
||||
g.players[0].advances.push('Tactics');
|
||||
}
|
||||
if (carriedTypes.some((t) => typeof t === 'object' && t.Leader === 'Alexander')) {
|
||||
g.players[0].civilization = 'Greece';
|
||||
g.players[0].recruited_leaders = ['Alexander'];
|
||||
}
|
||||
|
||||
const shipsNeeded = Math.max(1, Math.ceil(carriedTypes.length / 2));
|
||||
for (let s = 0; s < shipsNeeded; s++) {
|
||||
g.players[0].units.push({
|
||||
id: 100 + s,
|
||||
position: 'D8',
|
||||
unit_type: 'Ship',
|
||||
});
|
||||
}
|
||||
|
||||
const carriedIds = [];
|
||||
carriedTypes.forEach((unit_type, idx) => {
|
||||
const id = idx;
|
||||
g.players[0].units.push({
|
||||
id,
|
||||
position: 'D8',
|
||||
unit_type,
|
||||
carrier_id: 100 + Math.floor(idx / 2),
|
||||
});
|
||||
carriedIds.push(id);
|
||||
});
|
||||
|
||||
friendlyOnLand.forEach((unit_type, idx) => {
|
||||
g.players[0].units.push({
|
||||
id: 200 + idx,
|
||||
position: 'C8',
|
||||
unit_type,
|
||||
});
|
||||
});
|
||||
|
||||
g.players[0].next_unit_id = 300;
|
||||
return { g, carriedIds };
|
||||
}
|
||||
|
||||
test('disembarking >4 army units including Leader is excluded by webQuery and rejected by tryMove', () => {
|
||||
const { g, carriedIds } = setupDisembarkFixture([
|
||||
'Infantry',
|
||||
'Infantry',
|
||||
'Infantry',
|
||||
'Infantry',
|
||||
{ Leader: 'Alexander' },
|
||||
'Settler',
|
||||
]);
|
||||
|
||||
const overStackQuery = queryMovement(g, carriedIds);
|
||||
assert.ok(
|
||||
!overStackQuery.destinations.some((d) => d.position === 'C8'),
|
||||
'webQuery must exclude C8 for 5 army units + Settler',
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => tryMove(g, moveAction(carriedIds, 'C8')),
|
||||
/stack limit exceeded|not found/i,
|
||||
'tryMove must reject disembarking 5 army units + Settler',
|
||||
);
|
||||
});
|
||||
|
||||
test('disembarking 4 army units + Settler (with or without Leader) is allowed by webQuery and tryMove', () => {
|
||||
// 4 Infantry + 1 Settler
|
||||
{
|
||||
const { g } = setupDisembarkFixture([
|
||||
'Infantry',
|
||||
'Infantry',
|
||||
'Infantry',
|
||||
'Infantry',
|
||||
{ Leader: 'Alexander' },
|
||||
'Settler',
|
||||
]);
|
||||
const validIds = [0, 1, 2, 3, 5];
|
||||
const q = queryMovement(g, validIds);
|
||||
assert.ok(
|
||||
q.destinations.some((d) => d.position === 'C8'),
|
||||
'webQuery must include C8 for 4 Infantry + 1 Settler',
|
||||
);
|
||||
const after = tryMove(g, moveAction(validIds, 'C8'));
|
||||
for (const id of validIds) {
|
||||
const unit = after.players[0].units.find((u) => u.id === id);
|
||||
assert.equal(unit.position, 'C8');
|
||||
assert.equal(unit.carrier_id ?? null, null);
|
||||
}
|
||||
}
|
||||
|
||||
// 3 Infantry + non-Xerxes Leader + 1 Settler
|
||||
{
|
||||
const { g } = setupDisembarkFixture([
|
||||
'Infantry',
|
||||
'Infantry',
|
||||
'Infantry',
|
||||
'Infantry',
|
||||
{ Leader: 'Alexander' },
|
||||
'Settler',
|
||||
]);
|
||||
const validIds = [0, 1, 2, 4, 5];
|
||||
const q = queryMovement(g, validIds);
|
||||
assert.ok(
|
||||
q.destinations.some((d) => d.position === 'C8'),
|
||||
'webQuery must include C8 for 3 Infantry + Leader + 1 Settler',
|
||||
);
|
||||
const after = tryMove(g, moveAction(validIds, 'C8'));
|
||||
for (const id of validIds) {
|
||||
const unit = after.players[0].units.find((u) => u.id === id);
|
||||
assert.equal(unit.position, 'C8');
|
||||
assert.equal(unit.carrier_id ?? null, null);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('disembarking onto friendly army stack enforces limit for army units but allows Settlers', () => {
|
||||
// 2 Infantry onto 3 existing Infantry (5 > 4) rejected
|
||||
{
|
||||
const { g, carriedIds } = setupDisembarkFixture(
|
||||
['Infantry', 'Infantry'],
|
||||
['Infantry', 'Infantry', 'Infantry'],
|
||||
);
|
||||
const q = queryMovement(g, carriedIds);
|
||||
assert.ok(!q.destinations.some((d) => d.position === 'C8'));
|
||||
assert.throws(() => tryMove(g, moveAction(carriedIds, 'C8')));
|
||||
}
|
||||
|
||||
// 1 Settler onto 4 existing Infantry allowed
|
||||
{
|
||||
const { g, carriedIds } = setupDisembarkFixture(
|
||||
['Settler'],
|
||||
['Infantry', 'Infantry', 'Infantry', 'Infantry'],
|
||||
);
|
||||
const q = queryMovement(g, carriedIds);
|
||||
assert.ok(q.destinations.some((d) => d.position === 'C8'));
|
||||
const after = tryMove(g, moveAction(carriedIds, 'C8'));
|
||||
const settler = after.players[0].units.find((u) => u.id === carriedIds[0]);
|
||||
assert.equal(settler.position, 'C8');
|
||||
}
|
||||
});
|
||||
|
|
@ -791,7 +791,8 @@ fn move_route_result(
|
|||
return Err("Carthage may have at most 4 Ships in one space".into());
|
||||
}
|
||||
}
|
||||
if game.map.is_land(start)
|
||||
if (game.map.is_land(start) || (!game.map.is_sea(dest) && game.context != GameContext::Replay))
|
||||
&& stack_size > 0
|
||||
&& player
|
||||
.get_units(dest)
|
||||
.iter()
|
||||
|
|
|
|||
|
|
@ -569,3 +569,271 @@ fn legacy_move_payload_omits_optional_fleet_assignment() {
|
|||
assert!(action.boarding.is_empty());
|
||||
assert_eq!(serde_json::to_value(action).unwrap(), legacy);
|
||||
}
|
||||
|
||||
fn setup_disembark_game(
|
||||
dest_terrain: server::map::Terrain,
|
||||
carried: &[server::unit::UnitType],
|
||||
friendly_on_dest: &[server::unit::UnitType],
|
||||
enemy_on_dest: &[server::unit::UnitType],
|
||||
) -> (Game, Position, Position, Vec<u32>) {
|
||||
use server::{
|
||||
advance::Advance,
|
||||
game::GameState,
|
||||
map::Terrain,
|
||||
unit::{Unit, UnitType},
|
||||
};
|
||||
|
||||
let mut game = JSON.load_game("movement");
|
||||
game.state = GameState::Playing;
|
||||
game.actions_left = 3;
|
||||
let sea = Position::from_offset("D8");
|
||||
let land = Position::from_offset("C8");
|
||||
game.map.tiles.clear();
|
||||
game.map.tiles.insert(sea, Terrain::Water);
|
||||
game.map.tiles.insert(land, dest_terrain);
|
||||
for p in &mut game.players {
|
||||
p.units.clear();
|
||||
p.cities.clear();
|
||||
}
|
||||
game.player_mut(0).advances.insert(Advance::Tactics);
|
||||
|
||||
let ships_needed = carried.len().div_ceil(2).max(1) as u32;
|
||||
for s in 0..ships_needed {
|
||||
game.player_mut(0)
|
||||
.units
|
||||
.push(Unit::new(0, sea, UnitType::Ship, 100 + s));
|
||||
}
|
||||
|
||||
let mut carried_ids = Vec::with_capacity(carried.len());
|
||||
for (idx, &unit_type) in carried.iter().enumerate() {
|
||||
let id = idx as u32;
|
||||
let mut unit = Unit::new(0, sea, unit_type, id);
|
||||
unit.carrier_id = Some(100 + (idx / 2) as u32);
|
||||
game.player_mut(0).units.push(unit);
|
||||
carried_ids.push(id);
|
||||
}
|
||||
|
||||
for (idx, &unit_type) in friendly_on_dest.iter().enumerate() {
|
||||
let id = 200 + idx as u32;
|
||||
game.player_mut(0)
|
||||
.units
|
||||
.push(Unit::new(0, land, unit_type, id));
|
||||
}
|
||||
|
||||
for (idx, &unit_type) in enemy_on_dest.iter().enumerate() {
|
||||
let id = 300 + idx as u32;
|
||||
game.player_mut(1)
|
||||
.units
|
||||
.push(Unit::new(1, land, unit_type, id));
|
||||
}
|
||||
|
||||
game.player_mut(0).next_unit_id = 400;
|
||||
game.player_mut(1).next_unit_id = 400;
|
||||
(game, sea, land, carried_ids)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disembark_enforces_stack_limit_on_empty_and_enemy_land() {
|
||||
use server::{action::try_execute_action, leader::Leader, map::Terrain, unit::UnitType};
|
||||
|
||||
let five_army_plus_settler = [
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Leader(Leader::Alexander),
|
||||
UnitType::Settler,
|
||||
];
|
||||
|
||||
for enemy in [&[][..], &[UnitType::Infantry][..]] {
|
||||
let (game, sea, land, ids) =
|
||||
setup_disembark_game(Terrain::Barren, &five_army_plus_settler, &[], enemy);
|
||||
let routes = possible_move_routes(game.player(0), &game, &ids, sea, None).unwrap();
|
||||
assert!(
|
||||
!routes.iter().any(|r| r.destination == land),
|
||||
"5 army units + settler must not be able to disembark onto {enemy:?}"
|
||||
);
|
||||
assert!(
|
||||
try_execute_action(game, move_action(ids, land), 0).is_err(),
|
||||
"disembarking 5 army units + settler must be rejected"
|
||||
);
|
||||
}
|
||||
|
||||
// 4 Infantry + 1 Settler succeeds
|
||||
let (game, sea, land, ids) = setup_disembark_game(
|
||||
Terrain::Barren,
|
||||
&[
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Settler,
|
||||
],
|
||||
&[],
|
||||
&[],
|
||||
);
|
||||
let routes = possible_move_routes(game.player(0), &game, &ids, sea, None).unwrap();
|
||||
assert!(routes.iter().any(|r| r.destination == land));
|
||||
let after = try_execute_action(game, move_action(ids.clone(), land), 0).unwrap();
|
||||
for id in ids {
|
||||
assert_eq!(after.player(0).get_unit(id).position, land);
|
||||
assert_eq!(after.player(0).get_unit(id).carrier_id, None);
|
||||
}
|
||||
|
||||
// 3 Infantry + non-Xerxes Leader + 1 Settler succeeds
|
||||
let (game, sea, land, ids) = setup_disembark_game(
|
||||
Terrain::Barren,
|
||||
&[
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Leader(Leader::Alexander),
|
||||
UnitType::Settler,
|
||||
],
|
||||
&[],
|
||||
&[],
|
||||
);
|
||||
let routes = possible_move_routes(game.player(0), &game, &ids, sea, None).unwrap();
|
||||
assert!(routes.iter().any(|r| r.destination == land));
|
||||
let after = try_execute_action(game, move_action(ids.clone(), land), 0).unwrap();
|
||||
for id in ids {
|
||||
assert_eq!(after.player(0).get_unit(id).position, land);
|
||||
assert_eq!(after.player(0).get_unit(id).carrier_id, None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disembark_enforces_stack_limit_with_existing_friendly_units_and_allows_settlers() {
|
||||
use server::{action::try_execute_action, map::Terrain, unit::UnitType};
|
||||
|
||||
// 2 Infantry disembarking onto 3 existing army units (5 > 4) is rejected
|
||||
let (game, sea, land, ids) = setup_disembark_game(
|
||||
Terrain::Barren,
|
||||
&[UnitType::Infantry, UnitType::Infantry],
|
||||
&[UnitType::Infantry, UnitType::Infantry, UnitType::Infantry],
|
||||
&[],
|
||||
);
|
||||
let routes = possible_move_routes(game.player(0), &game, &ids, sea, None).unwrap();
|
||||
assert!(!routes.iter().any(|r| r.destination == land));
|
||||
assert!(try_execute_action(game, move_action(ids, land), 0).is_err());
|
||||
|
||||
// 1 Infantry disembarking onto 3 existing army units (4 <= 4) succeeds
|
||||
let (game, sea, land, ids) = setup_disembark_game(
|
||||
Terrain::Barren,
|
||||
&[UnitType::Infantry],
|
||||
&[UnitType::Infantry, UnitType::Infantry, UnitType::Infantry],
|
||||
&[],
|
||||
);
|
||||
let routes = possible_move_routes(game.player(0), &game, &ids, sea, None).unwrap();
|
||||
assert!(routes.iter().any(|r| r.destination == land));
|
||||
let after = try_execute_action(game, move_action(ids.clone(), land), 0).unwrap();
|
||||
assert_eq!(after.player(0).get_unit(ids[0]).position, land);
|
||||
|
||||
// 1 Settler disembarking onto 4 or legacy 5 existing army units succeeds
|
||||
for existing_count in [4, 5] {
|
||||
let existing = vec![UnitType::Infantry; existing_count];
|
||||
let (game, sea, land, ids) =
|
||||
setup_disembark_game(Terrain::Barren, &[UnitType::Settler], &existing, &[]);
|
||||
let routes = possible_move_routes(game.player(0), &game, &ids, sea, None).unwrap();
|
||||
assert!(
|
||||
routes.iter().any(|r| r.destination == land),
|
||||
"settler should disembark onto {existing_count} existing army units"
|
||||
);
|
||||
let after = try_execute_action(game, move_action(ids.clone(), land), 0).unwrap();
|
||||
assert_eq!(after.player(0).get_unit(ids[0]).position, land);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disembark_respects_xerxes_stack_limit() {
|
||||
use server::{action::try_execute_action, leader::Leader, map::Terrain, unit::UnitType};
|
||||
|
||||
// Xerxes + 4 Infantry (5) succeeds
|
||||
let (game, sea, land, ids) = setup_disembark_game(
|
||||
Terrain::Barren,
|
||||
&[
|
||||
UnitType::Leader(Leader::Xerxes),
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
],
|
||||
&[],
|
||||
&[],
|
||||
);
|
||||
let routes = possible_move_routes(game.player(0), &game, &ids, sea, None).unwrap();
|
||||
assert!(routes.iter().any(|r| r.destination == land));
|
||||
let after = try_execute_action(game, move_action(ids.clone(), land), 0).unwrap();
|
||||
for id in ids {
|
||||
assert_eq!(after.player(0).get_unit(id).position, land);
|
||||
}
|
||||
|
||||
// Xerxes + 5 Infantry (6) is rejected
|
||||
let (game, sea, land, ids) = setup_disembark_game(
|
||||
Terrain::Barren,
|
||||
&[
|
||||
UnitType::Leader(Leader::Xerxes),
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
],
|
||||
&[],
|
||||
&[],
|
||||
);
|
||||
let routes = possible_move_routes(game.player(0), &game, &ids, sea, None).unwrap();
|
||||
assert!(!routes.iter().any(|r| r.destination == land));
|
||||
assert!(try_execute_action(game, move_action(ids, land), 0).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disembark_enforces_stack_limit_on_unexplored_tile() {
|
||||
use server::{action::try_execute_action, map::Terrain, unit::UnitType};
|
||||
|
||||
let (game, sea, land, ids) = setup_disembark_game(
|
||||
Terrain::Unexplored,
|
||||
&[
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
],
|
||||
&[],
|
||||
&[],
|
||||
);
|
||||
let routes = possible_move_routes(game.player(0), &game, &ids, sea, None).unwrap();
|
||||
assert!(!routes.iter().any(|r| r.destination == land));
|
||||
assert!(try_execute_action(game, move_action(ids, land), 0).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disembark_allows_legacy_overstack_during_replay() {
|
||||
use server::{
|
||||
action::try_execute_action, game::GameContext, leader::Leader, map::Terrain, unit::UnitType,
|
||||
};
|
||||
|
||||
let (mut game, sea, land, ids) = setup_disembark_game(
|
||||
Terrain::Barren,
|
||||
&[
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Infantry,
|
||||
UnitType::Leader(Leader::Alexander),
|
||||
UnitType::Settler,
|
||||
],
|
||||
&[],
|
||||
&[],
|
||||
);
|
||||
game.context = GameContext::Replay;
|
||||
|
||||
let routes = possible_move_routes(game.player(0), &game, &ids, sea, None).unwrap();
|
||||
assert!(routes.iter().any(|r| r.destination == land));
|
||||
let after = try_execute_action(game, move_action(ids.clone(), land), 0).unwrap();
|
||||
for id in ids {
|
||||
assert_eq!(after.player(0).get_unit(id).position, land);
|
||||
assert_eq!(after.player(0).get_unit(id).carrier_id, None);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
"dev": "vite --host 127.0.0.1 --port 8643",
|
||||
"build": "vite build",
|
||||
"check": "svelte-check --tsconfig ./tsconfig.json",
|
||||
"test": "node --experimental-strip-types --test src/*.test.ts && node --test scripts/publish-gallery.test.mjs scripts/boarding.test.mjs scripts/raiders.test.mjs && npm run test:achievement-assets && npm run test:locales && node scripts/exhausted-movement-check.mjs && CLASH_WASM=\"$PWD/.engine/server.js\" node --test ../scripts/status-phase-battle.test.cjs ../scripts/postcombat-queue.test.cjs ../scripts/tributes.test.cjs ../scripts/withdrawal.test.cjs ../scripts/viking-cargo.test.cjs ../scripts/aggressor.test.cjs ../scripts/great-architect.test.cjs ../scripts/provinces.test.cjs ../scripts/barbarian-movement.test.cjs",
|
||||
"test": "node --experimental-strip-types --test src/*.test.ts && node --test scripts/publish-gallery.test.mjs scripts/boarding.test.mjs scripts/raiders.test.mjs && npm run test:achievement-assets && npm run test:locales && node scripts/exhausted-movement-check.mjs && CLASH_WASM=\"$PWD/.engine/server.js\" node --test ../scripts/status-phase-battle.test.cjs ../scripts/postcombat-queue.test.cjs ../scripts/tributes.test.cjs ../scripts/withdrawal.test.cjs ../scripts/viking-cargo.test.cjs ../scripts/aggressor.test.cjs ../scripts/great-architect.test.cjs ../scripts/provinces.test.cjs ../scripts/barbarian-movement.test.cjs ../scripts/disembark-stack-limit.test.cjs",
|
||||
"test:boarding": "npm run build:bridge && npm run build && node --test scripts/boarding.test.mjs && node scripts/boarding-smoke.mjs",
|
||||
"test:home-orientation": "npm run build && node scripts/home-orientation-smoke.mjs",
|
||||
"build:achievement-assets": "node scripts/build-achievement-assets.mjs",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue