Skip to content

Commit

Permalink
server submit
Browse files Browse the repository at this point in the history
  • Loading branch information
russellsamora committed Jan 15, 2025
1 parent c72ef32 commit be4cde4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/components/Game.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
import Grid from "$components/Grid.svelte";
import Keypad from "$components/Keypad.svelte";
import { game } from "$runes/misc.svelte.js";
import localStore from "$runes/localStore.svelte.js";
import server from "$utils/server.js";
const MAX_LENGTH = 1000;
const size = 10;
const targetCount = 100;
let storage = localStore("pudding_mowing", {});
let position = $state([0, 0]);
let path = $state([[0, 0]]);
let visited = $state({});
Expand All @@ -25,9 +29,13 @@
document.getElementById("results").classList.add("visible");
}
function submit() {
async function submit() {
const str = path.map((p) => p.join(",")).join("|");
console.log(str);
if (str.length < MAX_LENGTH) {
storage.value = { path };
// const response = await server("submit", str);
// console.log(response);
}
}
function onmove(key) {
Expand Down Expand Up @@ -60,6 +68,7 @@
<p class="skip">
<small>
<a href="#results" onclick={reveal}>just skip to results please</a>
<button onclick={submit}>test</button>
</small>
</p>

Expand Down
File renamed without changes.
37 changes: 37 additions & 0 deletions src/utils/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { dev } from "$app/environment";
import urlParams from "$utils/urlParams.js";

export default async function server(endpoint, data) {
const base = "http://localhost:3000";
// const base = "https://pudding-mowing-server-335031567fcb.herokuapp.com";
const url = `${base}/${endpoint}`;

const token = dev ? urlParams.get("token") : "";

try {
const start = Date.now();
const body = JSON.stringify({ data });
const options = {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"X-Pudding-Token": token
},
credentials: "include",
body
};

const response = await fetch(url, options);
if (response.status === 404) throw Error(response.statusText);
else {
const result = await response.json();
const end = Date.now();
const duration = `${end - start}ms`;
return { ...result, duration };
}
} catch (err) {
console.log(err?.message);
throw err;
}
}

0 comments on commit be4cde4

Please sign in to comment.