speed constraints for locomotives-only

This commit is contained in:
2024-02-20 12:14:09 +01:00
parent c9b681eaee
commit 1b5a26e380
3 changed files with 59 additions and 109 deletions
+23 -7
View File
@@ -5,7 +5,17 @@
},
"cargo": {
"2000": 70
}
},
"none": 110
},
"4E": {
"passenger": {
"650": 125
},
"cargo": {
"2000": 70
},
"none": 110
},
"EU07E": {
"passenger": {
@@ -13,25 +23,29 @@
},
"cargo": {
"2000": 70
}
},
"none": 110
},
"EP07": {
"passenger": {
"650": 125
},
"cargo": null
"cargo": null,
"none": 110
},
"EP08": {
"passenger": {
"650": 140
},
"cargo": null
"cargo": null,
"none": 110
},
"EP09": {
"passenger": {
"650": 160
},
"cargo": null
"cargo": null,
"none": 160
},
"ET41": {
"passenger": {
@@ -39,7 +53,8 @@
},
"cargo": {
"4000": 70
}
},
"none": 110
},
"SM42": {
"passenger": {
@@ -61,6 +76,7 @@
"1130": 40,
"1720": 30,
"2400": 20
}
},
"none": 90
}
}
+9 -17
View File
@@ -1,34 +1,26 @@
import speedLimits from "../constants/speedLimits.json";
import massLimits from "../constants/massLimits.json";
import speedLimits from '../constants/speedLimits.json';
import massLimits from '../constants/massLimits.json';
export type SpeedLimitLocoType = keyof typeof speedLimits;
export type MassLimitLocoType = keyof typeof massLimits;
export function calculateSpeedLimit(
locoType: SpeedLimitLocoType,
stockTotalWeight: number,
isTrainPassenger: boolean,
) {
console.log(speedLimits[locoType]);
export function calculateSpeedLimit(locoType: SpeedLimitLocoType, stockTotalWeight: number, stockCount: number, isTrainPassenger: boolean) {
if (speedLimits[locoType] === undefined) return 0;
const speedTable =
speedLimits[locoType][isTrainPassenger ? "passenger" : "cargo"];
if (stockCount == 1) return speedLimits[locoType]['none'];
const stockType = isTrainPassenger ? 'passenger' : 'cargo';
const speedTable = speedLimits[locoType][stockType];
if (!speedTable) return undefined;
let speedLimit = 0;
for (const mass in speedTable)
if (stockTotalWeight > Number(mass)) speedLimit = (speedTable as any)[mass];
for (const mass in speedTable) if (stockTotalWeight > Number(mass)) speedLimit = (speedTable as any)[mass];
return speedLimit;
}
export function calculateMassLimit(
locoType: MassLimitLocoType,
isTrainPassenger: boolean,
) {
export function calculateMassLimit(locoType: MassLimitLocoType, isTrainPassenger: boolean) {
if (massLimits[locoType] === undefined) return 0;
return massLimits[locoType][isTrainPassenger ? 0 : 1] || 0;
+27 -85
View File
@@ -1,21 +1,8 @@
import { EVehicleUseType } from "../enums/EVehicleUseType";
import {
ICarWagon,
ILocomotive,
IStore,
TCarWagonGroup,
TLocoGroup,
} from "../types";
import {
MassLimitLocoType,
SpeedLimitLocoType,
calculateMassLimit,
calculateSpeedLimit,
} from "./vehicleLimitsUtils";
import { EVehicleUseType } from '../enums/EVehicleUseType';
import { ICarWagon, ILocomotive, IStore, TCarWagonGroup, TLocoGroup } from '../types';
import { MassLimitLocoType, SpeedLimitLocoType, calculateMassLimit, calculateSpeedLimit } from './vehicleLimitsUtils';
export function isLocomotive(
vehicle: ILocomotive | ICarWagon,
): vehicle is ILocomotive {
export function isLocomotive(vehicle: ILocomotive | ICarWagon): vehicle is ILocomotive {
return (vehicle as ILocomotive).power !== undefined;
}
@@ -25,18 +12,15 @@ export function locoDataList(state: IStore) {
const stockData = state.stockData;
return Object.keys(stockData.info).reduce((acc, vehiclePower) => {
if (!vehiclePower.startsWith("loco")) return acc;
if (!vehiclePower.startsWith('loco')) return acc;
const locoVehiclesData = stockData.info[vehiclePower as TLocoGroup];
locoVehiclesData.forEach((loco) => {
if (state.showSupporter && !loco[4]) return;
const [type, constructionType, cabinType, maxSpeed, sponsorsTimestamp] =
loco;
const locoProps = stockData.props.find(
(prop) => constructionType == prop.type,
);
const [type, constructionType, cabinType, maxSpeed, sponsorsTimestamp] = loco;
const locoProps = stockData.props.find((prop) => constructionType == prop.type);
acc.push({
power: vehiclePower as TLocoGroup,
@@ -47,16 +31,10 @@ export function locoDataList(state: IStore) {
maxSpeed: Number(maxSpeed),
isSponsorsOnly: Number(sponsorsTimestamp) > Date.now(),
sponsorsOnlyTimestamp: Number(sponsorsTimestamp),
imageSrc: "",
imageSrc: '',
length:
locoProps?.length && type.startsWith("2EN")
? locoProps.length * 2
: locoProps?.length ?? 0,
weight:
locoProps?.weight && type.startsWith("2EN")
? 253000
: locoProps?.weight ?? 0,
length: locoProps?.length && type.startsWith('2EN') ? locoProps.length * 2 : locoProps?.length ?? 0,
weight: locoProps?.weight && type.startsWith('2EN') ? 253000 : locoProps?.weight ?? 0,
coldStart: locoProps?.coldStart ?? false,
doubleManned: locoProps?.doubleManned ?? false,
@@ -73,25 +51,16 @@ export function carDataList(state: IStore) {
const stockData = state.stockData;
return Object.keys(stockData.info).reduce((acc, vehicleUseType) => {
if (!vehicleUseType.startsWith("car")) return acc;
if (!vehicleUseType.startsWith('car')) return acc;
const carVehiclesData = stockData.info[vehicleUseType as TCarWagonGroup];
carVehiclesData.forEach((car) => {
const [
type,
constructionType,
loadable,
sponsorsOnlyTimestamp,
maxSpeed,
] = car;
const [type, constructionType, loadable, sponsorsOnlyTimestamp, maxSpeed] = car;
if (state.showSupporter && Number(sponsorsOnlyTimestamp) <= Date.now())
return;
if (state.showSupporter && Number(sponsorsOnlyTimestamp) <= Date.now()) return;
const carPropsData = stockData.props.find((v) =>
type.toString().startsWith(v.type),
);
const carPropsData = stockData.props.find((v) => type.toString().startsWith(v.type));
acc.push({
useType: vehicleUseType as TCarWagonGroup,
@@ -102,7 +71,7 @@ export function carDataList(state: IStore) {
isSponsorsOnly: Number(sponsorsOnlyTimestamp) > Date.now(),
sponsorsOnlyTimestamp: Number(sponsorsOnlyTimestamp),
maxSpeed: Number(maxSpeed),
imageSrc: "",
imageSrc: '',
cargoTypes: carPropsData?.cargoTypes ?? [],
weight: carPropsData?.weight || 0,
@@ -115,57 +84,34 @@ export function carDataList(state: IStore) {
}
export function totalWeight(state: IStore) {
return state.stockList.reduce(
(acc, stock) =>
acc + (stock.weight + (stock.cargo?.weight ?? 0)) * stock.count,
0,
);
return state.stockList.reduce((acc, stock) => acc + (stock.weight + (stock.cargo?.weight ?? 0)) * stock.count, 0);
}
export function totalLength(state: IStore) {
return state.stockList.reduce(
(acc, stock) => acc + stock.length * stock.count,
0,
);
return state.stockList.reduce((acc, stock) => acc + stock.length * stock.count, 0);
}
export function maxStockSpeed(state: IStore) {
const stockSpeedLimit = state.stockList.reduce(
(acc, stock) => (stock.maxSpeed < acc || acc == 0 ? stock.maxSpeed : acc),
0,
);
const headingLoco = state.stockList[0]?.isLoco
? state.stockList[0]
: undefined;
const stockSpeedLimit = state.stockList.reduce((acc, stock) => (stock.maxSpeed < acc || acc == 0 ? stock.maxSpeed : acc), 0);
const headingLoco = state.stockList[0]?.isLoco ? state.stockList[0] : undefined;
if (!headingLoco) return stockSpeedLimit;
const locoType = headingLoco.type.split("-")[0];
const locoType = headingLoco.type.split('-')[0];
if (/^(EN|2EN|SN)/.test(locoType)) return stockSpeedLimit;
const stockTotalWeight = totalWeight(state);
const speedLimitByMass = calculateSpeedLimit(locoType as SpeedLimitLocoType, totalWeight(state), state.stockList.length, isTrainPassenger(state));
const speedLimitByMass = calculateSpeedLimit(
locoType as SpeedLimitLocoType,
stockTotalWeight,
isTrainPassenger(state),
);
return speedLimitByMass
? Math.min(stockSpeedLimit, speedLimitByMass)
: stockSpeedLimit;
return speedLimitByMass ? Math.min(stockSpeedLimit, speedLimitByMass) : stockSpeedLimit;
}
export function acceptableMass(state: IStore) {
if (state.stockList.length == 0 || !state.stockList[0].isLoco) return 0;
const activeLocomotiveType = state.stockList[0].type.split("-")[0];
const activeLocomotiveType = state.stockList[0].type.split('-')[0];
const locoMassLimit = calculateMassLimit(
activeLocomotiveType as MassLimitLocoType,
isTrainPassenger(state),
);
const locoMassLimit = calculateMassLimit(activeLocomotiveType as MassLimitLocoType, isTrainPassenger(state));
return locoMassLimit;
}
@@ -174,9 +120,7 @@ export function isTrainPassenger(state: IStore) {
if (state.stockList.length == 0) return false;
if (state.stockList.every((stock) => stock.isLoco)) return false;
return state.stockList
.filter((stock) => !stock.isLoco)
.every((stock) => stock.useType === EVehicleUseType.CAR_PASSENGER);
return state.stockList.filter((stock) => !stock.isLoco).every((stock) => stock.useType === EVehicleUseType.CAR_PASSENGER);
}
export function chosenRealStock(state: IStore) {
@@ -185,11 +129,9 @@ export function chosenRealStock(state: IStore) {
for (let i = 0; i < stock.count; i++) acc.push(stock.type);
return acc;
}, [] as string[])
.join(";");
.join(';');
const realStockObj = state.readyStockList.find(
(readyStock) => readyStock.stockString == currentStockString,
);
const realStockObj = state.readyStockList.find((readyStock) => readyStock.stockString == currentStockString);
state.chosenRealStockName = realStockObj?.stockId ?? undefined;