Support group travel agreements
Verify and publish checkpoint container / container (push) Successful in 11m13s
Verify and publish checkpoint container / container (push) Successful in 11m13s
This commit is contained in:
1 parent
82d857b667
commit
e1e02fc1a1
10 files changed
+432
-56
No files matched your search
+36
-15
@@ -15,12 +15,15 @@ export type PublicDealPayment = {
|
||||
dueTurn: number;
|
||||
};
|
||||
export type PublicDealPaymentStatus = 'pending' | 'paid' | 'void' | 'released';
|
||||
/** The recipient commits to one native action (or its absence) during the offer turn. */
|
||||
/** One invited recipient commits to one native action (or its absence).
|
||||
* Old two-party records identify that recipient through `to`. */
|
||||
export type PublicDealTravel = {
|
||||
kind: 'ship' | 'move';
|
||||
territory: string;
|
||||
minimum: number;
|
||||
answer: boolean;
|
||||
/** Present only in a multi-party term; the selected recipient accepts last. */
|
||||
actor?: string;
|
||||
};
|
||||
export type PublicDeal = {
|
||||
id: string;
|
||||
@@ -86,6 +89,10 @@ export function publicDealAcceptVotes(deal: PublicDeal): string[] {
|
||||
return deal.status === 'active' || deal.status === 'released'
|
||||
? [deal.from, deal.to] : [deal.from];
|
||||
}
|
||||
/** Call after ledger validation; legacy two-party terms need no new field. */
|
||||
export function publicDealTravelActor(deal: PublicDeal): string {
|
||||
return deal.travel?.actor ?? deal.to;
|
||||
}
|
||||
function orderedSubset(votes: readonly string[], parties: readonly string[]): boolean {
|
||||
return new Set(votes).size === votes.length &&
|
||||
votes.every(vote => parties.includes(vote)) &&
|
||||
@@ -112,13 +119,16 @@ export function parsePublicDealPayment(value: unknown, parties: readonly string[
|
||||
amount: value.amount, dueTurn: value.dueTurn };
|
||||
}
|
||||
|
||||
/** Parse only the exact two-seat deal form, without a sector or an extra deadline. */
|
||||
/** Parse an exact travel claim; the multi-party actor is validated against the deal. */
|
||||
export function parsePublicDealTravel(value: unknown): PublicDealTravel {
|
||||
requireDeal(object(value) && Object.keys(value).sort().join(',') === 'answer,kind,minimum,territory' &&
|
||||
const keys = object(value) ? Object.keys(value).sort().join(',') : '';
|
||||
requireDeal(object(value) && (keys === 'answer,kind,minimum,territory' ||
|
||||
keys === 'actor,answer,kind,minimum,territory' && id(value.actor)) &&
|
||||
typeof value.answer === 'boolean', 'Choose an exact shipment or movement term and require or avoid it.');
|
||||
try {
|
||||
const claim = parseTravelActionClaim({ kind: value.kind, territory: value.territory, minimum: value.minimum });
|
||||
return { kind: claim.kind, territory: claim.territory, minimum: claim.minimum, answer: value.answer };
|
||||
return { kind: claim.kind, territory: claim.territory, minimum: claim.minimum, answer: value.answer,
|
||||
...(keys.startsWith('actor,') ? {actor: value.actor as string} : {}) };
|
||||
} catch (error) {
|
||||
if (error instanceof TravelPromiseError) throw new PublicDealError(error.message);
|
||||
throw error;
|
||||
@@ -164,10 +174,17 @@ export function validatePublicDeals(value: unknown, players: readonly PublicDeal
|
||||
const payment = Object.prototype.hasOwnProperty.call(deal, 'payment')
|
||||
? parsePublicDealPayment(deal.payment, parties, deal.turn as number) : undefined;
|
||||
if (Object.hasOwn(deal, 'travel')) {
|
||||
requireDeal(!multi && !Object.hasOwn(deal, 'paymentStatus') &&
|
||||
(!payment || payment.kind === 'immediateBribe' && payment.payer === deal.from && payment.recipient === deal.to),
|
||||
'A travel term requires a two-party deal with no payment or an immediate bribe from the proposer to the recipient.');
|
||||
parsePublicDealTravel(deal.travel);
|
||||
const travel = parsePublicDealTravel(deal.travel);
|
||||
requireDeal(!Object.hasOwn(deal, 'paymentStatus') && (multi
|
||||
? !!travel.actor && travel.actor !== deal.from && parties.includes(travel.actor) && !payment
|
||||
: !Object.hasOwn(travel, 'actor') && (!payment || payment.kind === 'immediateBribe' &&
|
||||
payment.payer === deal.from && payment.recipient === deal.to)),
|
||||
'Choose an invited traveler; payment is available only for a two-party proposer-to-recipient immediate bribe.');
|
||||
// The actor's final acceptance is checked privately. An unfinished offer
|
||||
// cannot already contain that vote, even after a decline or withdrawal.
|
||||
if (multi && deal.status !== 'active' && deal.status !== 'released')
|
||||
requireDeal(!(deal.acceptVotes as string[]).includes(travel.actor!),
|
||||
'The committed traveler accepts after all other invited participants.');
|
||||
}
|
||||
if (payment?.kind === 'deferredBribe') {
|
||||
const status = deal.paymentStatus;
|
||||
@@ -218,15 +235,16 @@ export function createMultipartyPublicDeal(
|
||||
...paymentTerm };
|
||||
}
|
||||
|
||||
/** Attach a travel term while constructing a fresh two-party offer. */
|
||||
/** Attach a travel term while constructing a fresh offer. */
|
||||
export function setPublicDealTravel(deal: PublicDeal, value: unknown, players: readonly PublicDealSeat[]): PublicDeal {
|
||||
validatePublicDeals([deal], players);
|
||||
requireDeal(deal.status === 'offered' && !Object.hasOwn(deal, 'participants') &&
|
||||
!Object.hasOwn(deal, 'acceptVotes') && !Object.hasOwn(deal, 'travel') &&
|
||||
!Object.hasOwn(deal, 'paymentStatus') && (!deal.payment ||
|
||||
deal.payment.kind === 'immediateBribe' && deal.payment.payer === deal.from && deal.payment.recipient === deal.to),
|
||||
'Only a fresh two-party offer with no payment or an immediate proposer-to-recipient bribe can add a travel term.');
|
||||
notAllied(participants(players, [deal.from, deal.to]));
|
||||
requireDeal(deal.status === 'offered' && !Object.hasOwn(deal, 'travel') &&
|
||||
!Object.hasOwn(deal, 'paymentStatus') && (deal.participants
|
||||
? !!deal.acceptVotes && deal.acceptVotes.length === 1 && deal.acceptVotes[0] === deal.from && !deal.payment
|
||||
: !Object.hasOwn(deal, 'acceptVotes') && (!deal.payment ||
|
||||
deal.payment.kind === 'immediateBribe' && deal.payment.payer === deal.from && deal.payment.recipient === deal.to)),
|
||||
'Only a fresh offer may add a travel term with a supported payment.');
|
||||
notAllied(participants(players, publicDealParticipants(deal)));
|
||||
const result = { ...deal, travel: parsePublicDealTravel(value) };
|
||||
validatePublicDeals([result], players);
|
||||
return result;
|
||||
@@ -243,6 +261,9 @@ 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))
|
||||
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));
|
||||
const active = accepts.length === parties.length;
|
||||
return { ...deal, acceptVotes: accepts, status: active ? 'active' : 'offered', releaseVotes: [],
|
||||
|
||||
Reference in new issue
Block a user