Skip to content

Commit

Permalink
leaderboard: done
Browse files Browse the repository at this point in the history
  • Loading branch information
deltea committed Aug 24, 2023
1 parent 6b57f73 commit 7806253
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
41 changes: 39 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
<script lang="ts">
import { onMount } from "svelte";
import type { PageData } from "./$types";
import IconTrophy from "~icons/gg/trophy";
export let data: PageData;
let username: string | null;
let usernameInput: string;
async function addUser() {
if (!usernameInput) return;
localStorage.setItem("username", usernameInput);
username = usernameInput;
await fetch("/api/new-user", {
method: "POST",
body: JSON.stringify({ username }),
headers: {
"Content-Type": "application/json",
},
});
}
onMount(() => {
username = localStorage.getItem("username");
});
</script>

<main class="bg-white h-full text-neutral">
Expand All @@ -19,7 +41,22 @@
<h2 class="text-2xl">Which YouTube video has more views?</h2>
</div>

<a href="/play" class="bg-youtube text-white rounded-full px-16 p-2 uppercase text-xl">Let's Go!</a>
{#if username}
<h1>Hello, {username}!</h1>
{:else}
<form class="flex flex-col items-stretch" on:submit|preventDefault={addUser}>
<label for="username">Enter a username to join the leaderboard!</label>
<input
type="text"
id="username"
placeholder="Username..."
bind:value={usernameInput}>
</form>
{/if}

<a href="/play" class="bg-youtube text-white rounded-full px-16 p-2 uppercase text-xl">
Let's Go!
</a>
</section>

<section class="inline-flex flex-col justify-center h-full w-3/6 float-right p-8 gap-4">
Expand Down
18 changes: 18 additions & 0 deletions src/routes/api/new-user/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { supabase, type Leader } from "$lib/supabase";
import { json, type RequestHandler } from "@sveltejs/kit";

export const POST: RequestHandler = async ({ request }) => {
const { username } = await request.json();
console.log(username);

try {
await supabase.from("leaderboard").insert({
name: username,
score: 0,
} satisfies Leader);

return json({ status: 200 });
} catch (error) {
return json({ status: 200, error });
}
}
15 changes: 15 additions & 0 deletions src/routes/api/update-score/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { supabase } from "$lib/supabase";
import { json, type RequestHandler } from "@sveltejs/kit";

export const POST: RequestHandler = async ({ request }) => {
const { username, score } = await request.json();
console.log(username, score);

try {
await supabase.from("leaderboard").update({ score }).eq("name", username);

return json({ status: 200 });
} catch (error) {
return json({ status: 200, error });
}
}
20 changes: 18 additions & 2 deletions src/routes/play/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,30 @@
return result;
}
async function setHighScore() {
localStorage.setItem("highScore", score.toString());
const username = localStorage.getItem("username");
if (!username) return;
await fetch("/api/update-score", {
method: "POST",
body: JSON.stringify({ username, score }),
headers: {
"Content-Type": "application/json",
},
});
}
function wrong() {
state = "incorrect";
setTimeout(() => {
setTimeout(async () => {
game = "over";
if (score > (highScore ?? 0)) {
localStorage.setItem("highScore", score.toString());
await setHighScore();
}
}, delay);
}
Expand Down

0 comments on commit 7806253

Please sign in to comment.