From 1dc77d6d2a86662165db9cd6fe21a5a26a2b4cd0 Mon Sep 17 00:00:00 2001 From: uku Date: Sat, 26 Oct 2024 12:51:40 +0200 Subject: [PATCH] fix: merge stops only considering current lines --- src/lib/index.ts | 38 +++++++++++++++++++++++++++------ src/routes/game/+page.server.ts | 23 ++++++++------------ 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/lib/index.ts b/src/lib/index.ts index 192853b..5c5a893 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,3 +1,5 @@ +import type { GeoJSON } from "leaflet"; + export interface GameOptions { mode: "easy" | "hard" | "extreme demon ultra miguel"; metro: boolean; @@ -80,12 +82,36 @@ export async function getTram(fetch: FetchType): Promise<[GeoJSON.Feature, strin return lazyTram; } -export async function getStops(fetch: FetchType): Promise { +async function getStops(fetch: FetchType): Promise { if (!lazyStops) { const stops: GeoJSON.FeatureCollection = await fetch(stopsUrl).then((r) => r.json()); + lazyStops = stops.features; + } - lazyStops = Array.from(new Set(stops.features.map((f) => f.properties?.nom))).map((nom) => { - const matching = stops.features.filter((f) => f.properties?.nom === nom); + return lazyStops; +} + +export async function getMergedStops( + fetch: FetchType, + lineCodes: Set, +): Promise { + const stops = await getStops(fetch); + + const crossingStops = stops.filter((f) => { + if (f.properties?.desserte) { + return f.properties.desserte + .split(",") + .map((d: string) => d.split(":")[0]) + .some((d: string) => lineCodes.has(d)); + } else { + return false; + } + }); + + // merge coordinates of the stops + const mergedStops = Array.from(new Set(crossingStops.map((f) => f.properties?.nom))).map( + (nom) => { + const matching = crossingStops.filter((f) => f.properties?.nom === nom); const longs = matching.map((f) => (f.geometry as GeoJSON.Point).coordinates[0]); const lonAvg = longs.reduce((a, b) => a + b) / longs.length; @@ -100,8 +126,8 @@ export async function getStops(fetch: FetchType): Promise { if (feature.properties) feature.properties.desserte = desserte; return feature; - }); - } + }, + ); - return lazyStops; + return mergedStops; } diff --git a/src/routes/game/+page.server.ts b/src/routes/game/+page.server.ts index 7ba54da..ba89062 100644 --- a/src/routes/game/+page.server.ts +++ b/src/routes/game/+page.server.ts @@ -1,13 +1,18 @@ import type { PageServerLoad } from "./$types"; -import { createGame, getMetro, getStops, getTram, type GameData, type GameOptions } from "$lib"; import { error } from "@sveltejs/kit"; +import { + createGame, + getMergedStops, + getMetro, + getTram, + type GameData, + type GameOptions, +} from "$lib"; export const load: PageServerLoad = async ({ fetch, url, platform }) => { const kv = platform?.env?.TCL_GUESSR_KV; if (!kv) return error(500, "could not connect to kv"); - const stops = await getStops(fetch); - const mode = url.searchParams.get("mode"); if (mode !== "easy" && mode !== "hard" && mode !== "extreme demon ultra miguel") { return error(400, "gamemode is invalid"); @@ -25,17 +30,7 @@ export const load: PageServerLoad = async ({ fetch, url, platform }) => { if (options.tram) lineColors.push(...(await getTram(fetch))); const lineCodes = new Set(lineColors.map(([f, _]) => f.properties!.code_ligne)); - - const crossingStops = stops.filter((f) => { - if (f.properties?.desserte) { - return f.properties.desserte - .split(",") - .map((d: string) => d.split(":")[0]) - .some((d: string) => lineCodes.has(d)); - } else { - return false; - } - }); + const crossingStops = await getMergedStops(fetch, lineCodes); const strippedStops = crossingStops.map((f) => { return { ...f, properties: {} };