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.

149 lignes · 6314 octetsBrut · Historique
1<script lang="ts">
2 import { planTrip, type GalaxyGraph } from '$lib/game/navigation';
3 import { describeOutcome } from '$lib/game/forms';
4 import { untilLabel } from '$lib/time';
5 import Button from '$lib/ui/Button.svelte';
6 import Countdown from '$lib/ui/Countdown.svelte';
7 import Panel from '$lib/ui/Panel.svelte';
8 import { systemOf, type ActiveShip, type ApiErrorBody, type GameAction } from '$lib/types';
9 import { runAction } from './run';
10
11 interface Props {
12 ship: ActiveShip;
13 actions: GameAction[];
14 graph: GalaxyGraph;
15 /** A waypoint (system view) or a system (galaxy view: we aim for its gate). */
16 target: { kind: 'waypoint' | 'system'; symbol: string };
17 /** Select a waypoint on the map (e.g. "go to the gate first"). */
18 onPick: (waypoint: string) => void;
19 }
20
21 let { ship, actions, graph, target, onPick }: Props = $props();
22
23 const here = $derived(ship.waypoint ?? ship.route?.to ?? null);
24 const dest = $derived(target.kind === 'waypoint' ? target.symbol : (graph.systems.get(target.symbol)?.gate?.symbol ?? null));
25 const plan = $derived(here && dest ? planTrip(graph, here, dest, ship.speed) : null);
26 const sameSystem = $derived(!!here && !!dest && systemOf(here) === systemOf(dest));
27
28 const navigate = $derived(actions.find((a) => a.name === 'navigate'));
29 const orbit = $derived(actions.find((a) => a.name === 'orbit'));
30 const jump = $derived(actions.find((a) => a.name === 'jump'));
31 const firstLeg = $derived(plan?.legs[0] ?? null);
32 /**
33 * At a gate: jump straight to the target system when its lane is offered,
34 * otherwise take the first hop of the planned route.
35 */
36 const jumpGate = $derived.by(() => {
37 const gates = jump?.fields.find((f) => f.name === 'gate')?.options ?? [];
38 if (target.kind === 'system') {
39 const direct = gates.find((g) => systemOf(g) === target.symbol);
40 if (direct) return direct;
41 }
42 return firstLeg?.kind === 'jump' && gates.includes(firstLeg.to) ? firstLeg.to : null;
43 });
44 const fuelShort = $derived(!!plan && plan.fuel > ship.fuel.current);
45
46 let busy = $state(false);
47 let preview = $state<string[] | null>(null);
48 let error = $state<ApiErrorBody | null>(null);
49
50 // Fresh target, fresh preview.
51 $effect(() => {
52 void target.symbol;
53 preview = null;
54 error = null;
55 });
56
57 const dur = (s: number) => untilLabel(new Date(Date.now() + s * 1000).toISOString());
58
59 async function go(dryRun: boolean) {
60 busy = true;
61 error = null;
62 try {
63 if (jumpGate && jump) return finish(await runAction(jump, { gate: jumpGate }, { dryRun }), dryRun);
64 if (!dest || !sameSystem) return;
65 if (navigate) return finish(await runAction(navigate, { waypoint: dest }, { dryRun }), dryRun);
66 if (orbit && !dryRun) {
67 // Undock, then fly with the navigate action the orbit response offers.
68 const undocked = await runAction(orbit, undefined);
69 if (!undocked.ok) return finish(undocked, false);
70 const nav = undocked.actions.find((a) => a.name === 'navigate');
71 if (nav) finish(await runAction(nav, { waypoint: dest }), false);
72 }
73 } finally {
74 busy = false;
75 }
76 }
77
78 function finish(res: Awaited<ReturnType<typeof runAction>>, dryRun: boolean) {
79 if (!res.ok) {
80 error = res.error;
81 return;
82 }
83 if (dryRun) preview = describeOutcome(ship, res.data);
84 }
85</script>
86
87<Panel title="Course to {target.symbol}" tone="accent">
88 {#if ship.status === 'in_transit' && ship.route?.to === dest}
89 <p class="text-sm text-slate-300">
90 {ship.symbol} is on its way — arrives in <Countdown until={ship.route.arrives} class="text-amber-200" />.
91 </p>
92 {:else if !plan}
93 <p class="text-sm text-slate-400">No known route from {here ?? 'here'} — the lanes there aren't charted.</p>
94 {:else if plan.legs.length === 0}
95 <p class="text-sm text-slate-400">{ship.symbol} is already there.</p>
96 {:else}
97 <ol class="mb-3 space-y-1 text-xs">
98 {#each plan.legs as leg, i (i)}
99 <li class="flex justify-between gap-2 font-mono">
100 <span class="text-slate-300">
101 <span class="text-slate-500">{leg.kind === 'jump' ? 'jump' : 'fly'}</span>
102 {leg.to}
103 </span>
104 <span class="text-slate-500">~{dur(leg.seconds)} · {leg.fuel} fuel</span>
105 </li>
106 {/each}
107 </ol>
108 <p class="mb-3 text-sm">
109 <span class="text-slate-400">Total</span>
110 <span class="font-mono text-cyan-200">~{dur(plan.seconds)}</span>
111 <span class="text-slate-400">·</span>
112 <span class="font-mono {fuelShort ? 'text-red-300' : 'text-cyan-200'}">{plan.fuel} fuel</span>
113 <span class="text-slate-500">of {ship.fuel.current}</span>
114 {#if plan.nebula}<span class="block text-xs text-fuchsia-300">Crosses a nebula: slower than shown.</span>{/if}
115 </p>
116 {#if fuelShort}
117 <p class="mb-3 rounded-md border border-red-500/40 bg-red-950/40 px-3 py-2 text-xs text-red-200">
118 Not enough fuel for the whole trip — refuel at a station first.
119 </p>
120 {/if}
121
122 {#if preview}
123 <ul class="mb-3 space-y-0.5 rounded-md border border-cyan-500/30 bg-cyan-950/30 px-3 py-2 text-xs text-cyan-100">
124 {#each preview.length ? preview : ['Would succeed.'] as line (line)}<li>{line}</li>{/each}
125 </ul>
126 {/if}
127 {#if error}
128 <p role="alert" class="mb-3 rounded-md border border-red-500/40 bg-red-950/40 px-3 py-2 text-xs text-red-200">{error.message}</p>
129 {/if}
130
131 <div class="flex flex-wrap justify-end gap-2">
132 {#if ship.status === 'in_transit'}
133 <p class="text-xs text-slate-400">In flight — orders open on arrival.</p>
134 {:else if jumpGate}
135 <Button size="sm" busy={busy && !preview} disabled={busy} onclick={() => go(true)}>Preview</Button>
136 <Button size="sm" variant="primary" disabled={busy} onclick={() => go(false)}>Jump to {systemOf(jumpGate)}</Button>
137 {:else if sameSystem && navigate}
138 <Button size="sm" disabled={busy} onclick={() => go(true)}>Preview</Button>
139 <Button size="sm" variant="primary" busy={busy} onclick={() => go(false)}>Fly here</Button>
140 {:else if sameSystem && orbit}
141 <Button size="sm" variant="primary" busy={busy} onclick={() => go(false)}>Undock &amp; fly here</Button>
142 {:else if firstLeg && firstLeg.kind === 'trip'}
143 <Button size="sm" variant="primary" onclick={() => onPick(firstLeg.to)}>Head for {firstLeg.to} first</Button>
144 {:else if firstLeg?.kind === 'jump'}
145 <p class="text-xs text-slate-400">Undock to jump from here.</p>
146 {/if}
147 </div>
148 {/if}
149</Panel>