Tidy the Rail and Transit ride fixes after review

- A train first seen standing takes its direction from the operator's
  course, not from a course drawn out of GPS jitter.
- Moving a hold to another vehicle releases the first one the same way as
  ending the hold.
- repaint always goes through placeSample, which hides the held vehicle and
  puts it on its current segment, so setShown is no longer exported.
- Tests cover the icon right after a hold, a segment with one known floor,
  a known floor at 0 m and selection by click after the layer comes back;
  shared helpers replace repeated setup.
- Comments, CURRENT-STATE and the release note say what the code does
  without internal figures.
This commit is contained in:
German Khokhlov committed 2026-11-18 15:07:06 +04:00
1 parent ee5a6ab317
commit df4d76384c
8 files changed
+209 -90

No files matched your search

+10 -7
View File
@@ -173,14 +173,16 @@ export function createRailCockpitSubject({
/**
* Which way the train runs along its shape: as its binding learnt it from
* successive reports, else from its drawn course against the line's
* bearing there; 0 while neither tells.
* successive reports, else from its reported bearing against the line's
* bearing there; 0 while neither tells. Not the drawn course: until the
* binding tells, the train has not moved past GPS jitter, and a course
* drawn between such reports is that jitter.
*/
function directionOf(entry) {
const binding = entry.path;
if (binding.direction === 1 || binding.direction === -1)
return binding.direction;
const course = finiteOrNull(entry.courseDeg);
const course = finiteOrNull(entry.record?.bearing);
const point = pointAtS(binding.path, binding.drawS, scratch);
if (course === null || !Number.isFinite(point?.bearingDeg)) return 0;
const turn = Math.abs(((course - point.bearingDeg + 540) % 360) - 180);
@@ -205,11 +207,12 @@ export function createRailCockpitSubject({
finiteOrNull(drawn?.lat) ?? finiteOrNull(sample?.lat) ?? record.lat;
position.longitude =
finiteOrNull(drawn?.lon) ?? finiteOrNull(sample?.lon) ?? record.lon;
// Quality 0 is the ellipsoid a train is drawn on while no floor is
// known: no height, so a view waits for the surface.
const quality = Number.isFinite(drawn?.q) ? drawn.q : 0;
// Quality 0: no floor known here, so no height; a view waits for the
// surface.
position.railHeightM =
Number.isFinite(drawn?.h) && drawn.q > 0 ? drawn.h - liftM : null;
position.heightQuality = Number.isFinite(drawn?.q) ? drawn.q : 0;
quality > 0 && Number.isFinite(drawn.h) ? drawn.h - liftM : null;
position.heightQuality = quality;
position.atMs = finiteOrNull(drawn?.at);
let path = null;
+33
View File
@@ -151,6 +151,39 @@ test('a train drawn on an unknown floor reports no rail height', async (t) => {
);
});
test('a train on a known floor at sea level reports a rail height of 0 m', async (t) => {
const app = await onTheLine(t, {
ground: { ...WARM_GROUND, cachedGroundFloor: () => 0 },
});
app.parts().selection.selectTrain(T1);
const { position } = app.layer.getSelectedTrain();
assert.ok(position.heightQuality > 0, 'the floor is known');
assert.equal(position.railHeightM, 0, 'a height, not none');
});
test('a train first seen standing takes no direction from its GPS jitter', async (t) => {
// Neither reports 13 m apart nor the course drawn between them say which way
// it runs; an operator's bearing does.
const app = harness(t, { paths: { 'mbta-rail': PACK }, at: BOSTON });
let jitterM = 0;
app.feeds.set('mbta-rail', () => [
train('T1', LAT, LON0 + (1000 + jitterM) / KX),
train('T2', LAT, LON0 + (3000 + jitterM) / KX, { bearing: 270 }),
]);
await app.enable();
const seen = [];
for (const next of [13, 0, 13, 0, 13, 0]) {
jitterM = next;
app.run(RAIL_POLL_MS);
await app.poll();
app.parts().selection.selectTrain(T1);
const standing = app.layer.getSelectedTrain().path.direction;
app.parts().selection.selectTrain(T2);
seen.push([standing, app.layer.getSelectedTrain().path.direction]);
}
assert.deepEqual(seen, Array(6).fill([0, -1]));
});
test('a snapshot read again while nothing changed is the one read before', async (t) => {
let list = stopList('Alpha', 'Bravo', 'Charlie');
const app = await onTheLine(t, {
+13 -24
View File
@@ -209,8 +209,8 @@ test('a binding learns which way its train runs, and keeps it through a stop', (
assert.equal(along.direction, 0, 'one report tells no direction');
assert.ok(followFix(along, fix(10_000, 700, 0)));
assert.equal(along.direction, 1);
// Standing at a stop, its reports jitter back a few metres: it keeps what
// it learnt.
// Standing at a stop, its reports jitter back 10 m: it keeps what it
// learnt.
assert.ok(followFix(along, fix(20_000, 690, 0)));
assert.equal(along.direction, 1);
const against = bindTrainPath(p, ids, fix(0, 1500, 0));
@@ -269,33 +269,22 @@ test('jitter segments played back at a standing train keep the direction it lear
const ids = { tripId: 'full', routeId: 'main' };
const binding = bindTrainPath(p, ids, fix(0, 1000, 0));
assert.ok(followFix(binding, fix(15_000, 1600, 0)));
// Playback runs the train east, 1000 m -> 1600 m: direction +1.
const out = {};
let seq = 0;
const play = (fromM, toM, t0) => {
const segment = {
from: { seq, ...fix(t0, fromM, 0) },
to: { seq: seq + 1, ...fix(t0 + 15_000, toM, 0) },
};
assert.ok(
placeOnPath(
binding,
{ fromSeq: seq, toSeq: seq + 1, fraction: 0.5 },
segment,
out,
),
const { sample, segment } = playback(
seq,
seq + 1,
fix(t0, fromM, 0),
fix(t0 + 15_000, toM, 0),
0.5,
);
assert.ok(placeOnPath(binding, sample, segment, {}));
seq += 1;
return binding.direction;
};
assert.equal(play(1000, 1600, 0), 1);
assert.equal(
binding.directionPlayed,
true,
'playback owns the direction now',
);
// At the platform: reports 3-5 m apart, back and forth, polled and played.
const learnt = [];
// It runs east, then stands at the platform: reports 3-5 m apart, back and
// forth, polled and played.
const learnt = [play(1000, 1600, 0)];
let t0 = 15_000;
for (const [fromM, toM] of [
[1600, 1597],
@@ -307,7 +296,7 @@ test('jitter segments played back at a standing train keep the direction it lear
learnt.push(play(fromM, toM, t0));
t0 += 15_000;
}
assert.deepEqual(learnt, [1, 1, 1, 1], 'from the played-back segments');
assert.deepEqual(learnt, [1, 1, 1, 1, 1], 'from the played-back segments');
});
test('GPS jitter in the newest reports before playback keeps the direction', () => {