Basic Routes

This commit is contained in:
Edward-1100
2025-11-19 17:52:29 -07:00
parent 0d33259f1e
commit a8fc313af3
10 changed files with 6341 additions and 31 deletions

View File

@@ -1,8 +1,106 @@
import fs from "fs";
import path from "path";
vehicleData=require('./data/vehicles.json');
const dataDir = path.join(process.cwd(), "src", "dal", "data");
exports.DAL={
getVehicles:function() {
return vehicleData;
},
};
function readJSON(name) {
try {
const p = path.join(dataDir, name);
if (!fs.existsSync(p)) return null;
const raw = fs.readFileSync(p, "utf8");
return JSON.parse(raw);
} catch (err) {
return null;
}
}
export function getVehicles() {
return readJSON("vehicles.json") || [];
}
export function getVehicleById(id) {
if (id == null) return null;
const vehicles = getVehicles();
return vehicles.find(v => String(v.vehicleId) === String(id)) || null;
}
export function getRoutes() {
const explicit = readJSON("routes.json");
if (Array.isArray(explicit) && explicit.length) return explicit;
const vehicles = getVehicles();
const map = new Map();
vehicles.forEach(v => {
const key = String(v.routeNum ?? "");
if (!map.has(key)) {
map.set(key, {
routeId: key,
routeName: v.routeName ?? null,
startTime: null,
endTime: null,
trains: []
});
}
map.get(key).trains.push(v.vehicleId);
});
return Array.from(map.values());
}
export function getRouteById(routeId) {
if (routeId == null) return null;
const routes = getRoutes();
return routes.find(r => String(r.routeId) === String(routeId)) || null;
}
export function getRoutePathsMap() {
const raw = readJSON("routepaths.json");
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
}
export function getRoutePath(routeId) {
if (routeId == null) return null;
const map = getRoutePathsMap();
const keys = Object.keys(map || {});
const foundKey = keys.find(k => String(k) === String(routeId));
return foundKey ? map[foundKey] : null;
}
export function getStations() {
return readJSON("stations.json") || [];
}
export function getStopsByRoute(routeId) {
if (routeId == null) return [];
const stations = getStations();
if (!Array.isArray(stations)) return [];
return stations.filter(s => {
const lines = s.lines ?? s.lines_arr ?? s.line_ids ?? null;
if (!Array.isArray(lines)) return false;
return lines.map(String).includes(String(routeId));
});
}
export function getStationById(stationId) {
if (stationId == null) return null;
const stations = getStations();
if (!Array.isArray(stations)) return null;
return stations.find(s => {
if (s.stop_id && String(s.stop_id) === String(stationId)) return true;
if (s.stationId && String(s.stationId) === String(stationId)) return true;
if (s.id && String(s.id) === String(stationId)) return true;
return false;
}) || null;
}