Kinda working lobbies WIP
This commit is contained in:
3
src/params/number.ts
Normal file
3
src/params/number.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export function match(value) {
|
||||
return /^\d*$/.test(value);
|
||||
}
|
||||
41
src/routes/api/createLobby/+server.ts
Normal file
41
src/routes/api/createLobby/+server.ts
Normal 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 })
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
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