Kinda working lobbies WIP
This commit is contained in:
15
package-lock.json
generated
15
package-lock.json
generated
@@ -5220,21 +5220,6 @@
|
|||||||
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
|
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
|
||||||
"license": "ISC"
|
"license": "ISC"
|
||||||
},
|
},
|
||||||
"node_modules/yaml": {
|
|
||||||
"version": "2.8.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.8.1.tgz",
|
|
||||||
"integrity": "sha512-lcYcMxX2PO9XMGvAJkJ3OsNMw+/7FKes7/hgerGUYWIoWu5j/+YQqcZr5JnPZWzOsEBgMbSbiSTn/dv/69Mkpw==",
|
|
||||||
"dev": true,
|
|
||||||
"license": "ISC",
|
|
||||||
"optional": true,
|
|
||||||
"peer": true,
|
|
||||||
"bin": {
|
|
||||||
"yaml": "bin.mjs"
|
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">= 14.6"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/yocto-queue": {
|
"node_modules/yocto-queue": {
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",
|
||||||
|
|||||||
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 { db } from "$lib/server/db";
|
||||||
import { usersTable } from "$lib/server/db/schema";
|
import { usersTable } from "$lib/server/db/schema";
|
||||||
import { json } from "@sveltejs/kit";
|
import { json } from "@sveltejs/kit";
|
||||||
import type { Actions } from "../../$types";
|
|
||||||
|
|
||||||
export async function POST({ request }) {
|
export async function POST({ request }) {
|
||||||
const user = await request.json();
|
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