fix: merge stops only considering current lines
All checks were successful
deploy to cloudflare pages / deploy (push) Successful in 32s

This commit is contained in:
uku 2024-10-26 12:51:40 +02:00
parent cf937afb01
commit 1dc77d6d2a
Signed by: uku
SSH key fingerprint: SHA256:4P0aN6M8ajKukNi6aPOaX0LacanGYtlfjmN+m/sHY/o
2 changed files with 41 additions and 20 deletions

View file

@ -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<GeoJSON.Feature[]> {
async function getStops(fetch: FetchType): Promise<GeoJSON.Feature[]> {
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<string>,
): Promise<GeoJSON.Feature[]> {
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<GeoJSON.Feature[]> {
if (feature.properties) feature.properties.desserte = desserte;
return feature;
});
}
},
);
return lazyStops;
return mergedStops;
}

View file

@ -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<string>(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: {} };