Add final Nexus alliances to combined turn commitments
This commit is contained in:
1 parent
921b4fe1e3
commit
1a79fd3131
15 files changed
+1143
-78
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { TERRITORIES } from './board';
|
||||
import { BribePromiseError, parseBribeClaim } from './bribe-promises';
|
||||
import { BidPromiseError, parseBidClaim } from './bid-promises';
|
||||
import { AlliancePromiseError, parseAllianceClaim } from './alliance-promises';
|
||||
import { parseRevivalActionClaim, type RevivalActionClaim } from './mixed-revival-question';
|
||||
import { RevivalExpressionPromiseError, type RevivalExpressionReturn } from './revival-expression-promises';
|
||||
import {
|
||||
@@ -22,19 +23,21 @@ export const TURN_ACTION_MAX_LEAVES = 16;
|
||||
export const TURN_ACTION_MAX_DEPTH = 4;
|
||||
|
||||
export type TurnActionLeaf = TravelActionClaim | { kind: 'payment'; recipient: string; minimum: number } |
|
||||
{ kind: 'bid'; lot: number; minimum: number } | RevivalActionClaim;
|
||||
{ kind: 'bid'; lot: number; minimum: number } | RevivalActionClaim | { kind: 'alliance'; partner: string };
|
||||
export type TurnActionClaim = { turn: number; mixed: MixedActionExpression<TurnActionLeaf> };
|
||||
export type TurnActionPayment = { recipient: string; amount: number };
|
||||
export type TurnActionBranchLeaf = TravelBranchLeaf |
|
||||
{ kind: 'payment'; recipient: string; minimum: number; progress: number; answer: boolean } |
|
||||
{ kind: 'bid'; lot: number; source: string; minimum: number; answer: boolean } |
|
||||
{ kind: 'forces'; counter: 'total' | 'normal' | 'elite'; minimum: number; progress: number; answer: boolean } |
|
||||
{ kind: 'leader'; leader: string; answer: boolean };
|
||||
{ kind: 'leader'; leader: string; answer: boolean } |
|
||||
{ kind: 'alliance'; partner: string; source: string; answer: boolean };
|
||||
export type TurnActionPreparation = {
|
||||
bid: { lot: number; source: string; maximum: number; closed: boolean } | null;
|
||||
revival: { total: number; normal: number; elite: number; leaders: string[]; closed: boolean } | null;
|
||||
};
|
||||
export type TurnActionBid = { lot: number; source: string; amount: number };
|
||||
export type TurnActionAlliance = { source: string; partner: string | null; closed: boolean };
|
||||
export type TurnActionPromise = TurnActionClaim & {
|
||||
event: string;
|
||||
player: string;
|
||||
@@ -53,6 +56,8 @@ export type TurnActionPromise = TurnActionClaim & {
|
||||
released: boolean;
|
||||
/** Optional only for original shipment/movement/payment records. */
|
||||
preparation?: TurnActionPreparation;
|
||||
/** Final membership is recorded only by the original Nexus closing event. */
|
||||
alliance?: TurnActionAlliance;
|
||||
};
|
||||
|
||||
export class TurnActionError extends Error {
|
||||
@@ -81,8 +86,13 @@ const printedTerritory = (value: unknown): value is string =>
|
||||
|
||||
/** Public leaves carry only their native destination/recipient and minimum. */
|
||||
export function parseTurnActionLeaf(value: unknown): TurnActionLeaf {
|
||||
check(object(value), 'Choose one future ordinary bid, Revival, reserve shipment, ground movement or bribe payment.');
|
||||
check(object(value), 'Choose one future ordinary bid, Revival, final Nexus alliance, reserve shipment, ground movement or bribe payment.');
|
||||
try {
|
||||
if (value.kind === 'alliance') {
|
||||
check(keys(value, 'kind,partner'), 'Supply only one final Nexus alliance partner.');
|
||||
const claim = parseAllianceClaim({ turn: 1, partner: value.partner });
|
||||
return { kind: 'alliance', partner: claim.partner };
|
||||
}
|
||||
if (value.kind === 'bid') {
|
||||
check(keys(value, 'kind,lot,minimum'), 'Supply only the current ordinary lot and positive bid minimum.');
|
||||
const claim = parseBidClaim({ turn: 1, lot: value.lot, minimum: value.minimum });
|
||||
@@ -95,7 +105,7 @@ export function parseTurnActionLeaf(value: unknown): TurnActionLeaf {
|
||||
return { kind: 'payment', recipient: claim.recipient, minimum: claim.minimum };
|
||||
} catch (error) {
|
||||
if (error instanceof TravelPromiseError || error instanceof BribePromiseError || error instanceof BidPromiseError ||
|
||||
error instanceof RevivalExpressionPromiseError)
|
||||
error instanceof RevivalExpressionPromiseError || error instanceof AlliancePromiseError)
|
||||
throw new TurnActionError(error.message);
|
||||
throw error;
|
||||
}
|
||||
@@ -116,6 +126,7 @@ function storedFact(value: unknown, depth = 0): TruthFact {
|
||||
}
|
||||
|
||||
function actionKey(action: TurnActionLeaf): string {
|
||||
if (action.kind === 'alliance') return JSON.stringify([action.kind, action.partner]);
|
||||
if (action.kind === 'leader') return JSON.stringify([action.kind, action.leader]);
|
||||
if (action.kind === 'forces') return JSON.stringify([action.kind, action.counter, action.minimum]);
|
||||
if (action.kind === 'bid') return JSON.stringify([action.kind, action.lot, action.minimum]);
|
||||
@@ -155,9 +166,11 @@ function validateEvent(event: unknown): asserts event is TravelEvent {
|
||||
}
|
||||
|
||||
function readPromise(promise: TurnActionPromise): CompiledMixedAction<TurnActionLeaf> {
|
||||
const fields = 'answer,asker,compiled,event,fulfilled,mixed,paymentClosed,payments,player,progress,released,shipment,transportClosed,turn';
|
||||
check(object(promise) && (keys(promise, fields) || keys(promise,
|
||||
'answer,asker,compiled,event,fulfilled,mixed,paymentClosed,payments,player,preparation,progress,released,shipment,transportClosed,turn')) &&
|
||||
check(object(promise), 'Retain the complete original turn-action commitment and its metadata.');
|
||||
const fields = 'answer,asker,compiled,event,fulfilled,mixed,paymentClosed,payments,player,progress,released,shipment,transportClosed,turn'.split(',');
|
||||
if ('preparation' in promise) fields.push('preparation');
|
||||
if ('alliance' in promise) fields.push('alliance');
|
||||
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.');
|
||||
let mixed: MixedActionExpression<TurnActionLeaf>;
|
||||
@@ -174,6 +187,7 @@ function readPromise(promise: TurnActionPromise): CompiledMixedAction<TurnAction
|
||||
const recipients = new Set<string>();
|
||||
const lots = new Set<number>();
|
||||
let hasRevival = false;
|
||||
let hasAlliance = false;
|
||||
const visit = (node: MixedActionExpression<TurnActionLeaf>): void => {
|
||||
if (node.kind === 'action') {
|
||||
originals.add(actionKey(node.action));
|
||||
@@ -181,11 +195,20 @@ function readPromise(promise: TurnActionPromise): CompiledMixedAction<TurnAction
|
||||
if (node.action.kind === 'payment') recipients.add(node.action.recipient);
|
||||
if (node.action.kind === 'bid') lots.add(node.action.lot);
|
||||
if (node.action.kind === 'forces' || node.action.kind === 'leader') hasRevival = true;
|
||||
if (node.action.kind === 'alliance') hasAlliance = true;
|
||||
} else if (node.kind !== 'fact') node.terms.forEach(visit);
|
||||
};
|
||||
visit(mixed);
|
||||
check(!lots.size || !!promise.preparation?.bid, 'Retain the original private lot binding for every ordinary bid leaf.');
|
||||
check(!hasRevival || !!promise.preparation?.revival, 'Retain this answer’s ordinary Revival interval.');
|
||||
check(!hasAlliance || !!promise.alliance, 'Retain the original private Nexus binding for every final-alliance leaf.');
|
||||
if ('alliance' in promise) {
|
||||
const alliance = promise.alliance;
|
||||
check(object(alliance) && keys(alliance, 'closed,partner,source') && identifier(alliance.source) &&
|
||||
typeof alliance.closed === 'boolean' && (alliance.partner === null || identifier(alliance.partner) &&
|
||||
alliance.partner !== promise.player) && (alliance.closed || alliance.partner === null),
|
||||
'Retain only the original Nexus source and its actual final membership after closure.');
|
||||
}
|
||||
if ('preparation' in promise) {
|
||||
const preparation = promise.preparation;
|
||||
check(object(preparation) && keys(preparation, 'bid,revival'), 'Retain only the private native preparation stages.');
|
||||
@@ -234,6 +257,7 @@ function readPromise(promise: TurnActionPromise): CompiledMixedAction<TurnAction
|
||||
}
|
||||
|
||||
function actionValue(action: TurnActionLeaf, promise: TurnActionPromise): boolean {
|
||||
if (action.kind === 'alliance') return !!promise.alliance?.closed && promise.alliance.partner === action.partner;
|
||||
if (action.kind === 'bid') return (promise.preparation?.bid?.maximum ?? 0) >= action.minimum;
|
||||
if (action.kind === 'forces') return (promise.preparation?.revival?.[action.counter] ?? 0) >= action.minimum;
|
||||
if (action.kind === 'leader') return promise.preparation?.revival?.leaders.includes(action.leader) ?? false;
|
||||
@@ -252,6 +276,7 @@ function evaluate(node: CompiledMixedAction<TurnActionLeaf>, promise: TurnAction
|
||||
}
|
||||
|
||||
function actionClosed(action: TurnActionLeaf, promise: TurnActionPromise): boolean {
|
||||
if (action.kind === 'alliance') return promise.alliance!.closed;
|
||||
if (action.kind === 'bid') return promise.preparation!.bid!.closed;
|
||||
if (action.kind === 'forces' || action.kind === 'leader') return promise.preparation!.revival!.closed;
|
||||
return action.kind === 'payment' ? promise.paymentClosed : action.kind === 'ship'
|
||||
@@ -289,6 +314,7 @@ 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 === 'leader') return right.kind === 'leader' && left.leader === right.leader;
|
||||
if (left.kind === 'alliance') return right.kind === 'alliance' && left.partner === right.partner && left.source === right.source;
|
||||
if (left.kind === 'forces') return right.kind === 'forces' && left.counter === right.counter && left.progress === right.progress;
|
||||
if (left.kind === 'bid') return right.kind === 'bid' && left.lot === right.lot && left.source === right.source;
|
||||
if (left.kind === 'payment') return right.kind === 'payment' && left.recipient === right.recipient && left.progress === right.progress;
|
||||
@@ -299,18 +325,22 @@ function sameAction(left: TurnActionBranchLeaf, right: TurnActionBranchLeaf): bo
|
||||
function mergeBranches(left: TurnActionBranchLeaf[], right: TurnActionBranchLeaf[]): TurnActionBranchLeaf[] | null {
|
||||
const merged = left.map(leaf => ({ ...leaf }));
|
||||
for (const leaf of right) {
|
||||
if (leaf.kind === 'leader') {
|
||||
if (leaf.kind === 'leader' || leaf.kind === 'alliance') {
|
||||
const prior = merged.find(other => sameAction(other, leaf));
|
||||
if (prior && prior.answer !== leaf.answer) return null;
|
||||
if (leaf.kind === 'alliance' && leaf.answer && merged.some(other => other.kind === 'alliance' &&
|
||||
other.answer && other.source === leaf.source && other.partner !== leaf.partner)) return null;
|
||||
if (!prior) merged.push({ ...leaf });
|
||||
continue;
|
||||
}
|
||||
const same = merged.find(other => sameAction(other, leaf) && other.answer === leaf.answer);
|
||||
if (same && same.kind !== 'leader') same.minimum = leaf.answer ? Math.max(same.minimum, leaf.minimum) : Math.min(same.minimum, leaf.minimum);
|
||||
if (same && same.kind !== 'leader' && same.kind !== 'alliance')
|
||||
same.minimum = leaf.answer ? Math.max(same.minimum, leaf.minimum) : Math.min(same.minimum, leaf.minimum);
|
||||
else merged.push({ ...leaf });
|
||||
const yes = merged.find(other => sameAction(other, leaf) && other.answer);
|
||||
const no = merged.find(other => sameAction(other, leaf) && !other.answer);
|
||||
if (yes && no && yes.kind !== 'leader' && no.kind !== 'leader' && yes.minimum >= no.minimum) return null;
|
||||
if (yes && no && yes.kind !== 'leader' && no.kind !== 'leader' && yes.kind !== 'alliance' && no.kind !== 'alliance' &&
|
||||
yes.minimum >= no.minimum) return null;
|
||||
if (leaf.kind === 'ship' && leaf.answer && merged.some(other =>
|
||||
other.kind === 'ship' && other.answer && other.territory !== leaf.territory)) return null;
|
||||
}
|
||||
@@ -328,6 +358,7 @@ export function* turnActionBranches(promise: TurnActionPromise): Generator<TurnA
|
||||
const achieved = actionValue(action, promise);
|
||||
if (achieved || actionClosed(action, promise)) { if (achieved === answer) yield []; return; }
|
||||
if (action.kind === 'bid') yield [{ ...action, source: promise.preparation!.bid!.source, answer }];
|
||||
else if (action.kind === 'alliance') yield [{ ...action, source: promise.alliance!.source, answer }];
|
||||
else if (action.kind === 'forces') yield [{ ...action, progress: promise.preparation!.revival![action.counter], answer }];
|
||||
else if (action.kind === 'payment') yield [{ ...action, answer,
|
||||
progress: promise.payments.find(payment => payment.recipient === action.recipient)?.amount ?? 0 }];
|
||||
@@ -360,6 +391,29 @@ function settle<T extends TurnActionPromise>(promise: T, update: T): T {
|
||||
? { ...update, fulfilled: true } : update;
|
||||
}
|
||||
|
||||
/** Only the exact original Nexus closure records final membership. Temporary
|
||||
* proposals/pairs never call this hook. A newly impossible whole No releases;
|
||||
* the engine rejects own voluntary closure by inspecting that detached result. */
|
||||
export function settleTurnActionAlliance<T extends TurnActionPromise>(promises: readonly T[], turn: number,
|
||||
source: string, allies: Readonly<Record<string, string | null>>): T[] {
|
||||
check(Array.isArray(promises) && positive(turn) && identifier(source) && object(allies) &&
|
||||
Object.entries(allies).every(([player, partner]) => identifier(player) &&
|
||||
(partner === null || identifier(partner) && partner !== player)),
|
||||
'Retain the original Nexus and its actual final alliance snapshot.');
|
||||
for (const promise of promises) check(object(promise), 'Retain an original turn-action record.');
|
||||
return promises.map(promise => {
|
||||
if (promise.turn !== turn || promise.fulfilled || promise.released) return promise;
|
||||
readPromise(promise);
|
||||
const stage = promise.alliance;
|
||||
if (!stage || stage.closed || stage.source !== source) return promise;
|
||||
check(Object.prototype.hasOwnProperty.call(allies, promise.player),
|
||||
'Retain the respondent’s actual final membership at this original Nexus closure.');
|
||||
const update = { ...promise, alliance: { ...stage, partner: allies[promise.player], closed: true } };
|
||||
if (!promise.answer && turnActionValue(update)) return { ...update, released: true };
|
||||
return settle(promise, update);
|
||||
});
|
||||
}
|
||||
|
||||
/** Only an actually accepted ordinary declaration counts for its original lot.
|
||||
* Repeated declarations retain their largest amount, never their sum or invoice. */
|
||||
export function recordTurnActionBid<T extends TurnActionPromise>(promises: readonly T[], player: string,
|
||||
@@ -515,7 +569,7 @@ export function finishTurnActionTurn<T extends TurnActionPromise>(promises: read
|
||||
bid: promise.preparation.bid ? { ...promise.preparation.bid, closed: true } : null,
|
||||
revival: promise.preparation.revival ? { ...promise.preparation.revival, closed: true } : null,
|
||||
} } : {}) };
|
||||
const fulfilled = turnActionValue(closed) === promise.answer;
|
||||
const fulfilled = !(promise.alliance && !promise.alliance.closed) && turnActionValue(closed) === promise.answer;
|
||||
return { ...closed, fulfilled, released: !fulfilled };
|
||||
});
|
||||
}
|
||||
@@ -530,12 +584,15 @@ export function validateTurnActionPromises(promises: readonly TurnActionPromise[
|
||||
const satisfied = evaluate(compiled, promise);
|
||||
const allClosed = promise.transportClosed && promise.paymentClosed &&
|
||||
(!promise.preparation?.bid || promise.preparation.bid.closed) &&
|
||||
(!promise.preparation?.revival || promise.preparation.revival.closed);
|
||||
(!promise.preparation?.revival || promise.preparation.revival.closed) &&
|
||||
(!promise.alliance || promise.alliance.closed);
|
||||
const recipientsValid = (node: MixedActionExpression<TurnActionLeaf>): boolean => node.kind === 'action'
|
||||
? node.action.kind !== 'payment' || players.includes(node.action.recipient) && node.action.recipient !== 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.alliance || promise.alliance.partner === null || players.includes(promise.alliance.partner)) &&
|
||||
(promise.released || (promise.answer || !satisfied) &&
|
||||
(!promise.answer || promise.fulfilled === satisfied) &&
|
||||
(!promise.fulfilled || satisfied === promise.answer && (promise.answer || relevantClosed(compiled, promise))) &&
|
||||
|
||||
Reference in new issue
Block a user