Aetheria/Aetheria.Rts.Web/Client/app.ts

135 lines
4.1 KiB
TypeScript
Raw Normal View History

import {
EveBrowserProviderHost,
type EveCommandIntent,
type EveProviderSurfaceAdvertisement,
2026-07-05 01:17:27 +01:00
type EveEmbeddedDocumentRequest,
type EveSurfaceComponent,
} from "../node_modules/@gamecult/eve-browser-lowering/dist/index.js";
2026-06-25 18:09:33 +01:00
import type {
2026-07-05 16:59:06 +01:00
AetheriaRtsApi,
2026-06-25 18:09:33 +01:00
} from "./aetheria-rts-contract.js";
export {};
declare global {
interface Window {
aetheriaRts: AetheriaRtsApi;
}
}
2026-07-05 16:59:06 +01:00
const host = requiredElement<HTMLElement>("#eve-surface-host");
2026-06-25 18:09:33 +01:00
const statusEl = requiredElement<HTMLElement>("#status");
function requiredElement<TElement extends Element>(selector: string): TElement {
const element = document.querySelector<TElement>(selector);
if (!element) {
2026-07-05 22:39:15 +01:00
throw new Error(`Aetheria Starbridge is missing ${selector}.`);
2026-06-25 18:09:33 +01:00
}
return element;
}
function setStatus(text: string): void {
statusEl.textContent = text;
}
async function showDaemonEveSurface(): Promise<void> {
document.body.classList.add("eve-game-mode");
host.setAttribute("aria-label", "Aetheria daemon Eve surface");
const requestedSurfaceId = new URLSearchParams(location.search).get("surface") || "";
const providerHost = new EveBrowserProviderHost(host, {
providerAdvertisement: () => window.aetheriaRts.eveProviderAdvertisement(),
surface: surface => window.aetheriaRts.eveSurface(surfaceRequest(surface)),
submitCommand: submitEveCommand,
resolveDocument: resolveEveDocument,
resolveAssetUrl: resolveAetheriaAssetUrl,
}, {
body: document.body,
clientId: "eve-electron-client",
pollMs: 250,
requestedSurfaceId,
source: "CultMesh provider",
statusElement: statusEl,
});
await providerHost.start();
wireWindowControls();
new MutationObserver(wireWindowControls).observe(host, { childList: true, subtree: true });
}
function surfaceRequest(surface: EveProviderSurfaceAdvertisement): { surfaceId?: string; recordKey?: string } {
return surface.key ? { recordKey: surface.key } : { surfaceId: surface.surfaceId };
}
2026-07-05 01:17:27 +01:00
async function resolveEveDocument(
request: EveEmbeddedDocumentRequest,
component: EveSurfaceComponent,
): ReturnType<AetheriaRtsApi["eveDocument"]> {
return window.aetheriaRts.eveDocument({
...request,
context: { viewport: viewportFromComponent(component) },
});
2026-07-05 01:17:27 +01:00
}
function viewportFromComponent(component: EveSurfaceComponent): Record<string, number> {
2026-07-05 01:17:27 +01:00
const props = component.props ?? {};
return {
minX: numberProp(props.minX, -1500),
minY: numberProp(props.minY, -1000),
maxX: numberProp(props.maxX, 1500),
maxY: numberProp(props.maxY, 1000),
};
}
function numberProp(value: unknown, fallback: number): number {
2026-07-05 16:59:06 +01:00
if (typeof value === "number" && Number.isFinite(value)) {
2026-07-05 01:17:27 +01:00
return value;
2026-07-05 16:59:06 +01:00
}
2026-07-05 01:17:27 +01:00
if (typeof value === "string") {
const parsed = Number.parseFloat(value);
2026-07-05 16:59:06 +01:00
if (Number.isFinite(parsed)) {
2026-07-05 01:17:27 +01:00
return parsed;
2026-07-05 16:59:06 +01:00
}
2026-07-05 01:17:27 +01:00
}
return fallback;
}
2026-07-05 13:48:28 +01:00
function resolveAetheriaAssetUrl(uri: string): string {
if (!uri.startsWith("cultmesh://")) {
2026-07-05 13:48:28 +01:00
return uri;
2026-07-05 16:59:06 +01:00
}
2026-07-05 13:48:28 +01:00
return `aetheria-cdn://asset?uri=${encodeURIComponent(uri)}`;
2026-07-05 13:48:28 +01:00
}
2026-07-05 16:59:06 +01:00
async function submitEveCommand(intent: EveCommandIntent): Promise<void> {
try {
const receipt = await window.aetheriaRts.submitEveCommand({
providerId: intent.providerId,
surfaceId: intent.surfaceId,
command: intent.command,
clientId: intent.clientId,
issuedAtUtc: intent.issuedAt,
payload: intent.payload,
2026-07-01 11:24:31 +01:00
});
2026-07-05 16:59:06 +01:00
setStatus(`${intent.command}: ${receipt.accepted ? "submitted" : "rejected"}`);
} catch (error) {
setStatus(error instanceof Error ? error.message : `Failed to submit ${intent.command}.`);
}
2026-07-01 11:24:31 +01:00
}
2026-07-05 16:59:06 +01:00
function wireWindowControls(): void {
document.querySelectorAll<HTMLButtonElement>("[data-window-control]").forEach(button => {
if (button.dataset.windowControlWired === "true") {
return;
2026-06-25 18:09:33 +01:00
}
2026-07-05 16:59:06 +01:00
button.dataset.windowControlWired = "true";
button.addEventListener("click", () => {
const action = button.dataset.windowControl as "minimize" | "maximize" | "close" | undefined;
if (action) {
void window.aetheriaRts.windowControl(action);
}
2026-06-25 18:09:33 +01:00
});
});
}
2026-07-05 16:59:06 +01:00
void showDaemonEveSurface();