Kinda working lobbies WIP
This commit is contained in:
29
src/routes/lobby/[id]/+page.server.ts
Normal file
29
src/routes/lobby/[id]/+page.server.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { db } from "$lib/server/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import type { PageServerLoad } from "./$types";
|
||||
import { lobbysTable } from "$lib/server/db/schema";
|
||||
import { redirect } from "@sveltejs/kit";
|
||||
|
||||
export const load: PageServerLoad = async ({ params, locals }) => {
|
||||
const id = Number(params.id);
|
||||
|
||||
const lobby = await db.query.lobbysTable.findFirst({
|
||||
with: {
|
||||
usersInLobby: {
|
||||
with: {
|
||||
user: true
|
||||
}
|
||||
}
|
||||
},
|
||||
where: eq(lobbysTable.id, id)
|
||||
})
|
||||
|
||||
if (!lobby) {
|
||||
redirect(307, "/error");
|
||||
}
|
||||
|
||||
return {
|
||||
lobby: lobby,
|
||||
username: locals.user.username
|
||||
}
|
||||
};
|
||||
15
src/routes/lobby/[id]/+page.svelte
Normal file
15
src/routes/lobby/[id]/+page.svelte
Normal file
@@ -0,0 +1,15 @@
|
||||
<script lang="ts">
|
||||
import type { PageProps } from './$types';
|
||||
|
||||
let { data }: PageProps = $props();
|
||||
</script>
|
||||
|
||||
<h1>You are in lobby with ID: {data.lobby?.id}</h1>
|
||||
<h2>Youre username is: {data.username}</h2>
|
||||
|
||||
<h2>Players in Lobby: </h2>
|
||||
<ul>
|
||||
{#each data.lobby.usersInLobby as player (player.userEmail)}
|
||||
<li>{player.user.username}</li>
|
||||
{/each}
|
||||
</ul>
|
||||
22
src/routes/lobby/create/+page.server.ts
Normal file
22
src/routes/lobby/create/+page.server.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { generateRandomString } from "$lib/server/auth/spotify";
|
||||
import type { states } from "$lib/server/db/schema";
|
||||
import { redirect } from "@sveltejs/kit";
|
||||
import type { PageServerLoad } from "./$types";
|
||||
import { setContext } from "svelte";
|
||||
|
||||
|
||||
export const load: PageServerLoad = async ({locals, fetch}) => {
|
||||
const response = await fetch("/api/createLobby", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(locals.user)
|
||||
});
|
||||
|
||||
if (!response.ok) redirect(307, "/error");
|
||||
|
||||
const lobby = await response.json();
|
||||
|
||||
redirect(307, `/lobby/${lobby.id}`);
|
||||
};
|
||||
Reference in New Issue
Block a user