Support mixed No-Field Homeworld transfers

This commit is contained in:
Quintine committed 2026-10-28 20:22:37 +11:00
1 parent 1f03ae673b
commit d0e4c177e4
12 files changed
+695 -38

No files matched your search

@@ -0,0 +1,108 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { readFileSync } from 'node:fs';
import { runInNewContext } from 'node:vm';
import { createElement, isValidElement, type ReactElement, type ReactNode } from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import * as jsxRuntime from 'react/jsx-runtime';
import ts from 'typescript';
import type { Action, GameView } from '../game/engine';
import { createRicheseNoField, deployRicheseNoField } from '../game/richese-no-field';
import { homeworldForceGroups, type HomeworldCustody } from '../game/homeworld-custody';
import { homeworldPopulations } from '../game/homeworld-population';
import * as combat from '../game/combat';
import * as options from '../game/homeworld-shipment-options';
import { HomeworldNoFieldShipment } from '../components/homeworld-shipment';
function fixture(): GameView {
const context = { advanced: true, players: [
{ id: 'r', faction: 'richese' as const, reserves: 12, eliteReserves: 0, ally: null as string | null },
{ id: 'a', faction: 'atreides' as const, reserves: 15, eliteReserves: 0, ally: null as string | null },
{ id: 'g', faction: 'guild' as const, reserves: 20, eliteReserves: 0, ally: null as string | null },
] };
const custody: HomeworldCustody = { salusa: null, visitors: { 'homeworld:atreides': { r: { normal: 3, elite: 0 } } } };
const populations = homeworldPopulations(context, custody);
const marker = deployRicheseNoField(createRicheseNoField(['zero', 'three', 'five']),
{ tokenId: 'five', controller: 'r', location: { homeworld: 'homeworld:atreides' } });
return {
me: 'r', active: 'r', advanced: true, status: 'playing', phase: 5,
homeworldShipment: { event: 'homeworld-event', blocked: null }, aid: { available: 0, pledged: 0 },
players: context.players.map(p => ({ ...p, name: p.faction, spice: p.id === 'r' ? 6 : undefined,
shipped: false, elites: { reserves: 0 } })),
homeworlds: { worlds: homeworldForceGroups(context, custody).map(world => ({ ...world,
...populations.find(population => population.location === world.id)! })) },
richeseNoField: { owner: 'r', event: 'marker-event', private: marker,
canShip: false, shipBlock: 'Reveal the existing No-Field before shipping another.' },
} as unknown as GameView;
}
void test('mixed transfer quotes the total, exact source, funding and purchased discount', () => {
const view = fixture(), before = structuredClone(view);
const quote = options.richeseHomeworldMarkerChoice(view, 'homeworld:guild', 'five', 0, 'homeworld:atreides', { normal: 3, elite: 0 });
assert.equal(quote.amount, 4); assert.equal(quote.cost, 4);
assert.deepEqual(quote.action, { type: 'homeworldShip', event: 'homeworld-event', destination: 'homeworld:guild',
sources: { 'homeworld:atreides': { normal: 3, elite: 0 } }, noField: 'five', noFieldEvent: 'marker-event',
allyPayment: 0, noFieldFrom: 'homeworld:atreides' });
assert.deepEqual(view, before);
const discounted = { ...view, karamaShipping: { player: 'r' } } as GameView;
const half = options.richeseHomeworldMarkerChoice(discounted, 'homeworld:guild', 'five', 0,
'homeworld:atreides', { normal: 2, elite: 0 });
assert.equal(half.amount, 3); assert.equal(half.cost, 2);
assert.equal(options.richeseHomeworldMarkerChoice(view, 'homeworld:guild', 'five', 0,
'homeworld:atreides', { normal: 4, elite: 0 }).action, null);
assert.equal(options.richeseHomeworldMarkerChoice(view, 'homeworld:guild', 'five', 0,
'homeworld:atreides', { normal: -1, elite: 0 }).action, null);
view.players[0].spice = 2;
assert.equal(options.richeseHomeworldMarkerChoice(view, 'homeworld:guild', 'five', 0,
'homeworld:atreides', { normal: 2, elite: 0 }).action, null);
});
type Element = ReactElement<{ children?: ReactNode; [key: string]: unknown }>;
function elements(node: ReactNode): Element[] {
if (Array.isArray(node)) return node.flatMap(elements);
if (!isValidElement<{ children?: ReactNode; [key: string]: unknown }>(node)) return [];
return [node, ...elements(node.props.children)];
}
void test('owner control bounds the group and submits selected physical forces with the marker', () => {
const view = fixture(), sent: Action[] = [], states: unknown[] = [];
const html = renderToStaticMarkup(createElement(HomeworldNoFieldShipment, { game: view, act() {}, busy: false }));
assert.match(html, /accompanying No-Field/); assert.match(html, /3.*available/);
let cursor = 0;
const exports: { HomeworldNoFieldShipment?: typeof HomeworldNoFieldShipment } = {};
runInNewContext(ts.transpileModule(readFileSync(new URL('../components/homeworld-shipment.tsx', import.meta.url), 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.ReactJSX },
}).outputText, { exports, require(name: string) {
if (name === 'react/jsx-runtime') return jsxRuntime;
if (name === 'react') return { useState(initial: unknown) {
const index = cursor++; if (!(index in states)) states[index] = initial;
return [states[index], (next: unknown) => { states[index] = next; }];
} };
if (name === '@/game/combat') return combat;
if (name === '@/game/homeworld-shipment-options') return options;
if (name === './ui/button') return { Button: 'button' };
if (name === './ui/input') return { Input: 'input' };
throw Error(`Unexpected import ${name}`);
} });
assert.ok(exports.HomeworldNoFieldShipment);
const render = () => { cursor = 0; return elements(exports.HomeworldNoFieldShipment!({ game: view, busy: false, act: action => sent.push(action) })); };
const input = render().find(element => element.type === 'input'); assert.ok(input);
assert.equal(input.props.max, 3);
(input.props.onChange as (event: unknown) => void)({ target: { value: '2' } });
const button = render().find(element => element.type === 'button'); assert.ok(button);
assert.equal(button.props.disabled, false);
(button.props.onClick as () => void)();
assert.deepEqual(JSON.parse(JSON.stringify(sent[0])), { type: 'homeworldShip', event: 'homeworld-event',
destination: 'homeworld:guild', sources: { 'homeworld:atreides': { normal: 2, elite: 0 } },
noField: 'five', noFieldEvent: 'marker-event', allyPayment: 0, noFieldFrom: 'homeworld:atreides' });
});
void test('minimal bots can propose the current marker with an available physical force', () => {
const view = fixture();
for (const level of [0, 1, 2, 3]) {
const action = options.homeworldShipmentActions(view, level).find(candidate =>
candidate.noFieldFrom === 'homeworld:atreides' && (candidate.sources as options.HomeworldShipmentSources | undefined)?.['homeworld:atreides']);
assert.ok(action, `profile ${level} can propose a mixed transfer`);
assert.deepEqual((action.sources as options.HomeworldShipmentSources | undefined)?.['homeworld:atreides'], { normal: 1, elite: 0 });
}
});
@@ -0,0 +1,168 @@
// @dune-suite integration
import assert from 'node:assert/strict';
import test from 'node:test';
import { TERRITORIES } from '../game/board';
import { botActions } from '../game/bots';
import { homeworldShipmentChoice, richeseHomeworldMarkerChoice } from '../game/homeworld-shipment-options';
import type { Action, GameView } from '../game/engine';
const base = process.env.DUNE_TEST_URL ?? 'http://localhost:3000';
async function request(path: string, cookie?: string, body?: unknown) {
const response = await fetch(base + path, {
method: body === undefined ? 'GET' : 'POST', signal: AbortSignal.timeout(15_000),
headers: { ...(body === undefined ? {} : { 'Content-Type': 'application/json' }), ...(cookie ? { cookie } : {}) },
...(body === undefined ? {} : { body: JSON.stringify(body) }),
});
return { status: response.status, cookie: response.headers.get('set-cookie')?.split(';')[0],
data: await response.json() as GameView & { error?: string } };
}
/** A fresh room uses real human seat credentials and native legal actions.
* No server phase, currency, force, token, or card state is manufactured. */
void test('authenticated mixed transfer moves a concealed marker and physical visitors together', async () => {
const host = await request('/api/rooms', undefined, {
name: 'Mixed transfer origin', faction: 'atreides', advanced: false, expansions: ['choam'],
});
assert.equal(host.status, 201, JSON.stringify(host.data)); assert.ok(host.cookie);
const path = `/api/rooms/${host.data.code}`;
const owner = await request(path, undefined, { type: 'join', name: 'Mixed transfer owner', faction: 'richese' });
assert.equal(owner.status, 200, JSON.stringify(owner.data)); assert.ok(owner.cookie);
const guild = await request(path, undefined, { type: 'join', name: 'Mixed transfer destination', faction: 'guild' });
assert.equal(guild.status, 200, JSON.stringify(guild.data)); assert.ok(guild.cookie);
const seats = [host, owner, guild].map(result => ({ cookie: result.cookie!, id: result.data.me }));
const ownerSeat = seats[1], hostSeat = seats[0], guildSeat = seats[2];
let view = guild.data;
const act = async (cookie: string, action: Action) => {
const next = await request(path, cookie, { version: view.version, action });
assert.equal(next.status, 200, `Rejected ${action.type}: ${JSON.stringify(next.data)}`);
view = next.data; return next.data;
};
const read = async (cookie: string) => {
const result = await request(path, cookie);
assert.equal(result.status, 200, JSON.stringify(result.data));
view = result.data; return result.data;
};
await act(hostSeat.cookie, { type: 'homeworlds', enabled: true });
for (const seat of seats) await act(seat.cookie, { type: 'ready' });
await act(hostSeat.cookie, { type: 'start' });
let firstMarkerPlaced = false;
async function advance(until: (ownerView: GameView) => boolean) {
for (let count = 0; count < 800; count++) {
const currentOwner = await read(ownerSeat.cookie);
if (until(currentOwner)) return currentOwner;
assert.notEqual(currentOwner.status, 'finished', 'The QA game ended before marker transfer.');
// Atreides sends its actual reserves away from its World, so the
// concealed marker can remain there through the next Battle phase.
if (currentOwner.status === 'playing' && currentOwner.phase === 5 && currentOwner.active === hostSeat.id &&
!currentOwner.response && !currentOwner.decision && !currentOwner.phaseOpening &&
!currentOwner.truthtrance) {
const at = await read(hostSeat.cookie);
const me = at.players.find(player => player.id === hostSeat.id)!;
if (!me.shipped && me.reserves > 0) {
let accepted = false;
for (const territory of TERRITORIES.filter(item => item.type === 'stronghold')) {
for (const sector of territory.sectors) {
if (sector === at.storm) continue;
const attempt = await request(path, hostSeat.cookie, { version: view.version, action: {
type: 'ship', territory: territory.id, sector, amount: me.reserves, elite: 0,
allyPayment: 0, homeworldSources: { 'homeworld:atreides': { normal: me.reserves, elite: 0 } },
} });
if (attempt.status === 200) { view = attempt.data; accepted = true; break; }
assert.equal(attempt.status, 409, JSON.stringify(attempt.data));
}
if (accepted) break;
}
assert.ok(accepted, 'Atreides can ship its physical reserves to an available stronghold.');
continue;
}
}
let proposed: { cookie: string; actor: string; action: Action } | null = null;
for (const seat of seats) {
const own = await read(seat.cookie);
const local = structuredClone(own);
const self = local.players.find(player => player.id === seat.id); assert.ok(self);
self.bot = 'Easy'; // Client-side adapter only; authoritative seat stays human.
const choices = botActions(local);
if (choices.length) { proposed = { cookie: seat.cookie, actor: seat.id, action: choices[0] }; break; }
}
assert.ok(proposed, `No native continuation at ${currentOwner.status}/${currentOwner.phase}/${currentOwner.decision?.kind ?? 'no decision'}.`);
if (view.response && !view.response.passed.includes(view.me)) proposed.action = { type: 'passResponse' };
// Keep the actual Atreides shipment affordable and avoid extra foreign
// arrivals to the temporary marker World; all passes/skips are legal.
else if ((proposed.actor === hostSeat.id || proposed.actor === ownerSeat.id) && proposed.action.type === 'bid') proposed.action = { type: 'passBid' };
else if ((proposed.actor === hostSeat.id || proposed.actor === ownerSeat.id) && proposed.action.type === 'richeseBid')
proposed.action = { type: 'richeseBid', event: proposed.action.event, amount: 0, allyPayment: 0 };
else if (view.phase === 5 && view.active === proposed.actor && !view.response && !view.decision &&
!view.truthtrance && !view.phaseOpening &&
(proposed.actor === guildSeat.id || proposed.actor === ownerSeat.id && firstMarkerPlaced))
proposed.action = { type: 'endMovement' };
await act(proposed.cookie, proposed.action);
}
throw Error('The dedicated HTTP room did not reach the native marker transfer boundary.');
}
const first = await advance(own => own.status === 'playing' && own.phase === 5 && own.active === own.me &&
!own.players.find(player => player.id === own.me)!.shipped &&
own.players.find(player => player.id === hostSeat.id)!.reserves === 0 &&
!!own.homeworldShipment && !own.homeworldShipment.blocked && !!own.richeseNoField?.canShip);
const physical = homeworldShipmentChoice(first, 'homeworld:atreides',
{ 'homeworld:richese': { normal: 2, elite: 0 } });
assert.ok(physical.action, physical.blocked ?? 'The physical shipment is available.');
await act(ownerSeat.cookie, physical.action);
firstMarkerPlaced = true; // Keep later owner turns for the test's explicit shipments.
const firstLanded = await advance(own => own.phase === 5 && own.players.find(player => player.id === own.me)?.shipped === true);
const physicalTurn = firstLanded.turn;
const markerTurn = await advance(own => own.status === 'playing' && own.turn > physicalTurn && own.phase === 5 &&
own.active === own.me && !own.players.find(player => player.id === own.me)!.shipped &&
!!own.homeworldShipment && !own.homeworldShipment.blocked && !!own.richeseNoField?.canShip);
assert.deepEqual(markerTurn.homeworlds?.worlds?.find(world => world.id === 'homeworld:atreides')?.forces[markerTurn.me],
{ normal: 2, elite: 0 });
const markerInfo = markerTurn.richeseNoField!; assert.ok(markerInfo.private);
const token = markerInfo.private.tokens.find(candidate => candidate.id !== markerInfo.private!.lastShipped);
assert.ok(token);
const ids = markerInfo.private.tokens.map(candidate => candidate.id);
const initial = richeseHomeworldMarkerChoice(markerTurn, 'homeworld:atreides', token.id);
assert.ok(initial.action, initial.blocked ?? 'The initial marker shipment is available.');
await act(ownerSeat.cookie, initial.action);
const landed = await advance(own => own.phase === 5 && !!own.richeseNoField?.private?.deployed &&
own.players.find(player => player.id === own.me)?.shipped === true);
assert.deepEqual(landed.richeseNoField!.private!.deployed!.location, { homeworld: 'homeworld:atreides' });
const firstTurn = landed.turn;
const beforeTransfer = await advance(own => own.status === 'playing' && own.turn > firstTurn && own.phase === 5 &&
own.active === own.me && !own.players.find(player => player.id === own.me)!.shipped &&
!!own.homeworldShipment && !own.homeworldShipment.blocked && !!own.richeseNoField?.private?.deployed);
const mover = beforeTransfer.players.find(player => player.id === beforeTransfer.me)!;
const oldSpice = mover.spice, oldReserves = mover.reserves;
assert.deepEqual(beforeTransfer.homeworlds?.worlds?.find(world => world.id === 'homeworld:atreides')?.forces[beforeTransfer.me],
{ normal: 2, elite: 0 });
const transfer = richeseHomeworldMarkerChoice(beforeTransfer, 'homeworld:guild', token.id, 0, 'homeworld:atreides',
{ normal: 1, elite: 0 });
assert.ok(transfer.action, transfer.blocked ?? 'The marker transfer is available.');
assert.equal(transfer.action.noFieldFrom, 'homeworld:atreides');
const stale = await request(path, ownerSeat.cookie, { version: view.version,
action: { ...transfer.action, noFieldEvent: 'stale-marker-event' } });
assert.equal(stale.status, 409, JSON.stringify(stale.data));
const unchanged = await read(ownerSeat.cookie);
assert.equal(unchanged.version, beforeTransfer.version);
assert.deepEqual(unchanged.homeworlds?.worlds?.find(world => world.id === 'homeworld:atreides')?.forces[unchanged.me],
{ normal: 2, elite: 0 });
await act(ownerSeat.cookie, transfer.action);
const after = await advance(own => own.phase === 5 && own.players.find(player => player.id === own.me)?.shipped === true &&
!!own.richeseNoField?.private?.deployed && 'homeworld' in own.richeseNoField.private.deployed.location &&
own.richeseNoField.private.deployed.location.homeworld === 'homeworld:guild');
const result = after.players.find(player => player.id === after.me)!;
assert.equal(result.spice, oldSpice! - 2); assert.equal(result.reserves, oldReserves);
assert.deepEqual(after.homeworlds?.worlds?.find(world => world.id === 'homeworld:atreides')?.forces[after.me],
{ normal: 1, elite: 0 });
assert.deepEqual(after.homeworlds?.worlds?.find(world => world.id === 'homeworld:guild')?.forces[after.me],
{ normal: 1, elite: 0 });
assert.equal(after.richeseNoField!.private!.deployed!.tokenId, token.id);
assert.equal(after.richeseNoField!.private!.lastShipped, token.id);
const other = await read(hostSeat.cookie);
assert.equal(other.richeseNoField!.private, null);
assert.deepEqual(other.richeseNoField!.public.deployed!.location, { homeworld: 'homeworld:guild' });
for (const id of ids) assert.equal(JSON.stringify(other).includes(id), false, 'A foreign seat cannot inspect marker identity.');
});
@@ -0,0 +1,81 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { HomeworldCustodyError, type HomeworldCustody } from '../game/homeworld-custody';
import type { HomeworldShipmentContext } from '../game/homeworld-shipment';
import { quoteRicheseHomeworldMarkerTransfer as quote, type RicheseHomeworldMarkerTransferIntent } from '../game/richese-homeworld-marker-shipment';
import { createRicheseNoField, deployRicheseNoField } from '../game/richese-no-field';
function fixture(tokenId = 'zero', physical = 20) {
const context: HomeworldShipmentContext = { advanced: true, players: [
{ id: 'r', faction: 'richese', reserves: 0, eliteReserves: 0, ally: null },
{ id: 'a', faction: 'atreides', reserves: 17, eliteReserves: 0, ally: null },
{ id: 'g', faction: 'guild', reserves: 20, eliteReserves: 0, ally: null },
] };
const custody: HomeworldCustody = { salusa: null, visitors: {
'homeworld:atreides': { r: { normal: physical, elite: 0 } },
} };
const noField = deployRicheseNoField(createRicheseNoField(['zero', 'three', 'five']),
{ tokenId, controller: 'r', location: { homeworld: 'homeworld:atreides' } });
const intent: RicheseHomeworldMarkerTransferIntent = { player: 'r', tokenId,
source: 'homeworld:atreides', destination: 'homeworld:guild', forces: { normal: 2, elite: 0 } };
return { context, custody, noField, intent };
}
void test('mixed transfers move actual same-source counters and preserve concealed marker custody in both modes', () => {
for (const advanced of [false, true]) for (const tokenId of ['zero', 'three', 'five']) {
const f = fixture(tokenId);
f.context.advanced = advanced;
const before = structuredClone(f);
const result = quote(f.context, f.custody, f.intent, f.noField);
assert.equal(result.amount, 3);
assert.equal(result.cost, 3);
assert.equal(result.elite, 0);
assert.equal(result.noField.lastShipped, tokenId);
assert.deepEqual(result.noField.deployed, { tokenId, controller: 'r', location: { homeworld: 'homeworld:guild' } });
assert.deepEqual(result.state?.visitors['homeworld:atreides'].r, { normal: 18, elite: 0 });
assert.deepEqual(result.state?.visitors['homeworld:guild'].r, { normal: 2, elite: 0 });
assert.deepEqual(result.sources?.[0], { homeworld: 'homeworld:atreides', player: 'r',
before: { normal: 20, elite: 0 }, after: { normal: 18, elite: 0 } });
assert.equal(result.players?.find(p => p.id === 'r')?.reserves, 0);
assert.deepEqual(f, before);
result.state!.visitors['homeworld:guild'].r.normal = 0;
result.noField.tokens[0].id = 'detached';
assert.deepEqual(f, before);
}
});
void test('purchased Guild rate rounds once over the full marker and physical group', () => {
for (const physical of [1, 2, 3, 19, 20]) {
const f = fixture();
f.context.purchasedGuildRate = true;
f.intent.forces = { normal: physical, elite: 0 };
const result = quote(f.context, f.custody, f.intent, f.noField);
assert.equal(result.amount, physical + 1);
assert.equal(result.cost, Math.ceil((physical + 1) / 2));
}
});
void test('zero physical forces preserve marker-only quote; malformed or unavailable groups reject immutably', () => {
const f = fixture();
const legacy = { ...f.intent }; delete legacy.forces;
const zero = { ...f.intent, forces: { normal: 0, elite: 0 } };
assert.deepEqual(quote(f.context, f.custody, zero, f.noField), quote(f.context, f.custody, legacy, f.noField));
for (const forces of [
{ normal: 21, elite: 0 }, { normal: 1, elite: 1 }, { normal: -1, elite: 0 },
{ normal: 0.5, elite: 0 }, { normal: 1 }, { normal: 1, elite: 0, extra: 0 }, null,
]) {
const bad = { ...f.intent, forces } as unknown as RicheseHomeworldMarkerTransferIntent;
const before = structuredClone({ f, bad });
assert.throws(() => quote(f.context, f.custody, bad, f.noField), HomeworldCustodyError);
assert.deepEqual({ f, bad }, before);
}
});
void test('physical counters at a different world cannot accompany this marker', () => {
const f = fixture('zero', 1);
f.custody.visitors['homeworld:guild'] = { r: { normal: 2, elite: 0 } };
f.intent.forces = { normal: 2, elite: 0 };
const before = structuredClone(f);
assert.throws(() => quote(f.context, f.custody, f.intent, f.noField), HomeworldCustodyError);
assert.deepEqual(f, before);
});
@@ -0,0 +1,184 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { applyAction, createGame, joinGame, newPlayer, viewGame, type Action, type Game } from '../game/engine';
import { TERRITORIES } from '../game/board';
import { homeworldContext } from '../game/homeworld-game';
import { currentHomeworldOccupationSnapshot, homeworldOccupationMarker, homeworldOccupationSnapshotPresence } from '../game/homeworld-occupation-history';
import { nextIndependentCardModulesStep } from './fixture-independent-card-modules';
const owner = 'richese', native = 'atreides', guild = 'guild';
const source = 'homeworld:atreides', destination = 'homeworld:guild';
const seat = (game: Game, id: string) => {
const result = game.players.find(player => player.id === id);
assert.ok(result); return result;
};
const quiet = (game: Game) => !game.response && !game.decision && !game.phaseOpening &&
!game.truthtrance && !game.pendingTreacheryDiscard;
/** Original native action/response continuations, including a real Guild
* interception when Advanced allows one. No saved phase or custody is staged. */
function advance(game: Game, until: (game: Game) => boolean): Game {
let state = game;
for (let count = 0; count < 2500; count++) {
if (until(state)) return state;
assert.notEqual(state.status, 'finished');
if (state.decision?.kind === 'homeworldShipmentGuild') {
const decision = state.decision;
state = applyAction(state, decision.player, { type: 'decision', event: decision.event, allow: true });
continue;
}
const next = nextIndependentCardModulesStep(state);
assert.ok(next, `Native continuation stopped at ${state.phase}/${state.decision?.kind ?? 'no decision'}.`);
// Preserve the native Atreides cash needed for its intentional full
// shipment by passing the actual Richese cache lot, rather than buying it.
if (next.actor === native && next.action.type === 'richeseBid') {
state = applyAction(state, native, { type: 'richeseBid', event: next.action.event, amount: 0, allyPayment: 0 });
continue;
}
state = applyAction(state, next.actor, next.action);
}
throw Error('The native Homeworld transfer programme did not reach its requested boundary.');
}
/** Actual original lobby/start and force choices. Atreides deliberately ships
* its whole real reserve to a legal stronghold before the first marker arrives,
* so that the marker survives the intervening Battle phase without inventing
* a battle exemption. */
function setup(advanced: boolean): Game {
let game = createGame('RICHESEWORLDMIXED', newPlayer(native, native, 'atreides'), advanced, ['choam']);
joinGame(game, newPlayer(owner, owner, 'richese'));
joinGame(game, newPlayer(guild, guild, 'guild'));
game = applyAction(game, native, { type: 'homeworlds', enabled: true });
for (const player of game.players) game = applyAction(game, player.id, { type: 'ready' });
game = applyAction(game, native, advanced ? { type: 'start', advancedPreview: true } : { type: 'start' });
// Select one undealt original card position, ensuring Guild has a real
// physical Karama for the response/stop case. Never add or duplicate cards.
const karama = game.deck.findIndex(card => card.effect === 'karama');
assert.ok(karama >= 0 && game.deck.length >= 3);
[game.deck[2], game.deck[karama]] = [game.deck[karama], game.deck[2]];
game = advance(game, state => state.status === 'playing');
assert.ok(seat(game, guild).hand.some(card => card.effect === 'karama'));
game = advance(game, state => state.phase === 5 && state.active === native && quiet(state));
const amount = seat(game, native).reserves;
assert.ok(amount > 0);
let shipped: Game | null = null;
for (const target of TERRITORIES.filter(territory => territory.type === 'stronghold')) {
for (const sector of target.sectors) {
if (sector === game.storm) continue;
try {
shipped = applyAction(game, native, { type: 'ship', territory: target.id, sector, amount,
elite: 0, allyPayment: 0, homeworldSources: { [source]: { normal: amount, elite: 0 } } });
break;
} catch { /* Try another legal native stronghold; rejected actions are immutable. */ }
}
if (shipped) break;
}
assert.ok(shipped, 'Atreides can ship its real starting reserve to a legal stronghold.');
game = advance(shipped, quiet);
assert.equal(seat(game, native).reserves, 0);
game = applyAction(game, native, { type: 'endMovement' });
game = advance(game, state => state.phase === 5 && state.active === owner && quiet(state) &&
!!viewGame(state, owner).homeworldShipment && !!viewGame(state, owner).richeseNoField?.canShip);
return game;
}
function nextOwnerTurn(game: Game): Game {
const turn = game.turn;
game = applyAction(game, owner, { type: 'endMovement' });
return advance(game, state => state.turn > turn && state.phase === 5 && state.active === owner &&
quiet(state) && !seat(state, owner).shipped && !!viewGame(state, owner).homeworldShipment &&
!viewGame(state, owner).homeworldShipment!.blocked);
}
function visitor(game: Game, world: string) {
return game.homeworlds!.custody!.visitors[world]?.[owner];
}
/** The source counter group and marker arrive in two actual consecutive turns.
* The native evacuation above ensures neither is invented or protected from battle. */
function prepare(advanced: boolean) {
let game = setup(advanced);
let own = viewGame(game, owner);
game = applyAction(game, owner, { type: 'homeworldShip', event: own.homeworldShipment!.event,
destination: source, sources: { 'homeworld:richese': { normal: 2, elite: 0 } }, allyPayment: 0 });
game = advance(game, state => state.phase === 5 && state.active === owner && quiet(state) &&
seat(state, owner).shipped && !state.pendingHomeworldShipment);
assert.deepEqual(visitor(game, source), { normal: 2, elite: 0 });
game = nextOwnerTurn(game);
assert.deepEqual(visitor(game, source), { normal: 2, elite: 0 });
own = viewGame(game, owner);
const info = own.richeseNoField!;
const token = info.private!.tokens.find(candidate => candidate.value === 3);
assert.ok(token);
game = applyAction(game, owner, { type: 'homeworldShip', event: own.homeworldShipment!.event,
destination: source, sources: {}, noField: token.id, noFieldEvent: info.event, allyPayment: 0 });
game = advance(game, state => state.phase === 5 && state.active === owner && quiet(state) &&
seat(state, owner).shipped && !!seat(state, owner).noField?.deployed);
assert.deepEqual(seat(game, owner).noField!.deployed!.location, { homeworld: source });
assert.deepEqual(visitor(game, source), { normal: 2, elite: 0 });
game = nextOwnerTurn(game);
assert.deepEqual(visitor(game, source), { normal: 2, elite: 0 });
assert.equal(seat(game, owner).noField!.lastShipped, token.id);
return { game, token: token.id };
}
function declaration(game: Game, token: string): Action {
const own = viewGame(game, owner);
return { type: 'homeworldShip', event: own.homeworldShipment!.event, destination,
sources: { [source]: { normal: 1, elite: 0 } }, noField: token,
noFieldEvent: own.richeseNoField!.event, noFieldFrom: source, allyPayment: 0 };
}
function presence(game: Game, world: string) {
const snapshot = currentHomeworldOccupationSnapshot(homeworldContext(game), game.homeworlds!.custody!,
homeworldOccupationMarker(game.players));
return homeworldOccupationSnapshotPresence(snapshot, world);
}
for (const advanced of [false, true]) void test(`${advanced ? 'Advanced' : 'Basic'} mixed marker and physical Homeworld transfer uses one shipment`, () => {
const ready = prepare(advanced);
let game = ready.game;
const before = structuredClone(game), action = declaration(game, ready.token);
assert.ok(seat(before, owner).spice >= 2, 'The actual Richese seat can pay the two-spice price.');
assert.deepEqual(presence(before, source), [owner]);
for (const stale of [{ ...action, noFieldEvent: 'stale-marker-event' }, { ...action, event: 'stale-shipment-event' }]) {
assert.throws(() => applyAction(game, owner, stale));
assert.deepEqual(game, before, 'A stale event leaves the real game unchanged.');
}
const malformed = { ...action, sources: { [source]: null } } as unknown as Action;
assert.throws(() => applyAction(game, owner, malformed), 'A null typed source cannot be dropped from a mixed transfer.');
assert.deepEqual(game, before, 'Rejecting a malformed source leaves the game unchanged.');
game = applyAction(game, owner, action);
game = advance(game, state => state.phase === 5 && state.active === owner && quiet(state) &&
seat(state, owner).shipped && !state.pendingHomeworldShipment);
assert.equal(seat(game, owner).spice, seat(before, owner).spice - 2);
assert.equal(seat(game, owner).reserves, seat(before, owner).reserves);
assert.deepEqual(visitor(game, source), { normal: 1, elite: 0 });
assert.deepEqual(visitor(game, destination), { normal: 1, elite: 0 });
assert.equal(seat(game, owner).noField!.deployed!.tokenId, ready.token);
assert.deepEqual(seat(game, owner).noField!.deployed!.location, { homeworld: destination });
assert.equal(seat(game, owner).noField!.lastShipped, ready.token);
assert.deepEqual(presence(game, source), [owner]);
assert.deepEqual(presence(game, destination).sort(), [guild, owner].sort());
});
void test('Advanced Guild can stop a mixed Homeworld transfer without payment, moving either group, or changing marker history', () => {
const ready = prepare(true);
let game = ready.game;
const before = structuredClone(game);
game = applyAction(game, owner, declaration(game, ready.token));
game = advance(game, state => state.decision?.kind === 'homeworldShipmentGuild');
assert.match(game.log.at(-1)!.text, /one concealed No-Field and 1 physical forces/);
const karama = seat(game, guild).hand.find(card => card.effect === 'karama'); assert.ok(karama);
game = applyAction(game, guild, { type: 'card', mode: 'special', card: karama.id });
game = advance(game, state => state.phase === 5 && state.active === owner && quiet(state) &&
!state.pendingHomeworldShipment);
assert.equal(seat(game, owner).shipped, true);
assert.deepEqual(seat(game, owner).noField, seat(before, owner).noField);
assert.equal(seat(game, owner).reserves, seat(before, owner).reserves);
assert.equal(seat(game, owner).spice, seat(before, owner).spice);
assert.deepEqual(game.homeworlds!.custody, before.homeworlds!.custody);
assert.deepEqual(presence(game, source), presence(before, source));
assert.deepEqual(presence(game, destination), presence(before, destination));
});