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.

162 lignes · 6700 octetsBrut · Historique
1/**
2 * Contextual hints: "what should this ship do next?" in plain language.
3 * Pure function of a snapshot of state, so it is easy to test and reason about.
4 * Hints suggest; the API's actions decide. A hint's `action` is only set when that
5 * action is on offer right now, so the UI can render it as a one-click button.
6 */
7import type { ActiveShip, CaptainContract, MarketGood, Ship, Waypoint, WaypointKind } from '../types';
8import { isActive, isDisabled } from '../types';
9import { bestSellFor, sellRevenue, type MarketSnapshot, type TradeRoute } from './trade';
10
11export type HintLevel = 'danger' | 'warn' | 'info';
12
13export interface Hint {
14 id: string;
15 level: HintLevel;
16 text: string;
17 /** Action to offer as the hint's button (present in the ship's actions). */
18 action?: string;
19 /** Waypoint the hint points at, to highlight or fly to. */
20 target?: string;
21}
22
23export interface AdviceContext {
24 ship: ActiveShip;
25 /** Names of the actions the API offers the ship right now. */
26 actions: readonly string[];
27 /** Kind of the waypoint the ship is at. */
28 here: WaypointKind | null;
29 systemWaypoints: readonly Waypoint[];
30 /** Market of the station the ship is docked at. */
31 market: readonly MarketGood[] | null;
32 snapshots: readonly MarketSnapshot[];
33 bestRoute: TradeRoute | null;
34 contracts: readonly CaptainContract[];
35 codex: { have: number; of: number } | null;
36 now: number;
37}
38
39export const LOW_FUEL = 0.25;
40export const LOW_HULL = 30;
41/** Only suggest carrying cargo elsewhere when it pays this much more per unit. */
42const ELSEWHERE_MARGIN = 1.1;
43const MAX_HINTS = 3;
44
45const cr = (n: number) => `${Math.round(n).toLocaleString('en-US')} cr`;
46const ORDER: Record<HintLevel, number> = { danger: 0, warn: 1, info: 2 };
47
48export function adviseShip(ctx: AdviceContext): Hint[] {
49 const { ship, actions } = ctx;
50 const has = (a: string) => actions.includes(a);
51 const offer = (a: string) => (has(a) ? a : undefined);
52 const hints: Hint[] = [];
53 const docked = ship.status === 'docked';
54 const free = ship.cargo.capacity - ship.cargo.units;
55
56 if (isDisabled(ship)) {
57 hints.push({
58 id: 'stranded',
59 level: 'danger',
60 text: 'Stranded: the hull is worn through. Call for a tow, or pay a station tug.',
61 action: offer('distress') ?? offer('tug')
62 });
63 return hints;
64 }
65 if (ship.status === 'in_transit') return hints;
66
67 // --- safety first
68 if (ship.fuel.capacity > 0 && ship.fuel.current / ship.fuel.capacity < LOW_FUEL) {
69 const refuelHere = docked && has('refuel');
70 hints.push({
71 id: 'fuel',
72 level: 'warn',
73 text: `Fuel low (${ship.fuel.current}/${ship.fuel.capacity}). ${refuelHere ? 'Refuel before leaving.' : 'Refuel at the next station.'}`,
74 action: refuelHere ? 'refuel' : undefined
75 });
76 }
77 if (ship.hull < LOW_HULL) {
78 hints.push({
79 id: 'hull',
80 level: 'warn',
81 text: `Hull worn (${ship.hull}/100). ${docked && has('repair') ? 'Repair it here.' : 'Repair at a station before long trips.'}`,
82 action: docked ? offer('repair') : undefined
83 });
84 }
85
86 // --- contracts: cargo that someone is waiting for
87 for (const c of ctx.contracts) {
88 if (c.status !== 'open') continue;
89 const held = ship.cargo.items.find((i) => i.good === c.good)?.units ?? 0;
90 if (!held) continue;
91 const need = c.units - c.delivered;
92 if (ship.waypoint === c.station && has('deliver')) {
93 hints.push({ id: `contract-${c.id}`, level: 'info', text: `Deliver ${Math.min(held, need)} ${c.good} for contract ${c.id}.`, action: 'deliver' });
94 } else {
95 hints.push({ id: `contract-${c.id}`, level: 'info', text: `Your ${c.good} is wanted at ${c.station} for a contract (${need} left).`, target: c.station });
96 }
97 break;
98 }
99
100 // --- trading at a market
101 if (docked && ctx.market) {
102 const item = ship.cargo.items.find((i) => ctx.market!.some((g) => g.good === i.good));
103 if (item) {
104 const here = ctx.market.find((g) => g.good === item.good)!;
105 const elsewhere = bestSellFor(item.good, ctx.snapshots, ship.waypoint ?? undefined);
106 if (elsewhere && elsewhere.price > here.sell * ELSEWHERE_MARGIN) {
107 hints.push({
108 id: 'sell-elsewhere',
109 level: 'info',
110 text: `${item.good} fetches ${here.sell} cr here but ${elsewhere.price} cr at ${elsewhere.station}.`,
111 target: elsewhere.station,
112 action: offer('sell')
113 });
114 } else {
115 hints.push({ id: 'sell', level: 'info', text: `Sell ${item.units} ${item.good} here for about ${cr(sellRevenue(here, item.units))}.`, action: offer('sell') });
116 }
117 } else if (ship.cargo.units === 0 && ctx.bestRoute) {
118 const r = ctx.bestRoute;
119 if (r.from === ship.waypoint) {
120 hints.push({ id: 'trade', level: 'info', text: `Buy ${r.units} ${r.good} here and sell at ${r.to}: about +${cr(r.profit)}.`, action: offer('buy'), target: r.to });
121 } else {
122 hints.push({ id: 'trade', level: 'info', text: `Best known run: ${r.good} from ${r.from} to ${r.to}, about +${cr(r.profit)}.`, target: r.from });
123 }
124 }
125 }
126
127 // --- in orbit: what this waypoint is for
128 if (ship.status === 'in_orbit') {
129 if (ctx.here === 'station' && ship.cargo.units > 0 && has('dock')) {
130 hints.push({ id: 'dock', level: 'info', text: 'Dock to sell your cargo.', action: 'dock' });
131 }
132 if (ctx.here === 'asteroid_field' && free > 0 && has('extract')) {
133 hints.push({ id: 'mine', level: 'info', text: `Mine here: ${free} units of space in the hold.`, action: 'extract' });
134 }
135 if (ctx.here === 'derelict' && has('scan')) {
136 const progress = ctx.codex ? ` (${ctx.codex.have}/${ctx.codex.of} recovered)` : '';
137 hints.push({ id: 'scan', level: 'info', text: `Scan the derelict for a Codex fragment${progress}.`, action: 'scan' });
138 }
139 }
140 const derelict = ctx.systemWaypoints.find((w) => w.kind === 'derelict');
141 const codexDone = ctx.codex !== null && ctx.codex.have >= ctx.codex.of;
142 if (derelict && ctx.here !== 'derelict' && !codexDone) {
143 hints.push({ id: 'derelict', level: 'info', text: `A derelict drifts at ${derelict.symbol}: fly there and scan it.`, target: derelict.symbol });
144 }
145
146 if (hints.length === 0) {
147 hints.push({ id: 'idle', level: 'info', text: 'Pick a waypoint on the map to plan a trip.' });
148 }
149 return hints.sort((a, b) => ORDER[a.level] - ORDER[b.level]).slice(0, MAX_HINTS);
150}
151
152/** Badge for the fleet rail: does this ship need the player? */
153export function attention(ship: Ship, now: number): HintLevel | null {
154 if (!isActive(ship)) return null;
155 if (isDisabled(ship)) return 'danger';
156 if (ship.status !== 'in_transit') {
157 if (ship.fuel.capacity > 0 && ship.fuel.current / ship.fuel.capacity < LOW_FUEL) return 'warn';
158 if (ship.hull < LOW_HULL) return 'warn';
159 return 'info'; // idle, awaiting orders
160 }
161 return ship.route && Date.parse(ship.route.arrives) <= now ? 'info' : null;
162}