- Fix Readme

- Fix co-op so it actually works
This commit is contained in:
tyuXX 2026-11-15 23:01:45 +03:00
parent d2645e6a26
commit 2642552891
45 changed files with 5069 additions and 1225 deletions

View file

@ -1,8 +1,13 @@
// Client HTTP API wrapper, token/settings localStorage helpers -> (this fails half the time but oh well), and SSE stream connection.
import type {
AgencyState,
CoopStateSnapshot,
LockedDesignItemInfo,
RocketDesign,
RocketPatchSection,
SavedRocketBlueprint,
TeamStationAssignment,
TeamStationId,
} from "../shared/types.js";
export type ShapeSet = "chamfered" | "smooth" | "sharp";
@ -91,7 +96,7 @@ export const DEFAULT_CUSTOM_TOKENS: CustomThemeTokens = {
};
export const DEFAULT_THEME_CONFIG: ThemeConfig = {
shapeSet: "chamfered",
shapeSet: "sharp",
colorSet: "catppuccin-mocha",
uiEffects: true,
motionEffects: true,
@ -135,7 +140,7 @@ export function normalizeThemeConfig(
if (!rawTheme) {
return {
shapeSet: legacyReduced ? "smooth" : "chamfered",
shapeSet: legacyReduced ? "smooth" : "sharp",
colorSet: legacyLight
? "daylight"
: legacyReduced
@ -174,7 +179,7 @@ export function normalizeThemeConfig(
return {
shapeSet: VALID_SHAPE_SETS.has(rawTheme.shapeSet as ShapeSet)
? (rawTheme.shapeSet as ShapeSet)
: "chamfered",
: "sharp",
colorSet: VALID_COLOR_SETS.has(rawTheme.colorSet as ColorSet)
? (rawTheme.colorSet as ColorSet)
: "catppuccin-mocha",
@ -232,9 +237,24 @@ export function normalizeThemeConfig(
}
const TOKEN_KEY = "spacegaem_proto_token";
const CALLSIGN_KEY = "spacegaem_coop_callsign";
const SETTINGS_KEY = "spacegaem_proto_settings";
const SERVER_JOIN_KEY = "spacegaem_server_join_ok";
export class ApiError extends Error {
status: number;
code?: string;
data?: any;
constructor(message: string, status: number, code?: string, data?: any) {
super(message);
this.name = "ApiError";
this.status = status;
this.code = code;
this.data = data;
}
}
export function getServerJoinOk(): boolean {
return sessionStorage.getItem(SERVER_JOIN_KEY) === "1";
}
@ -245,12 +265,51 @@ export function setServerJoinOk(ok: boolean): void {
}
export function getToken(): string | null {
return localStorage.getItem(TOKEN_KEY);
return sessionStorage.getItem(TOKEN_KEY) ?? localStorage.getItem(TOKEN_KEY);
}
export function setToken(t: string | null): void {
if (t) localStorage.setItem(TOKEN_KEY, t);
else localStorage.removeItem(TOKEN_KEY);
export function setToken(
t: string | null,
options: { tabOnly?: boolean } = {},
): void {
if (t) {
sessionStorage.setItem(TOKEN_KEY, t);
if (!options.tabOnly || !localStorage.getItem(TOKEN_KEY)) {
localStorage.setItem(TOKEN_KEY, t);
}
} else {
const active = getToken();
sessionStorage.removeItem(TOKEN_KEY);
if (
!options.tabOnly &&
active &&
localStorage.getItem(TOKEN_KEY) === active
) {
localStorage.removeItem(TOKEN_KEY);
} else if (!options.tabOnly && !active) {
localStorage.removeItem(TOKEN_KEY);
}
}
}
export function clearTabToken(): void {
sessionStorage.removeItem(TOKEN_KEY);
}
export function getSavedCallsign(): string {
return (
sessionStorage.getItem(CALLSIGN_KEY) ??
localStorage.getItem(CALLSIGN_KEY) ??
""
);
}
export function setSavedCallsign(callsign: string): void {
const clean = callsign.trim().slice(0, 24);
if (clean) {
sessionStorage.setItem(CALLSIGN_KEY, clean);
localStorage.setItem(CALLSIGN_KEY, clean);
}
}
let cachedSettings: ClientSettings | null = null;
@ -309,7 +368,12 @@ async function request<T = any>(
data = { error: text };
}
if (!res.ok) {
throw new Error(data?.error ?? `HTTP ${res.status}`);
throw new ApiError(
data?.error ?? `HTTP ${res.status}`,
res.status,
data?.code,
data,
);
}
return data as T;
}
@ -319,8 +383,46 @@ export const api = {
request<T>("GET", url, undefined, auth),
post: <T = any>(url: string, body: unknown, auth = true) =>
request<T>("POST", url, body, auth),
patch: <T = any>(url: string, body: unknown, auth = true) =>
request<T>("PATCH", url, body, auth),
delete: <T = any>(url: string, auth = true) =>
request<T>("DELETE", url, undefined, auth),
coopJoin: (joinCode: string, callsign: string) =>
request<{
token: string;
account: { name: string };
operator: { operatorId: string; callsign: string };
agency: AgencyState;
coopPresence: CoopStateSnapshot;
}>("POST", "/api/auth/coop-join", { joinCode, callsign }, false),
rotateCoopCode: () =>
request<{ ok: boolean; coopJoinCode: string; agency: AgencyState }>(
"POST",
"/api/agency/coop-code",
{},
true,
),
updateCoopStation: (payload: {
stationId: TeamStationId;
action: "claim" | "release" | "ready" | "fd_release";
ready?: boolean;
}) =>
request<{
ok: boolean;
teamStations: Record<TeamStationId, TeamStationAssignment>;
agency: AgencyState;
coopPresence: CoopStateSnapshot;
}>("POST", "/api/agency/station", payload, true),
patchRocketSection: (
section: RocketPatchSection,
design: Partial<RocketDesign>,
) =>
request<{
ok: boolean;
design: RocketDesign;
agency: AgencyState;
changedSection: RocketPatchSection;
}>("PATCH", "/api/agency/rocket", { section, design }, true),
getMlStatus: () => request<any>("GET", "/api/ml-status", undefined, false),
toggleMlBridge: (enabled?: boolean) =>
request<any>("POST", "/api/ml-toggle", { enabled }, true),
@ -383,8 +485,11 @@ export interface LiveEvent {
at?: number;
}
export function connectEvents(onEvent: (ev: LiveEvent) => void): EventSource {
const es = new EventSource("/api/events");
export function connectEvents(
token: string,
onEvent: (ev: LiveEvent) => void,
): EventSource {
const es = new EventSource(`/api/events?token=${encodeURIComponent(token)}`);
es.onmessage = (msg) => {
try {
const parsed = JSON.parse(msg.data) as LiveEvent;