diff --git a/src/components/SceneryView/SceneryTimetable.vue b/src/components/SceneryView/SceneryTimetable.vue index 2e43439..af3fd2b 100644 --- a/src/components/SceneryView/SceneryTimetable.vue +++ b/src/components/SceneryView/SceneryTimetable.vue @@ -238,11 +238,12 @@ export default defineComponent({ sceneryTimetables(): SceneryTimetableRow[] { if (!this.onlineScenery) return []; - const sceneryName = this.$route.query['station']?.toString() ?? ''; + const sceneryName = this.$route.query['station']?.toString().replace(/_/g, ' ') ?? ''; return this.onlineScenery.scheduledTrains .filter( (ct) => + ct.timetablePathElement.stationName == sceneryName && ct.train.region == this.mainStore.region.id && this.chosenCheckpoint && ct.checkpointStop.stopNameRAW.toLowerCase() == this.chosenCheckpoint.toLowerCase() @@ -251,75 +252,18 @@ export default defineComponent({ const trainStopStatus = getTrainStopStatus( ct.checkpointStop, ct.train.currentStationName, - sceneryName.replace(/_/g, ' ') + sceneryName ); - const trainStopIndex = - ct.train.timetableData?.followingStops.findIndex( - (stop) => stop.stopName == ct.checkpointStop.stopName - ) ?? -1; - - let prevStationName = '', - nextStationName = ''; - - let departureLine: string | null = null; - let arrivingLine: string | null = null; - - let prevDepartureLine: string | null = null, - nextArrivalLine: string | null = null; - - if (trainStopIndex > -1 && ct.train.timetableData?.followingStops !== undefined) { - for (let i = trainStopIndex; i >= 0; i--) { - const stop = ct.train.timetableData.followingStops[i]; - - if ( - /strong|podg\.|pe\./g.test(stop.stopName) && - !prevStationName && - i <= trainStopIndex - 1 - ) - prevStationName = stop.stopNameRAW.replace(/,.*/g, ''); - - if ( - stop.arrivalLine != null && - !arrivingLine && - !/-|_|it|sbl/gi.test(stop.arrivalLine) - ) { - arrivingLine = stop.arrivalLine; - prevDepartureLine = - ct.train.timetableData.followingStops[i - 1]?.departureLine || null; - } - } - - for (let i = trainStopIndex; i < ct.train.timetableData.followingStops.length; i++) { - const stop = ct.train.timetableData.followingStops[i]; - - if ( - /strong|podg\.|pe\./g.test(stop.stopName) && - !nextStationName && - i > trainStopIndex - ) - nextStationName = stop.stopNameRAW.replace(/,.*/g, ''); - - if ( - stop.departureLine && - !departureLine && - !/-|_|it|sbl/gi.test(stop.departureLine) - ) { - departureLine = stop.departureLine; - nextArrivalLine = ct.train.timetableData.followingStops[i + 1]?.arrivalLine || null; - } - } - } - return { checkpointStop: ct.checkpointStop, train: ct.train, - prevDepartureLine, - nextArrivalLine, - departureLine, - arrivingLine, - prevStationName, - nextStationName, + prevDepartureLine: ct.previousSceneryElement?.departureRouteExt ?? null, + nextArrivalLine: ct.nextSceneryElement?.arrivalRouteExt ?? null, + departureLine: ct.timetablePathElement.departureRouteExt ?? null, + arrivingLine: ct.timetablePathElement.arrivalRouteExt ?? null, + prevStationName: ct.previousSceneryElement?.stationName ?? null, + nextStationName: ct.nextSceneryElement?.stationName ?? null, status: trainStopStatus }; }) diff --git a/src/store/mainStore.ts b/src/store/mainStore.ts index 6db5296..1f53c51 100644 --- a/src/store/mainStore.ts +++ b/src/store/mainStore.ts @@ -96,7 +96,17 @@ export const useMainStore = defineStore('mainStore', { followingStops: timetable.stopList, routeDistance: timetable.stopList[timetable.stopList.length - 1].stopDistance, sceneries: timetable.sceneries, - sceneryNames: sceneryNames.reverse() + sceneryNames: sceneryNames.reverse(), + timetablePath: timetable.path.split(';').map((pathElementString) => { + const [arrival, station, departure] = pathElementString.split(','); + + return { + arrivalRouteExt: arrival, + departureRouteExt: departure, + stationName: station.split(' ').slice(0, -1).join(' '), + stationHash: station.split(' ').slice(-1).join(' ') + }; + }) } : undefined } as Train; @@ -110,21 +120,39 @@ export const useMainStore = defineStore('mainStore', { } else sceneriesTrains.set(train.currentStationName, [trainObj]); // Checkpoints trains map - timetable?.stopList.forEach((stop, i) => { - if (/strong|podg\.|pe\./.test(stop.stopName)) { - const checkpointTrain: CheckpointTrain = { - train: trainObj, - checkpointStop: stop - }; + if (trainObj.timetableData) { + let currentSceneryIndex = 0; + const timetablePath = trainObj.timetableData.timetablePath; - if (checkpointsTrains.has(stop.stopNameRAW.toLowerCase())) { - checkpointsTrains.set(stop.stopNameRAW.toLowerCase(), [ - ...checkpointsTrains.get(stop.stopNameRAW.toLowerCase())!, - checkpointTrain - ]); - } else checkpointsTrains.set(stop.stopNameRAW.toLowerCase(), [checkpointTrain]); - } - }); + trainObj.timetableData.followingStops.forEach((stop, i) => { + if (/strong|podg\.|pe\./.test(stop.stopName)) { + const checkpointTrain: CheckpointTrain = { + train: trainObj, + checkpointStop: stop, + + previousSceneryElement: + currentSceneryIndex > 0 ? timetablePath[currentSceneryIndex - 1] : null, + + nextSceneryElement: + currentSceneryIndex < timetablePath.length - 1 + ? timetablePath[currentSceneryIndex + 1] + : null, + + timetablePathElement: timetablePath[currentSceneryIndex] + }; + + if (checkpointsTrains.has(stop.stopNameRAW.toLowerCase())) { + checkpointsTrains.set(stop.stopNameRAW.toLowerCase(), [ + ...checkpointsTrains.get(stop.stopNameRAW.toLowerCase())!, + checkpointTrain + ]); + } else checkpointsTrains.set(stop.stopNameRAW.toLowerCase(), [checkpointTrain]); + } + + if (timetablePath[currentSceneryIndex].departureRouteExt == stop.departureLine) + currentSceneryIndex++; + }); + } return trainObj; }); @@ -252,8 +280,10 @@ export const useMainStore = defineStore('mainStore', { if (!scheduledTrains) return; - scheduledTrains.forEach(({ train, checkpointStop }) => { - scenery.scheduledTrains.push({ train, checkpointStop }); + scheduledTrains.forEach(({ train, checkpointStop, timetablePathElement, ...v }) => { + if (scenery.name != timetablePathElement.stationName) return; + + scenery.scheduledTrains.push({ train, checkpointStop, timetablePathElement, ...v }); if (uniqueTrainIds.includes(train.id) || train.region != this.region.id) return; diff --git a/src/typings/api.ts b/src/typings/api.ts index 4619cc0..42da469 100644 --- a/src/typings/api.ts +++ b/src/typings/api.ts @@ -195,6 +195,8 @@ export namespace API { TWR: boolean; SKR: boolean; sceneries: string[]; + + path: string; } } diff --git a/src/typings/common.ts b/src/typings/common.ts index 631aab5..3731794 100644 --- a/src/typings/common.ts +++ b/src/typings/common.ts @@ -39,6 +39,13 @@ export interface RegionCounters { timetablesCount: number; } +export interface TimetablePathElement { + arrivalRouteExt?: string; + departureRouteExt?: string; + stationName: string; + stationHash: string; +} + export interface Train { id: string; modalId: string; @@ -73,6 +80,7 @@ export interface Train { routeDistance: number; sceneries: string[]; sceneryNames: string[]; + timetablePath: TimetablePathElement[]; }; } @@ -188,6 +196,9 @@ export interface TrainStop { export interface CheckpointTrain { checkpointStop: TrainStop; train: Train; + timetablePathElement: TimetablePathElement; + previousSceneryElement: TimetablePathElement | null; + nextSceneryElement: TimetablePathElement | null; } // Vehicles Data