tcl-guessr/src/routes/api/game/+server.ts
uku 67d2358a37
All checks were successful
deploy to cloudflare pages / deploy (push) Successful in 32s
fix: make logging in not mandatory
2024-11-25 12:17:27 +01:00

22 lines
735 B
TypeScript

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);
};