backend improvements
This commit is contained in:
+1
-1
File diff suppressed because one or more lines are too long
@@ -132,6 +132,16 @@ for (let i = 0; i < stop_times.length - 1; i++) {
|
||||
}
|
||||
}
|
||||
|
||||
function routesForStation(station_id) {
|
||||
return [
|
||||
...new Set(
|
||||
Object.values(edges)
|
||||
.filter((e) => e.a == station_id || e.b == station_id)
|
||||
.flatMap((e) => [...e.route])
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
const stations = [
|
||||
...new Set(
|
||||
Object.values(edges)
|
||||
@@ -141,7 +151,7 @@ const stations = [
|
||||
];
|
||||
const output = {
|
||||
stations: indexRecords(
|
||||
stations.map((s) => indexedStops[s]),
|
||||
stations.map((s) => ({ ...indexedStops[s], routes: routesForStation(s) })),
|
||||
"stop_id"
|
||||
),
|
||||
edges: Object.values(edges).map((e) => ({ ...e, route: [...e.route] })),
|
||||
@@ -153,3 +163,5 @@ const output = {
|
||||
};
|
||||
|
||||
await Deno.writeTextFile("cities/boston.json", JSON.stringify(output));
|
||||
|
||||
console.log("done");
|
||||
|
||||
+12
-11
@@ -4,22 +4,23 @@ const agency = {
|
||||
..._agency,
|
||||
// Convert `route` arrays to `Set`s
|
||||
edges: _agency.edges.map((e) => ({ ...e, route: new Set(e.route) })),
|
||||
stations: Object.fromEntries(
|
||||
Object.entries(_agency.stations).map(([k, s]) => [
|
||||
k,
|
||||
{
|
||||
...s,
|
||||
routes: new Set(s.routes),
|
||||
},
|
||||
])
|
||||
),
|
||||
};
|
||||
|
||||
export function linesForStation(station_id) {
|
||||
return new Set(
|
||||
agency.edges
|
||||
.filter((e) => e.a == station_id || e.b == station_id)
|
||||
.flatMap((e) => [...e.route])
|
||||
);
|
||||
}
|
||||
|
||||
export function randomStationPair() {
|
||||
const keys = Object.keys(agency.stations);
|
||||
// select a random source station
|
||||
const a = keys[Math.floor(Math.random() * keys.length)];
|
||||
|
||||
const aLines = linesForStation(a);
|
||||
const aLines = agency.stations[a].routes;
|
||||
|
||||
const stationsOnSameLine = agency.edges
|
||||
.filter((e) => !aLines.isDisjointFrom(e.route))
|
||||
@@ -45,8 +46,8 @@ function stationNeighbors(station) {
|
||||
|
||||
// caleb's patented graph traversal algorithm (tm)
|
||||
function routeGoesBetweenStations(route, stationA, stationB) {
|
||||
let currentNodes = [stationA];
|
||||
let visited = [];
|
||||
const currentNodes = [stationA];
|
||||
const visited = [];
|
||||
let stops = 0;
|
||||
|
||||
let possible = false;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script>
|
||||
import { onMount, createEventDispatcher } from "svelte";
|
||||
import agency, { linesForStation, minStops, randomStationPair, stepsValidForStationPair } from "../agency"
|
||||
import agency, { minStops, randomStationPair, stepsValidForStationPair } from "../agency"
|
||||
import MbtaLine from "./MbtaLine.svelte";
|
||||
import RouteBuilder from "./RouteBuilder.svelte";
|
||||
import gsap from "gsap";
|
||||
@@ -76,7 +76,7 @@
|
||||
<h3 class="text-3xl">
|
||||
<span class="inline-flex items-center justify-center gap-2">
|
||||
<span class="font-bold">{agency.stations[from].stop_name}</span>
|
||||
{#each linesForStation(from) as line}
|
||||
{#each agency.stations[from].routes as line}
|
||||
<MbtaLine name={line} compact />
|
||||
{/each}
|
||||
to
|
||||
@@ -84,7 +84,7 @@
|
||||
<br>
|
||||
<span class="inline-flex items-center justify-center gap-2">
|
||||
<span class="font-bold">{agency.stations[to].stop_name}</span>
|
||||
{#each linesForStation(to) as line}
|
||||
{#each agency.stations[to].routes as line}
|
||||
<MbtaLine name={line} compact />
|
||||
{/each}
|
||||
</span>
|
||||
|
||||
@@ -53,7 +53,7 @@
|
||||
<div class="mb-5 max-w-96">
|
||||
<h1 class="font-black text-5xl">How It Works</h1>
|
||||
|
||||
<p class="mb-2">We'll show you a pair of T stations in Boston: an origin and a destination.</p>
|
||||
<p class="mb-2">You'll see a pair of T stations in Boston: an origin and a destination.</p>
|
||||
|
||||
<img src={screenshot.src} class="w-full border rounded-lg mb-2" />
|
||||
|
||||
|
||||
Reference in New Issue
Block a user