mirror of
https://github.com/Spythere/pojazdownik.git
synced 2026-05-03 05:18:10 +00:00
149 lines
4.3 KiB
TypeScript
149 lines
4.3 KiB
TypeScript
import {
|
|
IVehiclesAPIResponse,
|
|
ICarWagon,
|
|
ILocomotive,
|
|
ICargo,
|
|
IVehicle,
|
|
IStock,
|
|
IRealComposition,
|
|
LocoGroupType,
|
|
WagonGroupType,
|
|
IVehicleData,
|
|
StorageStockEntry,
|
|
} from './types/common.types';
|
|
import { defineStore } from 'pinia';
|
|
import {
|
|
acceptableWeight,
|
|
carDataList,
|
|
getCargoWarnings,
|
|
isTractionUnit,
|
|
isTrainPassenger,
|
|
locoDataList,
|
|
maxStockSpeed,
|
|
stockSupportsColdStart,
|
|
stockSupportsDoubleManning,
|
|
totalLength,
|
|
totalWeight,
|
|
} from './utils/vehicleUtils';
|
|
|
|
import realCompositionsJSON from './data/realCompositions.json';
|
|
import { HttpClient } from './http';
|
|
import { API } from './types/api.types';
|
|
|
|
const baseURL = import.meta.env.VITE_API_DEV === '1' && import.meta.env.DEV ? 'http://localhost:3001' : 'https://stacjownik.spythere.eu';
|
|
|
|
export const useStore = defineStore('store', {
|
|
state: () => ({
|
|
chosenCar: null as ICarWagon | null,
|
|
chosenLoco: null as ILocomotive | null,
|
|
chosenCargo: null as ICargo | null,
|
|
chosenVehicle: null as IVehicle | null,
|
|
|
|
isColdStart: false,
|
|
isDoubleManned: false,
|
|
|
|
chosenLocoGroup: 'loco-electric' as LocoGroupType,
|
|
chosenCarGroup: 'wagon-passenger' as WagonGroupType,
|
|
|
|
stockList: [] as IStock[],
|
|
cargoOptions: [] as any[][],
|
|
|
|
swapVehicles: false,
|
|
|
|
chosenStockListIndex: -1,
|
|
|
|
vehiclePreviewSrc: '',
|
|
|
|
isMigrationInfoOpen: false,
|
|
isRandomizerCardOpen: false,
|
|
isRealStockListCardOpen: false,
|
|
|
|
vehiclesData: undefined as IVehicleData[] | undefined,
|
|
|
|
lastFocusedElement: null as HTMLElement | null,
|
|
|
|
storageStockData: {} as Record<string, StorageStockEntry>,
|
|
chosenStorageStockName: '',
|
|
chosenStorageStockString: '',
|
|
|
|
compatibleSimulatorVersion: '2025.1.1',
|
|
|
|
httpClient: new HttpClient(baseURL),
|
|
}),
|
|
|
|
getters: {
|
|
locoDataList: (state) => locoDataList(state.vehiclesData),
|
|
carDataList: (state) => carDataList(state.vehiclesData),
|
|
vehicleDataList: (state) => [...locoDataList(state.vehiclesData), ...carDataList(state.vehiclesData)],
|
|
totalWeight: (state) => totalWeight(state.stockList),
|
|
totalLength: (state) => totalLength(state.stockList),
|
|
maxStockSpeed: (state) => maxStockSpeed(state.stockList),
|
|
isTrainPassenger: (state) => isTrainPassenger(state.stockList),
|
|
acceptableWeight: (state) => acceptableWeight(state.stockList),
|
|
cargoWarnings: (state) => getCargoWarnings(state.stockList),
|
|
|
|
stockSupportsColdStart: (state) => stockSupportsColdStart(state.stockList),
|
|
stockSupportsDoubleManning: (state) => stockSupportsDoubleManning(state.stockList),
|
|
|
|
stockString: (state) => {
|
|
if (state.stockList.length == 0) return '';
|
|
|
|
const coldStartActive = state.isColdStart && stockSupportsColdStart(state.stockList);
|
|
const doubleManningActive = state.isDoubleManned && stockSupportsDoubleManning(state.stockList);
|
|
|
|
return state.stockList
|
|
.map((stock, i) => {
|
|
let stockTypeStr = isTractionUnit(stock.vehicleRef) || !stock.cargo ? stock.vehicleRef.type : `${stock.vehicleRef.type}:${stock.cargo.id}`;
|
|
|
|
if (i == 0 && (coldStartActive || doubleManningActive))
|
|
return `${stockTypeStr},${coldStartActive ? 'c' : ''}${doubleManningActive ? 'd' : ''}`;
|
|
|
|
return stockTypeStr;
|
|
})
|
|
.join(';');
|
|
},
|
|
|
|
realCompositionList: (state) => {
|
|
if (!state.vehiclesData) return [];
|
|
|
|
return Object.keys(realCompositionsJSON).reduce<IRealComposition[]>((acc, key) => {
|
|
const [type, number, ...name] = key.split(' ');
|
|
|
|
const obj = {
|
|
number: number.replace(/_/g, '/'),
|
|
name: name.join(' '),
|
|
stockString: realCompositionsJSON[key as keyof typeof realCompositionsJSON],
|
|
type,
|
|
};
|
|
|
|
acc.push({
|
|
stockId: `${obj.type} ${obj.number} ${obj.name}`,
|
|
...obj,
|
|
});
|
|
|
|
return acc;
|
|
}, []);
|
|
},
|
|
},
|
|
|
|
actions: {
|
|
async fetchVehiclesAPI() {
|
|
try {
|
|
const response = await this.httpClient.get<API.VehiclesData.Response>('api/getVehiclesData');
|
|
this.vehiclesData = response.vehicles.map((v) => ({
|
|
...v,
|
|
group: response.vehicleGroups.find((g) => g.id == v.vehicleGroupsId)!,
|
|
}));
|
|
|
|
console.log(response);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
},
|
|
|
|
async setupAPIData() {
|
|
await this.fetchVehiclesAPI();
|
|
},
|
|
},
|
|
});
|