Add enforceable current-turn travel deals
Verify and publish checkpoint container / container (push) Successful in 10m49s
Verify and publish checkpoint container / container (push) Successful in 10m49s
This commit is contained in:
1 parent
ac1f32c859
commit
82c639c977
13 files changed
+777
-28
No files matched your search
+42
-1
@@ -1,3 +1,5 @@
|
||||
import { parseTravelActionClaim, TravelPromiseError } from './travel-expression-promises';
|
||||
|
||||
/** Written terms remain player-managed. Typed payment terms use the native
|
||||
* bribe rules; older two-seat records omit participants and acceptVotes. */
|
||||
export type PublicDealPayment = {
|
||||
@@ -13,6 +15,13 @@ 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. */
|
||||
export type PublicDealTravel = {
|
||||
kind: 'ship' | 'move';
|
||||
territory: string;
|
||||
minimum: number;
|
||||
answer: boolean;
|
||||
};
|
||||
export type PublicDeal = {
|
||||
id: string;
|
||||
from: string;
|
||||
@@ -28,6 +37,7 @@ export type PublicDeal = {
|
||||
payment?: PublicDealPayment;
|
||||
/** Present only for accepted or released deferred payments. */
|
||||
paymentStatus?: PublicDealPaymentStatus;
|
||||
travel?: PublicDealTravel;
|
||||
};
|
||||
|
||||
export type PublicDealSeat = { id: string; bot?: unknown; ally?: string | null; allies?: readonly string[] };
|
||||
@@ -102,6 +112,19 @@ 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. */
|
||||
export function parsePublicDealTravel(value: unknown): PublicDealTravel {
|
||||
requireDeal(object(value) && Object.keys(value).sort().join(',') === 'answer,kind,minimum,territory' &&
|
||||
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 };
|
||||
} catch (error) {
|
||||
if (error instanceof TravelPromiseError) throw new PublicDealError(error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/** Validate persisted ledger shape and lifecycle without applying current
|
||||
* alliance restrictions retroactively to existing agreements. */
|
||||
export function validatePublicDeals(value: unknown, players: readonly PublicDealSeat[]): asserts value is PublicDeal[] {
|
||||
@@ -109,7 +132,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').sort().join(',') : '';
|
||||
const keys = object(deal) ? Object.keys(deal).filter(key => key !== 'payment' && key !== 'paymentStatus' && key !== 'travel').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 &&
|
||||
@@ -138,6 +161,11 @@ export function validatePublicDeals(value: unknown, players: readonly PublicDeal
|
||||
parties = [deal.from, deal.to];
|
||||
participants(players, parties);
|
||||
}
|
||||
if (Object.hasOwn(deal, 'travel')) {
|
||||
requireDeal(!multi && !Object.hasOwn(deal, 'payment') && !Object.hasOwn(deal, 'paymentStatus'),
|
||||
'A travel term requires a two-party deal without a payment term.');
|
||||
parsePublicDealTravel(deal.travel);
|
||||
}
|
||||
const payment = Object.prototype.hasOwnProperty.call(deal, 'payment')
|
||||
? parsePublicDealPayment(deal.payment, parties, deal.turn as number) : undefined;
|
||||
if (payment?.kind === 'deferredBribe') {
|
||||
@@ -189,6 +217,19 @@ export function createMultipartyPublicDeal(
|
||||
...paymentTerm };
|
||||
}
|
||||
|
||||
/** Attach a travel term while constructing a fresh two-party 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, 'payment') && !Object.hasOwn(deal, 'paymentStatus'),
|
||||
'Only a fresh two-party offer without payment can add a travel term.');
|
||||
notAllied(participants(players, [deal.from, deal.to]));
|
||||
const result = { ...deal, travel: parsePublicDealTravel(value) };
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user