mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
A virtual aid (AIS message 21's VirtualAtoN flag) is a mark broadcast where nothing physical stands, so drawing it as a buoy showed a mark that is not there. The vessel source record now carries `virtual` (false unless a source says true; any source may set it), AISStream sets it from VirtualAtoN, and the vessel layer keeps such an aid's disc: vesselModelSpec returns null for it and the layer gathers no model item for it. Its card, selected card and readout add VIRTUAL after its kind.
245 lines
8.3 KiB
JavaScript
245 lines
8.3 KiB
JavaScript
import {
|
|
AID_TO_NAVIGATION_STYLE,
|
|
accentForVesselType,
|
|
displayVesselType,
|
|
normalizeVesselType,
|
|
} from '../../data/vesselLabels.js';
|
|
import { isAidToNavigation } from '../../data/vesselTypes.js';
|
|
import { formatNumber, t } from '../../i18n/index.js';
|
|
import { roundHeadingDegrees } from '../../data/headingDegrees.js';
|
|
|
|
export function createCards({
|
|
vesselState,
|
|
services,
|
|
parts: components,
|
|
layer,
|
|
options,
|
|
}) {
|
|
function updateSelectedVesselHud(record) {
|
|
// Pinned vessels missing from recent refreshes get a stale marker
|
|
const stale = (record.missedRefreshes || 0) > 0;
|
|
// An aid to navigation is a fixed mark: it has no speed or heading to show.
|
|
const aid = isAidToNavigation(record);
|
|
services.readouts?.presentSelectedVessel({
|
|
text: [
|
|
t('AIS: {name}', { name: trimHudValue(record.name, 32) }),
|
|
aid
|
|
? aidKind(
|
|
trimHudValue(record.type || t('AID TO NAVIGATION'), 24),
|
|
record,
|
|
)
|
|
: t('{type} SPD: {speed} HDG: {heading}', {
|
|
type: trimHudValue(record.type || t('VESSEL'), 24),
|
|
speed: formatSpeed(record.speed),
|
|
heading: formatHeading(record.heading ?? record.course),
|
|
}),
|
|
`MMSI: ${record.mmsi || '--'} ${formatPositionTime(record)}${stale ? ` · ${t('STALE')}` : ''}`,
|
|
].join('\n'),
|
|
});
|
|
}
|
|
|
|
function resetSelectedVesselHud() {
|
|
services.readouts?.presentSelectedVessel(null);
|
|
}
|
|
|
|
function trimHudValue(value, maxLength) {
|
|
const text = String(value || '--').trim() || '--';
|
|
return text.length > maxLength
|
|
? `${text.slice(0, maxLength - 3)}...`
|
|
: text;
|
|
}
|
|
|
|
/**
|
|
* Card model for an ambient (decluttered-in) vessel — name title plus one
|
|
* compact type/speed/heading detail line, anchored at the record's current
|
|
* rendered position (height-datum caveat: no datum work here). An aid to
|
|
* navigation's line is its kind alone. Pure — exported for unit tests.
|
|
* @param {Object} record - Vessel record.
|
|
* @returns {Object} vesselLabels entry.
|
|
*/
|
|
|
|
function buildVesselCard(record) {
|
|
const parts = [];
|
|
const aid = isAidToNavigation(record);
|
|
const type = vesselTypeShort(record);
|
|
if (type) parts.push(type);
|
|
if (record.virtual === true) parts.push(t('VIRTUAL'));
|
|
if (!aid && record.speed !== null && record.speed !== undefined)
|
|
parts.push(formatSpeed(record.speed));
|
|
const direction = record.heading ?? record.course;
|
|
if (!aid && Number.isFinite(direction))
|
|
parts.push(`${roundHeadingDegrees(direction)}°`);
|
|
return {
|
|
id: vesselOverlayEntryId(record),
|
|
actionable: Boolean(record?.id),
|
|
position:
|
|
components.rendering.getVisual(record).billboard?.position ||
|
|
components.rendering.getVisual(record).position,
|
|
gapPx: 10,
|
|
accent: vesselAccent(record, aid),
|
|
title: trimHudValue(displayVesselName(record), 26),
|
|
details: parts.length ? [parts.join(' · ')] : [],
|
|
selected: false,
|
|
priority: components.rendering.labelPriority(record, null),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Card model for the click-selected vessel — the full-detail card, drawn last
|
|
* (on top) and never distance-faded by the overlay. Pinned-but-vanished
|
|
* vessels carry a STALE marker (mirrors the HUD readout). An aid to
|
|
* navigation shows its kind with no speed or heading. Pure — exported for
|
|
* unit tests.
|
|
* @param {Object} record - Selected vessel record.
|
|
* @returns {Object} vesselLabels entry.
|
|
*/
|
|
|
|
function buildSelectedVesselCard(record) {
|
|
const aid = isAidToNavigation(record);
|
|
const direction = record.heading ?? record.course;
|
|
const details = [
|
|
aid
|
|
? aidKind(vesselTypeShort(record) || t('AID TO NAVIGATION'), record)
|
|
: [
|
|
vesselTypeShort(record) || t('VESSEL'),
|
|
formatSpeed(record.speed),
|
|
Number.isFinite(direction)
|
|
? `${roundHeadingDegrees(direction)}°`
|
|
: '--°',
|
|
].join(' · '),
|
|
];
|
|
const destination = String(record.destination || '').trim();
|
|
if (destination) details.push(`→ ${trimHudValue(destination, 24)}`);
|
|
const stale = (record.missedRefreshes || 0) > 0;
|
|
details.push(
|
|
`MMSI ${record.mmsi || '--'} · ${formatPositionTime(record)}${stale ? ` · ${t('STALE')}` : ''}`,
|
|
);
|
|
return {
|
|
id: vesselOverlayEntryId(record),
|
|
actionable: Boolean(record?.id),
|
|
position:
|
|
components.rendering.getVisual(record).billboard?.position ||
|
|
components.rendering.getVisual(record).position,
|
|
gapPx: 12,
|
|
accent: vesselAccent(record, aid),
|
|
title: trimHudValue(displayVesselName(record), 32),
|
|
details,
|
|
selected: true,
|
|
priority: 100000,
|
|
};
|
|
}
|
|
|
|
/** An aid's kind, marked virtual when nothing physical stands there. */
|
|
|
|
function aidKind(kind, record) {
|
|
return record.virtual === true ? `${kind} · ${t('VIRTUAL')}` : kind;
|
|
}
|
|
|
|
/** A card's accent: an aid's disc colour, else its ship type's. */
|
|
|
|
function vesselAccent(record, aid) {
|
|
return aid
|
|
? AID_TO_NAVIGATION_STYLE.accent
|
|
: accentForVesselType(record.type);
|
|
}
|
|
|
|
/** Stable overlay identity for keyed and source-retained unkeyed rows. */
|
|
|
|
function vesselOverlayEntryId(record) {
|
|
const id = String(record?.id || '').trim();
|
|
if (id) return `vessel:${id}`;
|
|
const name = String(record?.name || 'VESSEL').trim() || 'VESSEL';
|
|
const lat = Number.isFinite(record?.lat) ? record.lat.toFixed(5) : 'x';
|
|
const lon = Number.isFinite(record?.lon) ? record.lon.toFixed(5) : 'x';
|
|
return `vessel:unkeyed:${name}:${lat}:${lon}`;
|
|
}
|
|
|
|
/**
|
|
* Uppercased AIS type for the card (empty string when unknown). Free feed
|
|
* text is bounded to the card width; a translated type token is a short
|
|
* catalog word and shows whole, never cut mid-word ("AUXILIAR DE PUERTO").
|
|
* An aid's kind is the whole line, so it keeps the room speed and heading
|
|
* would take ("CARDINAL MARK N", not "CARDINAL MARK ").
|
|
*/
|
|
|
|
function vesselTypeShort(record) {
|
|
if (isAidToNavigation(record)) {
|
|
const kind = String(record.type || '').trim();
|
|
return kind ? trimHudValue(kind.toUpperCase(), 26) : '';
|
|
}
|
|
const feedType = normalizeVesselType(record.type);
|
|
const shown = displayVesselType(record.type);
|
|
return shown === feedType
|
|
? feedType.toUpperCase().slice(0, 14)
|
|
: shown.toUpperCase();
|
|
}
|
|
|
|
/**
|
|
* True when `screen` is at least `minSepPx` away from every accepted screen
|
|
* position (greedy card-declutter accept test, mirroring the FIRMS pass).
|
|
* Exported for unit tests.
|
|
* @param {Array<{x: number, y: number}>} accepted - Accepted card positions.
|
|
* @param {{x: number, y: number}} screen - Candidate window coordinates.
|
|
* @param {number} minSepPx - Minimum separation in pixels.
|
|
* @returns {boolean}
|
|
*/
|
|
|
|
function cardScreenSeparated(accepted, screen, minSepPx) {
|
|
const minSq = minSepPx * minSepPx;
|
|
for (let i = 0; i < accepted.length; i += 1) {
|
|
const dx = screen.x - accepted[i].x;
|
|
const dy = screen.y - accepted[i].y;
|
|
if (dx * dx + dy * dy < minSq) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function displayVesselName(record) {
|
|
const name = String(record.name || '').trim();
|
|
// A source may name a vessel by its id when it has no name.
|
|
if (name && name !== 'VESSEL' && name !== record.mmsi && name !== record.id)
|
|
return name;
|
|
return record.mmsi ? `MMSI ${record.mmsi}` : t('VESSEL');
|
|
}
|
|
|
|
function formatSpeed(speed) {
|
|
return t('{speed}KT', {
|
|
speed:
|
|
speed === null
|
|
? '--'
|
|
: formatNumber(speed, {
|
|
minimumFractionDigits: 1,
|
|
maximumFractionDigits: 1,
|
|
useGrouping: false,
|
|
}),
|
|
});
|
|
}
|
|
|
|
function formatHeading(heading) {
|
|
return t('{heading}DEG', {
|
|
heading: roundHeadingDegrees(heading) ?? '--',
|
|
});
|
|
}
|
|
|
|
function formatPositionTime(record) {
|
|
if (!record.lastPositionUtc) return t('POS: LIVE');
|
|
const date = new Date(record.lastPositionUtc);
|
|
if (Number.isNaN(date.getTime())) return t('POS: LIVE');
|
|
return t('POS: {time}Z', { time: date.toISOString().slice(11, 19) });
|
|
}
|
|
return {
|
|
updateSelectedVesselHud,
|
|
resetSelectedVesselHud,
|
|
trimHudValue,
|
|
buildVesselCard,
|
|
buildSelectedVesselCard,
|
|
vesselOverlayEntryId,
|
|
vesselTypeShort,
|
|
cardScreenSeparated,
|
|
displayVesselName,
|
|
formatSpeed,
|
|
formatHeading,
|
|
formatPositionTime,
|
|
};
|
|
}
|