import { error } from "@sveltejs/kit"; import type { RequestHandler } from "./$types"; import { createGame, getMergedStops, parseOptions, sample } from "$lib"; export const GET: RequestHandler = async ({ url, platform, locals }) => { const db = platform?.env?.TCL_GUESSR_D1 ?? null; if (db === null) error(500, "could not connect to d1"); const userId = locals.user?.id ?? null; const options = parseOptions(url); const crossingStops = await getMergedStops(options.stopsType); const randomStops = sample(crossingStops, 5); if (!randomStops) { error(400, "could not select random stop"); } const gameData = await createGame(userId, options.mode, options.stopsType, randomStops, db); return Response.json(gameData); };