Support current battle choice promises and persistent notices
Verify and publish checkpoint container / container (push) Successful in 11m42s

This commit is contained in:
Quintine committed 2026-11-16 21:49:48 +11:00
1 parent 77d02b3231
commit bd8377d87b
16 files changed
+684 -32

No files matched your search

+89 -1
View File
@@ -42,6 +42,7 @@ export function turnActionPromiseModeSupported(game: NativePromiseScope & {
export type TurnActionLeaf = TravelActionClaim | StormDialClaim | { kind: 'payment'; recipient: string; minimum: number } |
{ kind: 'bid'; lot: number; minimum: number } | RevivalActionClaim | { kind: 'alliance'; partner: string } |
{ kind: 'battleChoice'; source: string; territory: string; target: string } |
{ kind: 'battlePlan'; battle: string; territory: string; claim: MixedBattleActionClaim } |
{ kind: 'traitorCall'; battle: string; territory: string; call: boolean } |
{ kind: 'cardPlay'; effect: PrintedCardEffect } |
@@ -56,6 +57,7 @@ export type TurnActionBranchLeaf = TravelBranchLeaf |
{ kind: 'forces'; counter: 'total' | 'normal' | 'elite'; minimum: number; progress: number; answer: boolean } |
{ kind: 'leader'; leader: string; answer: boolean } |
{ kind: 'alliance'; partner: string; source: string; answer: boolean } |
{ kind: 'battleChoice'; source: string; territory: string; target: string; answer: boolean } |
{ kind: 'battlePlan'; battle: string; territory: string; claim: MixedBattleActionClaim; answer: boolean } |
{ kind: 'traitorCall'; battle: string; territory: string; call: boolean; answer: boolean;
beneficiary: string; target: string; leader: string | null } |
@@ -114,6 +116,8 @@ export type TurnActionPromise = TurnActionClaim & {
windowCards?: TurnActionWindowCards;
/** A submitted dial is private to its respondent until the native reveal. */
stormDial?: { source: string; closed: boolean; actual: number | null };
/** Only the original chooser opportunity and its one actual native choice. */
battleChoice?: { source: string; closed: boolean; selected: { territory: string; target: string } | null };
};
export class TurnActionError extends Error {
@@ -141,6 +145,19 @@ const printedTerritory = (value: unknown): value is string =>
typeof value === 'string' && TERRITORIES.some(destination => destination.id === value);
const windowEffect = (value: unknown): value is WindowCardEffect => WINDOW_CARD_EFFECTS.some(effect => effect === value);
/** Decode the exact existing public battle-chooser event, without inventing
* a competing event grammar or accepting a client-selected turn. */
function battleChoiceSource(value: unknown): { turn: number } | null {
if (!identifier(value) || !value.startsWith('battleOrder:')) return null;
try {
const tuple: unknown = JSON.parse(value.slice('battleOrder:'.length));
if (!Array.isArray(tuple) || tuple.length !== 3 || !positive(tuple[0]) ||
!(tuple[1] === null || identifier(tuple[1])) || !whole(tuple[2]) ||
value !== `battleOrder:${JSON.stringify(tuple)}`) return null;
return {turn:tuple[0]};
} catch { return null; }
}
/** Identity membership is decoded by Truthtrance; here retain one strict atom. */
function parseCombatClaim(value: unknown): MixedBattleActionClaim {
try {
@@ -189,6 +206,12 @@ export function parseTurnActionLeaf(value: unknown): TurnActionLeaf {
'Choose only a supported printed-card play after this answer.');
return { kind: 'cardPlay', effect: value.effect as PrintedCardEffect };
}
if (value.kind === 'battleChoice') {
check(keys(value, 'kind,source,target,territory') && !!battleChoiceSource(value.source) &&
identifier(value.territory) && identifier(value.target),
'Choose the original battle-selection opportunity, destination and opponent.');
return {kind:'battleChoice',source:value.source as string,territory:value.territory,target:value.target};
}
if (value.kind === 'battlePlan' || value.kind === 'traitorCall') {
check(keys(value, value.kind === 'battlePlan' ? 'battle,claim,kind,territory' : 'battle,call,kind,territory') &&
identifier(value.battle) && identifier(value.territory), 'Retain the exact original current battle and native location.');
@@ -235,6 +258,7 @@ function storedFact(value: unknown, depth = 0): TruthFact {
}
function actionKey(action: TurnActionLeaf): string {
if (action.kind === 'battleChoice') return JSON.stringify([action.kind,action.source,action.territory,action.target]);
if (action.kind === 'stormDial') return JSON.stringify(action.compare && action.compare !== 'eq'
? [action.kind, action.source, action.value, action.compare] : [action.kind, action.source, action.value]);
if (action.kind === 'windowCardPlay') return JSON.stringify([action.kind, action.effect, action.source]);
@@ -300,6 +324,7 @@ function readPromise(promise: TurnActionPromise): CompiledMixedAction<TurnAction
if ('cardPlays' in promise) fields.push('cardPlays');
if ('windowCards' in promise) fields.push('windowCards');
if ('stormDial' in promise) fields.push('stormDial');
if ('battleChoice' in promise) fields.push('battleChoice');
check(keys(promise, fields.sort().join(',')) &&
positive(promise.turn) && identifier(promise.event) && identifier(promise.player) && identifier(promise.asker),
'Retain the complete original turn-action commitment and its metadata.');
@@ -326,6 +351,7 @@ function readPromise(promise: TurnActionPromise): CompiledMixedAction<TurnAction
const cardEffects = new Set<PrintedCardEffect>();
const windowSources = new Map<WindowCardEffect, Set<string>>();
const stormSources = new Set<string>();
const choiceSources = new Set<string>();
const visit = (node: MixedActionExpression<TurnActionLeaf>): void => {
if (node.kind === 'action') {
originals.add(actionKey(node.action));
@@ -343,6 +369,7 @@ function readPromise(promise: TurnActionPromise): CompiledMixedAction<TurnAction
if (node.action.kind === 'traitorCall') hasTraitor = true;
if (node.action.kind === 'cardPlay') cardEffects.add(node.action.effect);
if (node.action.kind === 'stormDial') stormSources.add(node.action.source);
if (node.action.kind === 'battleChoice') choiceSources.add(node.action.source);
if (node.action.kind === 'battlePower') powerClaims[battlePowerOf(node.action.claim)].add(powerKey(node.action.claim));
if (node.action.kind === 'windowCardPlay') {
const sources = windowSources.get(node.action.effect) ?? new Set<string>();
@@ -360,6 +387,16 @@ function readPromise(promise: TurnActionPromise): CompiledMixedAction<TurnAction
for (const power of ['voice', 'prescience'] as const) check(!powerClaims[power].size || !!promise.combat?.powers?.[power],
'Retain the original private native battle-power stage, including folded original clauses.');
check(!stormSources.size || !!promise.stormDial, 'Retain the original native storm-dial interval, including folded clauses.');
check(!choiceSources.size || !!promise.battleChoice, 'Retain the original native battle-selection interval.');
if ('battleChoice' in promise) {
const stage = promise.battleChoice, source = stage && battleChoiceSource(stage.source);
check(object(stage) && keys(stage, 'closed,selected,source') && source && source.turn === promise.turn &&
choiceSources.size === 1 && choiceSources.has(stage.source) && typeof stage.closed === 'boolean' &&
(stage.selected === null || object(stage.selected) && keys(stage.selected, 'target,territory') &&
identifier(stage.selected.target) && stage.selected.target !== promise.player && identifier(stage.selected.territory)) &&
(stage.closed || stage.selected === null),
'Retain one original chooser source and only its actual selected battle after closure.');
}
if ('stormDial' in promise) {
const stage = promise.stormDial;
const source = stage && parseStormDialSource(stage.source);
@@ -535,6 +572,9 @@ function readPromise(promise: TurnActionPromise): CompiledMixedAction<TurnAction
}
function actionValue(action: TurnActionLeaf, promise: TurnActionPromise): boolean {
if (action.kind === 'battleChoice') return !!promise.battleChoice?.closed &&
promise.battleChoice.source === action.source && promise.battleChoice.selected?.territory === action.territory &&
promise.battleChoice.selected?.target === action.target;
if (action.kind === 'stormDial') return promise.stormDial?.source === action.source &&
promise.stormDial.actual !== null && stormDialMatches(action, promise.stormDial.actual);
if (action.kind === 'windowCardPlay') return promise.windowCards?.[action.effect]?.played ?? false;
@@ -569,6 +609,7 @@ function evaluate(node: CompiledMixedAction<TurnActionLeaf>, promise: TurnAction
}
function actionClosed(action: TurnActionLeaf, promise: TurnActionPromise): boolean {
if (action.kind === 'battleChoice') return promise.battleChoice!.closed;
if (action.kind === 'stormDial') return promise.stormDial!.closed;
if (action.kind === 'windowCardPlay') return promise.windowCards![action.effect]!.closed;
if (action.kind === 'battlePower') return promise.combat!.powers![battlePowerOf(action.claim)]!.closed;
@@ -612,6 +653,8 @@ export function liveTurnActionPromises<T extends TurnActionPromise>(promises: re
function sameAction(left: TurnActionBranchLeaf, right: TurnActionBranchLeaf): boolean {
if (left.kind !== right.kind) return false;
if (left.kind === 'battleChoice') return right.kind === 'battleChoice' && left.source === right.source &&
left.territory === right.territory && left.target === right.target;
if (left.kind === 'stormDial') return right.kind === 'stormDial' && left.source === right.source &&
left.value === right.value && (left.compare ?? 'eq') === (right.compare ?? 'eq');
if (left.kind === 'windowCardPlay') return right.kind === 'windowCardPlay' && left.effect === right.effect && left.source === right.source;
@@ -650,6 +693,14 @@ export function turnActionStormDialChoices(leaves: readonly TurnActionBranchLeaf
function mergeBranches(left: TurnActionBranchLeaf[], right: TurnActionBranchLeaf[]): TurnActionBranchLeaf[] | null {
const merged = left.map(leaf => ({ ...leaf }));
for (const leaf of right) {
if (leaf.kind === 'battleChoice') {
const prior = merged.find(other => sameAction(other,leaf));
if (prior && prior.answer !== leaf.answer) return null;
if (leaf.answer && merged.some(other => other.kind === 'battleChoice' && other.source === leaf.source &&
other.answer && (other.territory !== leaf.territory || other.target !== leaf.target))) return null;
if (!prior) merged.push({...leaf});
continue;
}
if (leaf.kind === 'traitorCall') {
const prior = merged.find(other => other.kind === 'traitorCall' && other.battle === leaf.battle &&
other.territory === leaf.territory && other.beneficiary === leaf.beneficiary && other.target === leaf.target && other.leader === leaf.leader);
@@ -747,6 +798,39 @@ function settle<T extends TurnActionPromise>(promise: T, update: T): T {
? { ...update, fulfilled: true } : update;
}
/** Record exactly the native chooser's accepted territory/opponent once.
* The engine owns candidate legality and reconciles external impossibility. */
export function recordTurnActionBattleChoice<T extends TurnActionPromise>(promises: readonly T[], player: string,
turn: number, source: string, selected: {territory:string; target:string}): T[] {
const identity = battleChoiceSource(source);
check(identity && identity.turn === turn && object(selected) && keys(selected, 'target,territory') &&
identifier(selected.territory) && identifier(selected.target) && selected.target !== player,
'Retain the original chooser opportunity and actual different opponent.');
const updates = new Map<T,T>();
for (const promise of liveTurnActionPromises(promises,player,turn)) {
readPromise(promise);
const stage = promise.battleChoice;
if (!stage || stage.closed || stage.source !== source) continue;
updates.set(promise,settle(promise,{...promise,battleChoice:{...stage,closed:true,selected:{...selected}}}));
}
return promises.map(promise => updates.get(promise) ?? promise);
}
/** A lost or closed chooser source has no synthetic selected battle. */
export function closeTurnActionBattleChoice<T extends TurnActionPromise>(promises: readonly T[], player: string,
turn: number, source: string): T[] {
const identity = battleChoiceSource(source);
check(identity && identity.turn === turn, 'Retain the original current-turn battle chooser source.');
const updates = new Map<T,T>();
for (const promise of liveTurnActionPromises(promises,player,turn)) {
readPromise(promise);
const stage = promise.battleChoice;
if (!stage || stage.closed || stage.source !== source) continue;
updates.set(promise,settle(promise,{...promise,battleChoice:{...stage,closed:true}}));
}
return promises.map(promise => updates.get(promise) ?? promise);
}
/** Record one actual sealed native dial against its exact original source.
* Closing the stage never publishes its private numeric receipt. */
export function recordTurnActionStormDial<T extends TurnActionPromise>(promises: readonly T[], player: string,
@@ -1169,6 +1253,7 @@ export function finishTurnActionTurn<T extends TurnActionPromise>(promises: read
.filter(effect => Object.hasOwn(promise.cardPlays!, effect)).map(effect => [effect, promise.cardPlays![effect]
? { ...promise.cardPlays![effect]!, closed: true } : null])) as TurnActionCardPlays } : {}),
...(promise.windowCards ? { windowCards } : {}),
...(promise.battleChoice ? { battleChoice: { ...promise.battleChoice, closed: true } } : {}),
...(promise.stormDial ? { stormDial: { ...promise.stormDial, closed: true } } : {}) };
const unresolvedCombat = promise.combat && (promise.combat.plan && !promise.combat.plan.closed ||
promise.combat.traitor && !promise.combat.traitor.closed || promise.combat.powers?.voice && !promise.combat.powers.voice.closed ||
@@ -1193,15 +1278,18 @@ export function validateTurnActionPromises(promises: readonly TurnActionPromise[
(!promise.combat?.traitor || promise.combat.traitor.closed) &&
(!promise.combat?.powers?.voice || promise.combat.powers.voice.closed) &&
(!promise.combat?.powers?.prescience || promise.combat.powers.prescience.closed) &&
(!promise.battleChoice || promise.battleChoice.closed) &&
WINDOW_CARD_EFFECTS.every(effect => promise.windowCards?.[effect]?.closed ?? true) &&
(!promise.stormDial || promise.stormDial.closed) &&
PRINTED_CARD_EFFECTS.every(effect => promise.cardPlays?.[effect]?.closed ?? true);
const recipientsValid = (node: MixedActionExpression<TurnActionLeaf>): boolean => node.kind === 'action'
? node.action.kind === 'alliance' ? players.includes(node.action.partner) && node.action.partner !== promise.player :
? node.action.kind === 'battleChoice' ? players.includes(node.action.target) && node.action.target !== promise.player :
node.action.kind === 'alliance' ? players.includes(node.action.partner) && node.action.partner !== promise.player :
node.action.kind !== 'payment' || players.includes(node.action.recipient) && node.action.recipient !== promise.player
: node.kind === 'fact' || node.terms.every(recipientsValid);
check(!events.has(promise.event) && players.includes(promise.player) && players.includes(promise.asker) &&
promise.player !== promise.asker && recipientsValid(promise.mixed) &&
(!promise.battleChoice?.selected || players.includes(promise.battleChoice.selected.target)) &&
(!promise.alliance || promise.alliance.partner === null || players.includes(promise.alliance.partner)) &&
(!promise.combat?.traitor || players.includes(promise.combat.traitor.beneficiary) &&
players.includes(promise.combat.traitor.target) && promise.combat.traitor.target !== promise.player) &&