From 058e6675a3d088ef2e5341dfd7dd2f698b0a06cb Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Tue, 17 Dec 2024 00:26:41 -0500 Subject: [PATCH] [+] Backend sdk --- server/host.py | 2 +- src/App.svelte | 7 +++---- src/utils.ts | 11 +++++++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/server/host.py b/server/host.py index 97d1251..2da1ba8 100644 --- a/server/host.py +++ b/server/host.py @@ -62,7 +62,7 @@ async def get_puzzle(id: str): tf = data / f"{id}.json" if not tf.exists(): return {"error": "Puzzle not found"} - return tf.read_text() + return json.loads(tf.read_text()) # Getting / redirects to main page diff --git a/src/App.svelte b/src/App.svelte index 1572051..0989dc0 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -9,10 +9,9 @@ const params = new URLSearchParams(location.search) // Can pass in puzzle data from props - interface Props { puzzleData?: { id: string, rows: number, cols: number, - numbers: i8s, nMask: i8s, hColors: i8s, vColors: i8s, colors: string[] } } - export let { puzzleData }: Props = {} - const pid = puzzleData?.id ?? 'slitherlink' + interface Props { puzzleId?: string, puzzleData?: Checkpoint } + export let { puzzleId, puzzleData }: Props = {} + const pid = puzzleId ?? 'slitherlink' // Main variables const [rows, cols] = [+(puzzleData?.rows ?? params.get('size') ?? 40), +(puzzleData?.cols ?? params.get('size') ?? 40)] diff --git a/src/utils.ts b/src/utils.ts index 2420fe4..86043fb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,11 +1,13 @@ export type i8s = Int8Array -export interface Checkpoint { hStates: i8s, vStates: i8s, hColors: i8s, vColors: i8s, numbers: i8s, nMask: i8s, colors: string[] } +export interface Checkpoint { rows: number, cols: number, + hStates: i8s, vStates: i8s, hColors: i8s, vColors: i8s, numbers: i8s, nMask: i8s, colors: string[] } export const cfg = { cellW: 20, lineW: 4, - totalW: 24 + totalW: 24, + backend: "https://slither0.hydev.org", } export const eStates = { @@ -67,3 +69,8 @@ export const Fmt = { return `${d ? `${d}d ` : ''}${h ? `${h}:` : ''}${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}` } } + +export const Backend = { + get: (id: string) => fetch(`${cfg.backend}/${id}`).then(r => r.text()).then(JsonTy.parse), + post: (data: Checkpoint) => fetch(`${cfg.backend}/`, { method: "post", body: JsonTy.stringify(data) }), +}