import { S } from "../state/store.js"; import { emit } from "../core/events.js"; import { audioRoles, directFireRoles, sessionRoles } from "../core/base.js"; import { finite } from "../core/format.js"; import { request } from "./request.js"; import { exactKeys } from "../state/schema.js"; import { lookoutView, view } from "../state/shared.js"; // Link state for the whole client; views follow via the "connection" topic. export function setConnection(state, message = null) { S.linkState = state; S.connected = state === "connected"; emit("connection", message); } export function forgetSession(message = "connection_unpaired") { emit("session:forgetting"); S.generation += 1; S.session = null; S.activatingStation = null; S.stationPickerOpen = false; S.lastSessionFetch = 0; S.activeRequest?.abort(); clearTimeout(S.pollTimer); emit("role:clear"); S.snapshot = null; S.chart = null; S.chartSession = null; S.chartEpoch = null; S.chartRole = null; S.selected = null; S.pending = null; S.commandMessage = null; view.initialized = false; lookoutView.rangeNm = 100; S.failures = 0; S.lastSuccess = 0; S.latestSimlogState = null; S.proposals = null; S.eventContext = null; S.eventHighWater = 0; S.eventHistory = []; S.lobbyMessage = null; S.stationMutation = false; S.hostView = null; S.hostPending = null; S.hostMessage = null; emit("session:forgotten"); setConnection("unpaired", message); } export function validateSession(value) { const fields = ["active_generation", "active_station", "client_id", "csrf", "grants", "name", "host", "next_command_seq", "observer", "ordinal", "presence", "protocol", "requested_station", "simlog", "station", "station_generation", "stations"]; if (!value || typeof value !== "object" || Array.isArray(value) || Object.keys(value).sort().join(",") !== fields.sort().join(",") || value.protocol !== 2 || typeof value.client_id !== "string" || !value.client_id || value.client_id.length > 128 || typeof value.name !== "string" || value.name.length < 1 || value.name.length > 32 || typeof value.csrf !== "string" || !value.csrf || value.csrf.length > 256 || !Number.isSafeInteger(value.ordinal) || value.ordinal < 0 || !Number.isSafeInteger(value.active_generation) || value.active_generation < 0 || !Number.isSafeInteger(value.station_generation) || value.station_generation < 0 || !Number.isSafeInteger(value.next_command_seq) || value.next_command_seq < 0 || !finite(value.presence) || value.presence < 0 || (value.station !== null && !sessionRoles.includes(value.station)) || (value.requested_station !== null && !sessionRoles.includes(value.requested_station)) || !value.grants || typeof value.grants !== "object" || Array.isArray(value.grants) || Object.keys(value.grants).sort().join(",") !== "command,direct_fire,simlog,sonar_audio" || Object.values(value.grants).some((grant) => typeof grant !== "boolean") || (value.grants.command && value.station === null) || (value.grants.direct_fire && (!value.grants.command || !directFireRoles.has(value.station))) || (value.grants.sonar_audio && !audioRoles.has(value.station)) || typeof value.simlog !== "boolean" || value.grants.simlog !== value.simlog || typeof value.observer !== "boolean" || (value.observer && (value.grants.command || value.host !== null || !value.simlog)) || (value.host !== null && (!exactKeys(value.host, ["generation"]) || !Number.isSafeInteger(value.host.generation) || value.host.generation < 0)) || value.active_station !== value.station || !value.stations || typeof value.stations !== "object" || Array.isArray(value.stations) || Object.keys(value.stations).join(",") !== sessionRoles.join(",")) throw new Error("session"); for (const station of sessionRoles) { const record = value.stations[station]; if (!exactKeys(record, ["status", "requested", "request_generation", "station_generation", "grants"]) || !["available", "occupied", "mine"].includes(record.status) || typeof record.requested !== "boolean" || !Number.isSafeInteger(record.request_generation) || record.request_generation < 0 || (record.station_generation !== null && (!Number.isSafeInteger(record.station_generation) || record.station_generation < 0)) || !exactKeys(record.grants, ["command", "direct_fire", "sonar_audio"]) || Object.values(record.grants).some((grant) => typeof grant !== "boolean") || (record.status === "mine") !== (record.station_generation !== null) || record.grants.direct_fire && (!record.grants.command || !directFireRoles.has(station)) || record.grants.sonar_audio && !audioRoles.has(station)) throw new Error("session"); } const mine = sessionRoles.filter((station) => value.stations[station].status === "mine"); if ((value.station === null) !== (mine.length === 0) || value.station !== null && !mine.includes(value.station) || value.requested_station !== null && !value.stations[value.requested_station].requested || value.station !== null && (value.station_generation !== value.stations[value.station].station_generation || value.grants.command !== value.stations[value.station].grants.command || value.grants.direct_fire !== value.stations[value.station].grants.direct_fire || value.grants.sonar_audio !== value.stations[value.station].grants.sonar_audio)) throw new Error("session"); } export async function resumeSession() { try { const resumed = await request("/session", { auth: false }); validateSession(resumed); S.pairingAvailable = true; emit("session:metadata", resumed); return true; } catch (error) { S.pairingAvailable = error.status === 401; if (!S.pairingAvailable) setConnection("stale"); return false; } }