style: add eslint and prettier
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import type { ValhallaCosting, ValhallaCostingOptions, ValhallaRequest } from "$lib/services/navigation/ValhallaRequest";
|
||||
import type {
|
||||
ValhallaCosting,
|
||||
ValhallaCostingOptions,
|
||||
ValhallaRequest,
|
||||
} from "$lib/services/navigation/ValhallaRequest";
|
||||
import type { Vehicle } from "./vehicles.svelte";
|
||||
|
||||
function getVehicleCosting(vehicle: Vehicle): ValhallaCosting {
|
||||
@@ -17,31 +21,43 @@ function getVehicleCosting(vehicle: Vehicle): ValhallaCosting {
|
||||
}
|
||||
}
|
||||
|
||||
export function createValhallaRequest(vehicle: Vehicle, locations: WorldLocation[]): ValhallaRequest {
|
||||
export function createValhallaRequest(
|
||||
vehicle: Vehicle,
|
||||
locations: WorldLocation[],
|
||||
): ValhallaRequest {
|
||||
const costing = getVehicleCosting(vehicle);
|
||||
let costingOptions: ValhallaCostingOptions = costing == "auto" ? {
|
||||
auto: {
|
||||
top_speed: vehicle.legalMaxSpeed,
|
||||
fixed_speed: vehicle.actualMaxSpeed
|
||||
}
|
||||
} : costing == "motor_scooter" ? {
|
||||
motor_scooter: {
|
||||
top_speed: vehicle.legalMaxSpeed,
|
||||
fixed_speed: vehicle.actualMaxSpeed
|
||||
}
|
||||
} : costing == "truck" ? {
|
||||
truck: {
|
||||
top_speed: vehicle.legalMaxSpeed,
|
||||
fixed_speed: vehicle.actualMaxSpeed,
|
||||
length: vehicle.length,
|
||||
weight: vehicle.weight,
|
||||
axle_load: vehicle.axisLoad
|
||||
}
|
||||
} : costing == "bicycle" ? {
|
||||
bicycle: {
|
||||
cycling_speed: vehicle.actualMaxSpeed
|
||||
}
|
||||
} : {};
|
||||
const costingOptions: ValhallaCostingOptions =
|
||||
costing == "auto"
|
||||
? {
|
||||
auto: {
|
||||
top_speed: vehicle.legalMaxSpeed,
|
||||
fixed_speed: vehicle.actualMaxSpeed,
|
||||
},
|
||||
}
|
||||
: costing == "motor_scooter"
|
||||
? {
|
||||
motor_scooter: {
|
||||
top_speed: vehicle.legalMaxSpeed,
|
||||
fixed_speed: vehicle.actualMaxSpeed,
|
||||
},
|
||||
}
|
||||
: costing == "truck"
|
||||
? {
|
||||
truck: {
|
||||
top_speed: vehicle.legalMaxSpeed,
|
||||
fixed_speed: vehicle.actualMaxSpeed,
|
||||
length: vehicle.length,
|
||||
weight: vehicle.weight,
|
||||
axle_load: vehicle.axisLoad,
|
||||
},
|
||||
}
|
||||
: costing == "bicycle"
|
||||
? {
|
||||
bicycle: {
|
||||
cycling_speed: vehicle.actualMaxSpeed,
|
||||
},
|
||||
}
|
||||
: {};
|
||||
return {
|
||||
locations,
|
||||
costing,
|
||||
@@ -49,6 +65,6 @@ export function createValhallaRequest(vehicle: Vehicle, locations: WorldLocation
|
||||
alternates: 2,
|
||||
language: "de-DE",
|
||||
costing_options: costingOptions,
|
||||
turn_lanes: true
|
||||
}
|
||||
turn_lanes: true,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,19 +6,42 @@ bicycle, prefer cycleways and bicycle lanes = bicycle
|
||||
truck, prioritizes truck routes = truck
|
||||
motor_scooter = motor scooter, moped, lkfz
|
||||
*/
|
||||
export const VehicleTypes = ["car", "truck", "motorcycle", "bicycle", "motor_scooter"] as const;
|
||||
export type VehicleType = typeof VehicleTypes[number];
|
||||
export const FuelTypes = ["petrol", "diesel", "electric"] as const;
|
||||
export type FuelType = typeof FuelTypes[number];
|
||||
export const EVConnectors = [
|
||||
"Type 2", "CCS", "CHAdeMO", "Tesla Supercharger", "Type 1", "Type 3",
|
||||
"SEV 1011 (Type 13)", "SEV 1011 (Type 15)", "SEV 1011 (Type 23)", "SEV 1011 (Type 25)",
|
||||
"CEE (red)", "CEE (blue)", "Schuko", "CCS Type 2", "Other"
|
||||
export const VehicleTypes = [
|
||||
"car",
|
||||
"truck",
|
||||
"motorcycle",
|
||||
"bicycle",
|
||||
"motor_scooter",
|
||||
] as const;
|
||||
export type EVConnector = typeof EVConnectors[number];
|
||||
export const PreferredFuels = ["Super", "Super E10", "Diesel", ...EVConnectors] as const;
|
||||
export type PreferredFuel = typeof PreferredFuels[number];
|
||||
export type Vehicle = {
|
||||
export type VehicleType = (typeof VehicleTypes)[number];
|
||||
export const FuelTypes = ["petrol", "diesel", "electric"] as const;
|
||||
export type FuelType = (typeof FuelTypes)[number];
|
||||
export const EVConnectors = [
|
||||
"Type 2",
|
||||
"CCS",
|
||||
"CHAdeMO",
|
||||
"Tesla Supercharger",
|
||||
"Type 1",
|
||||
"Type 3",
|
||||
"SEV 1011 (Type 13)",
|
||||
"SEV 1011 (Type 15)",
|
||||
"SEV 1011 (Type 23)",
|
||||
"SEV 1011 (Type 25)",
|
||||
"CEE (red)",
|
||||
"CEE (blue)",
|
||||
"Schuko",
|
||||
"CCS Type 2",
|
||||
"Other",
|
||||
] as const;
|
||||
export type EVConnector = (typeof EVConnectors)[number];
|
||||
export const PreferredFuels = [
|
||||
"Super",
|
||||
"Super E10",
|
||||
"Diesel",
|
||||
...EVConnectors,
|
||||
] as const;
|
||||
export type PreferredFuel = (typeof PreferredFuels)[number];
|
||||
export interface Vehicle {
|
||||
name: string;
|
||||
legalMaxSpeed: number;
|
||||
actualMaxSpeed: number;
|
||||
@@ -31,7 +54,7 @@ export type Vehicle = {
|
||||
emissionClass: string;
|
||||
fuelType: FuelType;
|
||||
preferredFuel: PreferredFuel;
|
||||
};
|
||||
}
|
||||
|
||||
export const DefaultVehicle: Vehicle = {
|
||||
name: "Default Vehicle",
|
||||
@@ -40,49 +63,64 @@ export const DefaultVehicle: Vehicle = {
|
||||
type: "motor_scooter",
|
||||
emissionClass: "Euro 4",
|
||||
fuelType: "diesel",
|
||||
preferredFuel: "Diesel"
|
||||
}
|
||||
preferredFuel: "Diesel",
|
||||
};
|
||||
|
||||
type StateValue<T> = {v: T};
|
||||
export const vehicles: Vehicle[] = $state(localStorage.getItem("vehicles") ? JSON.parse(localStorage.getItem("vehicles")!) : []);
|
||||
interface StateValue<T> {
|
||||
v: T;
|
||||
}
|
||||
export const vehicles: Vehicle[] = $state(
|
||||
localStorage.getItem("vehicles")
|
||||
? JSON.parse(localStorage.getItem("vehicles")!)
|
||||
: [],
|
||||
);
|
||||
export const selectedVehicleIdx: StateValue<number | null> = $state({
|
||||
v: localStorage.getItem("selectedVehicle") ? parseInt(localStorage.getItem("selectedVehicle")!) : null
|
||||
v: localStorage.getItem("selectedVehicle")
|
||||
? parseInt(localStorage.getItem("selectedVehicle")!)
|
||||
: null,
|
||||
});
|
||||
export const selectedVehicle: () => Vehicle | null = () => {
|
||||
return vehicles[selectedVehicleIdx.v !== null ? selectedVehicleIdx.v : 0] || null
|
||||
return (
|
||||
vehicles[selectedVehicleIdx.v !== null ? selectedVehicleIdx.v : 0] || null
|
||||
);
|
||||
};
|
||||
|
||||
export function setVehicles(_vehicles: Vehicle[]) {
|
||||
// vehicles = _vehicles;
|
||||
// Hack to update without reassigning the array
|
||||
vehicles.length = 0;
|
||||
_vehicles.forEach(vehicle => vehicles.push(vehicle));
|
||||
_vehicles.forEach((vehicle) => vehicles.push(vehicle));
|
||||
localStorage.setItem("vehicles", JSON.stringify(vehicles));
|
||||
}
|
||||
|
||||
export function selectVehicle(vehicle: Vehicle | null) {
|
||||
if(vehicle == null) {
|
||||
if (vehicle == null) {
|
||||
selectedVehicleIdx.v = null;
|
||||
} else {
|
||||
selectedVehicleIdx.v = vehicles.findIndex(v => v.name === vehicle.name);
|
||||
if(selectedVehicleIdx.v === -1) {
|
||||
selectedVehicleIdx.v = vehicles.findIndex((v) => v.name === vehicle.name);
|
||||
if (selectedVehicleIdx.v === -1) {
|
||||
selectedVehicleIdx.v = null;
|
||||
}
|
||||
}
|
||||
localStorage.setItem("selectedVehicle", selectedVehicleIdx.v !== null ? selectedVehicleIdx.v.toString() : "");
|
||||
localStorage.setItem(
|
||||
"selectedVehicle",
|
||||
selectedVehicleIdx.v !== null ? selectedVehicleIdx.v.toString() : "",
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the vehicle uses the correct preferred fuel type
|
||||
*/
|
||||
export function isValidFuel(vehicle: Vehicle): boolean {
|
||||
if(vehicle.fuelType == "petrol") {
|
||||
return vehicle.preferredFuel == "Super" || vehicle.preferredFuel == "Super E10";
|
||||
if (vehicle.fuelType == "petrol") {
|
||||
return (
|
||||
vehicle.preferredFuel == "Super" || vehicle.preferredFuel == "Super E10"
|
||||
);
|
||||
}
|
||||
if(vehicle.fuelType == "diesel") {
|
||||
if (vehicle.fuelType == "diesel") {
|
||||
return vehicle.preferredFuel == "Diesel";
|
||||
}
|
||||
if(vehicle.fuelType == "electric") {
|
||||
if (vehicle.fuelType == "electric") {
|
||||
return EVConnectors.includes(vehicle.preferredFuel as EVConnector);
|
||||
}
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user