/** * Pure helpers behind action forms: turning inputs into a request body, bounding * quantities by what the ship/market/wallet allow, and describing a dry-run outcome. */ import type { ActionField, ActiveShip, CaptainContract, MarketGood } from '../types'; import { unitBuyPrice } from './trade'; export interface FormContext { ship?: ActiveShip | null; market?: readonly MarketGood[] | null; credits?: number; contracts?: readonly CaptainContract[]; } /** Request body from string inputs: integers parsed, empty optional fields left out. */ export function buildBody(fields: readonly ActionField[], values: Record): Record { const body: Record = {}; for (const f of fields) { const raw = (values[f.name] ?? '').trim(); if (raw === '' && !f.required) continue; body[f.name] = f.type === 'integer' ? Number(raw) : raw; } return body; } /** Units affordable at a market, with each unit raising the price (see trade.ts). */ export function affordableUnits(g: MarketGood, credits: number, cap = Infinity): number { let units = 0; let spent = 0; while (units < cap) { const p = unitBuyPrice(g, units); if (!Number.isFinite(p) || spent + p > credits) break; spent += p; units++; } return units; } const held = (ship: ActiveShip | null | undefined, good: string) => ship?.cargo.items.find((i) => i.good === good)?.units ?? 0; /** * Largest sensible value for a quantity field, or null when we can't tell. * `values` are the form's current inputs (e.g. the chosen good). */ export function maxFor(action: string, field: string, ctx: FormContext, values: Record): number | null { const ship = ctx.ship; const free = ship ? ship.cargo.capacity - ship.cargo.units : Infinity; const good = values.good ?? ''; const market = (g: string) => ctx.market?.find((m) => m.good === g); switch (`${action}.${field}`) { case 'buy.units': { const g = market(good); if (!g || !ship) return null; return affordableUnits(g, ctx.credits ?? Infinity, Math.min(free, g.supply - 1)); } case 'sell.units': return ship ? held(ship, good) : null; case 'refuel.units': { if (!ship) return null; const room = ship.fuel.capacity - ship.fuel.current; const g = market('fuel'); return g && ctx.credits !== undefined ? affordableUnits(g, ctx.credits, room) : room; } case 'deliver.units': { const c = ctx.contracts?.find((x) => x.id === values.contract); if (!c || !ship) return null; return Math.min(held(ship, c.good), c.units - c.delivered); } case 'refine.units': return ship ? Math.min(Math.floor(held(ship, 'iron') / 2), held(ship, 'copper')) : null; case 'repair.points': return ship ? 100 - ship.hull : null; case 'voyage.days': return 30; case 'deposit.amount': return ctx.credits ?? null; default: return null; } } /** Plain-language differences between a ship before and after an action (dry run or real). */ export function describeOutcome(before: ActiveShip | null | undefined, after: unknown): string[] { const a = after as Partial | null; if (!before || !a || typeof a !== 'object' || a.symbol !== before.symbol) return []; const out: string[] = []; if (a.route?.arrives) { const secs = Math.max(0, Math.round((Date.parse(a.route.arrives) - Date.parse(a.route.departed)) / 1000)); out.push(`Trip to ${a.route.to}: ${Math.floor(secs / 60)}m ${String(secs % 60).padStart(2, '0')}s`); } if (a.fuel && a.fuel.current !== before.fuel.current) { const d = a.fuel.current - before.fuel.current; out.push(`Fuel ${d > 0 ? '+' : ''}${d} → ${a.fuel.current}/${a.fuel.capacity}`); } if (a.cargo && a.cargo.units !== before.cargo.units) { const d = a.cargo.units - before.cargo.units; out.push(`Cargo ${d > 0 ? '+' : ''}${d} → ${a.cargo.units}/${a.cargo.capacity}`); } if (typeof a.hull === 'number' && a.hull !== before.hull) out.push(`Hull ${before.hull} → ${a.hull}`); if (a.status && a.status !== before.status) out.push(`Status: ${a.status.replaceAll('_', ' ')}`); return out; }