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.

83 lignes · 2927 octetsBrut · Historique
1/**
2 * Galaxy-wide reads shared by the HUD layout and the market intel endpoint.
3 *
4 * REQUEST BUDGET: the API allows 120 requests/minute per captain, including everything the
5 * player does. With ~40 systems and ~N stations, the refresh intervals below keep the
6 * background load around 25 requests/minute:
7 * system details 10 min (40 req), influence 5 min (40 req), markets 2 min (N req),
8 * events 1 min (1 req), system list 5 min (1 req).
9 * All are stale-while-revalidate (see cache.ts), so only a cold start waits.
10 */
11import type {
12 Envelope,
13 GalaxyEvent,
14 Influence,
15 Market,
16 SystemDetail,
17 SystemSummary
18} from '$lib/types';
19import type { MarketSnapshot } from '$lib/game/trade';
20import { get } from './api';
21import { captainKey, cached, mapSettled } from './cache';
22
23const FAN_OUT = 6;
24const MIN = 60_000;
25
26export function galaxyReader(token: string) {
27 const ns = captainKey(token);
28
29 const systems = () =>
30 cached(`systems:${ns}`, 5 * MIN, async () => {
31 const all: SystemSummary[] = [];
32 for (let page = 1; ; page++) {
33 const res = await get<Envelope<SystemSummary>[]>(token, '/v1/systems', { page, size: 100 });
34 all.push(...res.data.map((e) => e.data));
35 if (!res.links.next) return all;
36 }
37 });
38
39 const details = () =>
40 cached(`system-details:${ns}`, 10 * MIN, async () => {
41 const list = await systems();
42 const settled = await mapSettled(list, FAN_OUT, (s) => get<SystemDetail>(token, `/v1/systems/${s.symbol}`));
43 return settled.flatMap((r) => (r.status === 'fulfilled' ? [r.value.data] : []));
44 });
45
46 const influence = () =>
47 cached(`influence:${ns}`, 5 * MIN, async () => {
48 const list = await systems();
49 const settled = await mapSettled(list, FAN_OUT, (s) =>
50 get<Influence>(token, `/v1/systems/${s.symbol}/influence`)
51 );
52 return Object.fromEntries(
53 settled.flatMap((r) => (r.status === 'fulfilled' ? [[r.value.data.system, r.value.data] as const] : []))
54 ) as Record<string, Influence>;
55 });
56
57 const events = () =>
58 cached(`events:${ns}`, MIN, async () => {
59 const res = await get<Envelope<GalaxyEvent>[]>(token, '/v1/galaxy/events');
60 return res.data.map((e) => e.data);
61 }).catch(() => [] as GalaxyEvent[]);
62
63 /** Every station market in the systems we can see, read remotely. */
64 const markets = () =>
65 cached(`markets:${ns}`, 2 * MIN, async () => {
66 const stations = (await details()).flatMap((d) =>
67 d.waypoints.filter((w) => w.data.kind === 'station').map((w) => w.data)
68 );
69 const settled = await mapSettled(stations, FAN_OUT, (w) =>
70 get<Market>(token, `/v1/systems/${w.system}/waypoints/${w.symbol}/market`).then(
71 (m): MarketSnapshot => ({
72 station: w.symbol,
73 system: w.system,
74 goods: m.data.goods,
75 at: new Date().toISOString()
76 })
77 )
78 );
79 return settled.flatMap((r) => (r.status === 'fulfilled' ? [r.value] : []));
80 });
81
82 return { systems, details, influence, events, markets };
83}