Files
gods-eye-view/src/ui/cockpitOrbitalSignals.js
T
German Khokhlov 69fa1bf222 i18n: follow-up to the review of the plural and context fixes
- The street-level plan step names its place the way show_street_level
  reads its arguments: a pointed or numbered thing has no place yet, and
  "this spot" needs both coordinates.
- The view-centre error message is removed: the Street Level layer always
  gives its own translated text for an empty search, so it never showed.
  "the view centre" is one exported constant for both voice modules.
- The orbital site status uses the word of the FOCUS card in es, fr, it,
  pt and he, and the Wildlife chip agrees with "avistamientos" and
  "avvistamenti".
- CURRENTLY OVER is the noun for an overflight in de, uk, it and pt,
  shorter than before, so the place name keeps its room on a phone.
- The glossaries record the new terms; I18N.md words two rules more
  exactly.
2026-11-16 12:46:22 +04:00

262 lines
9.0 KiB
JavaScript

/**
* The Live Signals rows in the orbital view. Orbit feeds no Contacts
* snapshot, so the rows are built here, the way the aircraft rows are built
* from one (src/ui/cockpitSignals.js): the TRACK ACQUIRED row, the nearest
* mapped sites in reach (a row focuses its site, or releases the one held),
* a RETURN row to the satellite ridden before, and the nearest satellites (a
* row rides it). With Mapped Installations off a status row says why no
* site is listed. Five rows at most, with stable keys, rebuilt on their own
* clock. Bound to the Cockpit controller.
*/
import { formatNumber, t } from '../i18n/index.js';
import { ORBITAL_REACH_REFRESH_MS } from '../cockpitOrbitalFocus.js';
import { bearingBetweenCoordinates, relativeBearing } from '../cockpitMath.js';
import {
installationClassLabel,
installationTitle,
} from '../layers/installations/classLabel.js';
import { formatCockpitDistance } from './cockpitPresentation.js';
import { orbitalSitesInReach } from './cockpitOrbitalFocus.js';
import {
orbitalNearbySatellites,
previousOrbitalSatellite,
} from './cockpitOrbitalSatellites.js';
/** The most rows Live Signals shows, as for aircraft. */
const SIGNAL_ROWS = 5;
/** Site rows when there are satellites to list too. */
const SITE_ROWS = 2;
const SITES_LAYER_ID = 'military-installations';
function inOrbit(controller) {
return (
controller.active &&
!controller.destroyed &&
controller.subjectKind === 'satellite' &&
Boolean(controller.orbital)
);
}
function sitesLayerOn(controller) {
const isLayerEnabled = controller.services?.isLayerEnabled;
return typeof isLayerEnabled === 'function'
? isLayerEnabled(SITES_LAYER_ID) !== false
: false;
}
/** Where another satellite is, from the top of the frame, worded. */
function bearingWords(info, satellite) {
const bearing = bearingBetweenCoordinates(
info.latitude,
info.longitude,
satellite.latitude,
satellite.longitude,
);
// Relative to the top of the frame: the ground track, or north.
const relative = relativeBearing(
bearing,
Number.isFinite(info.groundTrackDeg) ? info.groundTrackDeg : 0,
);
if (relative === null) return t('BRG —');
if (Math.abs(relative) < 8) return t('AHEAD');
return t('{side} {angle}', {
side: relative < 0 ? t('L') : t('R'),
angle: `${String(Math.round(Math.abs(relative))).padStart(3, '0')}°`,
});
}
/** The ids the rows of one layer show now, in their order. */
function shownIds(controller, layerId, keyPrefix) {
return controller.signalItems
.filter((item) => item.key.startsWith(keyPrefix))
.filter((item) => item.target?.layerId === layerId)
.map((item) => String(item.target.id));
}
/**
* Order rows so those already shown keep their places while they still
* qualify; new ones fill after them, nearest first. Sites pass under a low
* orbit in seconds: re-sorting by angle every beat would move a row away
* from the pointer reaching for it.
* @param {Array<object>} entries Nearest first.
* @param {(entry: object) => string} keyOf
* @param {Array<string>} shown Ids shown now, in order.
* @param {string|null} [first] An id that goes first (the focused site).
*/
function stickyOrder(entries, keyOf, shown, first = null) {
const byKey = new Map(entries.map((entry) => [keyOf(entry), entry]));
const ordered = [];
const take = (key) => {
const entry = byKey.get(key);
if (!entry) return;
ordered.push(entry);
byKey.delete(key);
};
if (first !== null) take(first);
for (const key of shown) take(key);
for (const entry of entries) take(keyOf(entry));
return ordered;
}
/** The site rows: the focused site first, then those shown, then the nearest. */
function siteRows(info, { settled = true } = {}) {
if (!sitesLayerOn(this)) {
// Also forgets the sites last listed, so none is focused from a row.
orbitalSitesInReach.call(this, info);
return [
{
key: 'site-status',
tone: 'warning',
title: t('SITES STANDBY'),
detail: t('ENABLE MAPPED INSTALLATIONS FOR SITE PINGS'),
target: null,
},
];
}
if (!settled) return [];
const { entries } = orbitalSitesInReach.call(this, info);
const focused = entries.find((entry) => entry.focused);
return stickyOrder(
entries,
(entry) => String(entry.site.id),
shownIds(this, SITES_LAYER_ID, 'site:'),
focused ? String(focused.site.id) : null,
).map(({ site, offNadirDeg, focused }) => {
const title = installationTitle(site.name) || t('Unnamed site');
const angle = t('OFF-NADIR {angle}°', {
angle: formatNumber(offNadirDeg, {
minimumFractionDigits: 1,
maximumFractionDigits: 1,
}),
});
const words = [installationClassLabel(site.class), angle];
// A status, as on the focus card, not the command: es «ENFOQUE», not «ENFOCAR».
if (focused) words.unshift(t('FOCUS', { context: 'site status' }));
return {
key: `site:${site.id}`,
// The held site reads as the subject does.
tone: focused ? 'track' : 'nearby',
title,
detail: words.join(' · '),
target: {
layerId: SITES_LAYER_ID,
id: String(site.id),
label: focused
? t('Release the camera from {name}', { name: title })
: t('Keep the camera on {name}', { name: title }),
},
};
});
}
/** The satellite rows: RETURN to the one before, then the nearest. */
function satelliteRows(info) {
const state = this.orbital;
state.satelliteNames ??= new Map();
state.satelliteNames.set(String(info.noradId), info.name);
const rows = [];
const back = previousOrbitalSatellite.call(this);
if (back !== null) {
const name = state.satelliteNames.get(String(back)) ?? String(back);
rows.push({
key: `satellite-return:${back}`,
tone: 'nearby',
title: t('RETURN · {name}', { name }),
detail: t('PREVIOUS SATELLITE'),
target: {
layerId: 'satellites',
id: String(back),
label: t('Return to {name}', { name }),
},
});
}
// Measured only once the camera is on the ridden satellite.
const measured = !state.hop && !state.awaitingFrame;
const nearby = measured
? stickyOrder(
orbitalNearbySatellites.call(this),
(satellite) => String(satellite.id),
shownIds(this, 'satellites', 'satellite:'),
)
: [];
for (const satellite of nearby) {
if (back !== null && String(satellite.id) === String(back)) continue;
const name = String(satellite.label ?? satellite.id);
state.satelliteNames.set(String(satellite.id), name);
rows.push({
key: `satellite:${satellite.id}`,
tone: 'nearby',
title: name,
detail: [
t('SATELLITE'),
bearingWords(info, satellite),
formatCockpitDistance(satellite.distanceM),
].join(' · '),
target: {
layerId: 'satellites',
id: String(satellite.id),
label: t('Ride {name}', { name }),
},
});
}
return rows;
}
/**
* Rebuild the orbital Live Signals rows now: after a focus change, a
* satellite switch, or on their clock. TRACK ACQUIRED stays first; sites
* take two rows when satellites fill the rest, more when they do not.
*/
export function refreshOrbitalSignals() {
if (!inOrbit(this)) return;
const info = this.lastSubjectInfo;
if (info?.kind !== 'satellite') return;
this.orbital.signalsMs = performance.now();
const previous = new Map(this.signalItems.map((item) => [item.key, item]));
const rows = [];
const track = this.signalItems.find((item) => item.key === 'track');
if (track) rows.push(track);
// While the camera hops to a new satellite its frame is not the
// satellite's yet: the site rows wait for it (the status row does not).
const settled = !this.orbital.hop && !this.orbital.awaitingFrame;
const sites = siteRows.call(this, info, { settled });
const satellites = satelliteRows.call(this, info);
const free = SIGNAL_ROWS - rows.length;
rows.push(...sites.slice(0, Math.max(SITE_ROWS, free - satellites.length)));
rows.push(...satellites.slice(0, SIGNAL_ROWS - rows.length));
const now = Date.now();
this.signalItems = rows.map((row) => ({
...row,
timestamp: previous.get(row.key)?.timestamp ?? row.timestamp ?? now,
}));
this.signalSignatures.clear();
this.publishCockpitSignals();
}
/**
* The rows' clock: rebuild once every ORBITAL_REACH_REFRESH_MS.
* @param {number} nowMs
*/
export function updateOrbitalSignals(nowMs) {
const state = this.orbital;
if (!state) return;
if (nowMs - (state.signalsMs ?? -Infinity) < ORBITAL_REACH_REFRESH_MS) return;
refreshOrbitalSignals.call(this);
}
/**
* A click on an orbital row: a site row focuses its site or releases it, a
* satellite row rides that satellite (RETURN goes back).
* @param {string} layerId
* @param {string} id
* @returns {boolean} True in orbit, where the row is this module's.
*/
export function handleOrbitalSignalClick(layerId, id) {
if (!inOrbit(this)) return false;
if (layerId === SITES_LAYER_ID) this.toggleOrbitalFocus(id);
else if (layerId === 'satellites') this.rideOrbitalSatellite(id);
return true;
}