mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-23 11:42:21 +00:00
Find a selected vessel or aircraft again from its card
While a selected vessel, aircraft or military aircraft is off screen, behind the globe or out of its layer's feed, its Context card shows Find it. One still in its feed is flown to (an aircraft followed). One out of its feed is looked up on its own track at the layer's time (the world clock's while the layer follows it), the card moves to that reading and says how old it is, and the camera flies there; once the feed has it again its layer selects it again. With no reading at or before the time the card says so and the camera stays. A vessel its layer announces is named by its own id, as a click names it, not by the context store's.
This commit is contained in:
1 parent
b57d50fe18
commit
22e9c58d84
27 files changed
+833
-22
No files matched your search
@@ -93,6 +93,26 @@ export const useEntityCardStore = defineStore('gev-entity-card', () => {
|
||||
const lost = ref(false);
|
||||
/** Bumped on each refresh, so ages shown on the card tick. */
|
||||
const tick = ref(0);
|
||||
/**
|
||||
* True while the item is not to be seen: out of its feed, off screen or
|
||||
* behind the globe. The card then offers to find it.
|
||||
*/
|
||||
const away = ref(false);
|
||||
/**
|
||||
* Finding the item again (see locate): `'idle'`, `'finding'` while its
|
||||
* layer looks it up, or `'none'` when it has no reading at the time.
|
||||
*/
|
||||
const finding = ref('idle');
|
||||
/**
|
||||
* Where an item out of its feed was found (see locate):
|
||||
* `{lat, lon, altitudeM, observedAtMs, atMs}`, or null.
|
||||
*/
|
||||
const located = shallowRef(null);
|
||||
/** The track reference of the card's item, kept for when it leaves its feed. */
|
||||
let reference = null;
|
||||
let locating = null;
|
||||
/** An aircraft found again is followed once it is back in its feed. */
|
||||
let followWhenBack = false;
|
||||
/**
|
||||
* What the camera shows, for the panel to describe when nothing is picked:
|
||||
* `{center: {lat, lon}, rect: {west, south, east, north}, heightM}`.
|
||||
@@ -229,12 +249,105 @@ export const useEntityCardStore = defineStore('gev-entity-card', () => {
|
||||
// Keep showing the last reading when the item drops out of its feed.
|
||||
if (!next || next.key !== current.key || next.basis !== 'record') {
|
||||
lost.value = current.basis === 'record';
|
||||
away.value = lost.value || !shown(current);
|
||||
tick.value++;
|
||||
return;
|
||||
}
|
||||
const wasLost = lost.value;
|
||||
card.value = markRaw(next);
|
||||
lost.value = false;
|
||||
// The camera follows it where it is drawn, which can be ahead of its last
|
||||
// reading; one being followed is always in view.
|
||||
away.value = !isFollowing() && !shown(next);
|
||||
tick.value++;
|
||||
if (wasLost) back(next);
|
||||
}
|
||||
|
||||
/** Whether the camera shows the item; true when the shell cannot tell. */
|
||||
function shown(current) {
|
||||
const test = shell.value?.onScreen;
|
||||
return typeof test === 'function' ? test(current) !== false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The item is back in its feed: its layer selects it again, as the click
|
||||
* that picked it did, and an aircraft just found is followed.
|
||||
*/
|
||||
function back(current) {
|
||||
const follows = followWhenBack;
|
||||
// A lookup still running is no longer needed: the feed has it.
|
||||
stopLocating();
|
||||
reference =
|
||||
layerOf(current.layerId)?.referenceOf?.(current.identity?.id) ??
|
||||
reference;
|
||||
shell.value?.reselect?.(current, { follow: follows });
|
||||
}
|
||||
|
||||
const layerOf = (layerId) =>
|
||||
shell.value?.dataManager?.()?.layers?.get?.(layerId)?.module ?? null;
|
||||
|
||||
/**
|
||||
* Take the camera back to the item. One the camera can see or one still in
|
||||
* its feed is flown to (an aircraft followed); one out of its feed is
|
||||
* looked up at the layer's time (the world clock's while it follows the
|
||||
* clock) on its own track, shown there and flown to, and its layer picks
|
||||
* it up again once its feed has it.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function locate() {
|
||||
const current = card.value;
|
||||
if (!current) return;
|
||||
if (!lost.value) {
|
||||
if (canFollow()) follow();
|
||||
else flyTo();
|
||||
return;
|
||||
}
|
||||
const layer = layerOf(current.layerId);
|
||||
if (typeof layer?.locate !== 'function') {
|
||||
flyTo();
|
||||
return;
|
||||
}
|
||||
locating?.abort();
|
||||
const controller = new AbortController();
|
||||
locating = controller;
|
||||
finding.value = 'finding';
|
||||
let result;
|
||||
try {
|
||||
result = await layer.locate(current.identity?.id, {
|
||||
reference,
|
||||
signal: controller.signal,
|
||||
});
|
||||
} catch {
|
||||
result = { status: 'none' };
|
||||
}
|
||||
if (locating !== controller) return;
|
||||
locating = null;
|
||||
const now = card.value;
|
||||
if (!now || now.key !== current.key) return;
|
||||
if (result?.status !== 'found') {
|
||||
finding.value = 'none';
|
||||
return;
|
||||
}
|
||||
finding.value = 'idle';
|
||||
located.value = markRaw({ ...result });
|
||||
card.value = markRaw({
|
||||
...now,
|
||||
lat: result.lat,
|
||||
lon: result.lon,
|
||||
altitudeM: Number.isFinite(result.altitudeM)
|
||||
? result.altitudeM
|
||||
: now.altitudeM,
|
||||
});
|
||||
followWhenBack = canFollow();
|
||||
shell.value?.flyTo?.(card.value);
|
||||
}
|
||||
|
||||
function stopLocating() {
|
||||
locating?.abort();
|
||||
locating = null;
|
||||
finding.value = 'idle';
|
||||
located.value = null;
|
||||
followWhenBack = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -261,6 +374,11 @@ export const useEntityCardStore = defineStore('gev-entity-card', () => {
|
||||
*/
|
||||
function select(identity, { fly = false, follow: autoFollow = true } = {}) {
|
||||
if (!read(identity)) return false;
|
||||
stopLocating();
|
||||
away.value = false;
|
||||
reference =
|
||||
layerOf(identity.layerId)?.referenceOf?.(card.value?.identity?.id) ??
|
||||
null;
|
||||
clearInterval(timer);
|
||||
timer = setInterval(refresh, REFRESH_MS);
|
||||
shell.value?.openPanel?.();
|
||||
@@ -292,6 +410,9 @@ export const useEntityCardStore = defineStore('gev-entity-card', () => {
|
||||
timer = null;
|
||||
card.value = null;
|
||||
lost.value = false;
|
||||
away.value = false;
|
||||
reference = null;
|
||||
stopLocating();
|
||||
}
|
||||
|
||||
const flyTo = () => card.value && shell.value?.flyTo?.(card.value);
|
||||
@@ -324,6 +445,10 @@ export const useEntityCardStore = defineStore('gev-entity-card', () => {
|
||||
card,
|
||||
lost,
|
||||
tick,
|
||||
away,
|
||||
finding,
|
||||
located,
|
||||
locate,
|
||||
connect,
|
||||
select,
|
||||
clear,
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { createPinia } from 'pinia';
|
||||
import { useEntityCardStore } from './entityCard.js';
|
||||
|
||||
/**
|
||||
* A vessels layer whose one vessel can leave its feed, and a shell that
|
||||
* records what the card asks of it.
|
||||
*/
|
||||
function setup({ found = true } = {}) {
|
||||
const vessel = {
|
||||
id: 'v1',
|
||||
name: 'ARCADIA',
|
||||
lat: 1,
|
||||
lon: 10,
|
||||
reference: 'vessel:v1',
|
||||
};
|
||||
let inFeed = true;
|
||||
const asked = [];
|
||||
const module = {
|
||||
getAnalystRecords: () => (inFeed ? [{ ...vessel }] : []),
|
||||
referenceOf: (id) => (id === 'v1' && inFeed ? vessel.reference : null),
|
||||
async locate(id, { reference }) {
|
||||
asked.push({ id, reference });
|
||||
return found
|
||||
? {
|
||||
status: 'found',
|
||||
lat: 5,
|
||||
lon: 50,
|
||||
altitudeM: null,
|
||||
observedAtMs: 1_000,
|
||||
atMs: 61_000,
|
||||
}
|
||||
: { status: 'none' };
|
||||
},
|
||||
};
|
||||
const dataManager = {
|
||||
isEnabled: () => true,
|
||||
layers: new Map([['ais-live-vessels', { module }]]),
|
||||
getAll: () => [],
|
||||
};
|
||||
const shell = {
|
||||
flown: [],
|
||||
followed: [],
|
||||
reselected: [],
|
||||
visible: true,
|
||||
dataManager: () => dataManager,
|
||||
openPanel() {},
|
||||
flyTo(card) {
|
||||
shell.flown.push([card.lat, card.lon]);
|
||||
},
|
||||
follow(card) {
|
||||
shell.followed.push(card.key);
|
||||
},
|
||||
reselect(card, options) {
|
||||
shell.reselected.push([card.identity.id, options.follow]);
|
||||
},
|
||||
onScreen: () => shell.visible,
|
||||
canFollow: () => false,
|
||||
};
|
||||
const store = useEntityCardStore(createPinia());
|
||||
store.connect(shell);
|
||||
return {
|
||||
store,
|
||||
shell,
|
||||
asked,
|
||||
leave: () => (inFeed = false),
|
||||
come: () => (inFeed = true),
|
||||
};
|
||||
}
|
||||
|
||||
test('a vessel out of its feed is looked up, shown where it was and flown to', async (t) => {
|
||||
const { store, shell, asked, leave, come } = setup();
|
||||
t.after(() => store.clear());
|
||||
assert.equal(store.select({ layerId: 'ais-live-vessels', id: 'v1' }), true);
|
||||
assert.equal(store.away, false, 'on screen and in its feed');
|
||||
|
||||
leave();
|
||||
store.refresh();
|
||||
assert.equal(store.lost, true);
|
||||
assert.equal(store.away, true, 'out of its feed: the card offers to find it');
|
||||
|
||||
await store.locate();
|
||||
assert.deepEqual(
|
||||
asked,
|
||||
[{ id: 'v1', reference: 'vessel:v1' }],
|
||||
'by the reference it had',
|
||||
);
|
||||
assert.equal(store.card.lat, 5);
|
||||
assert.equal(store.card.lon, 50);
|
||||
assert.deepEqual(shell.flown, [[5, 50]]);
|
||||
assert.equal(store.located.observedAtMs, 1_000);
|
||||
assert.equal(store.finding, 'idle');
|
||||
|
||||
come();
|
||||
store.refresh();
|
||||
assert.equal(store.lost, false, 'back in its feed');
|
||||
assert.equal(store.located, null);
|
||||
assert.deepEqual(
|
||||
shell.reselected,
|
||||
[['v1', false]],
|
||||
'its layer selects it again',
|
||||
);
|
||||
});
|
||||
|
||||
test('a vessel with no reading at the time says so and stays put', async (t) => {
|
||||
const { store, shell, leave } = setup({ found: false });
|
||||
t.after(() => store.clear());
|
||||
store.select({ layerId: 'ais-live-vessels', id: 'v1' });
|
||||
leave();
|
||||
store.refresh();
|
||||
await store.locate();
|
||||
assert.equal(store.finding, 'none');
|
||||
assert.deepEqual(shell.flown, [], 'the camera stays');
|
||||
assert.equal(store.card.lat, 1, 'the card keeps its last reading');
|
||||
});
|
||||
|
||||
test('an item in its feed but off screen is flown to without a lookup', async (t) => {
|
||||
const { store, shell, asked } = setup();
|
||||
t.after(() => store.clear());
|
||||
store.select({ layerId: 'ais-live-vessels', id: 'v1' });
|
||||
shell.visible = false;
|
||||
store.refresh();
|
||||
assert.equal(store.away, true, 'off screen');
|
||||
await store.locate();
|
||||
assert.deepEqual(asked, []);
|
||||
assert.deepEqual(shell.flown, [[1, 10]]);
|
||||
});
|
||||
|
||||
test('an item the camera follows is never away, wherever its last reading is', (t) => {
|
||||
const { store, shell } = setup();
|
||||
t.after(() => store.clear());
|
||||
store.select({ layerId: 'ais-live-vessels', id: 'v1' });
|
||||
shell.visible = false;
|
||||
store.following = store.card.key;
|
||||
store.refresh();
|
||||
assert.equal(store.away, false);
|
||||
});
|
||||
|
||||
test('a lookup still running when the item comes back is dropped', async (t) => {
|
||||
const { store, shell, leave, come } = setup();
|
||||
t.after(() => store.clear());
|
||||
store.select({ layerId: 'ais-live-vessels', id: 'v1' });
|
||||
leave();
|
||||
store.refresh();
|
||||
const pending = store.locate();
|
||||
come();
|
||||
store.refresh();
|
||||
await pending;
|
||||
assert.equal(store.lost, false);
|
||||
assert.equal(store.located, null, 'no old reading on a live card');
|
||||
assert.equal(store.card.lat, 1, 'the card keeps its live position');
|
||||
assert.deepEqual(shell.flown, [], 'the camera stays');
|
||||
});
|
||||
Reference in new issue
Block a user