refactor: stock & vehicle typings

This commit is contained in:
2024-04-15 18:32:19 +02:00
parent 61c7f15fcf
commit 541572e415
13 changed files with 175 additions and 133 deletions
+12 -17
View File
@@ -15,21 +15,11 @@ export default defineComponent({
return `${Math.random().toString(36).slice(5)}`;
},
getStockObject(vehicle: IVehicle, cargo?: ICargo | null, count = 1): IStock {
const isLoco = isTractionUnit(vehicle);
getStockObject(vehicle: IVehicle, cargo?: ICargo | null): IStock {
return {
id: this.getStockId(),
type: vehicle.type,
length: vehicle.length,
weight: vehicle.weight,
maxSpeed: vehicle.maxSpeed,
isLoco,
cargo: !isLoco && vehicle.loadable && cargo ? cargo : undefined,
count,
group: isLoco ? vehicle.group : vehicle.group,
constructionType: vehicle.constructionType,
restrictions: vehicle.restrictions,
vehicleRef: vehicle,
cargo: !isTractionUnit(vehicle) && vehicle.loadable && cargo ? cargo : undefined,
};
},
@@ -38,14 +28,19 @@ export default defineComponent({
const stock = this.getStockObject(vehicle, cargo);
if (stock.isLoco && !this.store.stockList[0]?.isLoco) this.store.stockList.unshift(stock);
if (
isTractionUnit(stock.vehicleRef) &&
this.store.stockList.length > 0 &&
!isTractionUnit(this.store.stockList[0].vehicleRef)
)
this.store.stockList.unshift(stock);
else this.store.stockList.push(stock);
},
addLocomotive(loco: ILocomotive) {
const stockObj = this.getStockObject(loco);
if (this.store.stockList.length > 0 && !this.store.stockList[0].isLoco)
if (this.store.stockList.length > 0 && !isTractionUnit(this.store.stockList[0].vehicleRef))
this.store.stockList.unshift(stockObj);
else this.store.stockList.push(stockObj);
},
@@ -72,9 +67,9 @@ export default defineComponent({
let vehicle: IVehicle | null = null;
let vehicleCargo: ICargo | null = null;
const isLoco = /^(EU|EP|ET|SM|EN|2EN|SN)/.test(type);
const isTractionUnit = /^(EU|EP|ET|SM|EN|2EN|SN)/.test(type);
if (isLoco) {
if (isTractionUnit) {
const [locoType, spawnProps] = type.split(',');
vehicle = this.store.locoDataList.find((loco) => loco.type == locoType) || null;
+10 -11
View File
@@ -14,19 +14,18 @@ export default defineComponent({
methods: {
previewStock(stock: IStock) {
if (stock.isLoco) {
const chosenLoco = this.store.locoDataList.find((v) => v.type == stock.type) || null;
this.store.chosenVehicle = chosenLoco;
this.store.chosenLoco = chosenLoco;
this.store.chosenCargo = null;
this.store.chosenLocoGroup = stock.group as LocoGroupType;
} else {
const chosenCar = this.store.carDataList.find((v) => v.type == stock.type) || null;
this.store.chosenVehicle = chosenCar;
this.store.chosenCar = chosenCar;
const vehicleRef = stock.vehicleRef;
this.store.chosenVehicle = vehicleRef;
if (isTractionUnit(vehicleRef)) {
this.store.chosenLoco = vehicleRef;
this.store.chosenCargo = null;
this.store.chosenLocoGroup = vehicleRef.group as LocoGroupType;
} else {
this.store.chosenCar = vehicleRef;
this.store.chosenCargo = stock.cargo || null;
this.store.chosenCarGroup = stock.group as WagonGroupType;
this.store.chosenCarGroup = vehicleRef.group as WagonGroupType;
}
},