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
+54
-3
@@ -24,6 +24,7 @@ export type PublicDealAdditionalPayment = {
|
||||
};
|
||||
export const MAX_ADDITIONAL_DEAL_PAYMENTS = 12;
|
||||
export const MAX_ADDITIONAL_IMMEDIATE_DEAL_PAYMENTS = 12;
|
||||
export const MAX_ADDITIONAL_DEAL_TRAVEL = 12;
|
||||
/** One participant commits to one native action (or its absence).
|
||||
* Old two-party records identify the recipient through `to`. */
|
||||
export type PublicDealTravel = {
|
||||
@@ -56,6 +57,9 @@ export type PublicDeal = {
|
||||
/** Immutable additional transfers, executed under final acceptance. */
|
||||
additionalImmediatePayments?: PublicDealImmediatePayment[];
|
||||
travel?: PublicDealTravel;
|
||||
/** Immutable additional actions; each actor is explicit and the array index
|
||||
* is its stable identity within the offer. */
|
||||
additionalTravel?: PublicDealTravel[];
|
||||
};
|
||||
|
||||
export type PublicDealSeat = { id: string; bot?: unknown; ally?: string | null; allies?: readonly string[] };
|
||||
@@ -123,6 +127,11 @@ export function publicDealTravelActor(deal: PublicDeal): string {
|
||||
export function publicDealTravelDueTurn(deal: PublicDeal): number {
|
||||
return deal.travel?.dueTurn ?? deal.turn;
|
||||
}
|
||||
/** Enumerate the original and indexed travel terms without rewriting saves. */
|
||||
export function publicDealTravelTerms(deal: PublicDeal): Array<{travel: PublicDealTravel; index?: number}> {
|
||||
return [...(deal.travel ? [{travel: deal.travel}] : []),
|
||||
...(deal.additionalTravel ?? []).map((travel,index) => ({travel,index}))];
|
||||
}
|
||||
function orderedSubset(votes: readonly string[], parties: readonly string[]): boolean {
|
||||
return new Set(votes).size === votes.length &&
|
||||
votes.every(vote => parties.includes(vote)) &&
|
||||
@@ -178,7 +187,7 @@ export function validatePublicDeals(value: unknown, players: readonly PublicDeal
|
||||
requireDeal(Array.isArray(value), 'The public deal ledger is malformed.');
|
||||
const ids = new Set<string>();
|
||||
for (const deal of value) {
|
||||
const keys = object(deal) ? Object.keys(deal).filter(key => key !== 'payment' && key !== 'paymentStatus' && key !== 'travel' && key !== 'additionalPayments' && key !== 'additionalImmediatePayments').sort().join(',') : '';
|
||||
const keys = object(deal) ? Object.keys(deal).filter(key => key !== 'payment' && key !== 'paymentStatus' && key !== 'travel' && key !== 'additionalTravel' && key !== 'additionalPayments' && key !== 'additionalImmediatePayments').sort().join(',') : '';
|
||||
const multi = keys === 'acceptVotes,from,id,participants,releaseVotes,status,terms,to,turn';
|
||||
requireDeal(object(deal) && (multi || keys === 'from,id,releaseVotes,status,terms,to,turn') &&
|
||||
id(deal.id) && id(deal.from) && id(deal.to) && deal.from !== deal.to &&
|
||||
@@ -218,10 +227,29 @@ export function validatePublicDeals(value: unknown, players: readonly PublicDeal
|
||||
'Choose a deal participant as traveler and an optional bribe between participants.');
|
||||
// An invited actor's final acceptance is checked privately. The proposer
|
||||
// has already consented by making the offer and remains in its votes.
|
||||
if (multi && travel.actor !== deal.from && deal.status !== 'active' && deal.status !== 'released')
|
||||
if (multi && !Object.hasOwn(deal, 'additionalTravel') && travel.actor !== deal.from && deal.status !== 'active' && deal.status !== 'released')
|
||||
requireDeal(!(deal.acceptVotes as string[]).includes(travel.actor!),
|
||||
'The committed traveler accepts after all other invited participants.');
|
||||
}
|
||||
if (Object.hasOwn(deal, 'additionalTravel')) {
|
||||
requireDeal(Object.hasOwn(deal, 'travel') && Array.isArray(deal.additionalTravel) &&
|
||||
deal.additionalTravel.length >= 1 && deal.additionalTravel.length <= MAX_ADDITIONAL_DEAL_TRAVEL,
|
||||
'Choose one original and between one and twelve additional travel terms.');
|
||||
const travelTerms = deal.additionalTravel.map(value => parsePublicDealTravel(value));
|
||||
requireDeal(travelTerms.every(term => !!term.actor && parties.includes(term.actor) &&
|
||||
(term.dueTurn === undefined || term.dueTurn >= (deal.turn as number))),
|
||||
'Choose a participant and unexpired turn for each additional travel term.');
|
||||
if (multi) {
|
||||
const original = parsePublicDealTravel(deal.travel);
|
||||
const travelers = new Set([original.actor!, ...travelTerms.map(term => term.actor!)]);
|
||||
const accepts = deal.acceptVotes as string[];
|
||||
// The proposer consented by offering. If another traveler has voted,
|
||||
// all invited non-travelers must already have consented.
|
||||
requireDeal(!accepts.some(seat => seat !== deal.from && travelers.has(seat)) ||
|
||||
parties.every(seat => seat === deal.from || travelers.has(seat) || accepts.includes(seat)),
|
||||
'Invited non-travelers must accept before committed travelers.');
|
||||
}
|
||||
}
|
||||
if (payment?.kind === 'deferredBribe') {
|
||||
const status = deal.paymentStatus;
|
||||
requireDeal(deal.status === 'active' ? status === 'pending' || status === 'paid' || status === 'void' :
|
||||
@@ -356,6 +384,24 @@ export function setPublicDealTravel(deal: PublicDeal, value: unknown, players: r
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Attach further explicit-actor travel terms to a fresh original offer. */
|
||||
export function setPublicDealAdditionalTravel(deal: PublicDeal, value: unknown, players: readonly PublicDealSeat[]): PublicDeal {
|
||||
validatePublicDeals([deal], players);
|
||||
requireDeal(deal.status === 'offered' && !!deal.travel && !Object.hasOwn(deal, 'additionalTravel') &&
|
||||
!Object.hasOwn(deal, 'paymentStatus') && (deal.participants
|
||||
? !!deal.acceptVotes && deal.acceptVotes.length === 1 && deal.acceptVotes[0] === deal.from
|
||||
: !Object.hasOwn(deal, 'acceptVotes')),
|
||||
'Only a fresh offer with an original travel term may add travel terms.');
|
||||
requireDeal(Array.isArray(value) && value.length >= 1 && value.length <= MAX_ADDITIONAL_DEAL_TRAVEL,
|
||||
'Choose between one and twelve additional travel terms.');
|
||||
const parties = publicDealParticipants(deal);
|
||||
notAllied(participants(players, parties));
|
||||
const additionalTravel = value.map(value => parsePublicDealTravel(value));
|
||||
const result = {...deal, additionalTravel};
|
||||
validatePublicDeals([result], players);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function respondPublicDeal(deal: PublicDeal, players: readonly PublicDealSeat[], actor: string, accept: boolean): PublicDeal {
|
||||
validatePublicDeals([deal], players);
|
||||
const parties = publicDealParticipants(deal);
|
||||
@@ -368,7 +414,12 @@ export function respondPublicDeal(deal: PublicDeal, players: readonly PublicDeal
|
||||
...(deal.payment?.kind === 'deferredBribe' ? { paymentStatus: 'pending' as const } : {}) };
|
||||
const alreadyAccepted = publicDealAcceptVotes(deal);
|
||||
requireDeal(!alreadyAccepted.includes(actor), 'This participant already accepted the offer.');
|
||||
if (deal.travel && actor === publicDealTravelActor(deal))
|
||||
if (deal.travel && deal.additionalTravel) {
|
||||
const travelers = new Set(publicDealTravelTerms(deal).map(({travel}) => travel.actor ?? deal.to));
|
||||
if (travelers.has(actor))
|
||||
requireDeal(parties.every(seat => seat === deal.from || travelers.has(seat) || alreadyAccepted.includes(seat)),
|
||||
'Invited non-travelers must accept before committed travelers.');
|
||||
} else if (deal.travel && actor === publicDealTravelActor(deal))
|
||||
requireDeal(parties.every(seat => seat === actor || alreadyAccepted.includes(seat)),
|
||||
'The committed traveler accepts after all other invited participants.');
|
||||
const accepts = parties.filter(seat => seat === actor || alreadyAccepted.includes(seat));
|
||||
|
||||
Reference in new issue
Block a user