From 59a5fbe5ac3db37b7cfd045a11fc19b1805b65f3 Mon Sep 17 00:00:00 2001 From: Spythere Date: Sat, 8 Jun 2024 20:53:22 +0200 Subject: [PATCH] chore: adjusted to new version of API vehicles data --- .../Tooltip/VehiclePreviewTooltip.vue | 36 ++++++++++--------- src/components/TrainsView/TrainInfo.vue | 26 +++++--------- src/store/apiStore.ts | 18 +++++----- src/typings/api.ts | 4 +-- src/typings/common.ts | 26 ++++++++------ vite.config.ts | 19 ++++------ 6 files changed, 61 insertions(+), 68 deletions(-) diff --git a/src/components/Tooltip/VehiclePreviewTooltip.vue b/src/components/Tooltip/VehiclePreviewTooltip.vue index 6e7f452..5853f7e 100644 --- a/src/components/Tooltip/VehiclePreviewTooltip.vue +++ b/src/components/Tooltip/VehiclePreviewTooltip.vue @@ -23,9 +23,9 @@ ({{ vehicleCargo.id }}) -
- {{ vehicleProps.speed }}km/h • {{ vehicleProps.length }}m • - {{ (vehicleProps.weight / 1000).toFixed(1) }}t +
+ {{ vehicleData.group.speed }}km/h • {{ vehicleData.group.length }}m • + {{ (vehicleData.group.weight / 1000).toFixed(1) }}t (+{{ (vehicleCargo.weight / 1000).toFixed(1) }}t)
@@ -72,23 +72,27 @@ export default defineComponent({ return this.tooltipStore.content.split(':')[0]; }, - vehicleCargo() { - return this.vehicleProps?.cargoTypes?.find( - (c) => c.id == this.tooltipStore.content.split(':')[1] - ); + vehicleData() { + return this.apiStore.vehiclesData?.find((v) => v.name == this.vehicleName); }, - vehicleProps() { - const vehicleDataArray = this.apiStore.vehiclesData?.vehicleList.find( - ([name]) => name === this.vehicleName - ); - - if (!vehicleDataArray) return null; - - return ( - this.apiStore.vehiclesData!.vehicleProps.find((v) => v.type == vehicleDataArray[1]) ?? null + vehicleCargo() { + return this.vehicleData?.group.cargoTypes?.find( + (c) => c.id == this.tooltipStore.content.split(':')[1] ); } + + // vehicleProps() { + // const vehicleDataArray = this.apiStore.vehiclesData?.vehicleList.find( + // ([name]) => name === this.vehicleName + // ); + + // if (!vehicleDataArray) return null; + + // return ( + // this.apiStore.vehiclesData!.vehicleProps.find((v) => v.type == vehicleDataArray[1]) ?? null + // ); + // } } }); diff --git a/src/components/TrainsView/TrainInfo.vue b/src/components/TrainsView/TrainInfo.vue index 7649429..17c0e15 100644 --- a/src/components/TrainsView/TrainInfo.vue +++ b/src/components/TrainsView/TrainInfo.vue @@ -132,7 +132,7 @@ speed icon {{ train.speed }} km/h - + - {{ maxSpeed }} km/h + {{ stockSpeedLimit }} km/h @@ -204,24 +204,14 @@ export default defineComponent({ }, computed: { - maxSpeed() { + stockSpeedLimit() { return this.train.stockList.reduce((acc, stockName) => { - const stockVehicleInfo = this.apiStore.vehiclesData?.vehicleList.find( - (v) => v[0] == stockName.split(':')[0] - ); + const vehicleSpeed = + this.apiStore.vehiclesData?.find((v) => v.name == stockName.split(':')[0])?.group.speed ?? + 300; - if (!stockVehicleInfo) return acc; - - const stockVehicleProps = this.apiStore.vehiclesData?.vehicleProps.find( - (v) => v.type == stockVehicleInfo[1] - ); - - if (!stockVehicleProps) return acc; - - if (stockVehicleProps.speed < acc) return stockVehicleProps.speed; - - return acc; - }, Infinity); + return Math.min(vehicleSpeed, acc); + }, 300); } }, diff --git a/src/store/apiStore.ts b/src/store/apiStore.ts index 8f0562b..bb828dd 100644 --- a/src/store/apiStore.ts +++ b/src/store/apiStore.ts @@ -106,18 +106,18 @@ export const useApiStore = defineStore('apiStore', { }, async fetchVehiclesInfo() { - if (import.meta.env.VITE_API_VEHICLES_MODE == 'mocking') { - import('../../tests/data/vehicles.json').then((data) => { - console.warn('vehicles.json: mocking mode'); - this.vehiclesData = data.default; - this.dataStatuses.vehicles = Status.Data.Loaded; - }); + // if (import.meta.env.VITE_API_VEHICLES_MODE == 'mocking') { + // import('../../tests/data/vehicles.json').then((data) => { + // console.warn('vehicles.json: mocking mode'); + // this.vehiclesData = data.default; + // this.dataStatuses.vehicles = Status.Data.Loaded; + // }); - return; - } + // return; + // } try { - const response = await this.client!.get('vehicles'); + const response = await this.client!.get('api/getVehicles'); 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 a37e82a..4619cc0 100644 --- a/src/typings/api.ts +++ b/src/typings/api.ts @@ -1,4 +1,4 @@ -import { Status, VehiclesData } from './common'; +import { Status, VehicleData } from './common'; export enum APIDataStatus { OK = 'OK', @@ -320,7 +320,7 @@ export namespace API { } export namespace Vehicles { - export type Response = VehiclesData; + export type Response = VehicleData[]; } } diff --git a/src/typings/common.ts b/src/typings/common.ts index e53b9da..631aab5 100644 --- a/src/typings/common.ts +++ b/src/typings/common.ts @@ -191,22 +191,28 @@ export interface CheckpointTrain { } // Vehicles Data -export interface VehiclesData { - simulatorVersion: string; - vehicleList: any[][]; - - vehicleProps: VehicleProps[]; +export interface VehicleData { + id: number; + name: string; + type: string; + cabinName: string | null; + restrictions: Record | null; + vehicleGroupsId: number; + group: VehiclesGroup; } -export interface VehicleProps { - type: string; +export interface VehiclesGroup { + id: number; + name: string; speed: number; length: number; weight: number; - cargoTypes?: VehicleCargo[]; - coldStart?: boolean; - doubleManned?: boolean; + cargoTypes: VehicleCargo[] | null; + locoProps: { + coldStart: boolean; + doubleManned: boolean; + } | null; } export interface VehicleCargo { diff --git a/vite.config.ts b/vite.config.ts index e32fe95..3dc3cb1 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -24,33 +24,26 @@ export default defineConfig({ cleanupOutdatedCaches: true, runtimeCaching: [ { - urlPattern: /^https:\/\/stacjownik.spythere.eu\/api\/getSceneries/i, - handler: 'StaleWhileRevalidate', + urlPattern: + /^https:\/\/stacjownik.spythere.eu\/api\/(getVehicles|getDonators|getSceneries)/i, + handler: 'NetworkFirst', options: { - cacheName: 'spythere-sceneries-cache', + cacheName: 'stacjownik-api-cache', cacheableResponse: { statuses: [0, 200] } } }, - { - urlPattern: /^https:\/\/stacjownik.spythere.eu\/vehicles/i, - handler: 'StaleWhileRevalidate', - options: { - cacheName: 'spythere-vehicles-cache' - } - }, { urlPattern: /^https:\/\/static.spythere.eu\/.*/i, - handler: 'CacheFirst', + handler: 'StaleWhileRevalidate', options: { cacheName: 'spythere-static-cache', cacheableResponse: { statuses: [0, 200] }, expiration: { - maxEntries: 100, - maxAgeSeconds: 60 * 60 * 8 + maxEntries: 100 } } }