chore: adjusted to new version of API vehicles data

This commit is contained in:
2024-06-08 20:53:22 +02:00
parent c252213ed9
commit 59a5fbe5ac
6 changed files with 61 additions and 68 deletions
@@ -23,9 +23,9 @@
<span v-if="vehicleCargo">({{ vehicleCargo.id }})</span> <span v-if="vehicleCargo">({{ vehicleCargo.id }})</span>
</div> </div>
<div class="vehicle-props" v-if="vehicleProps"> <div class="vehicle-props" v-if="vehicleData">
{{ vehicleProps.speed }}km/h &bull; {{ vehicleProps.length }}m &bull; {{ vehicleData.group.speed }}km/h &bull; {{ vehicleData.group.length }}m &bull;
{{ (vehicleProps.weight / 1000).toFixed(1) }}t {{ (vehicleData.group.weight / 1000).toFixed(1) }}t
<span v-if="vehicleCargo">(+{{ (vehicleCargo.weight / 1000).toFixed(1) }}t)</span> <span v-if="vehicleCargo">(+{{ (vehicleCargo.weight / 1000).toFixed(1) }}t)</span>
</div> </div>
</div> </div>
@@ -72,23 +72,27 @@ export default defineComponent({
return this.tooltipStore.content.split(':')[0]; return this.tooltipStore.content.split(':')[0];
}, },
vehicleCargo() { vehicleData() {
return this.vehicleProps?.cargoTypes?.find( return this.apiStore.vehiclesData?.find((v) => v.name == this.vehicleName);
(c) => c.id == this.tooltipStore.content.split(':')[1]
);
}, },
vehicleProps() { vehicleCargo() {
const vehicleDataArray = this.apiStore.vehiclesData?.vehicleList.find( return this.vehicleData?.group.cargoTypes?.find(
([name]) => name === this.vehicleName (c) => c.id == this.tooltipStore.content.split(':')[1]
);
if (!vehicleDataArray) return null;
return (
this.apiStore.vehiclesData!.vehicleProps.find((v) => v.type == vehicleDataArray[1]) ?? null
); );
} }
// 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
// );
// }
} }
}); });
</script> </script>
+8 -18
View File
@@ -132,7 +132,7 @@
<img src="/images/icon-speed.svg" alt="speed icon" /> <img src="/images/icon-speed.svg" alt="speed icon" />
{{ train.speed }} km/h {{ train.speed }} km/h
<span v-if="maxSpeed != Infinity"> <span v-if="stockSpeedLimit != Infinity">
&bull; &bull;
<em <em
class="text--grayed" class="text--grayed"
@@ -140,7 +140,7 @@
tabindex="0" tabindex="0"
:data-tooltip="$t('trains.vmax-tooltip')" :data-tooltip="$t('trains.vmax-tooltip')"
> >
{{ maxSpeed }} km/h {{ stockSpeedLimit }} km/h
</em> </em>
</span> </span>
</div> </div>
@@ -204,24 +204,14 @@ export default defineComponent({
}, },
computed: { computed: {
maxSpeed() { stockSpeedLimit() {
return this.train.stockList.reduce((acc, stockName) => { return this.train.stockList.reduce((acc, stockName) => {
const stockVehicleInfo = this.apiStore.vehiclesData?.vehicleList.find( const vehicleSpeed =
(v) => v[0] == stockName.split(':')[0] this.apiStore.vehiclesData?.find((v) => v.name == stockName.split(':')[0])?.group.speed ??
); 300;
if (!stockVehicleInfo) return acc; return Math.min(vehicleSpeed, acc);
}, 300);
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);
} }
}, },
+9 -9
View File
@@ -106,18 +106,18 @@ export const useApiStore = defineStore('apiStore', {
}, },
async fetchVehiclesInfo() { async fetchVehiclesInfo() {
if (import.meta.env.VITE_API_VEHICLES_MODE == 'mocking') { // if (import.meta.env.VITE_API_VEHICLES_MODE == 'mocking') {
import('../../tests/data/vehicles.json').then((data) => { // import('../../tests/data/vehicles.json').then((data) => {
console.warn('vehicles.json: mocking mode'); // console.warn('vehicles.json: mocking mode');
this.vehiclesData = data.default; // this.vehiclesData = data.default;
this.dataStatuses.vehicles = Status.Data.Loaded; // this.dataStatuses.vehicles = Status.Data.Loaded;
}); // });
return; // return;
} // }
try { try {
const response = await this.client!.get<API.Vehicles.Response>('vehicles'); const response = await this.client!.get<API.Vehicles.Response>('api/getVehicles');
this.vehiclesData = response.data; this.vehiclesData = response.data;
this.dataStatuses.vehicles = response.data ? Status.Data.Loaded : Status.Data.Warning; this.dataStatuses.vehicles = response.data ? Status.Data.Loaded : Status.Data.Warning;
+2 -2
View File
@@ -1,4 +1,4 @@
import { Status, VehiclesData } from './common'; import { Status, VehicleData } from './common';
export enum APIDataStatus { export enum APIDataStatus {
OK = 'OK', OK = 'OK',
@@ -320,7 +320,7 @@ export namespace API {
} }
export namespace Vehicles { export namespace Vehicles {
export type Response = VehiclesData; export type Response = VehicleData[];
} }
} }
+16 -10
View File
@@ -191,22 +191,28 @@ export interface CheckpointTrain {
} }
// Vehicles Data // Vehicles Data
export interface VehiclesData {
simulatorVersion: string;
vehicleList: any[][]; export interface VehicleData {
id: number;
vehicleProps: VehicleProps[]; name: string;
type: string;
cabinName: string | null;
restrictions: Record<string, any> | null;
vehicleGroupsId: number;
group: VehiclesGroup;
} }
export interface VehicleProps { export interface VehiclesGroup {
type: string; id: number;
name: string;
speed: number; speed: number;
length: number; length: number;
weight: number; weight: number;
cargoTypes?: VehicleCargo[]; cargoTypes: VehicleCargo[] | null;
coldStart?: boolean; locoProps: {
doubleManned?: boolean; coldStart: boolean;
doubleManned: boolean;
} | null;
} }
export interface VehicleCargo { export interface VehicleCargo {
+6 -13
View File
@@ -24,33 +24,26 @@ export default defineConfig({
cleanupOutdatedCaches: true, cleanupOutdatedCaches: true,
runtimeCaching: [ runtimeCaching: [
{ {
urlPattern: /^https:\/\/stacjownik.spythere.eu\/api\/getSceneries/i, urlPattern:
handler: 'StaleWhileRevalidate', /^https:\/\/stacjownik.spythere.eu\/api\/(getVehicles|getDonators|getSceneries)/i,
handler: 'NetworkFirst',
options: { options: {
cacheName: 'spythere-sceneries-cache', cacheName: 'stacjownik-api-cache',
cacheableResponse: { cacheableResponse: {
statuses: [0, 200] statuses: [0, 200]
} }
} }
}, },
{
urlPattern: /^https:\/\/stacjownik.spythere.eu\/vehicles/i,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'spythere-vehicles-cache'
}
},
{ {
urlPattern: /^https:\/\/static.spythere.eu\/.*/i, urlPattern: /^https:\/\/static.spythere.eu\/.*/i,
handler: 'CacheFirst', handler: 'StaleWhileRevalidate',
options: { options: {
cacheName: 'spythere-static-cache', cacheName: 'spythere-static-cache',
cacheableResponse: { cacheableResponse: {
statuses: [0, 200] statuses: [0, 200]
}, },
expiration: { expiration: {
maxEntries: 100, maxEntries: 100
maxAgeSeconds: 60 * 60 * 8
} }
} }
} }