From c3dec902640ba3bbc8c436d9f58a8593c31ea975 Mon Sep 17 00:00:00 2001 From: uku Date: Fri, 25 Oct 2024 15:00:50 +0200 Subject: [PATCH] feat: add difficulty selector --- src/lib/index.ts | 23 +++++- src/routes/+page.svelte | 132 +++++--------------------------- src/routes/+page.ts | 11 --- src/routes/api/check/+server.ts | 8 +- src/routes/api/data/+server.ts | 22 +++--- src/routes/game/+page.svelte | 102 ++++++++++++++++++++++++ src/routes/game/+page.ts | 16 ++++ 7 files changed, 175 insertions(+), 139 deletions(-) delete mode 100644 src/routes/+page.ts create mode 100644 src/routes/game/+page.svelte create mode 100644 src/routes/game/+page.ts diff --git a/src/lib/index.ts b/src/lib/index.ts index e0768de..529302e 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,13 +1,16 @@ +export interface GameOptions { + mode: "easy" | "hard" | "extreme demon ultra miguel"; +} + export interface GameData { - center: [number, number]; lines: [GeoJSON.Feature, string][]; stops: GeoJSON.Feature[]; stopName: string; - stopId: string; + gameId: string; } export interface CheckData { - stopId: string; + gameId: string; latlng: [number, number]; } @@ -27,6 +30,20 @@ const stopsUrl = let lazyLines: GeoJSON.FeatureCollection | null = null; let lazyStops: GeoJSON.Feature[] | null = null; +const games: Record = {}; + +export function createGame(stop: GeoJSON.Feature): string { + const uuid = crypto.randomUUID(); + games[uuid] = stop; + return uuid; +} + +export function stopGame(uuid: string): GeoJSON.Feature | null { + const stop = games[uuid]; + delete games[uuid]; + return stop; +} + export async function getLines(fetch: FetchType): Promise { return lazyLines ?? (lazyLines = await fetch(linesUrl).then((r) => r.json())); } diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index cd13152..d4afdf3 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,127 +1,37 @@
-

{data.gameData.stopName}

+

TCL-Guessr

- +
+ - - -
- - -
- -
+ +
diff --git a/src/routes/+page.ts b/src/routes/+page.ts deleted file mode 100644 index e6fc043..0000000 --- a/src/routes/+page.ts +++ /dev/null @@ -1,11 +0,0 @@ -import type { PageLoad } from "./$types"; -import type { GameData } from "$lib"; - -export const load: PageLoad = async ({ fetch }) => { - const res = await fetch("/api/data"); - const gameData: GameData = await res.json(); - - return { gameData }; -}; - -export const ssr = false; diff --git a/src/routes/api/check/+server.ts b/src/routes/api/check/+server.ts index 27b4483..137e6f1 100644 --- a/src/routes/api/check/+server.ts +++ b/src/routes/api/check/+server.ts @@ -1,11 +1,9 @@ -import { getStops, type CheckData, type CheckResponse } from "$lib"; +import { stopGame, type CheckData, type CheckResponse } from "$lib"; import type { RequestHandler } from "./$types"; -export const POST: RequestHandler = async ({ fetch, request }) => { - const stops = await getStops(fetch); - +export const POST: RequestHandler = async ({ request }) => { const data: CheckData = await request.json(); - const stop = stops.find((f) => f.id === data.stopId); + const stop = stopGame(data.gameId); if (stop) { // GeoJSON data is LonLat, not LatLon diff --git a/src/routes/api/data/+server.ts b/src/routes/api/data/+server.ts index 0b5070e..d3788e2 100644 --- a/src/routes/api/data/+server.ts +++ b/src/routes/api/data/+server.ts @@ -1,13 +1,17 @@ import type { RequestHandler } from "./$types"; -import { getLines, getStops, type GameData } from "$lib"; +import { createGame, getLines, getStops, type GameData, type GameOptions } from "$lib"; +import { error } from "@sveltejs/kit"; -export const GET: RequestHandler = async ({ fetch }) => { +export const GET: RequestHandler = async ({ fetch, url }) => { 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 mode = url.searchParams.get("mode"); + if (mode !== "easy" && mode !== "hard" && mode !== "extreme demon ultra miguel") { + return error(400, "gamemode is invalid"); + } + + const options: GameOptions = { mode }; const lineColors: [GeoJSON.Feature, string][] = lines.features.map((f) => { const components = f.properties!.couleur!.split(" "); @@ -28,13 +32,13 @@ export const GET: RequestHandler = async ({ fetch }) => { }); const randomStop = crossingStops[Math.floor(Math.random() * crossingStops.length)]; + const gameId = createGame(randomStop); const data: GameData = { - center: [centerLat, centerLon], - lines: lineColors, - stops: crossingStops, + lines: options.mode === "easy" || options.mode === "hard" ? lineColors : [], + stops: options.mode === "easy" ? crossingStops : [], stopName: randomStop.properties!.nom, - stopId: randomStop.id!.toString(), + gameId, }; return Response.json(data); diff --git a/src/routes/game/+page.svelte b/src/routes/game/+page.svelte new file mode 100644 index 0000000..7cc3a8d --- /dev/null +++ b/src/routes/game/+page.svelte @@ -0,0 +1,102 @@ + + +
+

{data.gameData.stopName}

+ + + + + +
+
+ + diff --git a/src/routes/game/+page.ts b/src/routes/game/+page.ts new file mode 100644 index 0000000..66c3b63 --- /dev/null +++ b/src/routes/game/+page.ts @@ -0,0 +1,16 @@ +import type { PageLoad } from "./$types"; +import type { GameData } from "$lib"; +import { error } from "@sveltejs/kit"; + +export const load: PageLoad = async ({ fetch, url }) => { + const res = await fetch("/api/data?" + url.searchParams); + + if (!res.ok) { + return error(400, await res.text()); + } else { + const gameData: GameData = await res.json(); + return { gameData }; + } +}; + +export const ssr = false;