tcl-guessr/src/routes/api/data/+server.ts

41 lines
1.2 KiB
TypeScript

import type { RequestHandler } from "./$types";
import { getLines, getStops, type GameData } from "$lib";
export const GET: RequestHandler = async ({ fetch }) => {
const lines = await getLines(fetch);
const stops = await getStops(fetch);
const bbox = lines.bbox ?? [0, 0, 0, 0];
const centerLat = (bbox[1] + bbox[3]) / 2;
const centerLon = (bbox[0] + bbox[2]) / 2;
const lineColors: [GeoJSON.Feature, string][] = lines.features.map((f) => {
const components = f.properties!.couleur!.split(" ");
return [f, `rgb(${components.join(",")})`];
});
const lineCodes = new Set<string>(lines.features.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 randomStop = crossingStops[Math.floor(Math.random() * crossingStops.length)];
const data: GameData = {
center: [centerLat, centerLon],
lines: lineColors,
stops: crossingStops,
stopName: randomStop.properties!.nom,
stopId: randomStop.id!.toString(),
};
return Response.json(data);
};