tobogganSe connecter

starfleet-ui

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

104 lignes · 4076 octetsBrut · Historique
1/**
2 * Pure helpers behind action forms: turning inputs into a request body, bounding
3 * quantities by what the ship/market/wallet allow, and describing a dry-run outcome.
4 */
5import type { ActionField, ActiveShip, CaptainContract, MarketGood } from '../types';
6import { unitBuyPrice } from './trade';
7
8export interface FormContext {
9 ship?: ActiveShip | null;
10 market?: readonly MarketGood[] | null;
11 credits?: number;
12 contracts?: readonly CaptainContract[];
13}
14
15/** Request body from string inputs: integers parsed, empty optional fields left out. */
16export function buildBody(fields: readonly ActionField[], values: Record<string, string>): Record<string, unknown> {
17 const body: Record<string, unknown> = {};
18 for (const f of fields) {
19 const raw = (values[f.name] ?? '').trim();
20 if (raw === '' && !f.required) continue;
21 body[f.name] = f.type === 'integer' ? Number(raw) : raw;
22 }
23 return body;
24}
25
26/** Units affordable at a market, with each unit raising the price (see trade.ts). */
27export function affordableUnits(g: MarketGood, credits: number, cap = Infinity): number {
28 let units = 0;
29 let spent = 0;
30 while (units < cap) {
31 const p = unitBuyPrice(g, units);
32 if (!Number.isFinite(p) || spent + p > credits) break;
33 spent += p;
34 units++;
35 }
36 return units;
37}
38
39const held = (ship: ActiveShip | null | undefined, good: string) =>
40 ship?.cargo.items.find((i) => i.good === good)?.units ?? 0;
41
42/**
43 * Largest sensible value for a quantity field, or null when we can't tell.
44 * `values` are the form's current inputs (e.g. the chosen good).
45 */
46export function maxFor(action: string, field: string, ctx: FormContext, values: Record<string, string>): number | null {
47 const ship = ctx.ship;
48 const free = ship ? ship.cargo.capacity - ship.cargo.units : Infinity;
49 const good = values.good ?? '';
50 const market = (g: string) => ctx.market?.find((m) => m.good === g);
51
52 switch (`${action}.${field}`) {
53 case 'buy.units': {
54 const g = market(good);
55 if (!g || !ship) return null;
56 return affordableUnits(g, ctx.credits ?? Infinity, Math.min(free, g.supply - 1));
57 }
58 case 'sell.units':
59 return ship ? held(ship, good) : null;
60 case 'refuel.units': {
61 if (!ship) return null;
62 const room = ship.fuel.capacity - ship.fuel.current;
63 const g = market('fuel');
64 return g && ctx.credits !== undefined ? affordableUnits(g, ctx.credits, room) : room;
65 }
66 case 'deliver.units': {
67 const c = ctx.contracts?.find((x) => x.id === values.contract);
68 if (!c || !ship) return null;
69 return Math.min(held(ship, c.good), c.units - c.delivered);
70 }
71 case 'refine.units':
72 return ship ? Math.min(Math.floor(held(ship, 'iron') / 2), held(ship, 'copper')) : null;
73 case 'repair.points':
74 return ship ? 100 - ship.hull : null;
75 case 'voyage.days':
76 return 30;
77 case 'deposit.amount':
78 return ctx.credits ?? null;
79 default:
80 return null;
81 }
82}
83
84/** Plain-language differences between a ship before and after an action (dry run or real). */
85export function describeOutcome(before: ActiveShip | null | undefined, after: unknown): string[] {
86 const a = after as Partial<ActiveShip> | null;
87 if (!before || !a || typeof a !== 'object' || a.symbol !== before.symbol) return [];
88 const out: string[] = [];
89 if (a.route?.arrives) {
90 const secs = Math.max(0, Math.round((Date.parse(a.route.arrives) - Date.parse(a.route.departed)) / 1000));
91 out.push(`Trip to ${a.route.to}: ${Math.floor(secs / 60)}m ${String(secs % 60).padStart(2, '0')}s`);
92 }
93 if (a.fuel && a.fuel.current !== before.fuel.current) {
94 const d = a.fuel.current - before.fuel.current;
95 out.push(`Fuel ${d > 0 ? '+' : ''}${d} → ${a.fuel.current}/${a.fuel.capacity}`);
96 }
97 if (a.cargo && a.cargo.units !== before.cargo.units) {
98 const d = a.cargo.units - before.cargo.units;
99 out.push(`Cargo ${d > 0 ? '+' : ''}${d} → ${a.cargo.units}/${a.cargo.capacity}`);
100 }
101 if (typeof a.hull === 'number' && a.hull !== before.hull) out.push(`Hull ${before.hull} → ${a.hull}`);
102 if (a.status && a.status !== before.status) out.push(`Status: ${a.status.replaceAll('_', ' ')}`);
103 return out;
104}