Kinda working lobbies WIP

This commit is contained in:
2025-09-03 21:07:13 +02:00
parent d32eee314f
commit 81ad47960a
7 changed files with 110 additions and 16 deletions

3
src/params/number.ts Normal file
View File

@@ -0,0 +1,3 @@
export function match(value) {
return /^\d*$/.test(value);
}

View File

@@ -0,0 +1,41 @@
import { db } from '$lib/server/db';
import { lobbysRelations, lobbysTable, usersInLobby, usersTable } from '$lib/server/db/schema';
import { json } from '@sveltejs/kit';
import { eq } from 'drizzle-orm';
export async function POST({ request }) {
const userReq = await request.json();
const userInLobby = (await db.$count(usersInLobby, eq(usersInLobby.userEmail, userReq.email))) > 0
if (userInLobby) {
const lobbys = await db.query.lobbysTable.findMany({
with: {
usersInLobby: true
},
});
// This should be done with database queries
const lobby = lobbys.find((l) => l.usersInLobby.find((u) => u.userEmail == userReq.email))
return json(lobby, { status: 200 })
}
// const lobby = await db.transaction(async (tx) => {
const l: typeof lobbysTable.$inferInsert = {
hostEmail: userReq.email
};
const [lobby] = await db.insert(lobbysTable).values(l).onConflictDoNothing().returning();
const uLobby: typeof usersInLobby.$inferInsert = {
userEmail: userReq.email,
lobbyId: lobby.id
};
await db.insert(usersInLobby).values(uLobby);
// })
if (!lobby) {
return new Response(null, { status: 500 })
}
return json(lobby, { status: 201 })
}

View File

@@ -1,7 +1,6 @@
import { db } from "$lib/server/db";
import { usersTable } from "$lib/server/db/schema";
import { json } from "@sveltejs/kit";
import type { Actions } from "../../$types";
export async function POST({ request }) {
const user = await request.json();

View 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
}
};

View 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>

View 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}`);
};