105 lines
5.6 KiB
TypeScript
105 lines
5.6 KiB
TypeScript
import { quoteJointEarlyRicheseBids } from './joint-early-richese-bids';
|
|
import { jointRicheseBidCompletionRange } from './joint-richese-bids';
|
|
import {
|
|
richeseBidRequired,
|
|
RicheseBidPromiseError,
|
|
type RicheseBidContext,
|
|
type RicheseBidOpportunity,
|
|
type RicheseBidPromise,
|
|
} from './richese-bid-promises';
|
|
import { livePhaseBidPromises, phaseBidCompletionRange, type PhaseBidPromise, type PhaseBidRange } from './turn-bid-promises';
|
|
import type { EarlyRicheseBidPromise } from './early-richese-bid';
|
|
|
|
/** Every number is supplied from the engine's current native quote, not a
|
|
* prediction of rival bids, refunds or income. The cache is after the market. */
|
|
export type JointMarketEarlyBidInput = {
|
|
context: RicheseBidContext;
|
|
native: readonly RicheseBidPromise[];
|
|
early: readonly EarlyRicheseBidPromise[];
|
|
phase: readonly PhaseBidPromise[];
|
|
opportunity: RicheseBidOpportunity | null;
|
|
/** Own cash and unlocked pledged aid available for the market bid. */
|
|
cashAvailable: number;
|
|
aidAvailable: number;
|
|
/** Own cash available for the later cache if the market is declined. */
|
|
cacheMaximum: number;
|
|
/** Free slots after any pre-existing purchase, before this market bid. */
|
|
freeSlots: number;
|
|
/** Current sale needs one slot for a buyer and none for paid self-retention. */
|
|
marketEntrySlots: number;
|
|
/** Slots reserved after a possible market win, including a Harkonnen bonus. */
|
|
marketWinSlots: number;
|
|
};
|
|
|
|
/** Inclusive market interval and the ally contribution needed for its
|
|
* minimum while preserving the stated cache reservation. */
|
|
export type JointMarketBidRange = PhaseBidRange & { allyPayment: number };
|
|
export type JointMarketEarlyBidQuote = {
|
|
nativeRequired: boolean;
|
|
earlyRequired: boolean;
|
|
phaseRequired: boolean;
|
|
/** A pass/zero satisfies the native answer and retains the early cache. */
|
|
canPass: boolean;
|
|
/** Legal current bid preserving the independent early-cache Yes. */
|
|
market: JointMarketBidRange | null;
|
|
/** Current bid also fulfilling every live phase Yes. */
|
|
marketForPhase: JointMarketBidRange | null;
|
|
/** Current bid leaving the cache able to fulfill every phase Yes. */
|
|
marketForCache: JointMarketBidRange | null;
|
|
/** A cache bid if this market is declined; phase Yes may remain open. */
|
|
cacheAfterPass: PhaseBidRange | null;
|
|
/** A cache bid fulfilling every phase Yes if this market is declined. */
|
|
phaseCacheAfterPass: PhaseBidRange | null;
|
|
};
|
|
|
|
const whole = (value: unknown): value is number =>
|
|
typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
|
|
|
|
/** Intersect three ledgers around the actual one-declaration market lot. The
|
|
* caller supplies event eligibility and later ordinary-lot witnesses. When a
|
|
* later cache bid must be funded, reserve personal cash and the potential
|
|
* first market purchase before exposing a market bid. No cache refund,
|
|
* non-owner sale, hidden offer, or pledged future aid is assumed. */
|
|
export function quoteJointMarketEarlyBids(input: JointMarketEarlyBidInput): JointMarketEarlyBidQuote {
|
|
if (!input || !input.context || !Array.isArray(input.native) || !Array.isArray(input.early) ||
|
|
!Array.isArray(input.phase) || !whole(input.cashAvailable) || !whole(input.aidAvailable) ||
|
|
!whole(input.cacheMaximum) || !whole(input.freeSlots) || !whole(input.marketEntrySlots) ||
|
|
!whole(input.marketWinSlots) || input.marketWinSlots < input.marketEntrySlots)
|
|
throw new RicheseBidPromiseError('Retain the original market, funded cache and hand capacity.');
|
|
const {context, native, early, phase, opportunity} = input;
|
|
const nativeRequired = richeseBidRequired(native,context);
|
|
if (opportunity && (opportunity.turn !== context.turn || opportunity.event !== context.event ||
|
|
opportunity.player !== context.player))
|
|
throw new RicheseBidPromiseError('The bid opportunity belongs to a different market event.');
|
|
const cache = quoteJointEarlyRicheseBids({player:context.player,turn:context.turn,
|
|
early,phase,cacheMaximum:input.cacheMaximum});
|
|
const phaseRequired = livePhaseBidPromises(phase,context.player,context.turn).some(row => row.answer);
|
|
const local = opportunity && jointRicheseBidCompletionRange(native,phase,opportunity,{includePhaseYes:false});
|
|
|
|
const marketRange = (reserve: number | null): JointMarketBidRange | null => {
|
|
if (!local || reserve === null || input.cashAvailable < reserve) return null;
|
|
const slots = reserve > 0 ? input.marketWinSlots + 1 : input.marketEntrySlots;
|
|
if (input.freeSlots < slots) return null;
|
|
const maximum = Math.min(local.maximum,Number.MAX_SAFE_INTEGER,
|
|
input.cashAvailable - reserve + input.aidAvailable);
|
|
if (local.minimum > maximum) return null;
|
|
return {minimum:local.minimum,maximum,
|
|
allyPayment:Math.max(0,local.minimum - (input.cashAvailable - reserve))};
|
|
};
|
|
const earlyReserve = cache.earlyRequired ? cache.cache?.minimum ?? null : 0;
|
|
const market = marketRange(earlyReserve);
|
|
let marketForPhase: JointMarketBidRange | null = null;
|
|
if (market && phaseRequired) {
|
|
const range = phaseBidCompletionRange(phase,{turn:context.turn,player:context.player,
|
|
kind:'current',minimum:market.minimum,maximum:market.maximum});
|
|
if (range) marketForPhase = {...range,
|
|
allyPayment:Math.max(0,range.minimum - (input.cashAvailable - (earlyReserve ?? 0)))};
|
|
}
|
|
const marketForCache = phaseRequired && cache.shared ? marketRange(cache.shared.minimum) : null;
|
|
const canPass = !nativeRequired && (!cache.earlyRequired || !!cache.cache);
|
|
return {nativeRequired,earlyRequired:cache.earlyRequired,phaseRequired,canPass,
|
|
market,marketForPhase,marketForCache,
|
|
cacheAfterPass:canPass ? cache.cache : null,
|
|
phaseCacheAfterPass:canPass && phaseRequired ? cache.shared : null};
|
|
}
|