mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-23 11:42:21 +00:00
* fix(voice): voice Cockpit entry no longer turns Contacts on Voice entry now changes no Context mode or detection setting, as the COCKPIT button does not. An entry that names a layer or class searches that layer's own aircraft, nearest first from the followed aircraft or the ground under the camera, and follows the nearest match before entering; the followed aircraft counts when it matches. A military request while Military Flights is off switches that layer on as a plain layer. The class filter is shared with Contacts navigation. * fix(voice): a military Cockpit entry waits for the layer's first data and leaves no layer behind A military entry that switched Military Flights on waits for that layer to report its first data before searching it, bounded by that update, the layer failing or going off, and the request's cancellation. If entry then fails, the layer is switched off again; a layer that was already on stays on. The search covers only the aircraft the person's filters show. * Add the changelog entry
213 lines
6.7 KiB
JavaScript
213 lines
6.7 KiB
JavaScript
/** Pure proximity helpers for Global Context. No capability modeling. */
|
|
|
|
import { msg, t } from '../i18n/index.js';
|
|
import { UNNAMED_PLACE_NAMES } from './installationNames.js';
|
|
import { formatDistanceText } from './distanceFormat.js';
|
|
|
|
const UNNAMED_NAMES = new Set(UNNAMED_PLACE_NAMES);
|
|
|
|
export const AWARENESS_RADIUS_M = 250000;
|
|
export const AWARENESS_MAX_FLIGHT_SEARCH_RADIUS_M = 16000000;
|
|
export const AWARENESS_RELATIONSHIP = Object.freeze({
|
|
NEARBY: 'NEARBY',
|
|
OUTSIDE_RANGE: 'OUTSIDE_RANGE',
|
|
UNKNOWN: 'UNKNOWN',
|
|
});
|
|
|
|
/**
|
|
* Whether an aircraft matches a spoken class filter ("helicopter", "tanker"),
|
|
* on its class or type fields. No filter matches everything; an aircraft
|
|
* with no class or type matches no filter.
|
|
* @param {object} item Aircraft summary (`getNearby` row or cohort item).
|
|
* @param {string|null} aircraftClass Requested class.
|
|
* @returns {boolean}
|
|
*/
|
|
export function aircraftClassMatches(item, aircraftClass) {
|
|
const normalize = (value) =>
|
|
String(value || '')
|
|
.trim()
|
|
.toLowerCase();
|
|
const requested = normalize(aircraftClass);
|
|
if (!requested) return true;
|
|
return [
|
|
item?.aircraftClass,
|
|
item?.klass,
|
|
item?.type,
|
|
item?.typeCode,
|
|
item?.typeName,
|
|
]
|
|
.map(normalize)
|
|
.filter(Boolean)
|
|
.some(
|
|
(candidate) => candidate === requested || candidate.includes(requested),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Search an expanding area synchronously, doubling the radius after each miss.
|
|
* The maximum covers the farthest possible Earth-surface chord while keeping
|
|
* an empty feed bounded.
|
|
*
|
|
* @template T
|
|
* @param {(radiusM: number) => T|null|undefined} search Search callback.
|
|
* @param {{initialRadiusM?: number, maxRadiusM?: number}} [options] Bounds.
|
|
* @returns {{candidate: T, radiusM: number}|null} First match and radius used.
|
|
*/
|
|
export function findByDoublingRadius(
|
|
search,
|
|
{
|
|
initialRadiusM = AWARENESS_RADIUS_M,
|
|
maxRadiusM = AWARENESS_MAX_FLIGHT_SEARCH_RADIUS_M,
|
|
} = {},
|
|
) {
|
|
if (typeof search !== 'function') return null;
|
|
if (!Number.isFinite(initialRadiusM) || initialRadiusM <= 0) return null;
|
|
if (!Number.isFinite(maxRadiusM) || maxRadiusM < initialRadiusM) return null;
|
|
|
|
let radiusM = initialRadiusM;
|
|
while (radiusM <= maxRadiusM) {
|
|
const candidate = search(radiusM);
|
|
if (candidate) return { candidate, radiusM };
|
|
if (radiusM === maxRadiusM) break;
|
|
radiusM = Math.min(radiusM * 2, maxRadiusM);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** @param {number} meters @returns {string} */
|
|
export function formatAwarenessDistance(meters) {
|
|
return formatDistanceText(meters) ?? '—';
|
|
}
|
|
|
|
/**
|
|
* Return a stable visible label without leaking missing values into UI text.
|
|
* The English name the data gives an unnamed installation ("Military area")
|
|
* is shown in the UI language; mapped names and callsigns show as they are.
|
|
*/
|
|
export function formatAwarenessLabel(value) {
|
|
if (value && typeof value === 'object') {
|
|
for (const candidate of [
|
|
value.callsign,
|
|
value.name,
|
|
value.label,
|
|
value.mmsi,
|
|
value.id,
|
|
value.icao24,
|
|
]) {
|
|
const label = formatAwarenessLabel(candidate);
|
|
if (label !== '—') return label;
|
|
}
|
|
return '—';
|
|
}
|
|
if (typeof value === 'string') {
|
|
const text = value.trim();
|
|
if (!text) return '—';
|
|
return UNNAMED_NAMES.has(text) ? t(text) : text;
|
|
}
|
|
if (Number.isFinite(value)) return String(value);
|
|
return '—';
|
|
}
|
|
|
|
/**
|
|
* Summarize sorted nearby results without promoting missing/stale feeds to a
|
|
* negative finding. `UNKNOWN` is an input-data state, never an out-of-range
|
|
* claim in this MVP. `reason` is English data; the awareness panel shows it
|
|
* in the UI language.
|
|
*/
|
|
export function summarizeAwarenessCohort(
|
|
items,
|
|
{ available = true, stale = false, limit = 3 } = {},
|
|
) {
|
|
if (!available)
|
|
return {
|
|
relationship: AWARENESS_RELATIONSHIP.UNKNOWN,
|
|
count: null,
|
|
nearest: [],
|
|
reason: msg('feed unavailable'),
|
|
};
|
|
if (stale)
|
|
return {
|
|
relationship: AWARENESS_RELATIONSHIP.UNKNOWN,
|
|
count: null,
|
|
nearest: [],
|
|
reason: msg('feed stale'),
|
|
};
|
|
const normalized = (Array.isArray(items) ? items : [])
|
|
.filter(
|
|
(item) =>
|
|
Number.isFinite(item?.distanceM ?? item?.distance) &&
|
|
(item.distanceM ?? item.distance) >= 0,
|
|
)
|
|
.map((item) => ({ ...item, distanceM: item.distanceM ?? item.distance }))
|
|
.sort((a, b) => a.distanceM - b.distanceM);
|
|
return {
|
|
relationship: normalized.length
|
|
? AWARENESS_RELATIONSHIP.NEARBY
|
|
: AWARENESS_RELATIONSHIP.UNKNOWN,
|
|
count: normalized.length,
|
|
nearest: normalized.slice(0, limit),
|
|
reason: normalized.length
|
|
? msg('observed or mapped nearby context')
|
|
: msg('no observed or mapped objects in current feeds'),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Return selectable nearby targets in a stable, subject-first order.
|
|
* @param {Array<{id: string, summary?: {nearest?: Array<Object>}}>} cohorts Awareness cohorts.
|
|
* @param {{layerId?: string}|null} [subject] Current subject.
|
|
* @param {Iterable<string>} [visitedKeys] Previously visited layer/id keys.
|
|
* @returns {Array<{layerId: string, id: string, item: Object}>} Selectable targets.
|
|
*/
|
|
export function getAwarenessNavigationTargets(
|
|
cohorts,
|
|
subject = null,
|
|
visitedKeys = [],
|
|
) {
|
|
const visited = new Set(visitedKeys);
|
|
const targets = [];
|
|
for (const cohort of Array.isArray(cohorts) ? cohorts : []) {
|
|
const layerId = cohort?.id;
|
|
if (
|
|
![
|
|
'flights',
|
|
'military',
|
|
'ais-live-vessels',
|
|
'military-installations',
|
|
].includes(layerId)
|
|
)
|
|
continue;
|
|
const sourceItems = Array.isArray(cohort?.summary?.navigationNearest)
|
|
? cohort.summary.navigationNearest
|
|
: Array.isArray(cohort?.summary?.nearest)
|
|
? cohort.summary.nearest
|
|
: [];
|
|
for (const item of sourceItems) {
|
|
const id = item?.icao24 || item?.id || item?.mmsi;
|
|
if (!id) continue;
|
|
const key = `${layerId}:${id}`;
|
|
if (key === `${subject?.layerId}:${subject?.id}`) continue;
|
|
targets.push({
|
|
layerId,
|
|
id: String(id),
|
|
item,
|
|
visited: visited.has(key),
|
|
});
|
|
}
|
|
}
|
|
targets.sort((a, b) => {
|
|
// UNVISITED outranks layer affinity. Ordering subject-layer first put
|
|
// already-visited same-layer contacts ahead of unvisited contacts in every
|
|
// other layer, so NEXT ping-ponged inside the subject's own layer and never
|
|
// reached vessels, installations, or the other flight layer at all.
|
|
const visitedOrder = Number(a.visited) - Number(b.visited);
|
|
if (visitedOrder) return visitedOrder;
|
|
const subjectOrder =
|
|
Number(a.layerId !== subject?.layerId) -
|
|
Number(b.layerId !== subject?.layerId);
|
|
if (subjectOrder) return subjectOrder;
|
|
return (a.item.distanceM ?? Infinity) - (b.item.distanceM ?? Infinity);
|
|
});
|
|
return targets;
|
|
}
|