Support multiple travel terms in public deals
Verify and publish checkpoint container / container (push) Canceled after 0s
Verify and publish checkpoint container / container (push) Canceled after 0s
This commit is contained in:
1 parent
13c337a24a
commit
d7d0f89625
13 files changed
+790
-51
No files matched your search
+55
-28
@@ -11,8 +11,9 @@ import { createFactionSwap, validateFactionSwap, answerFactionSwap, cancelFactio
|
||||
FactionSwapError, type FactionSwap } from './faction-swap';
|
||||
import { createPublicDeal, createMultipartyPublicDeal, validatePublicDeals, respondPublicDeal, withdrawPublicDeal,
|
||||
releasePublicDeal, markPublicDealPaid, voidPublicDealPayment, setPublicDealTravel, setPublicDealAdditionalPayments,
|
||||
setPublicDealAdditionalImmediatePayments, publicDealDeferredTerms, publicDealTravelActor, publicDealTravelDueTurn,
|
||||
PublicDealError, type PublicDeal, type PublicDealPayment, type PublicDealImmediatePayment } from './public-deals';
|
||||
setPublicDealAdditionalImmediatePayments, setPublicDealAdditionalTravel, publicDealTravelTerms,
|
||||
publicDealDeferredTerms, publicDealTravelActor,
|
||||
PublicDealError, type PublicDeal, type PublicDealTravel, type PublicDealPayment, type PublicDealImmediatePayment } from './public-deals';
|
||||
import { outstandingPublicDealPayments, pendingPublicDealPayments, publicDealPaymentTotal, publicDealPaymentOptionKey, PublicDealObligationError, type PendingPublicDealPayment } from './public-deal-obligations';
|
||||
import { CharityPromiseError, liveCharityPromises, charityPromiseAnswers, bindCharityPromise, guardCharityClaim, guardCharityReady, reconcileCharityPromises, type CharityPromise } from './charity-promises';
|
||||
import { victoryFinalTurn, continueVictoryCalendar, VictoryCalendarError } from './victory-calendar';
|
||||
@@ -6062,15 +6063,16 @@ function publicDealImmediateLegs(deal: PublicDeal): PublicDealImmediatePayment[]
|
||||
/** Bind receipts to immutable terms, not release votes or later deferred status. */
|
||||
function publicDealImmediateSignature(deal: PublicDeal): string {
|
||||
return JSON.stringify([deal.id,deal.turn,deal.from,deal.to,deal.participants,deal.terms,
|
||||
deal.payment,deal.additionalImmediatePayments,deal.additionalPayments?.map(term => term.payment),deal.travel]);
|
||||
deal.payment,deal.additionalImmediatePayments,deal.additionalPayments?.map(term => term.payment),deal.travel,
|
||||
...(deal.additionalTravel ? [deal.additionalTravel] : [])]);
|
||||
}
|
||||
function publicDealSettlementSignature(deal: PublicDeal, frame: NonNullable<Game['pendingPublicDealSettlement']>): string {
|
||||
return JSON.stringify([publicDealImmediateSignature(deal),frame.next,frame.initiator,frame.turn,frame.phase]);
|
||||
}
|
||||
/** Travel ledgers need another participant as the originating party. Older
|
||||
* recipient-travel deals continue to identify their original proposer. */
|
||||
function publicDealTravelAsker(deal: PublicDeal): string {
|
||||
return publicDealTravelActor(deal) === deal.from ? deal.to : deal.from;
|
||||
function publicDealTravelAsker(deal: PublicDeal, actor = publicDealTravelActor(deal)): string {
|
||||
return actor === deal.from ? deal.to : deal.from;
|
||||
}
|
||||
function publicDealIntegrity(g: Game, internalSettlement?: string): void {
|
||||
if (g.deals !== undefined) publicDealRule(() => validatePublicDeals(g.deals, g.players));
|
||||
@@ -6106,19 +6108,22 @@ function publicDealIntegrity(g: Game, internalSettlement?: string): void {
|
||||
const linked = (g.travelPromises ?? []).filter(row => row.sourceDeal !== undefined);
|
||||
for (const row of linked) {
|
||||
const deal = g.deals?.find(entry => entry.id === row.sourceDeal);
|
||||
const term = deal?.travel;
|
||||
const term = row.sourceDealIndex === undefined ? deal?.travel : deal?.additionalTravel?.[row.sourceDealIndex];
|
||||
const actor = term?.actor ?? deal?.to ?? '';
|
||||
requireRule(deal && term && ['active', 'released'].includes(deal.status) &&
|
||||
row.player === publicDealTravelActor(deal) && row.asker === publicDealTravelAsker(deal) && row.turn === publicDealTravelDueTurn(deal) &&
|
||||
row.player === actor && row.asker === publicDealTravelAsker(deal,actor) && row.turn === (term.dueTurn ?? deal.turn) &&
|
||||
row.answer === term.answer && row.mixed.kind === 'action' &&
|
||||
row.mixed.action.kind === term.kind && row.mixed.action.territory === term.territory &&
|
||||
row.mixed.action.minimum === term.minimum && row.mixed.action.sector === undefined &&
|
||||
(deal.status !== 'released' || row.fulfilled || row.released) &&
|
||||
(!row.releasedByDeal || deal.status === 'released') &&
|
||||
linked.filter(other => other.sourceDeal === deal.id).length === 1,
|
||||
linked.filter(other => other.sourceDeal === deal.id && other.sourceDealIndex === row.sourceDealIndex).length === 1,
|
||||
'The public travel deal lost its original accepted commitment.');
|
||||
}
|
||||
for (const deal of g.deals ?? []) if (deal.travel && ['active', 'released'].includes(deal.status))
|
||||
requireRule(linked.some(row => row.sourceDeal === deal.id), 'The accepted public travel deal is missing its commitment.');
|
||||
for (const {index} of publicDealTravelTerms(deal))
|
||||
requireRule(linked.some(row => row.sourceDeal === deal.id && row.sourceDealIndex === index),
|
||||
'The accepted public travel deal is missing its commitment.');
|
||||
}
|
||||
/** Exercise the actual native payments on detached states before consent.
|
||||
* Passing Bureaucrat can leave a later payment eligible, so prove both native
|
||||
@@ -24350,7 +24355,7 @@ function travelTargetAvailable(g: Game, actor: string): boolean {
|
||||
/** A later-turn deal has no resource witness when it is first accepted. */
|
||||
function isFutureDealTravel(g: Game, promise: TravelPromise): boolean {
|
||||
const deal = promise.sourceDeal ? g.deals?.find(entry => entry.id === promise.sourceDeal) : undefined;
|
||||
return !!deal?.travel && publicDealTravelDueTurn(deal) > deal.turn;
|
||||
return !!deal?.travel && promise.turn > deal.turn;
|
||||
}
|
||||
function appendTravelOrder(g: Game, kind: NonNullable<Game['travelPromiseOrder']>[number]['kind'], index: number) {
|
||||
g.travelPromiseOrder?.push({ kind, index });
|
||||
@@ -24440,9 +24445,9 @@ function bindTravelTruth(g: Game, p: Player, claim: TravelClaim, answer: boolean
|
||||
}
|
||||
/** A deal is a separately consented public source for the same physical
|
||||
* travel constraint, not a Truthtrance question or a fabricated card play. */
|
||||
function bindPublicDealTravel(g: Game, deal: PublicDeal) {
|
||||
const term = deal.travel, player = getPlayer(g, publicDealTravelActor(deal));
|
||||
const due = publicDealTravelDueTurn(deal);
|
||||
function bindPublicDealTravel(g: Game, deal: PublicDeal, term: PublicDealTravel, index?: number) {
|
||||
const player = getPlayer(g, term.actor ?? deal.to);
|
||||
const due = term.dueTurn ?? deal.turn;
|
||||
requireRule(term && due >= g.turn && !g.advanced && g.expansions.length === 0 && nativePromiseScopeSupported(g),
|
||||
'This travel agreement needs a supported Basic table and an unexpired turn.');
|
||||
const action = { kind: term.kind, territory: term.territory, minimum: term.minimum };
|
||||
@@ -24472,7 +24477,8 @@ function bindPublicDealTravel(g: Game, deal: PublicDeal) {
|
||||
}
|
||||
initializeTravelOrder(g);
|
||||
(g.travelPromises ??= []).push({ ...claim, event: crypto.randomUUID(), sourceDeal: deal.id,
|
||||
player: player.id, asker: publicDealTravelAsker(deal), answer: term.answer, compiled: { kind: 'action', action },
|
||||
...(index === undefined ? {} : {sourceDealIndex:index}),
|
||||
player: player.id, asker: publicDealTravelAsker(deal,player.id), answer: term.answer, compiled: { kind: 'action', action },
|
||||
shipment: { pending: due > g.turn || g.phase < 5 || shipmentAvailable(g, player), event: null },
|
||||
progress: [], movementClosed: false, fulfilled: false, released: false });
|
||||
appendTravelOrder(g, 'travel', g.travelPromises.length - 1);
|
||||
@@ -24566,19 +24572,35 @@ function projectedTravelPromises(g: Game, actor: string) {
|
||||
event: promise.event, player: promise.player, asker: promise.asker, turn: promise.turn, mixed: structuredClone(promise.mixed),
|
||||
answer: promise.answer, fulfilled: promise.fulfilled, released: promise.released,
|
||||
...(promise.sourceDeal ? {sourceDeal: promise.sourceDeal} : {}),
|
||||
...(promise.sourceDealIndex === undefined ? {} : {sourceDealIndex:promise.sourceDealIndex}),
|
||||
}));
|
||||
}
|
||||
function projectedDealTravelStatus(g: Game): Record<string, 'scheduled' | 'pending' | 'fulfilled' | 'void' | 'released'> {
|
||||
const status: Record<string, 'scheduled' | 'pending' | 'fulfilled' | 'void' | 'released'> = {};
|
||||
for (const deal of g.deals ?? []) {
|
||||
if (!deal.travel || !['active', 'released'].includes(deal.status)) continue;
|
||||
const receipt = g.travelPromises?.find(row => row.sourceDeal === deal.id);
|
||||
const receipt = g.travelPromises?.find(row => row.sourceDeal === deal.id && row.sourceDealIndex === undefined);
|
||||
if (!receipt) continue; // Integrity validation reports malformed saved records.
|
||||
status[deal.id] = receipt.fulfilled ? 'fulfilled' : receipt.releasedByDeal ? 'released'
|
||||
: receipt.released ? 'void' : receipt.turn > g.turn ? 'scheduled' : 'pending';
|
||||
}
|
||||
return status;
|
||||
}
|
||||
function projectedAdditionalDealTravelStatus(g: Game) {
|
||||
type Status = 'scheduled' | 'pending' | 'fulfilled' | 'void' | 'released';
|
||||
const status: Record<string, Status[]> = {};
|
||||
for (const deal of g.deals ?? []) {
|
||||
if (!deal.additionalTravel || !['active','released'].includes(deal.status)) continue;
|
||||
status[deal.id] = deal.additionalTravel.map((_term,index) => {
|
||||
const row = g.travelPromises?.find(promise => promise.sourceDeal === deal.id && promise.sourceDealIndex === index);
|
||||
// Integrity validation reports a missing receipt before this projection.
|
||||
if (!row) throw new RuleError('The accepted public travel deal is missing its commitment.');
|
||||
return row.fulfilled ? 'fulfilled' : row.releasedByDeal ? 'released' : row.released ? 'void'
|
||||
: row.turn > g.turn ? 'scheduled' : 'pending';
|
||||
});
|
||||
}
|
||||
return status;
|
||||
}
|
||||
function projectedTravelGuide(g: Game, player: Player): { event: string; action: Action } | null {
|
||||
const promise = liveTravelPromises(g.travelPromises ?? [], player.id, g.turn)[0];
|
||||
const lot = promise && g.phase === 3 && !g.decision ? bidOpportunity(g) : null;
|
||||
@@ -43843,7 +43865,7 @@ function applyActionWithInitiator(state: Game, id: string, action: Action, initi
|
||||
const deals = g.deals ?? [];
|
||||
if (action.type === 'dealOffer') {
|
||||
const keys = Object.keys(action);
|
||||
const supportedKeys = keys.every(key => ['type','terms','target','targets','payment','additionalPayments','additionalImmediatePayments','travel'].includes(key));
|
||||
const supportedKeys = keys.every(key => ['type','terms','target','targets','payment','additionalPayments','additionalImmediatePayments','travel','additionalTravel'].includes(key));
|
||||
const single = supportedKeys && !Object.hasOwn(action,'targets') && typeof action.target === 'string';
|
||||
const multiple = supportedKeys && !Object.hasOwn(action,'target') && Array.isArray(action.targets) &&
|
||||
action.targets.every(target => typeof target === 'string');
|
||||
@@ -43870,6 +43892,8 @@ function applyActionWithInitiator(state: Game, id: string, action: Action, initi
|
||||
'Choose a travel agreement in a supported Basic table.');
|
||||
offer = publicDealRule(() => setPublicDealTravel(offer, action.travel, seats));
|
||||
}
|
||||
if (Object.hasOwn(action, 'additionalTravel'))
|
||||
offer = publicDealRule(() => setPublicDealAdditionalTravel(offer, action.additionalTravel, seats));
|
||||
requireRule(!deals.some(deal => deal.id === offer.id), 'This deal identifier is already in use.');
|
||||
g.deals = [...deals, offer];
|
||||
} else {
|
||||
@@ -43883,7 +43907,7 @@ function applyActionWithInitiator(state: Game, id: string, action: Action, initi
|
||||
let next: PublicDeal;
|
||||
if (action.type === 'dealRespond') {
|
||||
requireRule(typeof action.accept === 'boolean', 'Choose whether to accept or decline.');
|
||||
requireRule(!action.accept || !current.travel || publicDealTravelDueTurn(current) >= g.turn,
|
||||
requireRule(!action.accept || publicDealTravelTerms(current).every(({travel}) => (travel.dueTurn ?? current.turn) >= g.turn),
|
||||
'The travel agreement’s due turn has passed; decline or withdraw it.');
|
||||
requireRule(!action.accept || publicDealDeferredTerms(current).every(term => term.payment.dueTurn >= g.turn),
|
||||
'The deferred payment deadline has passed; decline or withdraw this offer.');
|
||||
@@ -43893,23 +43917,25 @@ function applyActionWithInitiator(state: Game, id: string, action: Action, initi
|
||||
next = publicDealRule(() => releasePublicDeal(current, g.players, id, action.agree as boolean));
|
||||
} else next = publicDealRule(() => withdrawPublicDeal(current, g.players, id));
|
||||
if (current.status === 'offered' && next.status === 'active' && next.travel) {
|
||||
requireRule(publicDealTravelActor(next) === next.from || id === publicDealTravelActor(next),
|
||||
requireRule(!!next.additionalTravel || publicDealTravelActor(next) === next.from || id === publicDealTravelActor(next),
|
||||
'The traveler must give the final consent.');
|
||||
bindPublicDealTravel(g, next);
|
||||
for (const {travel,index} of publicDealTravelTerms(next)) bindPublicDealTravel(g, next, travel, index);
|
||||
}
|
||||
if (current.status === 'active' && next.status === 'released' && next.travel) {
|
||||
const receipt = g.travelPromises?.find(row => row.sourceDeal === next.id);
|
||||
requireRule(receipt, 'The public travel deal lost its accepted commitment.');
|
||||
if (!receipt.fulfilled && !receipt.released) {
|
||||
receipt.released = true;
|
||||
receipt.releasedByDeal = true;
|
||||
for (const {index} of publicDealTravelTerms(next)) {
|
||||
const receipt = g.travelPromises?.find(row => row.sourceDeal === next.id && row.sourceDealIndex === index);
|
||||
requireRule(receipt, 'The public travel deal lost its accepted commitment.');
|
||||
if (!receipt.fulfilled && !receipt.released) {
|
||||
receipt.released = true;
|
||||
receipt.releasedByDeal = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
g.deals = deals.map((deal, position) => position === index ? next : deal);
|
||||
if (current.status === 'offered' && next.status === 'active' && next.travel &&
|
||||
publicDealTravelDueTurn(next) === g.turn) {
|
||||
const traveler = getPlayer(g,publicDealTravelActor(next));
|
||||
requireRule(publicDealCompletion(g,traveler),
|
||||
if (current.status === 'offered' && next.status === 'active' && next.travel) {
|
||||
const actors = new Set(publicDealTravelTerms(next).filter(({travel}) => (travel.dueTurn ?? next.turn) === g.turn)
|
||||
.map(({travel}) => travel.actor ?? next.to));
|
||||
for (const actor of actors) requireRule(publicDealCompletion(g,getPlayer(g,actor)),
|
||||
'The traveler needs a funded completion for the travel and their payments due this turn.');
|
||||
}
|
||||
if (current.status === 'offered' && next.status === 'active') for (const term of publicDealDeferredTerms(next)) {
|
||||
@@ -49675,6 +49701,7 @@ export function viewGame(state: Game, id: string, now?: number) {
|
||||
status: g.status,
|
||||
deals: g.deals ?? [],
|
||||
dealTravelStatus: projectedDealTravelStatus(g),
|
||||
dealAdditionalTravelStatus: projectedAdditionalDealTravelStatus(g),
|
||||
deferredDealPaymentOptions: projectedDeferredDealPaymentOptions(g,id),
|
||||
factionDraft: g.factionDraft ?? null,
|
||||
factionSwap: g.factionSwap ?? null,
|
||||
|
||||
Reference in new issue
Block a user