fix: merge stops only considering current lines
All checks were successful
deploy to cloudflare pages / deploy (push) Successful in 32s
All checks were successful
deploy to cloudflare pages / deploy (push) Successful in 32s
This commit is contained in:
parent
cf937afb01
commit
1dc77d6d2a
2 changed files with 41 additions and 20 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
import type { GeoJSON } from "leaflet";
|
||||||
|
|
||||||
export interface GameOptions {
|
export interface GameOptions {
|
||||||
mode: "easy" | "hard" | "extreme demon ultra miguel";
|
mode: "easy" | "hard" | "extreme demon ultra miguel";
|
||||||
metro: boolean;
|
metro: boolean;
|
||||||
|
@ -80,12 +82,36 @@ export async function getTram(fetch: FetchType): Promise<[GeoJSON.Feature, strin
|
||||||
return lazyTram;
|
return lazyTram;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getStops(fetch: FetchType): Promise<GeoJSON.Feature[]> {
|
async function getStops(fetch: FetchType): Promise<GeoJSON.Feature[]> {
|
||||||
if (!lazyStops) {
|
if (!lazyStops) {
|
||||||
const stops: GeoJSON.FeatureCollection = await fetch(stopsUrl).then((r) => r.json());
|
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) => {
|
return lazyStops;
|
||||||
const matching = stops.features.filter((f) => f.properties?.nom === nom);
|
}
|
||||||
|
|
||||||
|
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 longs = matching.map((f) => (f.geometry as GeoJSON.Point).coordinates[0]);
|
||||||
const lonAvg = longs.reduce((a, b) => a + b) / longs.length;
|
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;
|
if (feature.properties) feature.properties.desserte = desserte;
|
||||||
|
|
||||||
return feature;
|
return feature;
|
||||||
});
|
},
|
||||||
}
|
);
|
||||||
|
|
||||||
return lazyStops;
|
return mergedStops;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,18 @@
|
||||||
import type { PageServerLoad } from "./$types";
|
import type { PageServerLoad } from "./$types";
|
||||||
import { createGame, getMetro, getStops, getTram, type GameData, type GameOptions } from "$lib";
|
|
||||||
import { error } from "@sveltejs/kit";
|
import { error } from "@sveltejs/kit";
|
||||||
|
import {
|
||||||
|
createGame,
|
||||||
|
getMergedStops,
|
||||||
|
getMetro,
|
||||||
|
getTram,
|
||||||
|
type GameData,
|
||||||
|
type GameOptions,
|
||||||
|
} from "$lib";
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ fetch, url, platform }) => {
|
export const load: PageServerLoad = async ({ fetch, url, platform }) => {
|
||||||
const kv = platform?.env?.TCL_GUESSR_KV;
|
const kv = platform?.env?.TCL_GUESSR_KV;
|
||||||
if (!kv) return error(500, "could not connect to kv");
|
if (!kv) return error(500, "could not connect to kv");
|
||||||
|
|
||||||
const stops = await getStops(fetch);
|
|
||||||
|
|
||||||
const mode = url.searchParams.get("mode");
|
const mode = url.searchParams.get("mode");
|
||||||
if (mode !== "easy" && mode !== "hard" && mode !== "extreme demon ultra miguel") {
|
if (mode !== "easy" && mode !== "hard" && mode !== "extreme demon ultra miguel") {
|
||||||
return error(400, "gamemode is invalid");
|
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)));
|
if (options.tram) lineColors.push(...(await getTram(fetch)));
|
||||||
|
|
||||||
const lineCodes = new Set<string>(lineColors.map(([f, _]) => f.properties!.code_ligne));
|
const lineCodes = new Set<string>(lineColors.map(([f, _]) => f.properties!.code_ligne));
|
||||||
|
const crossingStops = await getMergedStops(fetch, lineCodes);
|
||||||
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 strippedStops = crossingStops.map((f) => {
|
const strippedStops = crossingStops.map((f) => {
|
||||||
return { ...f, properties: {} };
|
return { ...f, properties: {} };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue