mirror of
https://github.com/Spythere/stacjownik.git
synced 2026-05-03 05:18:11 +00:00
rework reaktywności danych z API i WS
This commit is contained in:
@@ -10,6 +10,8 @@ export enum StopStatus {
|
||||
}
|
||||
|
||||
export interface ScheduledTrain {
|
||||
checkpointName: string;
|
||||
|
||||
trainId: string;
|
||||
trainNo: number;
|
||||
|
||||
|
||||
@@ -2,17 +2,16 @@ import { Socket } from 'socket.io-client';
|
||||
import { DataStatus } from '../../enums/DataStatus';
|
||||
import StationAPIData from '../api/StationAPIData';
|
||||
import { TrainAPIData } from '../api/TrainAPIData';
|
||||
import Station from '../Station';
|
||||
import Train from '../Train';
|
||||
import { DispatcherStatsAPIData } from '../api/DispatcherStatsAPIData';
|
||||
import { DriverStatsAPIData } from '../api/DriverStatsAPIData';
|
||||
import { RollingStockGithubData } from '../github_api/StockInfoGithubData';
|
||||
import Station from '../Station';
|
||||
import { ScheduledTrain } from '../ScheduledTrain';
|
||||
|
||||
export type Availability = 'default' | 'unavailable' | 'nonPublic' | 'abandoned' | 'nonDefault';
|
||||
|
||||
export interface StoreState {
|
||||
stationList: Station[];
|
||||
trainList: Train[];
|
||||
apiData: APIData;
|
||||
rollingStockData?: RollingStockGithubData;
|
||||
|
||||
@@ -91,3 +90,31 @@ export interface StationJSONData {
|
||||
|
||||
availability: Availability;
|
||||
}
|
||||
|
||||
export interface StationTrain {
|
||||
driverName: string;
|
||||
driverId: number;
|
||||
trainNo: number;
|
||||
trainId: string;
|
||||
stopStatus: string;
|
||||
}
|
||||
|
||||
export interface OnlineScenery {
|
||||
name: string;
|
||||
hash: string;
|
||||
region: string;
|
||||
maxUsers: number;
|
||||
currentUsers: number;
|
||||
spawns: { spawnName: string; spawnLength: number; isElectrified: boolean }[];
|
||||
dispatcherName: string;
|
||||
dispatcherRate: number;
|
||||
dispatcherId: number;
|
||||
dispatcherExp: number;
|
||||
dispatcherIsSupporter: boolean;
|
||||
|
||||
statusTimestamp: number;
|
||||
statusID: string;
|
||||
|
||||
stationTrains?: StationTrain[];
|
||||
scheduledTrains?: ScheduledTrain[];
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { ScheduledTrain, StopStatus } from '../interfaces/ScheduledTrain';
|
||||
import Station from '../interfaces/Station';
|
||||
import Train from '../interfaces/Train';
|
||||
import TrainStop from '../interfaces/TrainStop';
|
||||
import StationAPIData from '../interfaces/api/StationAPIData';
|
||||
import { StationTrain, StoreState } from '../interfaces/store/storeTypes';
|
||||
|
||||
export const getLocoURL = (locoType: string): string =>
|
||||
`https://rj.td2.info.pl/dist/img/thumbnails/${
|
||||
@@ -79,7 +82,7 @@ export const getTimestamp = (date: string | null): number => (date ? new Date(da
|
||||
export const getTrainStopStatus = (
|
||||
stopInfo: TrainStop,
|
||||
currentStationName: string,
|
||||
stationName: string
|
||||
sceneryName: string
|
||||
) => {
|
||||
let stopStatus = StopStatus['arriving'],
|
||||
stopLabel = '',
|
||||
@@ -89,23 +92,23 @@ export const getTrainStopStatus = (
|
||||
stopStatus = StopStatus['terminated'];
|
||||
stopLabel = 'Skończył bieg';
|
||||
stopStatusID = 5;
|
||||
} else if (!stopInfo.terminatesHere && stopInfo.confirmed && currentStationName == stationName) {
|
||||
} else if (!stopInfo.terminatesHere && stopInfo.confirmed && currentStationName == sceneryName) {
|
||||
stopStatus = StopStatus['departed'];
|
||||
stopLabel = 'Odprawiony';
|
||||
stopStatusID = 2;
|
||||
} else if (!stopInfo.terminatesHere && stopInfo.confirmed && currentStationName != stationName) {
|
||||
} else if (!stopInfo.terminatesHere && stopInfo.confirmed && currentStationName != sceneryName) {
|
||||
stopStatus = StopStatus['departed-away'];
|
||||
stopLabel = 'Odjechał';
|
||||
stopStatusID = 4;
|
||||
} else if (currentStationName == stationName && !stopInfo.stopped) {
|
||||
} else if (currentStationName == sceneryName && !stopInfo.stopped) {
|
||||
stopStatus = StopStatus['online'];
|
||||
stopLabel = 'Na stacji';
|
||||
stopStatusID = 0;
|
||||
} else if (currentStationName == stationName && stopInfo.stopped) {
|
||||
} else if (currentStationName == sceneryName && stopInfo.stopped) {
|
||||
stopStatus = StopStatus['stopped'];
|
||||
stopLabel = 'Postój';
|
||||
stopStatusID = 1;
|
||||
} else if (currentStationName != stationName) {
|
||||
} else if (currentStationName != sceneryName) {
|
||||
stopStatus = StopStatus['arriving'];
|
||||
stopLabel = 'W drodze';
|
||||
stopStatusID = 3;
|
||||
@@ -114,16 +117,16 @@ export const getTrainStopStatus = (
|
||||
return { stopStatus, stopLabel, stopStatusID };
|
||||
};
|
||||
|
||||
export function getScheduledTrain(
|
||||
export function getCheckpointTrain(
|
||||
train: Train,
|
||||
trainStopIndex: number,
|
||||
stationName: string
|
||||
sceneryName: string
|
||||
): ScheduledTrain {
|
||||
const timetable = train.timetableData!;
|
||||
const followingStops = timetable.followingStops;
|
||||
const trainStop = followingStops[trainStopIndex];
|
||||
|
||||
const trainStopStatus = getTrainStopStatus(trainStop, train.currentStationName, stationName);
|
||||
const trainStopStatus = getTrainStopStatus(trainStop, train.currentStationName, sceneryName);
|
||||
|
||||
let prevStationName = '',
|
||||
nextStationName = '';
|
||||
@@ -177,6 +180,8 @@ export function getScheduledTrain(
|
||||
}
|
||||
|
||||
return {
|
||||
checkpointName: trainStop.stopNameRAW,
|
||||
|
||||
trainNo: train.trainNo,
|
||||
trainId: train.trainId,
|
||||
|
||||
@@ -206,3 +211,119 @@ export function getScheduledTrain(
|
||||
prevDepartureLine
|
||||
};
|
||||
}
|
||||
|
||||
export function getDispatcherStatus(state: StoreState, onlineStationData: StationAPIData) {
|
||||
const { dispatchers } = state.apiData;
|
||||
|
||||
const prevDispatcherStatus = state.lastDispatcherStatuses.find(
|
||||
(dispatcher) => dispatcher.hash === onlineStationData.stationHash
|
||||
);
|
||||
|
||||
const stationStatus = !dispatchers
|
||||
? undefined
|
||||
: dispatchers.find(
|
||||
(status: string[]) =>
|
||||
status[0] == onlineStationData.stationHash && status[1] == state.region.id
|
||||
) || -1;
|
||||
|
||||
const statusTimestamp =
|
||||
prevDispatcherStatus && !dispatchers
|
||||
? prevDispatcherStatus.statusTimestamp
|
||||
: getStatusTimestamp(stationStatus);
|
||||
|
||||
const statusID =
|
||||
prevDispatcherStatus && !dispatchers
|
||||
? prevDispatcherStatus.statusID
|
||||
: getStatusID(stationStatus);
|
||||
|
||||
return {
|
||||
hash: onlineStationData.stationHash,
|
||||
statusID,
|
||||
statusTimestamp
|
||||
};
|
||||
}
|
||||
|
||||
export function getScheduledTrains(
|
||||
trainList: Train[],
|
||||
stationAPIData: StationAPIData,
|
||||
stationGeneralInfo: Station['generalInfo']
|
||||
): ScheduledTrain[] {
|
||||
const stationName = stationAPIData.stationName.toLocaleLowerCase();
|
||||
|
||||
stationGeneralInfo?.checkpoints.forEach((cp) => (cp.scheduledTrains.length = 0));
|
||||
|
||||
return trainList.reduce((acc: ScheduledTrain[], train) => {
|
||||
if (!train.timetableData) return acc;
|
||||
|
||||
const timetable = train.timetableData;
|
||||
if (!timetable.sceneries.includes(stationAPIData.stationHash)) return acc;
|
||||
|
||||
const stopInfoIndex = timetable.followingStops.findIndex((stop) => {
|
||||
const stopName = stop.stopNameRAW.toLowerCase();
|
||||
|
||||
return (
|
||||
stationName == stopName ||
|
||||
(!/(po\.|podg\.)/.test(stationName) && stopName.includes(stationName)) ||
|
||||
(!/(po\.|podg\.)/.test(stopName) && stationName.includes(stopName)) ||
|
||||
(stopName.split(', podg.')[0] !== undefined &&
|
||||
stationName.startsWith(stopName.split(', podg.')[0]))
|
||||
);
|
||||
});
|
||||
|
||||
const checkpointScheduledTrains: ScheduledTrain[] = [];
|
||||
|
||||
if (stopInfoIndex != -1) {
|
||||
checkpointScheduledTrains.push(
|
||||
getCheckpointTrain(train, stopInfoIndex, stationAPIData.stationName)
|
||||
);
|
||||
}
|
||||
|
||||
stationGeneralInfo?.checkpoints?.forEach((checkpoint) => {
|
||||
if (checkpoint.checkpointName.toLocaleLowerCase() == stationName) return;
|
||||
|
||||
if (
|
||||
checkpointScheduledTrains.findIndex(
|
||||
(cpTrain) =>
|
||||
cpTrain.checkpointName.toLocaleLowerCase() ==
|
||||
checkpoint.checkpointName.toLocaleLowerCase()
|
||||
) != -1
|
||||
)
|
||||
return;
|
||||
|
||||
const index = timetable.followingStops.findIndex(
|
||||
(stop) => stop.stopNameRAW.toLowerCase() == checkpoint.checkpointName.toLowerCase()
|
||||
);
|
||||
|
||||
if (index > -1)
|
||||
checkpointScheduledTrains.push(
|
||||
getCheckpointTrain(train, index, stationAPIData.stationName)
|
||||
);
|
||||
});
|
||||
|
||||
acc.push(...checkpointScheduledTrains);
|
||||
return acc;
|
||||
}, []) as ScheduledTrain[];
|
||||
}
|
||||
|
||||
export function getStationTrains(
|
||||
trainList: Train[],
|
||||
scheduledTrainList: ScheduledTrain[],
|
||||
region: string,
|
||||
apiStation: StationAPIData
|
||||
): StationTrain[] {
|
||||
return trainList
|
||||
.filter(
|
||||
(train) =>
|
||||
train?.region === region &&
|
||||
train.online &&
|
||||
train.currentStationName === apiStation.stationName
|
||||
)
|
||||
.map((train) => ({
|
||||
driverName: train.driverName,
|
||||
driverId: train.driverId,
|
||||
trainNo: train.trainNo,
|
||||
trainId: train.trainId,
|
||||
stopStatus:
|
||||
scheduledTrainList.find((st) => st.trainNo === train.trainNo)?.stopStatus || 'no-timetable'
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user