diff --git a/bun.lockb b/bun.lockb index 81c73d5..6d350b7 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 30b97b6..906ddcb 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "leaflet-defaulticon-compatibility": "^0.1.2" }, "devDependencies": { + "@cloudflare/workers-types": "^4.20241022.0", "@sveltejs/adapter-cloudflare": "^4.7.4", "@sveltejs/kit": "^2.7.3", "@sveltejs/vite-plugin-svelte": "^4.0.0", diff --git a/src/app.d.ts b/src/app.d.ts index 743f07b..fb61326 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -6,7 +6,11 @@ declare global { // interface Locals {} // interface PageData {} // interface PageState {} - // interface Platform {} + interface Platform { + env?: { + TCL_GUESSR_KV: KVNamespace; + }; + } } } diff --git a/src/lib/index.ts b/src/lib/index.ts index 9d84954..192853b 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -35,17 +35,22 @@ let lazyMetro: [GeoJSON.Feature, string][] | null = null; let lazyTram: [GeoJSON.Feature, string][] | null = null; let lazyStops: GeoJSON.Feature[] | null = null; -const games: Record = {}; - -export function createGame(stop: GeoJSON.Feature): string { +export async function createGame(stop: GeoJSON.Feature, kv: KVNamespace): Promise { const uuid = crypto.randomUUID(); - games[uuid] = stop; + + await kv.put(`game:${uuid}`, JSON.stringify(stop), { expirationTtl: 600 }); + return uuid; } -export function stopGame(uuid: string): GeoJSON.Feature | null { - const stop = games[uuid]; - delete games[uuid]; +export async function stopGame( + uuid: string, + kv: KVNamespace, +): Promise { + const stop: GeoJSON.Feature | null = await kv.get(`game:${uuid}`, "json"); + + if (stop) await kv.delete(`game:${uuid}`); + return stop; } diff --git a/src/routes/api/check/+server.ts b/src/routes/api/check/+server.ts index 137e6f1..6d5635b 100644 --- a/src/routes/api/check/+server.ts +++ b/src/routes/api/check/+server.ts @@ -1,9 +1,13 @@ import { stopGame, type CheckData, type CheckResponse } from "$lib"; +import { error } from "@sveltejs/kit"; import type { RequestHandler } from "./$types"; -export const POST: RequestHandler = async ({ request }) => { +export const POST: RequestHandler = async ({ request, platform }) => { + const kv = platform?.env?.TCL_GUESSR_KV; + if (!kv) return error(500, "could not connect to kv"); + const data: CheckData = await request.json(); - const stop = stopGame(data.gameId); + const stop = await stopGame(data.gameId, kv); if (stop) { // GeoJSON data is LonLat, not LatLon diff --git a/src/routes/game/+page.server.ts b/src/routes/game/+page.server.ts index feb5753..ecd7593 100644 --- a/src/routes/game/+page.server.ts +++ b/src/routes/game/+page.server.ts @@ -2,7 +2,10 @@ import type { PageServerLoad } from "./$types"; import { createGame, getMetro, getStops, getTram, type GameData, type GameOptions } from "$lib"; import { error } from "@sveltejs/kit"; -export const load: PageServerLoad = async ({ fetch, url }) => { +export const load: PageServerLoad = async ({ fetch, url, platform }) => { + const kv = platform?.env?.TCL_GUESSR_KV; + if (!kv) return error(500, "could not connect to kv"); + const stops = await getStops(fetch); const mode = url.searchParams.get("mode"); @@ -40,7 +43,7 @@ export const load: PageServerLoad = async ({ fetch, url }) => { return error(400, "could not select random stop"); } - const gameId = createGame(randomStop); + const gameId = await createGame(randomStop, kv); const gameData: GameData = { lines: options.mode === "easy" || options.mode === "hard" ? lineColors : [], stops: options.mode === "easy" ? crossingStops : [], diff --git a/tsconfig.json b/tsconfig.json index fc93cbd..cccb10c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,8 @@ "skipLibCheck": true, "sourceMap": true, "strict": true, - "moduleResolution": "bundler" + "moduleResolution": "bundler", + "types": ["@cloudflare/workers-types"] } // Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias // except $lib which is handled by https://kit.svelte.dev/docs/configuration#files diff --git a/wrangler.toml b/wrangler.toml index c6dd13b..7040cf6 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -1,3 +1,7 @@ name = "tcl-guessr" compatibility_date = "2024-09-25" pages_build_output_dir = ".svelte-kit/cloudflare" + +[[kv_namespaces]] +binding = "TCL_GUESSR_KV" +id = "b2d8980ac3a74a80854c35bf9569dbf8"