From a8cac9ebe99e69a83a4b947ca8ebce9dd03c5eeb Mon Sep 17 00:00:00 2001 From: Spythere Date: Mon, 5 Jan 2026 22:45:30 +0100 Subject: [PATCH] refactor(vehicles): replaced URL for fetching vehicles data; changed vehicle group finding --- api-mock/index.js | 4 +- src/components/DriverView/DriverTrainCard.vue | 2 +- .../Tooltip/VehiclePreviewTooltip.vue | 18 ++++--- src/mixins/trainInfoMixin.ts | 35 +++++++++---- src/store/apiStore.ts | 4 +- src/typings/api.ts | 49 +++++++++++++++++-- src/typings/common.ts | 44 ++--------------- vite.config.ts | 2 +- 8 files changed, 94 insertions(+), 64 deletions(-) diff --git a/api-mock/index.js b/api-mock/index.js index 167ff96..f077b9c 100644 --- a/api-mock/index.js +++ b/api-mock/index.js @@ -15,8 +15,8 @@ app.get('/api/getSceneries', (_, res) => { res.sendFile(path.join(cwd(), 'endpoints', 'getSceneries.json')); }); -app.get('/api/getVehicles', (_, res) => { - res.sendFile(path.join(cwd(), 'endpoints', 'getVehicles.json')); +app.get('/api/getVehiclesData', (_, res) => { + res.sendFile(path.join(cwd(), 'endpoints', 'getVehiclesData.json')); }); app.get('/api/getDonators', (_, res) => { diff --git a/src/components/DriverView/DriverTrainCard.vue b/src/components/DriverView/DriverTrainCard.vue index 4bc75e0..54331d6 100644 --- a/src/components/DriverView/DriverTrainCard.vue +++ b/src/components/DriverView/DriverTrainCard.vue @@ -205,7 +205,7 @@ const availableCategories = computed(() => { for (const stockName of stockList) { const [vehicleName, ...cargoList] = stockName.split(':'); - const vehicleData = apiStore.vehiclesData?.find((v) => v.name == vehicleName); + const vehicleData = apiStore.vehiclesData?.vehicles.find((v) => v.name == vehicleName); if (!vehicleData) continue; diff --git a/src/components/Tooltip/VehiclePreviewTooltip.vue b/src/components/Tooltip/VehiclePreviewTooltip.vue index c70a88b..ca584f7 100644 --- a/src/components/Tooltip/VehiclePreviewTooltip.vue +++ b/src/components/Tooltip/VehiclePreviewTooltip.vue @@ -18,9 +18,9 @@ ({{ vehicleCargo.id }}) -
- {{ vehicleData.group.speed }}km/h • {{ vehicleData.group.length }}m • - {{ (vehicleData.group.weight / 1000).toFixed(1) }}t +
+ {{ vehicleGroup.speed }}km/h • {{ vehicleGroup.length }}m • + {{ (vehicleGroup.weight / 1000).toFixed(1) }}t (+{{ (vehicleCargo.weight / 1000).toFixed(1) }}t)
@@ -73,12 +73,18 @@ export default defineComponent({ return this.tooltipStore.content.split(':')[0]; }, - vehicleData() { - return this.apiStore.vehiclesData?.find((v) => v.name == this.vehicleName); + vehicleGroup() { + if (!this.apiStore.vehiclesData) return null; + + const vehicle = this.apiStore.vehiclesData.vehicles.find((v) => v.name == this.vehicleName); + + if (!vehicle) return null; + + return this.apiStore.vehiclesData.vehicleGroups.find((g) => g.id == vehicle?.vehicleGroupsId); }, vehicleCargo() { - const x = this.vehicleData?.group.cargoTypes?.find( + const x = this.vehicleGroup?.cargoTypes?.find( (c) => c.id == this.tooltipStore.content.split(':')[1] ); diff --git a/src/mixins/trainInfoMixin.ts b/src/mixins/trainInfoMixin.ts index bac9a93..6909d44 100644 --- a/src/mixins/trainInfoMixin.ts +++ b/src/mixins/trainInfoMixin.ts @@ -122,19 +122,27 @@ export default defineComponent({ // Check the whole consist speed limit const vehicleMaxSpeed = stockList.reduce((acc, stockName, i) => { + if (!this.apiStore.vehiclesData) return acc; + const [vehicleName, vehicleCargo] = stockName.split(':'); - const vehicleData = this.apiStore.vehiclesData?.find((v) => v.name == vehicleName); + const vehicle = this.apiStore.vehiclesData.vehicles.find((v) => v.name == vehicleName); - if (!vehicleData) return acc; + if (!vehicle) return acc; - let vehicleSpeed = vehicleData.group.speed; + const vehicleGroup = this.apiStore.vehiclesData.vehicleGroups.find( + (g) => g.id == vehicle.vehicleGroupsId + ); - if (vehicleData.type == 'wagon-freight') { + if (!vehicleGroup) return acc; + + let vehicleSpeed = vehicleGroup.speed; + + if (vehicle.type == 'wagon-freight') { isPassenger = false; - if (vehicleCargo !== undefined && vehicleData.group.speedLoaded) { - vehicleSpeed = vehicleData.group.speedLoaded; + if (vehicleCargo !== undefined && vehicleGroup.speedLoaded) { + vehicleSpeed = vehicleGroup.speedLoaded; } } @@ -143,14 +151,23 @@ export default defineComponent({ // Check the head vehicle speed limit const headLocoName = stockList[0]; - const headLocoVehicleData = this.apiStore.vehiclesData?.find((v) => v.name == headLocoName); + + const headLocoVehicle = this.apiStore.vehiclesData!.vehicles.find( + (v) => v.name == headLocoName + ); + + const headLocoVehicleGroup = this.apiStore.vehiclesData!.vehicleGroups.find( + (g) => g.id == headLocoVehicle?.vehicleGroupsId + ); + + if (!headLocoVehicleGroup) return vehicleMaxSpeed; // Omit speed check for head vehicle if there's no data for it - if (!headLocoName || !headLocoVehicleData || !headLocoVehicleData.group.massSpeeds) + if (!headLocoName || !headLocoVehicle || !headLocoVehicleGroup.massSpeeds) return vehicleMaxSpeed; const massSpeeds = - headLocoVehicleData.group.massSpeeds[ + headLocoVehicleGroup.massSpeeds[ stockList.length == 1 ? 'none' : isPassenger ? 'passenger' : 'cargo' ]; diff --git a/src/store/apiStore.ts b/src/store/apiStore.ts index 18bef6e..3977102 100644 --- a/src/store/apiStore.ts +++ b/src/store/apiStore.ts @@ -13,7 +13,7 @@ export const useApiStore = defineStore('apiStore', { }, activeData: undefined as API.ActiveData.Response | undefined, - vehiclesData: undefined as API.Vehicles.Response | undefined, + vehiclesData: undefined as API.VehiclesData.Response | undefined, donatorsData: [] as API.Donators.Response, sceneryData: [] as StationJSONData[], @@ -111,7 +111,7 @@ export const useApiStore = defineStore('apiStore', { async fetchVehiclesInfo() { try { - const response = await this.client!.get('api/getVehicles'); + const response = await this.client!.get('api/getVehiclesData'); this.vehiclesData = response.data; this.dataStatuses.vehicles = response.data ? Status.Data.Loaded : Status.Data.Warning; diff --git a/src/typings/api.ts b/src/typings/api.ts index 7e49c6d..2bccda7 100644 --- a/src/typings/api.ts +++ b/src/typings/api.ts @@ -1,4 +1,4 @@ -import { Status, VehicleData } from './common'; +import { Status, Vehicle, VehicleGroup } from './common'; export enum APIDataStatus { OK = 'OK', @@ -329,8 +329,51 @@ export namespace API { export type Response = string[]; } - export namespace Vehicles { - export type Response = VehicleData[]; + export namespace VehiclesData { + export interface VehicleObject { + id: number; + name: string; + type: string; + cabinName: string | null; + restrictions: Record | null; + vehicleGroupsId: number; + } + + export interface VehicleGroupObject { + id: number; + name: string; + speed: number; + speedLoaded?: number; + speedLoco?: number; + length: number; + weight: number; + cargoTypes: VehicleCargo[] | null; + + locoProps: { + coldStart: boolean; + doubleManned: boolean; + } | null; + + massSpeeds: VehicleGroupMassSpeeds | null; + } + + export interface VehicleGroupMassSpeeds { + passenger: Record | null; + cargo: Record | null; + none: number | null; + } + + export interface VehicleCargo { + id: string; + weight: number; + } + + export interface Data { + vehicles: VehicleObject[]; + vehicleGroups: VehicleGroupObject[]; + } + + export type Response = Data; } } diff --git a/src/typings/common.ts b/src/typings/common.ts index 331f88b..5f95d17 100644 --- a/src/typings/common.ts +++ b/src/typings/common.ts @@ -1,5 +1,6 @@ import { RouteLocationRaw } from 'vue-router'; import { StationJSONData } from '../store/typings'; +import { API } from './api'; export type Availability = 'default' | 'unavailable' | 'nonPublic' | 'abandoned' | 'nonDefault'; export type ScenerySpawnType = 'passenger' | 'freight' | 'loco' | 'all'; @@ -169,7 +170,7 @@ export interface ActiveScenery { confirmed: number; unconfirmed: number; }; - missingCheckpoints: string[]; + missingCheckpoints: string[]; } export interface ScenerySpawn { @@ -214,45 +215,8 @@ export interface CheckpointTrain { } // Vehicles Data - -export interface VehicleData { - id: number; - name: string; - type: string; - cabinName: string | null; - restrictions: Record | null; - vehicleGroupsId: number; - group: VehiclesGroup; -} - -export interface VehiclesGroup { - id: number; - name: string; - speed: number; - speedLoaded?: number; - speedLoco?: number; - length: number; - weight: number; - cargoTypes: VehicleCargo[] | null; - - locoProps: { - coldStart: boolean; - doubleManned: boolean; - } | null; - - massSpeeds: VehicleGroupMassSpeeds | null; -} - -export interface VehicleGroupMassSpeeds { - passenger: Record | null; - cargo: Record | null; - none: number | null; -} - -export interface VehicleCargo { - id: string; - weight: number; -} +export type Vehicle = API.VehiclesData.VehicleObject; +export type VehicleGroup = API.VehiclesData.VehicleGroupObject; export interface TooltipUserTrain { driverName: string; diff --git a/vite.config.ts b/vite.config.ts index db95cae..78a3c9e 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -28,7 +28,7 @@ export default defineConfig({ runtimeCaching: [ { urlPattern: - /^https:\/\/stacjownik.spythere.eu\/api\/(getVehicles|getDonators|getSceneries)/i, + /^https:\/\/stacjownik.spythere.eu\/api\/(getVehiclesData|getDonators|getSceneries)/i, handler: 'NetworkFirst', options: { cacheName: 'stacjownik-api-cache',