mirror of
https://github.com/Spythere/stacjownik.git
synced 2026-05-03 13:28:11 +00:00
Zmiana API z danymi scenerii
This commit is contained in:
+159
-109
@@ -1,19 +1,24 @@
|
||||
/* eslint-disable */
|
||||
|
||||
import { InjectionKey } from 'vue'
|
||||
import { createStore, useStore as baseUseStore, Store } from 'vuex'
|
||||
import { InjectionKey } from 'vue';
|
||||
import { createStore, useStore as baseUseStore, Store } from 'vuex';
|
||||
|
||||
import axios from "axios";
|
||||
import axios from 'axios';
|
||||
|
||||
import Train from "@/scripts/interfaces/Train";
|
||||
import Train from '@/scripts/interfaces/Train';
|
||||
|
||||
import { StoreData } from "@/scripts/interfaces/StoreData";
|
||||
import { StoreData } from '@/scripts/interfaces/StoreData';
|
||||
|
||||
import { ACTIONS, MUTATIONS } from '@/constants/storeConstants';
|
||||
import { DataStatus } from '@/scripts/enums/DataStatus';
|
||||
|
||||
import { ACTIONS, MUTATIONS } from "@/constants/storeConstants";
|
||||
import { DataStatus } from "@/scripts/enums/DataStatus";
|
||||
|
||||
import { getLocoURL, getScheduledTrain, getStatusID, getStatusTimestamp, parseSpawns } from "@/scripts/utils/storeUtils";
|
||||
import {
|
||||
getLocoURL,
|
||||
getScheduledTrain,
|
||||
getStatusID,
|
||||
getStatusTimestamp,
|
||||
parseSpawns,
|
||||
} from '@/scripts/utils/storeUtils';
|
||||
import { URLs } from '@/scripts/utils/apiURLs';
|
||||
import ScheduledTrain from '@/scripts/interfaces/ScheduledTrain';
|
||||
import StationRoutes from '@/scripts/interfaces/StationRoutes';
|
||||
@@ -22,7 +27,7 @@ import { APIData, State, StationJSONData } from './types';
|
||||
|
||||
const connectToDevAPI = false;
|
||||
|
||||
export const key: InjectionKey<Store<State>> = Symbol()
|
||||
export const key: InjectionKey<Store<State>> = Symbol();
|
||||
|
||||
export const store = createStore<State>({
|
||||
state: () => ({
|
||||
@@ -33,7 +38,7 @@ export const store = createStore<State>({
|
||||
sceneryData: [],
|
||||
lastDispatcherStatuses: [],
|
||||
|
||||
region: { id: "eu", value: "PL1" },
|
||||
region: { id: 'eu', value: 'PL1' },
|
||||
|
||||
trainCount: 0,
|
||||
stationCount: 0,
|
||||
@@ -47,7 +52,7 @@ export const store = createStore<State>({
|
||||
dispatcherDataStatus: DataStatus.Loading,
|
||||
trainsDataStatus: DataStatus.Loading,
|
||||
|
||||
listenerLaunched: false
|
||||
listenerLaunched: false,
|
||||
}),
|
||||
|
||||
getters: {
|
||||
@@ -59,7 +64,7 @@ export const store = createStore<State>({
|
||||
|
||||
sceneryDataStatus: state.sceneryDataStatus,
|
||||
dispatcherDataStatus: state.dispatcherDataStatus,
|
||||
trainsDataStatus: state.trainsDataStatus
|
||||
trainsDataStatus: state.trainsDataStatus,
|
||||
}),
|
||||
|
||||
sceneryDataStatus: (state): DataStatus => state.sceneryDataStatus,
|
||||
@@ -68,7 +73,7 @@ export const store = createStore<State>({
|
||||
|
||||
currentRegion: (state): { id: string; value: string } => state.region,
|
||||
|
||||
webSocket: (state): Socket | undefined => state.webSocket
|
||||
webSocket: (state): Socket | undefined => state.webSocket,
|
||||
},
|
||||
|
||||
actions: {
|
||||
@@ -76,12 +81,14 @@ export const store = createStore<State>({
|
||||
await dispatch(ACTIONS.loadStaticStationData);
|
||||
|
||||
// Websocket config
|
||||
const socket = io(process.env.NODE_ENV !== 'production' && connectToDevAPI ? URLs.stacjownikAPIDev : URLs.stacjownikAPI,
|
||||
const socket = io(
|
||||
process.env.NODE_ENV !== 'production' && connectToDevAPI ? URLs.stacjownikAPIDev : URLs.stacjownikAPI,
|
||||
{
|
||||
transports: ["websocket", "polling"],
|
||||
transports: ['websocket', 'polling'],
|
||||
rememberUpgrade: true,
|
||||
reconnection: true
|
||||
})
|
||||
reconnection: true,
|
||||
}
|
||||
);
|
||||
|
||||
socket.on('UPDATE', (data: APIData) => {
|
||||
this.dispatch(ACTIONS.fetchOnlineData, data);
|
||||
@@ -92,12 +99,14 @@ export const store = createStore<State>({
|
||||
},
|
||||
|
||||
async loadStaticStationData({ commit }) {
|
||||
const sceneryData: StationJSONData = await (await axios.get(`${URLs.sceneryData}?timestamp=${Math.floor(Date.now() / 1800000)}`)).data;
|
||||
const sceneryData: StationJSONData = await (
|
||||
await axios.get(`${URLs.stacjownikAPI}/api/getSceneryData?timestamp=${Math.floor(Date.now() / 1800000)}`)
|
||||
).data.response;
|
||||
|
||||
if (!sceneryData)
|
||||
commit(MUTATIONS.SET_SCENERY_DATA_STATUS, DataStatus.Error);
|
||||
else
|
||||
commit(MUTATIONS.SET_SCENERY_DATA, sceneryData);
|
||||
|
||||
|
||||
if (!sceneryData) commit(MUTATIONS.SET_SCENERY_DATA_STATUS, DataStatus.Error);
|
||||
else commit(MUTATIONS.SET_SCENERY_DATA, sceneryData);
|
||||
},
|
||||
|
||||
async fetchOnlineData({ commit }, data: APIData) {
|
||||
@@ -110,15 +119,15 @@ export const store = createStore<State>({
|
||||
}
|
||||
|
||||
commit(MUTATIONS.SET_SCENERY_DATA_STATUS, DataStatus.Loaded);
|
||||
commit(MUTATIONS.SET_DISPATCHER_DATA_STATUS, !data.dispatchers ? DataStatus.Warning : DataStatus.Loaded)
|
||||
commit(MUTATIONS.SET_DISPATCHER_DATA_STATUS, !data.dispatchers ? DataStatus.Warning : DataStatus.Loaded);
|
||||
commit(MUTATIONS.SET_TRAINS_DATA_STATUS, !data.trains ? DataStatus.Warning : DataStatus.Loaded);
|
||||
|
||||
// Zaktualizuj listę pociągów
|
||||
const updatedTrainList: Train[] =
|
||||
data.trains
|
||||
?.filter(train => train.region === this.state.region.id && train.online)
|
||||
.map(train => {
|
||||
const stock = train.stockString.split(";");
|
||||
?.filter((train) => train.region === this.state.region.id && train.online)
|
||||
.map((train) => {
|
||||
const stock = train.stockString.split(';');
|
||||
const locoType = stock ? stock[0] : train.stockString;
|
||||
|
||||
const timetable = train.timetable;
|
||||
@@ -142,18 +151,20 @@ export const store = createStore<State>({
|
||||
locoURL: getLocoURL(locoType),
|
||||
cars: stock.slice(1),
|
||||
|
||||
timetableData: timetable ? {
|
||||
timetableId: timetable.timetableId,
|
||||
SKR: timetable.SKR,
|
||||
TWR: timetable.TWR,
|
||||
route: timetable.route,
|
||||
category: timetable.category,
|
||||
followingStops: timetable.stopList,
|
||||
routeDistance: timetable.stopList[timetable.stopList.length - 1].stopDistance,
|
||||
sceneries: timetable.sceneries
|
||||
} : undefined
|
||||
timetableData: timetable
|
||||
? {
|
||||
timetableId: timetable.timetableId,
|
||||
SKR: timetable.SKR,
|
||||
TWR: timetable.TWR,
|
||||
route: timetable.route,
|
||||
category: timetable.category,
|
||||
followingStops: timetable.stopList,
|
||||
routeDistance: timetable.stopList[timetable.stopList.length - 1].stopDistance,
|
||||
sceneries: timetable.sceneries,
|
||||
}
|
||||
: undefined,
|
||||
};
|
||||
}) || []
|
||||
}) || [];
|
||||
|
||||
const onlineStationNames: string[] = [];
|
||||
const prevDispatcherStatuses: State['lastDispatcherStatuses'] = [];
|
||||
@@ -164,25 +175,40 @@ export const store = createStore<State>({
|
||||
onlineStationNames.push(stationAPI.stationName);
|
||||
|
||||
const stationName = stationAPI.stationName.toLowerCase();
|
||||
const station = this.state.stationList.find(s => s.name == stationAPI.stationName);
|
||||
const station = this.state.stationList.find((s) => s.name == stationAPI.stationName);
|
||||
|
||||
const prevDispatcherStatus = this.state.lastDispatcherStatuses.find(dispatcher => dispatcher.hash === stationAPI.stationHash);
|
||||
const stationStatus = !data.dispatchers ? undefined : data.dispatchers.find((status: string[]) => status[0] == stationAPI.stationHash && status[1] == this.state.region.id) || -1;
|
||||
|
||||
const statusTimestamp = prevDispatcherStatus && !data.dispatchers ? prevDispatcherStatus.statusTimestamp : getStatusTimestamp(stationStatus);
|
||||
const statusID = prevDispatcherStatus && !data.dispatchers ? prevDispatcherStatus.statusID : getStatusID(stationStatus);
|
||||
const prevDispatcherStatus = this.state.lastDispatcherStatuses.find(
|
||||
(dispatcher) => dispatcher.hash === stationAPI.stationHash
|
||||
);
|
||||
const stationStatus = !data.dispatchers
|
||||
? undefined
|
||||
: data.dispatchers.find(
|
||||
(status: string[]) => status[0] == stationAPI.stationHash && status[1] == this.state.region.id
|
||||
) || -1;
|
||||
|
||||
const statusTimestamp =
|
||||
prevDispatcherStatus && !data.dispatchers
|
||||
? prevDispatcherStatus.statusTimestamp
|
||||
: getStatusTimestamp(stationStatus);
|
||||
const statusID =
|
||||
prevDispatcherStatus && !data.dispatchers ? prevDispatcherStatus.statusID : getStatusID(stationStatus);
|
||||
|
||||
prevDispatcherStatuses.push({
|
||||
hash: stationAPI.stationHash,
|
||||
statusID,
|
||||
statusTimestamp
|
||||
statusTimestamp,
|
||||
});
|
||||
|
||||
const stationTrains = data.trains
|
||||
?.filter(train => train?.region === this.state.region.id && train.online && train.currentStationName === stationAPI.stationName)
|
||||
.map(train => ({ driverName: train.driverName, driverId: train.driverId, trainNo: train.trainNo }));
|
||||
?.filter(
|
||||
(train) =>
|
||||
train?.region === this.state.region.id &&
|
||||
train.online &&
|
||||
train.currentStationName === stationAPI.stationName
|
||||
)
|
||||
.map((train) => ({ driverName: train.driverName, driverId: train.driverId, trainNo: train.trainNo }));
|
||||
|
||||
station?.generalInfo?.checkpoints.forEach(cp => cp.scheduledTrains.length = 0);
|
||||
station?.generalInfo?.checkpoints.forEach((cp) => (cp.scheduledTrains.length = 0));
|
||||
|
||||
const scheduledTrains: ScheduledTrain[] = updatedTrainList.reduce((acc: ScheduledTrain[], train) => {
|
||||
if (!train.timetableData) return acc;
|
||||
@@ -190,22 +216,32 @@ export const store = createStore<State>({
|
||||
const timetable = train.timetableData;
|
||||
if (!timetable.sceneries.includes(stationAPI.stationHash)) return acc;
|
||||
|
||||
const stopInfoIndex = timetable.followingStops.findIndex(stop => {
|
||||
const stopInfoIndex = timetable.followingStops.findIndex((stop) => {
|
||||
const stopName = stop.stopNameRAW.toLowerCase();
|
||||
|
||||
// if (stop.stopName == "ARKADIA ZDRÓJ" && station.name == "Arkadia Zdrój 2019" && stop.pointId != "1583014379097") return false;
|
||||
// if (stop.stopName == "ARKADIA ZDRÓJ" && station.name == "Arkadia Zdrój 2012" && stop.pointId != "1519258642187") return false;
|
||||
|
||||
if (stationName === stopName) return true;
|
||||
if (stopName.includes(stationName) && !stop.stopName.includes("po.") && !stop.stopName.includes("podg.")) return true;
|
||||
if (stationName.includes(stopName) && !stop.stopName.includes("po.") && !stop.stopName.includes("podg.")) return true;
|
||||
if (stopName.includes("podg.") && stopName.split(", podg.")[0] && stationName.includes(stopName.split(", podg.")[0])) return true;
|
||||
|
||||
if (station?.generalInfo
|
||||
&& station.generalInfo.checkpoints
|
||||
&& station.generalInfo.checkpoints.length > 0
|
||||
&& station.generalInfo.checkpoints.some(cp => cp.checkpointName.toLowerCase().includes(stop.stopNameRAW.toLowerCase())))
|
||||
if (stopName.includes(stationName) && !stop.stopName.includes('po.') && !stop.stopName.includes('podg.'))
|
||||
return true;
|
||||
if (stationName.includes(stopName) && !stop.stopName.includes('po.') && !stop.stopName.includes('podg.'))
|
||||
return true;
|
||||
if (
|
||||
stopName.includes('podg.') &&
|
||||
stopName.split(', podg.')[0] &&
|
||||
stationName.includes(stopName.split(', podg.')[0])
|
||||
)
|
||||
return true;
|
||||
|
||||
if (
|
||||
station?.generalInfo &&
|
||||
station.generalInfo.checkpoints &&
|
||||
station.generalInfo.checkpoints.length > 0 &&
|
||||
station.generalInfo.checkpoints.some((cp) =>
|
||||
cp.checkpointName.toLowerCase().includes(stop.stopNameRAW.toLowerCase())
|
||||
)
|
||||
)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
@@ -217,18 +253,20 @@ export const store = createStore<State>({
|
||||
|
||||
if (station && station.generalInfo?.checkpoints && station.generalInfo.checkpoints.length > 0) {
|
||||
for (const checkpoint of station.generalInfo.checkpoints) {
|
||||
const index = timetable.followingStops.findIndex(stop => stop.stopNameRAW.toLowerCase() == checkpoint.checkpointName.toLowerCase());
|
||||
|
||||
const index = timetable.followingStops.findIndex(
|
||||
(stop) => stop.stopNameRAW.toLowerCase() == checkpoint.checkpointName.toLowerCase()
|
||||
);
|
||||
|
||||
if (index == -1) continue;
|
||||
|
||||
|
||||
const scheduledCheckpointTrain = getScheduledTrain(train, index, stationAPI.stationName);
|
||||
checkpoint.scheduledTrains.push(scheduledCheckpointTrain);
|
||||
|
||||
// timetable.followingStops
|
||||
// .filter(trainStop => trainStop.stopNameRAW.toLowerCase() === checkpoint.checkpointName.toLowerCase())
|
||||
// .forEach((trainCheckpointStop, i) => {
|
||||
// .filter(trainStop => trainStop.stopNameRAW.toLowerCase() === checkpoint.checkpointName.toLowerCase())
|
||||
// .forEach((trainCheckpointStop, i) => {
|
||||
|
||||
// });
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,80 +289,93 @@ export const store = createStore<State>({
|
||||
statusTimestamp,
|
||||
statusID,
|
||||
scheduledTrains,
|
||||
}
|
||||
};
|
||||
|
||||
if (!station) {
|
||||
this.state.stationList.push({
|
||||
name: stationAPI.stationName,
|
||||
onlineInfo
|
||||
})
|
||||
onlineInfo,
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
station.onlineInfo = { ...onlineInfo };
|
||||
|
||||
});
|
||||
|
||||
this.state.stationList
|
||||
.filter(station => !onlineStationNames.includes(station.name) && station.onlineInfo)
|
||||
.forEach(offlineStation => {
|
||||
.filter((station) => !onlineStationNames.includes(station.name) && station.onlineInfo)
|
||||
.forEach((offlineStation) => {
|
||||
offlineStation.onlineInfo = undefined;
|
||||
});
|
||||
|
||||
this.state.trainList = updatedTrainList;
|
||||
this.state.trainsDataStatus = DataStatus.Loaded;
|
||||
|
||||
if (data.dispatchers != null)
|
||||
this.state.lastDispatcherStatuses = prevDispatcherStatuses;
|
||||
if (data.dispatchers != null) this.state.lastDispatcherStatuses = prevDispatcherStatuses;
|
||||
},
|
||||
},
|
||||
|
||||
mutations: {
|
||||
SET_SCENERY_DATA(state, data: StationJSONData[]) {
|
||||
state.stationList = data.map(stationData => ({
|
||||
state.stationList = data.map((stationData) => ({
|
||||
name: stationData.name,
|
||||
|
||||
generalInfo: {
|
||||
...stationData,
|
||||
routes: stationData.routes?.split(";").filter(routeString => routeString).reduce((acc, routeString) => {
|
||||
const specs1 = routeString.split("_")[0];
|
||||
const isInternal = specs1.startsWith('!');
|
||||
const name = isInternal ? specs1.replace("!", "") : specs1;
|
||||
routes:
|
||||
stationData.routes
|
||||
?.split(';')
|
||||
.filter((routeString) => routeString)
|
||||
.reduce(
|
||||
(acc, routeString) => {
|
||||
const specs1 = routeString.split('_')[0];
|
||||
const isInternal = specs1.startsWith('!');
|
||||
const name = isInternal ? specs1.replace('!', '') : specs1;
|
||||
|
||||
const specs2 = routeString.split("_")[1].split("");
|
||||
const twoWay = specs2[0] == "2";
|
||||
const catenary = specs2[1] == "E";
|
||||
const SBL = specs2[2] == "S";
|
||||
const TWB = specs2[3] ? true : false;
|
||||
const specs2 = routeString.split('_')[1].split('');
|
||||
const twoWay = specs2[0] == '2';
|
||||
const catenary = specs2[1] == 'E';
|
||||
const SBL = specs2[2] == 'S';
|
||||
const TWB = specs2[3] ? true : false;
|
||||
|
||||
const propName = twoWay
|
||||
? catenary
|
||||
? 'twoWayCatenaryRouteNames'
|
||||
: 'twoWayNoCatenaryRouteNames'
|
||||
: catenary
|
||||
? 'oneWayCatenaryRouteNames'
|
||||
: 'oneWayNoCatenaryRouteNames';
|
||||
const propName = twoWay
|
||||
? catenary
|
||||
? 'twoWayCatenaryRouteNames'
|
||||
: 'twoWayNoCatenaryRouteNames'
|
||||
: catenary
|
||||
? 'oneWayCatenaryRouteNames'
|
||||
: 'oneWayNoCatenaryRouteNames';
|
||||
|
||||
acc[twoWay ? 'twoWay' : 'oneWay'].push({ name, SBL, TWB, catenary, isInternal, tracks: twoWay ? 2 : 1 });
|
||||
if (!isInternal) acc[propName].push(name);
|
||||
acc[twoWay ? 'twoWay' : 'oneWay'].push({
|
||||
name,
|
||||
SBL,
|
||||
TWB,
|
||||
catenary,
|
||||
isInternal,
|
||||
tracks: twoWay ? 2 : 1,
|
||||
});
|
||||
if (!isInternal) acc[propName].push(name);
|
||||
|
||||
if (SBL) acc['sblRouteNames'].push(name);
|
||||
if (SBL) acc['sblRouteNames'].push(name);
|
||||
|
||||
return acc;
|
||||
}, {
|
||||
oneWay: [],
|
||||
twoWay: [],
|
||||
sblRouteNames: [],
|
||||
oneWayCatenaryRouteNames: [],
|
||||
oneWayNoCatenaryRouteNames: [],
|
||||
twoWayCatenaryRouteNames: [],
|
||||
twoWayNoCatenaryRouteNames: []
|
||||
} as StationRoutes) || {},
|
||||
checkpoints: stationData.checkpoints ? stationData.checkpoints.split(";").map(sub => ({ checkpointName: sub, scheduledTrains: [] })) : [],
|
||||
}
|
||||
return acc;
|
||||
},
|
||||
{
|
||||
oneWay: [],
|
||||
twoWay: [],
|
||||
sblRouteNames: [],
|
||||
oneWayCatenaryRouteNames: [],
|
||||
oneWayNoCatenaryRouteNames: [],
|
||||
twoWayCatenaryRouteNames: [],
|
||||
twoWayNoCatenaryRouteNames: [],
|
||||
} as StationRoutes
|
||||
) || {},
|
||||
checkpoints: stationData.checkpoints
|
||||
? stationData.checkpoints.split(';').map((sub) => ({ checkpointName: sub, scheduledTrains: [] }))
|
||||
: [],
|
||||
},
|
||||
}));
|
||||
|
||||
},
|
||||
|
||||
SET_SCENERY_DATA_STATUS(state, status: DataStatus) {
|
||||
@@ -343,10 +394,9 @@ export const store = createStore<State>({
|
||||
state.region = region;
|
||||
state.webSocket?.emit('FETCH_DATA');
|
||||
},
|
||||
|
||||
}
|
||||
})
|
||||
},
|
||||
});
|
||||
|
||||
export function useStore(): Store<State> {
|
||||
return baseUseStore(key)
|
||||
}
|
||||
return baseUseStore(key);
|
||||
}
|
||||
|
||||
+3
-5
@@ -5,6 +5,8 @@ import Station from "@/scripts/interfaces/Station";
|
||||
import Train from "@/scripts/interfaces/Train";
|
||||
import { Socket } from "socket.io-client";
|
||||
|
||||
export type Availability = 'default' | 'unavailable' | 'nonPublic' | 'abandoned' | 'nonDefault';
|
||||
|
||||
export interface State {
|
||||
stationList: Station[],
|
||||
trainList: Train[],
|
||||
@@ -46,8 +48,6 @@ export interface StationJSONData {
|
||||
|
||||
reqLevel: number;
|
||||
|
||||
// supportersOnly: boolean;
|
||||
|
||||
signalType: string;
|
||||
controlType: string;
|
||||
|
||||
@@ -56,7 +56,5 @@ export interface StationJSONData {
|
||||
routes: string;
|
||||
checkpoints: string | null;
|
||||
|
||||
default: boolean;
|
||||
nonPublic: boolean;
|
||||
unavailable: boolean;
|
||||
availability: Availability;
|
||||
}
|
||||
Reference in New Issue
Block a user