100 lines
4.8 KiB
TypeScript
100 lines
4.8 KiB
TypeScript
import {
|
|
EarlyRicheseBidError,
|
|
liveEarlyRicheseBidPromises,
|
|
type EarlyRicheseBidPromise,
|
|
} from './early-richese-bid';
|
|
import {
|
|
livePhaseBidPromises,
|
|
phaseBidCompletionRange,
|
|
type PhaseBidOpportunity,
|
|
type PhaseBidPromise,
|
|
type PhaseBidRange,
|
|
} from './turn-bid-promises';
|
|
|
|
/** Only facts established by the engine are supplied here: the physical cache
|
|
* must still be pending, and a separate ordinary lot must have its own native
|
|
* reachability and bid quote. No rival bid, refund or future income is assumed. */
|
|
export type JointEarlyRicheseBidInput = {
|
|
player: string;
|
|
turn: number;
|
|
early: readonly EarlyRicheseBidPromise[];
|
|
phase: readonly PhaseBidPromise[];
|
|
/** Current owned, uncommitted Spice available for a future cache bid.
|
|
* Zero also represents an unavailable cache or insufficient hand space. */
|
|
cacheMaximum: number;
|
|
/** Present only when the engine proves a separate ordinary opportunity.
|
|
* Its cash and aid are available before the hypothetical cache purchase;
|
|
* freeSlots excludes existing pending invoices and their physical cards. */
|
|
ordinary?: {
|
|
opportunity: PhaseBidOpportunity;
|
|
cashAvailable: number;
|
|
aidAvailable: number;
|
|
freeSlots: number;
|
|
/** Slots occupied by a potentially won cache card (including bonuses). */
|
|
cacheWinSlots: number;
|
|
};
|
|
};
|
|
|
|
export type JointEarlyRicheseBidQuote = {
|
|
earlyRequired: boolean;
|
|
phaseRequired: boolean;
|
|
/** A positive cache declaration satisfying early Yes and both sets of No. */
|
|
cache: PhaseBidRange | null;
|
|
/** One cache declaration satisfying all early and phase Yes answers. */
|
|
shared: PhaseBidRange | null;
|
|
/** A distinct ordinary phase bid after reserving any required cache bid.
|
|
* A null cacheBid means the cache may be declined. */
|
|
separate: { cacheBid: number | null; ordinary: PhaseBidRange } | null;
|
|
possible: boolean;
|
|
};
|
|
|
|
const whole = (value: unknown): value is number =>
|
|
typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
|
|
|
|
/** Intersect cache constraints before admitting both questions or preserving
|
|
* their witnesses. Distinct bids are funded pessimistically as if the first
|
|
* purchase is won: reserve its cost and card capacity before quoting the
|
|
* second. The engine retains responsibility for current-lot rules, timing,
|
|
* escrow, releases, physical settlement and actual reachability. */
|
|
export function quoteJointEarlyRicheseBids(input: JointEarlyRicheseBidInput): JointEarlyRicheseBidQuote {
|
|
if (!input || typeof input.player !== 'string' || !input.player || input.player.trim() !== input.player ||
|
|
!whole(input.turn) || input.turn < 1 || !Array.isArray(input.early) || !Array.isArray(input.phase) ||
|
|
!whole(input.cacheMaximum))
|
|
throw new EarlyRicheseBidError('Retain the respondent, turn and funded cache opportunity.');
|
|
|
|
const early = liveEarlyRicheseBidPromises(input.early, input.player, input.turn);
|
|
const phase = livePhaseBidPromises(input.phase, input.player, input.turn);
|
|
const earlyRequired = early.some(row => row.answer);
|
|
const phaseRequired = phase.some(row => row.answer);
|
|
const minimum = Math.max(1, ...early.filter(row => row.answer).map(row => row.minimum));
|
|
const maximum = Math.min(input.cacheMaximum,
|
|
...early.filter(row => !row.answer).map(row => row.minimum - 1),
|
|
...phase.filter(row => !row.answer).map(row => row.minimum - 1));
|
|
const cache = minimum <= maximum ? {minimum, maximum} : null;
|
|
const sharedMinimum = Math.max(minimum, ...phase.filter(row => row.answer).map(row => row.minimum));
|
|
const shared = cache && sharedMinimum <= maximum ? {minimum:sharedMinimum, maximum} : null;
|
|
|
|
let separate: JointEarlyRicheseBidQuote['separate'] = null;
|
|
if (input.ordinary && phaseRequired) {
|
|
const {opportunity, cashAvailable, aidAvailable, freeSlots, cacheWinSlots} = input.ordinary;
|
|
if (!opportunity || opportunity.player !== input.player || opportunity.turn !== input.turn ||
|
|
!whole(cashAvailable) || !whole(aidAvailable) || !whole(freeSlots) || !whole(cacheWinSlots))
|
|
throw new EarlyRicheseBidError('Retain the funded separate ordinary bid and hand capacity.');
|
|
const reserved = earlyRequired ? cache?.minimum : 0;
|
|
if (reserved !== undefined && cashAvailable >= reserved &&
|
|
freeSlots > (earlyRequired ? cacheWinSlots : 0)) {
|
|
const maximumOrdinary = Math.min(opportunity.maximum, Number.MAX_SAFE_INTEGER,
|
|
cashAvailable - reserved + aidAvailable);
|
|
if (maximumOrdinary >= opportunity.minimum) {
|
|
const ordinary = phaseBidCompletionRange(phase, {...opportunity, maximum:maximumOrdinary});
|
|
if (ordinary) separate = {cacheBid:earlyRequired ? reserved : null, ordinary};
|
|
}
|
|
}
|
|
}
|
|
|
|
const possible = earlyRequired
|
|
? !!cache && (!phaseRequired || !!shared || !!separate)
|
|
: !phaseRequired || !!shared || !!separate;
|
|
return {earlyRequired, phaseRequired, cache, shared, separate, possible};
|
|
}
|