Files
uconsole_asw_game/data/commander/js/views/host.js
T
Claude 13d8997771 Fix browser and cache flakes: real-time page probes, host command after side switch, pure ray caches
- Browser tests that drive a live host (uboot nav, console, observer
  timeline, bridge glasses, map hover) ran under Chromium virtual time, which
  races ahead of the Python host and hits the wall cap under load. They now
  run in real time and read the probe result over DevTools (RealTimeHost).
- Direct-fire sweep check: a pending ResizeObserver redraw could land after
  the "settled" sample; the probe now waits until the canvas backing store
  matches its layout size and stays put.
- Remote Crew host form: after a side switch the new-game / own-mission
  command was dropped silently or sent with a stale host view; it is now
  sent once the console's host view matches its new world session.
- raytrace: convergence-zone and ray-picture caches computed their result
  from the unrounded arguments of the first caller, so results depended on
  call order. They now compute from the quantized key alone (regression test
  added; calibrate 77/77 in tolerance).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KBWAva5Bhs5KJy75vSJDRT
2026-10-02 23:22:15 +00:00

259 lines
13 KiB
JavaScript

import { S } from "../state/store.js";
import { $ } from "../core/base.js";
import { t } from "../core/format.js";
import { renderDisabledReasons, unavailable } from "./controls.js";
import { node } from "./dom.js";
import { simlogActive } from "./simlog.js";
import { sendHostAction, sendHostActionWhenReady } from "../net/host.js";
import { fetchLibrary } from "../net/missions.js";
import { mutateStation, switchSoloSide } from "./lobby.js";
import { opforRoles } from "../core/base.js";
// ---- Solo host surface --------------------------------------------------
export const scenarioText = {s1_patrouille: "scenario_s1_patrouille", s2_doppeljagd: "scenario_s2_doppeljagd",
s3_abfang: "scenario_s3_abfang", s4_zufall: "scenario_s4_zufall",
s5_durchbruch: "scenario_s5_durchbruch", s6_aufklaerung: "scenario_s6_aufklaerung",
s7_geleitzug: "scenario_s7_geleitzug", s8_meerenge: "scenario_s8_meerenge",
s9_kampfschwimmer: "scenario_s9_kampfschwimmer", s10_versorger: "scenario_s10_versorger",
s11_geleitschutz: "scenario_s11_geleitschutz", s12_datum: "scenario_s12_datum",
s13_fuehlung: "scenario_s13_fuehlung", s14_hafenschutz: "scenario_s14_hafenschutz",
s15_versorgung: "scenario_s15_versorgung", s16_seenot: "scenario_s16_seenot",
s17_duell: "scenario_s17_duell", s18_heimkehr: "scenario_s18_heimkehr",
s19_abholung: "scenario_s19_abholung", s20_lauschposten: "scenario_s20_lauschposten",
s21_suchgruppe: "scenario_s21_suchgruppe", s22_jagdgruppe: "scenario_s22_jagdgruppe",
frei_fregatte: "scenario_frei_fregatte", frei_uboot: "scenario_frei_uboot"};
const hostPhaseAllows = (kind) => {
const phase = S.hostView?.phase;
return kind === "any" ? phase === "live" :
phase === "live" || phase === "menu" || phase === "ended";
};
export function hostUnavailableReason(control) {
if (!S.hostView) return unavailable("connection_syncing");
if (S.hostPending) return unavailable("host_pending");
if (!S.connected) return unavailable("connection_stale", {age: 0});
if (S.hostView.phase === "blocked") return unavailable("host_blocked");
if (control.dataset.slot && !S.hostView.slots.find((row) => String(row.slot) === control.dataset.slot)?.saved) {
return unavailable("host_slot_empty");
}
return unavailable("host_phase");
}
export function renderHost() {
const active = S.session?.host !== null && S.session !== null;
$("host-bar").hidden = !active;
$("web-admin-link").hidden = !active || !S.webHostAvailable;
const menu = active && S.hostView?.phase === "menu";
$("host-screen").hidden = !menu || simlogActive();
if (!active) {
for (const id of ["host-save", "host-load", "host-new", "host-missions",
"host-instructor", "host-side", "host-screen-new", "host-screen-load",
"host-new-start"]) $(id).disabled = true;
for (const button of $("host-slot-list").querySelectorAll("button"))
button.disabled = true;
return;
}
const ready = Boolean(S.hostView) && !S.hostPending && S.connected;
const any = ready && hostPhaseAllows("any");
const replacing = ready && hostPhaseAllows("replacing");
$("host-save").disabled = !any;
$("host-load").disabled = !replacing;
$("host-new").disabled = !replacing;
$("host-missions").disabled = !ready;
$("host-instructor").disabled = !any;
const boat = opforRoles.has(S.session.station);
$("host-side").textContent = t(boat ? "host_play_frigate" : "host_play_opfor");
$("host-side").disabled = !ready || S.stationMutation;
$("host-screen-new").disabled = !replacing;
$("host-screen-load").disabled = !replacing;
$("host-new-start").disabled = !replacing;
for (const button of $("host-slot-list").querySelectorAll("button")) {
button.disabled = !replacing || (button.dataset.mode === "load" &&
!S.hostView.slots.find((row) => String(row.slot) === button.dataset.slot)?.saved);
}
const text = S.hostMessage ? t(S.hostMessage.key, S.hostMessage.values ?? {}) : "";
for (const id of ["host-status", "host-screen-status"]) {
$(id).textContent = text;
$(id).dataset.status = S.hostMessage?.status ?? "";
}
}
function closeHostDialog(dialog) { if (dialog.open) dialog.close(); }
function openSlotDialog(mode) {
if (!S.hostView) return;
const dialog = $("host-slot-dialog");
$("host-slot-title").textContent = t(mode === "save" ? "host_save_title" : "host_load_title");
$("host-slot-note").textContent = t(mode === "save" ? "host_save_note" : "host_load_note");
$("host-slot-list").replaceChildren(...S.hostView.slots.map((row) => {
const item = node("li");
const button = node("button", t(row.saved ? "host_slot_saved" : "host_slot_empty_label", {
slot: row.slot, time: row.modified === null ? "" : new Date(row.modified * 1000).toLocaleString(S.language)}));
button.type = "button";
button.dataset.slot = String(row.slot);
button.dataset.mode = mode;
button.disabled = mode === "load" && !row.saved;
button.addEventListener("click", () => {
closeHostDialog(dialog);
sendHostAction(mode === "save" ? "host_save" : "host_load", {slot: row.slot});
});
item.append(button);
return item;
}));
renderDisabledReasons();
dialog.hidden = false;
if (!dialog.open) dialog.showModal();
dialog.querySelector("button:not(:disabled)")?.focus();
}
function syncNewGameDifficulty() {
const value = $("host-new-scenario").value;
const scenario = S.hostView?.scenarios.find((row) => row.key === value);
const free = Boolean(scenario) && !scenario.fixed;
for (const input of $("host-new-difficulty").querySelectorAll("input")) input.disabled = !free;
// An own mission brings its own weather, time and length.
const own = value.startsWith(OWN_PREFIX);
for (const id of ["host-new-world", "host-new-weather", "host-new-time", "host-new-length", "host-new-seed"])
$(id).disabled = own;
$("host-new-difficulty-note").textContent = own ? t("host_new_own_note") : scenario
? t(free ? "host_new_difficulty_free" : "host_new_difficulty_fixed") : "";
}
const OWN_PREFIX = "own:";
// The library's valid missions of the chosen side, after the built-in ones.
function appendOwnMissions(side, preferred) {
const rows = (S.missionLibrary?.missions ?? []).filter((row) => row.valid && row.side === side);
$("host-new-scenario").querySelector("optgroup")?.remove();
if (!rows.length) return;
const group = node("optgroup");
group.label = t("host_new_own_group");
for (const row of rows) {
const option = node("option", row.name || row.key);
option.value = OWN_PREFIX + row.key;
group.append(option);
}
$("host-new-scenario").append(group);
if (preferred && $("host-new-scenario").querySelector(`option[value="${CSS.escape(preferred)}"]`))
$("host-new-scenario").value = preferred;
syncNewGameDifficulty();
}
// The scenario list shows only the missions of the chosen side.
function fillNewGameScenarios(preferred) {
const side = $("host-new-side").value;
const rows = S.hostView.scenarios.filter((row) => row.side === side);
$("host-new-scenario").replaceChildren(...rows.map((row) => {
const option = node("option", t(scenarioText[row.key] ?? "unknown"));
option.value = row.key;
return option;
}));
if (rows.some((row) => row.key === preferred)) $("host-new-scenario").value = preferred;
appendOwnMissions(side, preferred);
syncNewGameDifficulty();
}
function openNewGameDialog() {
if (!S.hostView) return;
const dialog = $("host-new-dialog");
$("host-new-difficulty").replaceChildren(...S.hostView.difficulty_fields.map((field) => {
const wrapper = node("div", undefined, "field");
const inputId = `host-new-difficulty-${field.name}`;
const label = node("label", t(`difficulty_${field.name}`));
label.htmlFor = inputId;
const input = node("input");
input.type = "number";
input.id = inputId;
input.dataset.field = field.name;
input.min = String(field.min);
input.max = String(field.max);
input.step = String(field.step);
input.value = String(S.hostView.difficulty[field.name]);
wrapper.append(label, input);
return wrapper;
}));
$("host-new-world").value = S.hostView.world_mode;
$("host-new-side").value = opforRoles.has(S.session?.station) ? "uboot" : "frigate";
fillNewGameScenarios(S.hostView.scenario);
$("host-new-seed").value = "";
renderDisabledReasons();
dialog.hidden = false;
if (!dialog.open) dialog.showModal();
$("host-new-side").focus();
// The own missions arrive with the library; the list grows when it is read.
fetchLibrary().then(() => {
if (dialog.open) appendOwnMissions($("host-new-side").value, $("host-new-scenario").value);
}).catch(() => {});
}
export function init() {
for (const id of ["host-slot-dialog", "host-new-dialog", "instructor-dialog"]) {
$(id).addEventListener("close", () => { $(id).hidden = true; });
}
$("host-slot-cancel").addEventListener("click", () => closeHostDialog($("host-slot-dialog")));
$("host-new-cancel").addEventListener("click", () => closeHostDialog($("host-new-dialog")));
$("instructor-cancel").addEventListener("click", () => closeHostDialog($("instructor-dialog")));
$("host-new-scenario").addEventListener("change", syncNewGameDifficulty);
$("host-new-side").addEventListener("change", () => {
if (S.hostView) fillNewGameScenarios($("host-new-scenario").value);
});
$("host-new-form").addEventListener("submit", async (event) => {
event.preventDefault();
if (!$("host-new-form").reportValidity()) return;
const choice = $("host-new-scenario").value;
if (choice.startsWith(OWN_PREFIX)) {
closeHostDialog($("host-new-dialog"));
const boat = opforRoles.has(S.session?.station);
if (($("host-new-side").value === "uboot") !== boat) {
await mutateStation("/stations/request", {station: boat ? "bridge" : "uboot"});
if (opforRoles.has(S.session?.station) === boat) return;
// The console resyncs its new station first; a command sent now
// would be dropped.
sendHostActionWhenReady("host_start_mission", {key: choice.slice(OWN_PREFIX.length)});
return;
}
sendHostAction("host_start_mission", {key: choice.slice(OWN_PREFIX.length)});
return;
}
const params = {scenario: $("host-new-scenario").value, world_mode: $("host-new-world").value,
weather: $("host-new-weather").value, time: $("host-new-time").value,
length: $("host-new-length").value};
const difficultyInputs = $("host-new-difficulty").querySelectorAll("input");
if (difficultyInputs.length && !difficultyInputs[0].disabled) {
const difficulty = {};
for (const input of difficultyInputs) {
const field = S.hostView.difficulty_fields.find((row) => row.name === input.dataset.field);
difficulty[input.dataset.field] = field.kind === "int"
? parseInt(input.value, 10) : Number(input.value);
}
params.difficulty = difficulty;
}
if ($("host-new-seed").value.trim()) params.seed = $("host-new-seed").valueAsNumber;
closeHostDialog($("host-new-dialog"));
// The side is the solo session's: switch it first, the new world keeps it.
const boat = opforRoles.has(S.session?.station);
if (($("host-new-side").value === "uboot") !== boat) {
await mutateStation("/stations/request", {station: boat ? "bridge" : "uboot"});
if (opforRoles.has(S.session?.station) === boat) return;
// The console resyncs its new station first; a command sent now
// would be dropped.
sendHostActionWhenReady("host_new_game", params);
return;
}
sendHostAction("host_new_game", params);
});
for (const id of ["host-save"]) $(id).addEventListener("click", () => openSlotDialog("save"));
for (const id of ["host-load", "host-screen-load"]) $(id).addEventListener("click", () => openSlotDialog("load"));
for (const id of ["host-new", "host-screen-new"]) $(id).addEventListener("click", openNewGameDialog);
$("host-side").addEventListener("click", switchSoloSide);
$("host-instructor").addEventListener("click", () => {
if (!S.hostView) return;
const dialog = $("instructor-dialog");
const sea = S.v2State?.environment?.sea_state;
$("instructor-sea-state").value = String(Number.isSafeInteger(sea) ? sea : 3);
$("instructor-event").value = "";
dialog.hidden = false;
if (!dialog.open) dialog.showModal();
$("instructor-sea-state").focus();
});
$("instructor-form").addEventListener("submit", (event) => {
event.preventDefault();
const seaState = Number($("instructor-sea-state").value);
if (!Number.isSafeInteger(seaState) || seaState < 0 || seaState > 6) return;
closeHostDialog($("instructor-dialog"));
sendHostAction("host_instructor_environment", {sea_state: seaState,
event: $("instructor-event").value || null});
});
}