Files
Dune/game/ecaz-occupy.ts
Quintine f0256c1b38 Prototype Discovery spice encounters, private tokens and stash rewards
Connect real Basic/Advanced base-faction audit setup to all seven Discovery
Spice Cards and eight physical tokens. Add Great Maker's ordered majority
vote and Fremen reserve ride, destructive Discovery blows, private faction
peeks and Collection inspection/reveal, stash rewards and mandatory saved
card overflow, and revealed nested board locations. Share private legal
choices with human controls and all four AI profiles. Preserve normal gates.

Independent review repairs orphaned worm/vote/ride controls, retargeted
reserve arrivals, actor bindings and incomplete overflow discard receipts.
Harden room-aware admission against unrevealed nested locations. Extend the
backed-up local prototype starter with a Discovery profile; preserve seats.

Still missing: next-turn free entry, five special location benefits and
Ornithopter movement spending. Current prototype playthroughs do not certify
those effects or complete module/expansion compliance.

Verification: npm run check passed 4264 offline tests plus types/lint;
npm run build passed; npm run test:integration passed 40 tests. The single
source-bound broad run took 129358ms / 6782ms / 23918ms respectively.
Report: /home/quintine/.local/share/dune/checkpoints/20260913-discovery-prototype/verification/report.json
Source fingerprint: 4ea7b5209a58c369b0b1f8dc239b5dd2f3302bb759d83c32ba5406e80d8ae154

Two seeded Basic/Advanced games complete with component custody intact.
SQLite races pay stashes once and resume committed discards without replay.
Browser 3RJBH26Y verifies genuine human Fremen setup, a Discovery blow from
one reordered physical card, native private Hiereg visibility, enlarged
card/rules reading and refresh. Room remains paused at spice acceptance.
All 125 opening saved rooms unchanged; 31 added, 156 total. No server restart.

Efficiency: three reused bounded helpers; aggregate counter 60591430 to
61812513 after verification, 1221083 tokens before final Git/push overhead.
47 added offline tests. One stable broad run after focused review fixes;
no calibration or exhaustive optional-module interaction matrix.
2026-09-13 13:27:58 +10:00

128 lines
4.4 KiB
TypeScript

import { isDiscoveryLocationId } from './discoveries';
import { FACTIONS, type FactionId } from './catalog';
import { MOBILE_STRONGHOLD, TERRITORIES, validLocation } from './board';
/** Only seated public identity is relevant. No force totals, advisor stance,
* hidden markers, hands, combat plans or faction bonuses are read here. */
export type EcazOccupancySeat = Readonly<{
id: string;
faction: FactionId;
ally?: string | null;
}>;
export type EcazOccupancyLocation =
| Readonly<{ kind: 'territory'; id: string; sector?: number }>
| Readonly<{ kind: 'homeworld'; id: string }>;
export type EcazOccupancyIdentity = {
kind: 'seat' | 'ecazAlliance';
members: string[];
/** Collision-free, deterministic identity for movement/occupancy grouping. */
key: string;
};
export type EcazOccupancyRelation = 'sameSeat' | 'ecazAlliance' | 'different';
export class EcazOccupancyError extends Error {
constructor(message: string) {
super(message);
this.name = 'EcazOccupancyError';
}
}
const text = (value: unknown): value is string =>
typeof value === 'string' && value.trim().length > 0;
const record = (value: unknown): value is Record<string, unknown> =>
!!value && typeof value === 'object' && !Array.isArray(value);
function requireOccupancy(
condition: unknown,
message: string,
): asserts condition {
if (!condition) throw new EcazOccupancyError(message);
}
function validate(
seats: readonly EcazOccupancySeat[],
at: EcazOccupancyLocation,
) {
requireOccupancy(
Array.isArray(seats) &&
seats.length > 0 &&
seats.every(
(seat) =>
record(seat) &&
text(seat.id) &&
FACTIONS.some((faction) => faction.id === seat.faction) &&
(seat.ally === undefined || seat.ally === null || text(seat.ally)),
) &&
new Set(seats.map((seat) => seat.id)).size === seats.length &&
new Set(seats.map((seat) => seat.faction)).size === seats.length,
'Occupancy requires unique valid seated identities and factions.',
);
for (const seat of seats) {
if (seat.ally === undefined || seat.ally === null) continue;
const ally = seats.find((other) => other.id === seat.ally);
requireOccupancy(
ally && ally.id !== seat.id && ally.ally === seat.id,
'Occupancy requires a seated, distinct, reciprocal ally.',
);
}
requireOccupancy(
record(at) && text(at.id),
'Choose a valid occupancy location.',
);
if (at.kind === 'homeworld') return;
requireOccupancy(
at.kind === 'territory' &&
(at.id === MOBILE_STRONGHOLD || isDiscoveryLocationId(at.id) ||
TERRITORIES.some((t) => t.id === at.id)) &&
(at.sector === undefined ||
(Number.isSafeInteger(at.sector) && validLocation(at.id, at.sector))),
'Occupancy requires a board territory and valid optional sector.',
);
}
function identity(
seats: readonly EcazOccupancySeat[],
player: string,
at: EcazOccupancyLocation,
): EcazOccupancyIdentity {
const seat = seats.find((seat) => seat.id === player);
requireOccupancy(
text(player) && seat,
'This occupying player is not seated.',
);
const ally = seats.find((other) => other.id === seat.ally);
const coalition =
at.kind === 'territory' &&
ally &&
(seat.faction === 'ecaz' || ally.faction === 'ecaz');
const members = coalition ? [seat.id, ally.id].sort() : [seat.id];
return {
kind: coalition ? 'ecazAlliance' : 'seat',
members,
key: JSON.stringify(members),
};
}
/** E3 Occupy identifies Ecaz and its reciprocal ally as one faction for
* territory entry/occupancy. Homeworlds do not inherit that permission.
* A result is identity only: the caller still checks actual presence, stance,
* capacity, storm and transport rules. It is not combat or victory scoring. */
export function ecazOccupancyIdentity(
seats: readonly EcazOccupancySeat[],
player: string,
at: EcazOccupancyLocation,
): EcazOccupancyIdentity {
validate(seats, at);
return identity(seats, player, at);
}
/** Distinguish an actual seat from its Ecaz alliance partner. Ordinary allied
* factions remain different identities; no ordinary alliance entry is granted. */
export function ecazOccupancyRelation(
seats: readonly EcazOccupancySeat[],
left: string,
right: string,
at: EcazOccupancyLocation,
): EcazOccupancyRelation {
validate(seats, at);
const a = identity(seats, left, at);
const b = identity(seats, right, at);
if (left === right) return 'sameSeat';
return a.key === b.key ? 'ecazAlliance' : 'different';
}