diff --git a/client/src/components/Roadtrip/RoadtripSidebarBookingRows.test.tsx b/client/src/components/Roadtrip/RoadtripSidebarBookingRows.test.tsx index 1640420f1..ed4cce035 100644 --- a/client/src/components/Roadtrip/RoadtripSidebarBookingRows.test.tsx +++ b/client/src/components/Roadtrip/RoadtripSidebarBookingRows.test.tsx @@ -102,6 +102,23 @@ describe('TerminalStop', () => { expect(screen.getByText('HAM')).toBeInTheDocument() }) + it('FE-ROADTRIP-BOOKINGROWS-001d: a place the booking is driven through reads the place, not the booking', () => { + // The booking's name belongs on the desks, where the car changes hands. On a place + // the drive only passes through, it says the same thing twice and names no place: + // a car taken Hanover → Osnabrück → Bremen read "Weird Roadtrip" where Osnabrück goes. + wrap( + , + ) + expect(screen.getByText('Osnabrück')).toBeInTheDocument() + expect(screen.queryByText('Weird Roadtrip')).toBeNull() + }) + it('FE-ROADTRIP-BOOKINGROWS-001c: a check-in the drive cannot make takes no pill on the card, neither the check-in nor that it is missed', () => { wrap( s.settings.time_format) === '12h' const { end, desk, checkIn } = terminalReading(stop, entry, late, t, is12h) + // Only ever called for a stop a booking put on the day, which is what carries one. + const carrier = stop.carrier! + // A place the booking is driven through is that place. The booking's own name belongs + // on the desks, where the car changes hands and the name is what the traveller asks + // for; on the road between them it says the same thing twice and names no place. + const title = carrier.role === 'stop' ? end.place : (booking?.title ?? end.place) return ( : entry?.arrival ? : null} checkIn={checkIn?.state === 'ok' ? checkIn : null} starts={starts} diff --git a/client/src/components/Roadtrip/useRoadtripRoutes.test.ts b/client/src/components/Roadtrip/useRoadtripRoutes.test.ts index 30116c409..47bf25b64 100644 --- a/client/src/components/Roadtrip/useRoadtripRoutes.test.ts +++ b/client/src/components/Roadtrip/useRoadtripRoutes.test.ts @@ -1146,6 +1146,99 @@ describe('a booking the traveller rides (#2428)', () => { expect(result.current.totalStops).toBe(2) expect(result.current.lines).toHaveLength(3) }) + + it('FE-ROADTRIP-ROUTES-069: the drive to the return desk runs over the stops the booking names', async () => { + // Reported on a hire car booked with a stop in the middle: the day plan routed over + // that stop and the road trip drew the short way round, which is a different + // journey from the one booked. + calculateRouteWithLegs.mockImplementation(async (points: { lat: number; lng: number }[]) => hourly(points)) + const viaBerlin = flight({ + id: 72, + type: 'car', + title: 'Sixt Hamburg', + reservation_time: '09:00', + reservation_end_time: '18:00', + endpoints: [ + { role: 'from', sequence: 0, name: 'Hamburg', code: null, lat: HAMBURG[0], lng: HAMBURG[1], timezone: null, local_time: null, local_date: null }, + { role: 'stop', sequence: 1, name: 'Lüneburg', code: null, lat: LUENEBURG[0], lng: LUENEBURG[1], timezone: null, local_time: null, local_date: null }, + { role: 'to', sequence: 2, name: 'Berlin', code: null, lat: BERLIN[0], lng: BERLIN[1], timezone: null, local_time: null, local_date: null }, + ], + }) + const { result } = renderHook(() => useRoadtripRoutes(7, [day(1, 1)], map(1, []), 'driving', {}, [], [], [viaBerlin])) + await waitFor(() => expect(result.current.loading).toBe(false)) + + // Two desks with the place it is driven through standing between them as a stop of + // the drive, and the road asked for runs over it. + expect(result.current.days[0].stops.map(s => `${s.carrier?.role}:${s.name}`)) + .toEqual(['pickup:Hamburg', 'stop:Lüneburg', 'return:Berlin']) + // Driven through and nothing more: it holds the drive up by no minutes at all. + expect(result.current.days[0].stops[1]).toMatchObject({ dwellMinutes: 0, time: null }) + const asked = calculateRouteWithLegs.mock.calls.map(c => c[0].map((p: { lat: number }) => p.lat)) + expect(asked).toEqual([[HAMBURG[0], LUENEBURG[0], BERLIN[0]]]) + }) + + it('FE-ROADTRIP-ROUTES-070: the destination standing on the day as well keeps the road on the booking', async () => { + // The usual shape: the car goes back to a desk at the destination, and the destination + // is on the day as a place too (a stay seats it there, a clock here), so the run holds + // a stop of the day's own between the two desks. The booking's own stop keeps its + // place behind the pick-up, and the drive still passes through it first. + calculateRouteWithLegs.mockImplementation(async (points: { lat: number; lng: number }[]) => hourly(points)) + const viaBerlin = flight({ + id: 72, + type: 'car', + title: 'Sixt Hamburg', + reservation_time: '09:00', + reservation_end_time: '18:00', + endpoints: [ + { role: 'from', sequence: 0, name: 'Hamburg', code: null, lat: HAMBURG[0], lng: HAMBURG[1], timezone: null, local_time: null, local_date: null }, + { role: 'stop', sequence: 1, name: 'Lüneburg', code: null, lat: LUENEBURG[0], lng: LUENEBURG[1], timezone: null, local_time: null, local_date: null }, + { role: 'to', sequence: 2, name: 'Berlin', code: null, lat: BERLIN[0], lng: BERLIN[1], timezone: null, local_time: null, local_date: null }, + ], + }) + const berlinOnTheDay = map(1, [{ id: 1, at: BERLIN, time: '17:00' }]) + const { result } = renderHook(() => useRoadtripRoutes(7, [day(1, 1)], berlinOnTheDay, 'driving', {}, [], [], [viaBerlin])) + await waitFor(() => expect(result.current.loading).toBe(false)) + + expect(result.current.days[0].stops.map(s => s.carrier?.role ?? 'day')).toEqual(['pickup', 'stop', 'day', 'return']) + const asked = calculateRouteWithLegs.mock.calls.map(c => c[0].map((p: { lat: number }) => p.lat)) + // Hamburg → Lüneburg → the day's Berlin stop → the desk there, not Hamburg → Berlin. + expect(asked).toEqual([[HAMBURG[0], LUENEBURG[0], BERLIN[0], BERLIN[0]]]) + }) + + it('FE-ROADTRIP-ROUTES-071: the drive back to the desk is asked for across midnight without one continuous drive', async () => { + // A car taken out in the morning and handed back at 02:00 the next day. The road to + // the desk is the booking's own, not the gap a day-at-a-time routing leaves between + // two cards, so it is asked for with "one continuous drive" off, as it is here. + calculateRouteWithLegs.mockImplementation(async (points: { lat: number; lng: number }[]) => hourly(points)) + const overnight = flight({ + id: 73, + type: 'car', + title: 'Sixt Hamburg', + day_id: 1, + end_day_id: 2, + reservation_time: '06:00', + reservation_end_time: '02:00', + endpoints: [ + { role: 'from', sequence: 0, name: 'Hamburg', code: null, lat: HAMBURG[0], lng: HAMBURG[1], timezone: null, local_time: null, local_date: null }, + { role: 'stop', sequence: 1, name: 'Lüneburg', code: null, lat: LUENEBURG[0], lng: LUENEBURG[1], timezone: null, local_time: null, local_date: null }, + { role: 'to', sequence: 2, name: 'Berlin', code: null, lat: BERLIN[0], lng: BERLIN[1], timezone: null, local_time: null, local_date: null }, + ], + }) + const days = [day(1, 1), day(2, 2)] + const { result } = renderHook(() => useRoadtripRoutes(7, days, map(1, []), 'driving', {}, [], [], [overnight])) + await waitFor(() => expect(result.current.loading).toBe(false)) + + // A desk on each day, and the place it is driven through on the day it set out. + expect(result.current.days.map(d => d.stops.map(s => `${s.carrier?.role}:${s.name}`))).toEqual([ + ['pickup:Hamburg', 'stop:Lüneburg'], + ['return:Berlin'], + ]) + await waitFor(() => { + const asked = calculateRouteWithLegs.mock.calls.map(c => c[0].map((p: { lat: number }) => p.lat)) + // The day's own road, then the one leading to the desk it goes back to. + expect(asked).toEqual([[HAMBURG[0], LUENEBURG[0]], [LUENEBURG[0], BERLIN[0]]]) + }) + }) }) /** diff --git a/client/src/components/Roadtrip/useRoadtripRoutes.ts b/client/src/components/Roadtrip/useRoadtripRoutes.ts index c5b796af5..06b3e10b0 100644 --- a/client/src/components/Roadtrip/useRoadtripRoutes.ts +++ b/client/src/components/Roadtrip/useRoadtripRoutes.ts @@ -701,6 +701,18 @@ export function useRoadtripRoutes( const to = routingChains[d + 1].stops[0] if (from && to) want(from, to, routingChains[d + 1].dayId) } + } else { + // The drive back to a hire car's desk is the booking's own road, not the gap + // between two cards, so it is drawn whether or not one continuous drive was asked + // for. A car taken out in the morning and handed back after midnight had the day + // it set out on drawn and nothing from there to the desk, which reads as a drive + // that stops halfway. + for (let d = 1; d < routingChains.length; d++) { + const to = routingChains[d].stops[0] + if (to?.carrier?.role !== 'return') continue + const from = routingChains[d - 1].stops[routingChains[d - 1].stops.length - 1] + if (from) want(from, to, routingChains[d].dayId) + } } return { seams: out, seamMisses: misses } }, [chains, plan, quietDays, window, legsByDay, seamLegs, rideLegs, viasByDay, connectDays, legModeOf, avoidKey, distanceUnit]) diff --git a/client/tests/unit/mobile/trip/MRoadtripRows.test.tsx b/client/tests/unit/mobile/trip/MRoadtripRows.test.tsx index 5a657cbad..2c251963d 100644 --- a/client/tests/unit/mobile/trip/MRoadtripRows.test.tsx +++ b/client/tests/unit/mobile/trip/MRoadtripRows.test.tsx @@ -293,7 +293,7 @@ function carrier(role: CarrierTerminal['role'], over: Partial = role, title: car ? 'Sixt Hamburg' : 'LH 2020', code: role === 'departure' ? 'HAM' : role === 'arrival' ? 'MUC' : null, - at: { departure: '13:20', arrival: '14:30', pickup: '09:00', return: '11:30' }[role], + at: { departure: '13:20', arrival: '14:30', pickup: '09:00', return: '11:30', stop: null }[role], ...over, } } diff --git a/server/src/nest/roadtrip/roadtrip-plan.service.ts b/server/src/nest/roadtrip/roadtrip-plan.service.ts index 23728ab3a..49c6df4e1 100644 --- a/server/src/nest/roadtrip/roadtrip-plan.service.ts +++ b/server/src/nest/roadtrip/roadtrip-plan.service.ts @@ -224,9 +224,11 @@ export class RoadtripPlanService { stops.forEach((stop, index) => { stopAt.push(points.length); points.push({ lat: stop.lat, lng: stop.lng }); - // None bend the drive from or to a booked night's hotel (`viasOnLeg`). - if (index < stops.length - 1) + // A via dropped on the leg shapes the drive between its stops. None bend the drive + // from or to a booked night's hotel (`viasOnLeg`). + if (index < stops.length - 1) { points.push(...viasOnLeg(stop, stops[index + 1], context.vias).map((v) => ({ lat: v.lat, lng: v.lng }))); + } }); try { if (points.length > 100) throw new Error('Too many waypoints'); diff --git a/shared/src/assistant/cards/roadtrip.card.ts b/shared/src/assistant/cards/roadtrip.card.ts index 7334b985d..bf3c9c024 100644 --- a/shared/src/assistant/cards/roadtrip.card.ts +++ b/shared/src/assistant/cards/roadtrip.card.ts @@ -69,7 +69,7 @@ export const roadtripCardStopSchema = z.object({ .object({ reservationId: idSchema, type: z.string().min(1).max(20), - role: z.enum(['departure', 'arrival', 'pickup', 'return']), + role: z.enum(['departure', 'arrival', 'pickup', 'return', 'stop']), title: cardText(80), code: z.string().min(1).max(8).nullable(), at: cardClockSchema.nullable(), diff --git a/shared/src/roadtrip/assemble.spec.ts b/shared/src/roadtrip/assemble.spec.ts index 1b12116ba..3c774babd 100644 --- a/shared/src/roadtrip/assemble.spec.ts +++ b/shared/src/roadtrip/assemble.spec.ts @@ -118,10 +118,10 @@ describe('assembleRoadtrip connected days', () => { ], }); - function assembleTwoDays(connectDays: boolean) { + function assembleTwoDays(connectDays: boolean, opensSecond: Partial = {}) { const first = [stop({ ownerIndex: 0 }), stop({ ownerIndex: 1 }), stop({ ownerIndex: 2 })]; const second = [ - stop({ ownerDayId: 2, ownerIndex: 0, assignmentId: 300, placeId: 400, lat: 60 }), + stop({ ownerDayId: 2, ownerIndex: 0, assignmentId: 300, placeId: 400, lat: 60, ...opensSecond }), stop({ ownerDayId: 2, ownerIndex: 1, assignmentId: 301, placeId: 401, lat: 61 }), ]; const allLegs: Record = {}; @@ -201,6 +201,23 @@ describe('assembleRoadtrip connected days', () => { expect(routes.days[1]!.geometry[0]).toEqual([60, 10]); }); + it('ROADTRIP-ASSEMBLE-028: the drive back to a hire car’s desk is drawn without one continuous drive', () => { + // A car taken out in the morning and handed back after midnight: the desk opens the + // day it goes back on, and the road to it is the booking's own rather than the gap a + // day-at-a-time routing leaves. Drawn only for a connected trip, the day it set out + // on was drawn and nothing led from there to the desk, which reads as a drive that + // stops halfway. + const routes = assembleTwoDays(false, { + carrier: { reservationId: 42, type: 'car', role: 'return', title: 'Sixt Hamburg', code: null, at: '02:00' }, + }); + + expect(routes.days[1]!.arrivingFrom).toEqual(expect.objectContaining({ ownerDayId: 1, ownerIndex: 2 })); + expect(routes.days[1]!.geometry[0]).toEqual([52, 10]); + // Still the road leading into the day it arrives on, the way a connected one is. + expect(routes.lineDays).toEqual([1, 1, 2, 2]); + expect(routes.lineJoins).toEqual([false, false, true, false]); + }); + it('ROADTRIP-ASSEMBLE-006: that drive is marked as the connection it is, in the colour of the day it arrives on (#3123)', () => { const routes = assembleTwoDays(true); diff --git a/shared/src/roadtrip/assemble.ts b/shared/src/roadtrip/assemble.ts index 8585cab7b..3f14d183a 100644 --- a/shared/src/roadtrip/assemble.ts +++ b/shared/src/roadtrip/assemble.ts @@ -120,14 +120,21 @@ export function assembleRoadtrip({ let arrivingLeg: RouteSegment | undefined; let arrivingLine: [number, number][] | undefined; let arrivingFrom: RoadtripStop | undefined; + const first = chain.stops[0]; + // The drive back to a hire car's desk is the booking's own road rather than the gap a + // day-at-a-time routing leaves between two cards, so it is drawn whether or not one + // continuous drive was asked for. A car taken out in the morning and handed back after + // midnight had the day it set out on drawn and nothing from there to the desk, which + // reads as a drive that stops halfway. + const ownDrive = first?.carrier?.role === 'return'; if (connectDays && !automaticSchedule) { for (const spill of chain.spills) { inboundAt.set(spill.at, { seg: spill.leg, line: spill.line }); } - + } + if ((connectDays || ownDrive) && !automaticSchedule) { // No band for a night spent at one hotel: nothing is driven between the evening and // the morning, and a band would read "0 km" between a hotel and itself. - const first = chain.stops[0]; const joined = inboundAt.has(0) || !previousStop || !first || isStationaryJoin(previousStop, first) ? undefined diff --git a/shared/src/roadtrip/carriers.spec.ts b/shared/src/roadtrip/carriers.spec.ts index 9fd2c7230..e8a32caa3 100644 --- a/shared/src/roadtrip/carriers.spec.ts +++ b/shared/src/roadtrip/carriers.spec.ts @@ -16,6 +16,7 @@ */ import { assembleRoadtrip } from './assemble'; import { + carrierStopAssignmentId, CHECK_IN_MINUTES, carrierClock, carrierLeg, @@ -40,6 +41,7 @@ import { } from './carriers'; import { planDayWindow } from './dayWindow'; import type { PlanDay, RoadtripStop, RoutedLeg } from './planning-types'; +import { standsAsDay } from './roadtripModel'; import { roadtripLegKey } from './routeRun'; import { describe, expect, it } from 'vitest'; @@ -1051,3 +1053,117 @@ describe('tonight’s stay at the far end of a ride within one day', () => { ]); }); }); + +describe('the stops a hire car names between its desks', () => { + const driven = (over = {}) => + ({ + id: 42, + type: 'car', + title: 'Sixt Hamburg', + day_id: 1, + end_day_id: 1, + reservation_time: '2026-06-01T10:00', + endpoints: [ + { role: 'from', sequence: 0, name: 'Hamburg', code: null, lat: 53.5511, lng: 9.9937 }, + { role: 'stop', sequence: 1, name: 'Lüneburg', code: null, lat: 53.2464, lng: 10.4115 }, + { role: 'to', sequence: 2, name: 'Berlin', code: null, lat: 52.52, lng: 13.405 }, + ], + ...over, + }) as CarrierBooking; + + it('ROADTRIP-CARRIERS-038: a hire car keeps the places it is driven through, a ride keeps none', () => { + expect(carrierSeam(driven())!.waypoints).toEqual([{ lat: 53.2464, lng: 10.4115, name: 'Lüneburg' }]); + // A train calls at its intermediate stations; nobody drives between them. + expect(carrierSeam(driven({ type: 'train' }))!.waypoints).toEqual([]); + // Nothing between the two desks is nothing to drive through. + expect( + carrierSeam( + driven({ + endpoints: [ + { role: 'from', sequence: 0, name: 'Hamburg', code: null, lat: 53.5511, lng: 9.9937 }, + { role: 'to', sequence: 1, name: 'Berlin', code: null, lat: 52.52, lng: 13.405 }, + ], + }), + )!.waypoints, + ).toEqual([]); + }); + + /** The day the car is taken out and handed back on, with a stop of the day's own. */ + const sameDay = (over = {}) => + carrierSeam(driven({ end_day_id: 1, reservation_end_time: '2026-06-01T18:00', ...over }))!; + + it('ROADTRIP-CARRIERS-039: the places it is driven through are stops of the drive, behind the pick-up', () => { + const seated = seatCarrierStops(1, [stop({ ownerIndex: 0, time: '12:00' })], [0], [sameDay()]); + // Taken out, driven through Lüneburg, and the day's own stop still on the road. + expect(seated.map((s) => (s.carrier ? `${s.carrier.role}:${s.name}` : s.name))).toEqual([ + 'pickup:Hamburg', + 'stop:Lüneburg', + 'Stop 0', + 'return:Berlin', + ]); + const through = seated[1]!; + // A place driven through keeps no minutes of its own: the drive passes and goes on. + expect(through).toMatchObject({ + dwellMinutes: 0, + time: null, + leaveAt: null, + night: false, + legMode: null, + incomingLegMode: null, + lat: 53.2464, + lng: 10.4115, + }); + // An id of its own, below every terminal's, so no stop of the day collides with it. + expect(through.assignmentId).toBe(carrierStopAssignmentId(42, 0)); + expect(through.assignmentId).toBeLessThan(terminalAssignmentId(42, 'return')); + }); + + it('ROADTRIP-CARRIERS-040: a car still out tomorrow puts them on the day it was taken out, and a ride puts none', () => { + // The road leaves the pick-up and runs over them whether the car goes back that day + // or days later. Kept off the day it sets off, a hire car booked across two days had + // one desk on each and the drive ran over neither of the places it was booked through. + const stillOut = seatCarrierStops(1, [stop({ ownerIndex: 0 })], [0], [carrierSeam(driven({ end_day_id: 3 }))!]); + expect(stillOut.map((s) => s.carrier?.role ?? s.name)).toEqual(['pickup', 'stop', 'Stop 0']); + // The day it goes back on holds its desk, and stands as a day on that alone. + expect(standsAsDay(seatCarrierStops(3, [], [], [carrierSeam(driven({ end_day_id: 3 }))!]))).toBe(true); + // A train calls at its intermediate stations and nobody drives between them. + const ride = seatCarrierStops( + 1, + [stop({ ownerIndex: 0 })], + [0], + [carrierSeam(driven({ type: 'train', end_day_id: 1, reservation_end_time: '2026-06-01T18:00' }))!], + ); + expect(ride.map((s) => s.carrier?.role ?? s.name).sort()).toEqual(['Stop 0', 'arrival', 'departure']); + // Two of them keep the booking's own order. + const twice = sameDay({ + endpoints: [ + { role: 'from', sequence: 0, name: 'Hamburg', code: null, lat: 53.5511, lng: 9.9937 }, + { role: 'stop', sequence: 1, name: 'Lüneburg', code: null, lat: 53.2464, lng: 10.4115 }, + { role: 'stop', sequence: 2, name: 'Wittenberge', code: null, lat: 52.9939, lng: 11.7536 }, + { role: 'to', sequence: 3, name: 'Berlin', code: null, lat: 52.52, lng: 13.405 }, + ], + }); + expect( + seatCarrierStops(1, [stop({ ownerIndex: 0 })], [0], [twice]) + .filter((s) => s.carrier?.role === 'stop') + .map((s) => s.name), + ).toEqual(['Lüneburg', 'Wittenberge']); + }); + + it('ROADTRIP-CARRIERS-041: a booking that states no hours still puts the places it is driven through on the drive', () => { + // Reported on a car booked with neither hour filled in: the desks are seated by the + // day's shape alone (the pick-up opens it, the hand-back closes it), and the places + // between them are the booking's whatever the clock says. Stating the hours is what + // made the road run over them before, which is no part of what they are. + const noHours = carrierSeam(driven({ reservation_time: null, reservation_end_time: null, end_day_id: 1 }))!; + expect(noHours.arrival).toMatchObject({ dayId: 1, clock: null, name: 'Berlin' }); + const seated = seatCarrierStops(1, [stop({ ownerIndex: 0 })], [0], [noHours]); + expect(seated.map((s) => (s.carrier ? `${s.carrier.role}:${s.name}` : s.name))).toEqual([ + 'pickup:Hamburg', + 'stop:Lüneburg', + 'Stop 0', + 'return:Berlin', + ]); + expect(seated[1]).toMatchObject({ dwellMinutes: 0, time: null }); + }); +}); diff --git a/shared/src/roadtrip/carriers.ts b/shared/src/roadtrip/carriers.ts index d18e3b52f..ba7ea482e 100644 --- a/shared/src/roadtrip/carriers.ts +++ b/shared/src/roadtrip/carriers.ts @@ -108,6 +108,7 @@ export interface CarrierSeam { * booking by its own clock, so the seat by geography (`rideSeatAfter`) leaves it alone. */ stopover: boolean; + waypoints: { lat: number; lng: number; name: string }[]; departure: CarrierEnd; /** * A ride always has both ends. A hire car has a return only when the booking says @@ -178,7 +179,7 @@ function isLocated(e: Endpoint): e is LocatedEndpoint { function seamEnds( booking: CarrierBooking, kind: CarrierSeam['kind'], -): { from: LocatedEndpoint; to: LocatedEndpoint | undefined } | null { +): { from: LocatedEndpoint; to: LocatedEndpoint | undefined; between: LocatedEndpoint[] } | null { const located = (booking.endpoints ?? []).filter(isLocated).sort((a, b) => (a.sequence ?? 0) - (b.sequence ?? 0)); const from = kind === 'ride' ? (located.find((e) => e.role === 'from') ?? located[0]) : located.find((e) => e.role === 'from'); @@ -188,7 +189,8 @@ function seamEnds( : [...located].reverse().find((e) => e.role === 'to'); if (!from) return null; if (kind === 'ride' && (!to || from === to)) return null; - return { from, to }; + const between = kind === 'rental' && to ? located.slice(located.indexOf(from) + 1, located.indexOf(to)) : []; + return { from, to, between }; } /** The day a booking leaves on: its first leg's, else its own. Null when it names none. */ @@ -213,7 +215,7 @@ export function carrierSeam(booking: CarrierBooking): CarrierSeam | null { if (!kind) return null; const ends = seamEnds(booking, kind); if (!ends) return null; - const { from, to } = ends; + const { from, to, between } = ends; const legs = legsOf(booking); const first = legs[0]; @@ -250,6 +252,7 @@ export function carrierSeam(booking: CarrierBooking): CarrierSeam | null { title: booking.title, kind, stopover: legs.length > 1, + waypoints: between.map((e) => ({ lat: e.lat, lng: e.lng, name: e.name })), departure: { dayId: depDayId, position: positionOn(first?.day_positions, depDayId) ?? bookingPosition(depDayId), @@ -429,6 +432,18 @@ export function terminalAssignmentId(reservationId: number, role: CarrierTermina return TERMINAL_ID_BASE - reservationId * 2 - (role === 'arrival' || role === 'return' ? 1 : 0); } +/** + * Below the terminals, which take two ids per booking: a booking's stops need one id each, + * and there is no room for them between its two desks. A booking names at most + * `MAX_CARRIER_STOPS` of them, so an id of one booking never reaches the next one's. + */ +const CARRIER_STOP_ID_BASE = -4_000_000_000; +const MAX_CARRIER_STOPS = 64; + +export function carrierStopAssignmentId(reservationId: number, sequence: number): number { + return CARRIER_STOP_ID_BASE - reservationId * MAX_CARRIER_STOPS - (sequence % MAX_CARRIER_STOPS); +} + /** The two roles of a seam, by which end of it a terminal stands at. */ function rolesOf(seam: CarrierSeam): { start: CarrierTerminal['role']; end: CarrierTerminal['role'] } { return seam.kind === 'rental' ? { start: 'pickup', end: 'return' } : { start: 'departure', end: 'arrival' }; @@ -480,6 +495,52 @@ function terminalStop( }; } +/** + * A place a hire car's booking names between its desks, as a stop of the drive. + * + * A car taken Hamburg → Lüneburg → Berlin is driven through Lüneburg, so the drive stops + * there on the way: the day plan has always routed through it (`orderedWaypoints`), while + * the road trip saw only the two desks and drew the short way round, which reads as a + * shorter journey than the one booked, on a road nobody takes. It is a stop the booking + * makes rather than a place the trip is for, so it keeps no minutes of its own — the drive + * passes through and goes on — and carries no number on the rail, as a desk carries none. + */ +function carrierWaypointStop( + seam: CarrierSeam, + point: CarrierSeam['waypoints'][number], + sequence: number, + dayId: number, + ownerIndex: number, +): RoadtripStop { + return { + carrier: { + reservationId: seam.reservationId, + type: seam.type, + role: 'stop', + title: seam.title, + code: null, + at: null, + }, + assignmentId: carrierStopAssignmentId(seam.reservationId, sequence), + ownerDayId: dayId, + ownerIndex, + placeId: -seam.reservationId, + name: point.name, + lat: point.lat, + lng: point.lng, + time: null, + leaveAt: null, + dwellMinutes: 0, + checkInTime: null, + night: false, + endDay: false, + legMode: null, + incomingLegMode: null, + stopType: null, + fillPercent: null, + }; +} + /** * Tonight's stays that wait behind a ride of their check-in day, by their index among the * day's stops, each with the rides that land nearer it than they leave. @@ -674,6 +735,16 @@ export function seatCarrierStops( // The index of the stored stop that follows the one the terminal is seated behind. const insertAt = slot.after + 1; out.push(terminalStop(slot.seam, slot.role, dayId, insertAt)); + // The places the booking is driven through follow its pick-up, in its own order, as + // stops of the drive: the road leaves that desk and runs over them, whether the car + // goes back the same day or days later. Held to the day it is taken out on, because + // that is the one day the booking names for the drive; a stop the traveller reaches + // on a later day of a long hire is theirs to move, and moving it is what says so. + if (slot.role === 'pickup') { + slot.seam.waypoints.forEach((point, sequence) => + out.push(carrierWaypointStop(slot.seam, point, sequence, dayId, insertAt)), + ); + } if (slot.role === 'departure' && slot.seam.arrival?.dayId === dayId) { out.push(terminalStop(slot.seam, 'arrival', dayId, insertAt)); for (const index of waiting.get(slot.seam) ?? []) out.push(reachedFrom(stops[index]!, slot.seam)); diff --git a/shared/src/roadtrip/planning-types.ts b/shared/src/roadtrip/planning-types.ts index 0d804fba8..0564bbbe5 100644 --- a/shared/src/roadtrip/planning-types.ts +++ b/shared/src/roadtrip/planning-types.ts @@ -19,7 +19,12 @@ export interface CarrierTerminal { reservationId: number; /** flight | train | ferry | cruise | bus for a ride, car for a hire car. */ type: string; - role: 'departure' | 'arrival' | 'pickup' | 'return'; + /** + * Which end of the booking this is, or `stop` for a place it names between its ends: a + * hire car taken Hamburg → Lüneburg → Berlin is driven through Lüneburg, and that is a + * stop of the drive rather than a desk (`seatCarrierStops`). + */ + role: 'departure' | 'arrival' | 'pickup' | 'return' | 'stop'; /** The booking's title, which is what the rail prints on the ride. */ title: string; /** IATA code or station code, when the booking carries one. */ diff --git a/shared/src/roadtrip/roadtripModel.ts b/shared/src/roadtrip/roadtripModel.ts index cf8f04034..19c8a543f 100644 --- a/shared/src/roadtrip/roadtripModel.ts +++ b/shared/src/roadtrip/roadtripModel.ts @@ -407,9 +407,16 @@ export function isStoredStop(stop: Pick 1 || stops.some((stop) => stop.night === true); +export function standsAsDay(stops: readonly { night?: boolean; carrier?: unknown }[]): boolean { + return stops.length > 1 || stops.some((stop) => stop.night === true || !!stop.carrier); } export function splitIntoRuns(stops: T[], modeOfLeg: (from: T, to: T) => string): { stops: T[]; mode: string }[] {