mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16:47 +00:00
154c88b48a
A visit's End could be edited and was shown in road trip mode, but the schedule never read it: the drive always left a stop after its stay, so a traveller who knew when they had to leave but not how long they would stand there had no way to say so. The End is now the time the drive leaves. RoadtripStop.leaveAt is filled from the visit's End (or its place's) on the client by useRoadtripRoutes and on the server by roadtrip-plan.service, so calculate_roadtrip and get_roadtrip_context agree with the planner, and computeSchedule takes it as the departure it already knew how to hold. With Start and End both set the stay is the time between them. With daily travel times on, the stay is the time until then, and an End before the window start starts the day early the way a Start does. Before the first fixed time the chain works backwards through an End as it does through a pinned Start. An End is read against the drive: the last passing of that clock time counts as missed when the drive went past it on the road or earlier the same day, anything else is the next one, so a drive across midnight neither waits a whole day nor pretends to be on time. A drive that gets there after the End leaves on arrival and files a missedLeave finding, shown beside the late badge in the rail and on the phone. The rail's STAY badge, the inspector's Stay tile, the phone stop sheet and the phone chain show the stay the End makes, with an "until 14:00" hint. It is read off the schedule and never written to duration_minutes: the stay belongs to the place, the End to one visit, and a stored copy would go stale as soon as the arrival moved. For such a stop the stay dialog, desktop and phone, shows that stay read only and offers to remove the End, through the visit's time route with an offline path (assignmentRepo.setTimes, setAssignmentTimes). While the road trip is on, the End field in both place forms says the drive leaves at that time. The dead checkoutAt in the plan service is gone. New strings in all 23 locales.
373 lines
15 KiB
TypeScript
373 lines
15 KiB
TypeScript
import { lineMetres, projectOntoRoute, sliceAtMeters } from './corridor';
|
|
import type { RoadtripDayBoundary } from './day-boundary.schema';
|
|
import type { SpillChain } from './nightSpill';
|
|
import type { RoadtripDay, RoadtripStop, RoutedLeg } from './planning-types';
|
|
import type { DistanceUnit } from './planning-types';
|
|
import { formatClock, hasChosenArrival, leaveAfter, parseClock } from './roadtripModel';
|
|
import { formatDurationShort } from './roadtripModel';
|
|
import { formatDistance } from './units';
|
|
|
|
export interface DayWindow {
|
|
start: number;
|
|
end: number;
|
|
endMode?: 'route' | 'stop';
|
|
}
|
|
|
|
export interface AutomaticNight {
|
|
phase: 'start' | 'end';
|
|
fromDayNumber: number;
|
|
position?: number;
|
|
manual?: boolean;
|
|
}
|
|
|
|
type PlanDay = Pick<RoadtripDay, 'dayId' | 'dayNumber' | 'date' | 'title' | 'stops'>;
|
|
type LegLookup = (from: RoadtripStop, to: RoadtripStop) => RoutedLeg | undefined;
|
|
|
|
export function dayWindow(start: unknown, end: unknown, endMode?: unknown): DayWindow | null {
|
|
const minutes = (clock: unknown): number | null => {
|
|
if (typeof clock !== 'string' || !/^([01]\d|2[0-3]):[0-5]\d$/.test(clock)) return null;
|
|
return Number(clock.slice(0, 2)) * 60 + Number(clock.slice(3));
|
|
};
|
|
const from = minutes(start);
|
|
const to = minutes(end);
|
|
return from !== null && to !== null && to > from
|
|
? { start: from, end: to, ...(endMode === 'stop' ? ({ endMode } as const) : {}) }
|
|
: null;
|
|
}
|
|
|
|
function portion(leg: RoutedLeg, from: number, to: number, unit: DistanceUnit): RoutedLeg {
|
|
if (from === 0 && to === 1) return leg;
|
|
const coordinates = leg.line.length ? leg.line : [leg.seg.from, leg.seg.to];
|
|
const spine = coordinates.map(([lat, lng]) => ({ lat, lng }));
|
|
const metres = lineMetres(spine);
|
|
const line =
|
|
metres === 0
|
|
? coordinates
|
|
: sliceAtMeters(spine, from * metres, to * metres).map((p) => [p.lat, p.lng] as [number, number]);
|
|
const distance = leg.seg.distance * (to - from);
|
|
const duration = leg.seg.duration * (to - from);
|
|
const durationText = formatDurationShort(duration);
|
|
const distanceText = formatDistance(distance / 1000, unit);
|
|
return {
|
|
seg: {
|
|
...leg.seg,
|
|
distance,
|
|
duration,
|
|
distanceText,
|
|
durationText,
|
|
drivingText: `${distanceText} · ${durationText}`,
|
|
walkingText: `${distanceText} · ${durationText}`,
|
|
from: line[0]!,
|
|
to: line[line.length - 1]!,
|
|
mid: line[Math.floor(line.length / 2)]!,
|
|
},
|
|
line,
|
|
vias: leg.vias.filter((v) => {
|
|
const along = (projectOntoRoute(v, spine)?.alongKm ?? 0) * 1000;
|
|
return along >= from * metres && (to === 1 ? along <= metres : along < to * metres);
|
|
}),
|
|
};
|
|
}
|
|
|
|
function stationary(stop: RoadtripStop): RoutedLeg {
|
|
const at: [number, number] = [stop.lat, stop.lng];
|
|
return {
|
|
line: [],
|
|
vias: [],
|
|
seg: {
|
|
from: at,
|
|
to: at,
|
|
mid: at,
|
|
distance: 0,
|
|
duration: 0,
|
|
mode: 'driving',
|
|
walkingText: '',
|
|
drivingText: '',
|
|
distanceText: '',
|
|
},
|
|
};
|
|
}
|
|
|
|
export interface WindowPlan {
|
|
chains: SpillChain[];
|
|
legFor: LegLookup;
|
|
issue: 'incomplete' | 'conflict' | 'tooLong' | 'legTooLong' | null;
|
|
}
|
|
|
|
export function planDayWindow(
|
|
days: PlanDay[],
|
|
window: DayWindow,
|
|
lookup: LegLookup,
|
|
unit: DistanceUnit,
|
|
labels: { start: string; end: string },
|
|
boundaries: RoadtripDayBoundary[] = [],
|
|
): WindowPlan {
|
|
const ordered = [...days].sort((a, b) => a.dayNumber - b.dayNumber);
|
|
const stops = ordered.flatMap((day) => day.stops.map((stop) => ({ stop, day })));
|
|
const failed = (issue: WindowPlan['issue']): WindowPlan => ({ chains: [], legFor: lookup, issue });
|
|
if (!stops.length) return { chains: [], legFor: lookup, issue: null };
|
|
const positions = new Map(stops.map(({ stop }, i) => [stop.assignmentId, i]));
|
|
const targets = new Map<number, number>();
|
|
let lastTarget = -1;
|
|
for (const boundary of [...boundaries].sort((a, b) => a.day_number - b.day_number)) {
|
|
const from = positions.get(boundary.from_assignment_id);
|
|
const to = boundary.to_assignment_id === null ? null : positions.get(boundary.to_assignment_id);
|
|
if (from === undefined || (to !== null && to !== from + 1)) return failed('conflict');
|
|
const target = from + (to === null ? 0 : boundary.fraction);
|
|
if (target <= lastTarget) return failed('conflict');
|
|
targets.set(boundary.day_number, target);
|
|
lastTarget = target;
|
|
}
|
|
const legs = stops.slice(0, -1).map((s, i) => lookup(s.stop, stops[i + 1]!.stop));
|
|
if (
|
|
legs.some(
|
|
(l) => !l || !Number.isFinite(l.seg.duration) || l.seg.duration < 0 || (l.line.length < 2 && l.seg.distance > 0),
|
|
)
|
|
) {
|
|
return failed('incomplete');
|
|
}
|
|
|
|
const chains = new Map<number, SpillChain>();
|
|
const pieces = new Map<RoadtripStop, Map<RoadtripStop, RoutedLeg>>();
|
|
const firstDay = stops[0]!.day.dayNumber;
|
|
let number = firstDay;
|
|
let clock = window.start;
|
|
let issue: WindowPlan['issue'] = null;
|
|
let position = 0;
|
|
|
|
const chainAt = (n: number): SpillChain => {
|
|
const existing = chains.get(n);
|
|
if (existing) return existing;
|
|
const stored = ordered.find((d) => d.dayNumber === n);
|
|
const dated = ordered.find((d) => d.date);
|
|
let date = stored?.date ?? null;
|
|
if (!stored && dated?.date) {
|
|
const dayDate = new Date(`${dated.date}T12:00:00Z`);
|
|
dayDate.setUTCDate(dayDate.getUTCDate() + n - dated.dayNumber);
|
|
date = dayDate.toISOString().slice(0, 10);
|
|
}
|
|
const chain: SpillChain = {
|
|
dayId: stored?.dayId ?? -1000000000 - n,
|
|
dayNumber: n,
|
|
date,
|
|
title: stored?.title ?? null,
|
|
stops: [],
|
|
schedule: { entries: [], warnings: [] },
|
|
spills: [],
|
|
};
|
|
chains.set(n, chain);
|
|
return chain;
|
|
};
|
|
const putLeg = (from: RoadtripStop, to: RoadtripStop, leg: RoutedLeg) => {
|
|
if (!pieces.has(from)) pieces.set(from, new Map());
|
|
pieces.get(from)!.set(to, leg);
|
|
};
|
|
const append = (stop: RoadtripStop, arrival: number, departure = arrival) => {
|
|
const chain = chainAt(number);
|
|
chain.stops.push(stop);
|
|
chain.schedule.entries.push({
|
|
arrival: formatClock(Math.round(arrival)),
|
|
departure: formatClock(Math.round(departure)),
|
|
anchored: hasChosenArrival(stop),
|
|
dayOffset: Math.floor(arrival / 1440),
|
|
});
|
|
};
|
|
const night = (at: RoadtripStop, final = false): RoadtripStop => {
|
|
const chain = chainAt(number);
|
|
const last = chain.stops[chain.stops.length - 1]!;
|
|
const end: RoadtripStop = {
|
|
...at,
|
|
assignmentId: -2000000000 - number * 2,
|
|
name: labels.end,
|
|
time: null,
|
|
leaveAt: undefined,
|
|
dwellMinutes: 0,
|
|
checkInTime: undefined,
|
|
stopType: null,
|
|
fillPercent: null,
|
|
automaticNight: { phase: 'end', fromDayNumber: number, position, manual: targets.has(number) },
|
|
};
|
|
append(end, clock);
|
|
if (last) putLeg(last, end, stationary(at));
|
|
if (final) return end;
|
|
number += 1;
|
|
if (number - firstDay > 365) issue = 'tooLong';
|
|
clock = window.start;
|
|
const start: RoadtripStop = {
|
|
...end,
|
|
assignmentId: end.assignmentId - 1,
|
|
name: labels.start,
|
|
automaticNight: { ...end.automaticNight!, phase: 'start' },
|
|
};
|
|
append(start, clock);
|
|
return start;
|
|
};
|
|
|
|
let previous: RoadtripStop | undefined;
|
|
for (let i = 0; i < stops.length; i++) {
|
|
const { stop, day } = stops[i]!;
|
|
const pin = parseClock(stop.time);
|
|
const leave = parseClock(stop.leaveAt);
|
|
// When the drive into this stop set out, on this day's clock: for one a night broke
|
|
// up, the morning it went on.
|
|
let setOut: number | null = null;
|
|
if (previous && number < day.dayNumber && !targets.has(number)) {
|
|
if (window.endMode !== 'stop') clock = Math.max(clock, window.end);
|
|
previous = night(previous);
|
|
while (number < day.dayNumber && !issue) {
|
|
clock = window.end;
|
|
previous = night(previous);
|
|
}
|
|
} else if (!previous) {
|
|
number = day.dayNumber;
|
|
// A time set to leave the first stop starts the day early the way a pinned arrival
|
|
// does. Otherwise a ferry at seven on a day whose travel hours begin at nine was
|
|
// reported missed by two hours, when nothing but the preference made it late.
|
|
clock = pin ?? (leave === null ? window.start : Math.min(window.start, leave));
|
|
}
|
|
if (issue) return failed(issue);
|
|
if (pin !== null && day.dayNumber !== number) return failed('conflict');
|
|
|
|
if (previous) {
|
|
const leg = legs[i - 1]!;
|
|
const minutes = leg.seg.duration / 60;
|
|
// The same for the first stop after a night: the morning starts early enough to get
|
|
// there by its pinned time, or to leave it by the time it is set to be left at. A
|
|
// pin that cannot be made is a conflict. A leave time is only ever missed, so one
|
|
// out of reach even from midnight moves nothing and says so once the stop is reached.
|
|
let early: number | null = null;
|
|
if (pin !== null) early = pin - minutes;
|
|
else if (leave !== null && leave - minutes >= 0) early = leave - minutes;
|
|
if (early !== null && early < clock && chainAt(number).stops.every((s) => s.automaticNight)) {
|
|
clock = early;
|
|
if (clock < 0) return failed('conflict');
|
|
const startEntry = chainAt(number).schedule.entries[0]!;
|
|
startEntry.arrival = formatClock(Math.round(clock));
|
|
startEntry.departure = formatClock(Math.round(clock));
|
|
}
|
|
if (pin !== null && clock + minutes > pin + 1) return failed('conflict');
|
|
|
|
let fraction = 0;
|
|
const split = pin === null && (leg.seg.mode === 'driving' || leg.seg.mode === undefined);
|
|
if (split && !targets.has(number) && window.endMode === 'stop' && clock + minutes > window.end + 0.000001) {
|
|
if (minutes > window.end - window.start) return failed('legTooLong');
|
|
previous = night(previous);
|
|
if (issue) return failed(issue);
|
|
}
|
|
while (fraction < 1) {
|
|
const target = targets.get(number);
|
|
if (target !== undefined && target < i - 1 + fraction - 0.000001) return failed('conflict');
|
|
const manualCut = target !== undefined && target < i && target >= i - 1 + fraction;
|
|
if (!manualCut && (target !== undefined || !split || clock + minutes * (1 - fraction) <= window.end + 0.000001))
|
|
break;
|
|
const available = manualCut ? minutes * (target - (i - 1 + fraction)) : Math.max(0, window.end - clock);
|
|
if (available === 0) {
|
|
previous = night(previous);
|
|
} else {
|
|
const until = Math.min(1, fraction + available / minutes);
|
|
const part = portion(leg, fraction, until, unit);
|
|
const at = part.line[part.line.length - 1]!;
|
|
const point = { ...previous, lat: at[0], lng: at[1] };
|
|
clock += available;
|
|
position = i - 1 + until;
|
|
const before = previous;
|
|
previous = night(point);
|
|
const previousStops = chainAt(number - 1).stops;
|
|
const ending = previousStops[previousStops.length - 1]!;
|
|
putLeg(before, ending, part);
|
|
fraction = until;
|
|
}
|
|
if (issue) return failed(issue);
|
|
}
|
|
const remainder = portion(leg, fraction, 1, unit);
|
|
setOut = clock;
|
|
clock += minutes * (1 - fraction);
|
|
putLeg(previous, stop, remainder);
|
|
}
|
|
position = i;
|
|
if (pin !== null && number !== day.dayNumber) return failed('conflict');
|
|
const target = targets.get(number);
|
|
if (target !== undefined && target < position) return failed('conflict');
|
|
if (pin !== null) clock = pin;
|
|
if (pin === null && number === day.dayNumber) clock = Math.max(clock, parseClock(stop.checkInTime) ?? 0);
|
|
append(stop, clock);
|
|
previous = stop;
|
|
|
|
// How long the traveller is here, spent against the clock rather than against the
|
|
// driving hours. The night between two of them is not a pause in the stay: standing
|
|
// somewhere for twenty-four hours takes twenty-four hours, and counting only the
|
|
// hours the window is open stretched a single night over three days.
|
|
//
|
|
// A leave time decides the stay on its own: the traveller is here until then, however
|
|
// long the stop usually takes. Reached after it, the drive leaves at once and the
|
|
// stop says by how much it missed.
|
|
const overnight = Math.max(0, 1440 - window.end + window.start);
|
|
const left = leave === null ? null : leaveAfter(clock, leave, setOut);
|
|
if (left && left.missedBy !== null) {
|
|
const chain = chainAt(number);
|
|
chain.schedule.warnings.push({ index: chain.stops.length - 1, code: 'missedLeave', minutes: left.missedBy });
|
|
}
|
|
let dwell = left ? left.departure - clock : Math.max(0, stop.dwellMinutes ?? 0);
|
|
while (dwell > 0) {
|
|
const remaining = targets.has(number) ? dwell : Math.max(0, window.end - clock);
|
|
const spend = Math.min(dwell, remaining);
|
|
clock += spend;
|
|
dwell -= spend;
|
|
const entries = chainAt(number).schedule.entries;
|
|
entries[entries.length - 1]!.departure = formatClock(Math.round(clock));
|
|
if (dwell > 0) {
|
|
previous = night(previous);
|
|
if (issue) return failed(issue);
|
|
// The hours the window was shut passed too.
|
|
dwell = Math.max(0, dwell - overnight);
|
|
if (dwell === 0) {
|
|
const resumed = chainAt(number).schedule.entries;
|
|
resumed[resumed.length - 1]!.departure = formatClock(Math.round(clock));
|
|
}
|
|
}
|
|
}
|
|
if (target === position || (!targets.has(number) && stop.endDay && i < stops.length - 1)) {
|
|
previous = night(previous, i === stops.length - 1);
|
|
if (issue) return failed(issue);
|
|
}
|
|
}
|
|
|
|
if ([...targets.keys()].some((day) => !chains.get(day)?.stops.some((stop) => stop.automaticNight?.phase === 'end')))
|
|
return failed('conflict');
|
|
|
|
for (const chain of chains.values()) {
|
|
for (let i = 0; i < chain.stops.length; i++) {
|
|
const stop = chain.stops[i]!;
|
|
if (stop.automaticNight) continue;
|
|
const owner = ordered.find((d) => d.dayId === stop.ownerDayId);
|
|
if (!owner || owner.dayNumber === chain.dayNumber) continue;
|
|
const last = chain.spills[chain.spills.length - 1]!;
|
|
if (last?.fromDayNumber === owner.dayNumber && last.at + last.count === i) last.count += 1;
|
|
else
|
|
chain.spills.push({
|
|
automatic: true,
|
|
at: i,
|
|
count: 1,
|
|
fromDayNumber: owner.dayNumber,
|
|
departure: null,
|
|
leg: undefined,
|
|
line: [],
|
|
fromStop: undefined,
|
|
});
|
|
}
|
|
}
|
|
for (const day of ordered) chainAt(day.dayNumber);
|
|
return {
|
|
chains: [...chains.values()].sort((a, b) => a.dayNumber - b.dayNumber),
|
|
legFor: (from, to) => pieces.get(from)?.get(to),
|
|
issue: null,
|
|
};
|
|
}
|
|
|
|
export function roadtripInsertion(day: Pick<RoadtripDay, 'stops'>, index: number) {
|
|
const stop = day.stops[Math.min(Math.max(0, index), day.stops.length - 1)]!;
|
|
return stop
|
|
? { dayId: stop.ownerDayId, position: stop.ownerIndex + (stop.automaticNight || index >= day.stops.length ? 1 : 0) }
|
|
: null;
|
|
}
|