Show live Horizon terrain and radar views in the web panel
This commit is contained in:
@@ -73,6 +73,9 @@ const groups = [["units", "unit"], ["objects", "object"], ["contacts", "contact"
|
||||
const prefs = read("view", {layers: {}, views: {}, selected: null});
|
||||
prefs.layers ||= {};
|
||||
prefs.views ||= {};
|
||||
const LIVE_POLL_MS = 650;
|
||||
let mapMode = prefs.mapMode === "radar" ? "radar" : "map";
|
||||
let radarOptions = "";
|
||||
let auth = null, network = null, live = null, historical = null, tab = "live", perspective = "network";
|
||||
let selected = prefs.selected, connected = false, http = false, polling = false, catalog = null;
|
||||
let events = [], raids = [], raid = null, more = false, historyGeneration = 0, frameGeneration = 0, chainGeneration = 0;
|
||||
@@ -81,18 +84,22 @@ let offlineEvents = [], offlineFrames = {}, offlineRaids = {raids: []};
|
||||
let offlineTimelines = {}, selectedEvent = null, timelineGeneration = 0;
|
||||
const timelineRequests = new Map();
|
||||
const timeline = createTimeline($("timeline-markers"), openEvent, time);
|
||||
const viewKey = () => mapMode === "radar" ? `radar:${perspective}` : perspective;
|
||||
const eventFields = {unit: "unit", contact: "target", object: "object"};
|
||||
const eventOrder = {observations: 0, decisions: 1, events: 2};
|
||||
let latestObject = {key: "", checked: 0, event: null};
|
||||
const map = createMap($("map"), {
|
||||
onSelect: key => select(key),
|
||||
onTerrain: message => {
|
||||
if ($("map-source").textContent !== message) $("map-source").textContent = message;
|
||||
},
|
||||
onView: view => {
|
||||
if (historical) return;
|
||||
prefs.views[perspective] = view;
|
||||
prefs.views[viewKey()] = view;
|
||||
write("view", prefs);
|
||||
},
|
||||
});
|
||||
map.restore(prefs.views.network);
|
||||
map.restore(prefs.views[viewKey()]);
|
||||
|
||||
function time(ticks) {
|
||||
const tenths = Math.floor(Math.max(0, ticks) / 2);
|
||||
@@ -111,6 +118,7 @@ function showAuth() {
|
||||
option.hidden = option.value !== "network" && !auth?.permissions?.debug;
|
||||
option.disabled = option.hidden;
|
||||
}
|
||||
$("perspective-label").hidden = !auth?.permissions?.debug;
|
||||
if (!auth?.permissions?.debug) perspective = "network";
|
||||
updateStatus();
|
||||
}
|
||||
@@ -118,9 +126,14 @@ function updateStatus() {
|
||||
const status = $("connection");
|
||||
const updated = live?.updated ?? network?.updated;
|
||||
status.className = connected ? "success" : "warning";
|
||||
status.textContent = connected ? `Игра в сети · ${when(live?.updated)}`
|
||||
status.textContent = connected ? "Игра в сети"
|
||||
: auth ? updated ? `Нет связи · снимок от ${when(updated)}` : "Ожидание игры"
|
||||
: http ? "Ожидает подключения" : "Нет соединения";
|
||||
$("refresh-rate").hidden = !connected;
|
||||
if (connected) {
|
||||
const rate = (live.sampleMs || 1000) / 1000;
|
||||
$("refresh-rate").textContent = `Снимок каждые ${rate.toLocaleString("ru-RU")} с · получен в ${when(live.updated)}`;
|
||||
}
|
||||
$("logout").disabled = !http;
|
||||
}
|
||||
function activeFrame() {
|
||||
@@ -136,9 +149,29 @@ function select(key, centre = false) {
|
||||
draw();
|
||||
if (centre) map.focus(key);
|
||||
}
|
||||
function radarStations(frame) {
|
||||
return frame?.units?.filter(row => row.radarRange > 0) || [];
|
||||
}
|
||||
function draw() {
|
||||
const frame = activeFrame();
|
||||
map.draw(frame, {selected, layers: prefs.layers});
|
||||
const stations = radarStations(frame);
|
||||
const signature = JSON.stringify(stations.map(({id, label}) => [id, label]));
|
||||
if (signature !== radarOptions) {
|
||||
radarOptions = signature;
|
||||
const options = [new Option("Вся сеть", ""), ...stations.map(row => new Option(`${fullname(row)} №${row.id}`, row.id))];
|
||||
$("radar-site").replaceChildren(...options);
|
||||
}
|
||||
const station = stations.find(row => String(row.id) === prefs.radarSite);
|
||||
$("radar-site").value = station ? prefs.radarSite : "";
|
||||
$("radar-site-label").hidden = mapMode !== "radar" || stations.length === 0;
|
||||
for (const node of document.querySelectorAll("[data-map-mode]")) {
|
||||
node.setAttribute("aria-pressed", String(node.dataset.mapMode === mapMode));
|
||||
}
|
||||
const scope = historical ? historical.frame?.terrain : live?.terrain ?? network?.terrain;
|
||||
const center = historical ? null : live?.mapCenter ?? network?.mapCenter;
|
||||
$("player-center").hidden = !center;
|
||||
map.draw(frame, {selected, layers: prefs.layers, mode: mapMode, radarId: $("radar-site").value, scope, center,
|
||||
online: !historical && connected && Boolean(live?.terrain)});
|
||||
$("return-live").hidden = !historical;
|
||||
$("history-live").hidden = !historical;
|
||||
$("perspective").disabled = !!historical;
|
||||
@@ -164,19 +197,32 @@ function draw() {
|
||||
showObject(opened, frame);
|
||||
const filter = $("object-search").value.toLocaleLowerCase("ru");
|
||||
const matched = data.filter(({row}) => !filter || fullname(row).toLocaleLowerCase("ru").includes(filter) || String(row.id) === filter);
|
||||
const oldScroll = $("object-list").scrollTop;
|
||||
const focused = document.activeElement?.dataset.objectKey;
|
||||
const nodes = matched.slice(0, 130).map(({row, key}) => {
|
||||
const node = button([fullname(row), row.status].filter(Boolean).join(" · "), () => select(key, true));
|
||||
const list = $("object-list");
|
||||
const existing = new Map([...list.children].map(node => [node.dataset.objectKey, node]));
|
||||
const nodes = matched.slice(0, 130).map(({row, key, kind}) => {
|
||||
const node = existing.get(key) || button("", () => select(key, true));
|
||||
node.dataset.objectKey = key;
|
||||
node.dataset.kind = kind;
|
||||
node.setAttribute("aria-pressed", String(key === selected));
|
||||
const label = fullname(row);
|
||||
if (!node.firstChild) node.append(make("span", label), make("small", row.status || ""));
|
||||
if (node.firstChild.textContent !== label) node.firstChild.textContent = label;
|
||||
const sublabel = row.status || "";
|
||||
if (node.lastChild.textContent !== sublabel) node.lastChild.textContent = sublabel;
|
||||
node.lastChild.hidden = !sublabel;
|
||||
return node;
|
||||
});
|
||||
if (matched.length > nodes.length) nodes.push(paragraph(`Показано ${nodes.length} из ${matched.length}. Уточните поиск.`));
|
||||
if (!nodes.length) nodes.push(make("p", "Подходящих объектов нет", "empty"));
|
||||
$("object-list").replaceChildren(...nodes);
|
||||
$("object-list").scrollTop = oldScroll;
|
||||
if (focused) nodes.find(node => node.dataset.objectKey === focused)?.focus({preventScroll: true});
|
||||
const note = matched.length > nodes.length ? `Показано ${nodes.length} из ${matched.length}. Уточните поиск.` :
|
||||
nodes.length === 0 ? "Подходящих объектов нет" : "";
|
||||
if (note) {
|
||||
const node = existing.get(undefined) || make("p", "", "empty");
|
||||
if (node.textContent !== note) node.textContent = note;
|
||||
nodes.push(node);
|
||||
}
|
||||
for (const [index, node] of nodes.entries()) {
|
||||
if (list.children[index] !== node) list.insertBefore(node, list.children[index] ?? null);
|
||||
}
|
||||
while (list.children.length > nodes.length) list.lastChild.remove();
|
||||
drawScenario();
|
||||
}
|
||||
function showObject(opened, frame) {
|
||||
@@ -200,6 +246,7 @@ function showObject(opened, frame) {
|
||||
if (row.age != null) nodes.push(paragraph(`Давность ${(row.age / 20).toFixed(1)} с`));
|
||||
if (row.confidence) nodes.push(paragraph(`Достоверность · ${row.confidence}`));
|
||||
if (row.quality != null) nodes.push(paragraph(`Качество · ${row.quality}`));
|
||||
if (row.radarRange > 0) nodes.push(paragraph(`Номинальная дальность радара · ${Math.round(row.radarRange)} бл.`));
|
||||
if (row.radius > 0) nodes.push(paragraph(`Область оценки · ±${Math.round(row.radius)} бл.`));
|
||||
if (row.target) nodes.push(button(`Контакт №${row.target}`, () => select(`contact:${row.target}`, true)));
|
||||
if (row.source) {
|
||||
@@ -255,6 +302,7 @@ function forgetCachedData() {
|
||||
historyGeneration++;
|
||||
clearEvent();
|
||||
cache("all", null);
|
||||
map.clearTerrain();
|
||||
network = live = historical = null;
|
||||
connected = false;
|
||||
latestObject = {key: "", checked: 0, event: null};
|
||||
@@ -297,7 +345,9 @@ async function poll() {
|
||||
if (mode === "network" && data.frame) {
|
||||
if (prefs.world && prefs.world !== data.world) {
|
||||
prefs.selected = null;
|
||||
delete prefs.views.network;
|
||||
delete prefs.radarSite;
|
||||
map.clearTerrain();
|
||||
prefs.views = {};
|
||||
if (!historical) {
|
||||
selected = null;
|
||||
map.restore(null);
|
||||
@@ -375,7 +425,7 @@ function chooseRaid(value) {
|
||||
else {
|
||||
historical = null;
|
||||
selected = perspective === "network" ? prefs.selected : null;
|
||||
map.restore(prefs.views[perspective]);
|
||||
map.restore(prefs.views[viewKey()]);
|
||||
draw();
|
||||
}
|
||||
refreshTimeline();
|
||||
@@ -763,15 +813,41 @@ for (const node of document.querySelectorAll("[data-layer]")) {
|
||||
draw();
|
||||
});
|
||||
}
|
||||
function centerRadar() {
|
||||
const station = radarStations(activeFrame()).find(row => String(row.id) === prefs.radarSite);
|
||||
if (station) map.centerAt(station.x, station.z, station.radarRange);
|
||||
}
|
||||
for (const node of document.querySelectorAll("[data-map-mode]")) {
|
||||
node.addEventListener("click", () => {
|
||||
if (mapMode === node.dataset.mapMode) return;
|
||||
mapMode = prefs.mapMode = node.dataset.mapMode;
|
||||
if (!historical) {
|
||||
map.restore(prefs.views[viewKey()]);
|
||||
if (mapMode === "radar" && !prefs.views[viewKey()]) centerRadar();
|
||||
}
|
||||
write("view", prefs);
|
||||
draw();
|
||||
});
|
||||
}
|
||||
$("radar-site").addEventListener("change", () => {
|
||||
prefs.radarSite = $("radar-site").value;
|
||||
centerRadar();
|
||||
write("view", prefs);
|
||||
draw();
|
||||
});
|
||||
$("object-search").addEventListener("input", draw);
|
||||
$("zoom-in").addEventListener("click", () => map.zoom(1.3));
|
||||
$("zoom-out").addEventListener("click", () => map.zoom(1 / 1.3));
|
||||
$("fit").addEventListener("click", map.fit);
|
||||
$("player-center").addEventListener("click", () => {
|
||||
const center = live?.mapCenter ?? network?.mapCenter;
|
||||
if (center) map.centerAt(...center);
|
||||
});
|
||||
$("perspective").addEventListener("change", () => {
|
||||
perspective = $("perspective").value;
|
||||
selected = perspective === "network" ? prefs.selected : null;
|
||||
live = perspective === "network" ? network : null;
|
||||
map.restore(prefs.views[perspective]);
|
||||
map.restore(prefs.views[viewKey()]);
|
||||
draw();
|
||||
poll();
|
||||
});
|
||||
@@ -784,7 +860,7 @@ function returnLive() {
|
||||
perspective = "network";
|
||||
$("perspective").value = "network";
|
||||
live = network;
|
||||
map.restore(prefs.views.network);
|
||||
map.restore(prefs.views[viewKey()]);
|
||||
setTab("live");
|
||||
poll();
|
||||
}
|
||||
@@ -877,5 +953,5 @@ window.addEventListener("online", async () => {
|
||||
poll();
|
||||
});
|
||||
document.addEventListener("visibilitychange", () => { if (!document.hidden) poll(); });
|
||||
setInterval(poll, 1200);
|
||||
setInterval(poll, LIVE_POLL_MS);
|
||||
start();
|
||||
|
||||
Reference in New Issue
Block a user