From 2b9b5c3b39e028aaad3e2ae08428d32d2d05b12d Mon Sep 17 00:00:00 2001 From: RezHackXYZ Date: Thu, 15 May 2025 19:42:58 +0530 Subject: [PATCH] trying to get the host game lobby work with the newer rDB --- .gitignore | 2 + .prettierrc | 4 +- .vscode/settings.json | 3 + eslint.config.js | 2 +- jsconfig.json | 5 +- package.json | 1 - src/routes/+page.svelte | 2 +- src/routes/create/logic/InsertGameInDB.js | 22 +-- src/routes/create/logic/StartGame.js | 5 +- src/routes/host/[gamePin]/+page.js | 5 + src/routes/host/[gamePin]/+page.svelte | 82 +++++++++++ src/routes/hostgame/[gamePin]/+page.svelte | 109 -------------- src/routes/join/+page.svelte | 45 ++++++ src/routes/join/logic/InsertPlayerInDB.js | 14 ++ src/routes/join/logic/JoinGameData.svelte.js | 3 + src/routes/join/logic/joinGame.js | 20 +++ src/routes/join/logic/validateGamePin.js | 11 ++ src/routes/play/[gamePin]/+page.svelte | 142 ------------------- src/routes/results/[gamePin]/+page.svelte | 71 ---------- svelte.config.js | 20 +-- 20 files changed, 213 insertions(+), 355 deletions(-) create mode 100644 src/routes/host/[gamePin]/+page.js create mode 100644 src/routes/host/[gamePin]/+page.svelte delete mode 100644 src/routes/hostgame/[gamePin]/+page.svelte create mode 100644 src/routes/join/+page.svelte create mode 100644 src/routes/join/logic/InsertPlayerInDB.js create mode 100644 src/routes/join/logic/JoinGameData.svelte.js create mode 100644 src/routes/join/logic/joinGame.js create mode 100644 src/routes/join/logic/validateGamePin.js delete mode 100644 src/routes/play/[gamePin]/+page.svelte delete mode 100644 src/routes/results/[gamePin]/+page.svelte diff --git a/.gitignore b/.gitignore index 3b462cb..70f61e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules +package-lock.json # Output .output @@ -21,3 +22,4 @@ Thumbs.db # Vite vite.config.js.timestamp-* vite.config.ts.timestamp-* + diff --git a/.prettierrc b/.prettierrc index 7ebb855..7e71c41 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,7 +1,7 @@ { "useTabs": true, - "singleQuote": true, - "trailingComma": "none", + "singleQuote": false, + "trailingComma": "all", "printWidth": 100, "plugins": ["prettier-plugin-svelte", "prettier-plugin-tailwindcss"], "overrides": [ diff --git a/.vscode/settings.json b/.vscode/settings.json index 8a9beca..6e40d33 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,7 +8,10 @@ "correctanswer", "creationdate", "gameid", + "gamepin", + "hostgame", "kahoot", + "playername", "questionid", "questionstext", "SUPABASE" diff --git a/eslint.config.js b/eslint.config.js index 489022d..829a6ce 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,3 +1,3 @@ import prettier from 'eslint-config-prettier'; import svelte from 'eslint-plugin-svelte'; -export default [prettier, ...svelte.configs.prettier]; +export default [prettier, ...svelte.configs.prettier]; \ No newline at end of file diff --git a/jsconfig.json b/jsconfig.json index 428e42f..851471e 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -4,5 +4,8 @@ "allowJs": true, "checkJs": true, "moduleResolution": "bundler" - } + }, + "exclude": [ + "node_modules" + ] } diff --git a/package.json b/package.json index d04f917..7b25e2b 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ }, "devDependencies": { "@sveltejs/adapter-auto": "^4.0.0", - "@sveltejs/adapter-static": "^3.0.8", "@sveltejs/kit": "^2.16.0", "@sveltejs/vite-plugin-svelte": "^5.0.0", "@tailwindcss/postcss": "^4.1.6", diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 6bd1214..4ad9622 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -15,4 +15,4 @@ > - + \ No newline at end of file diff --git a/src/routes/create/logic/InsertGameInDB.js b/src/routes/create/logic/InsertGameInDB.js index 15fe789..229396f 100644 --- a/src/routes/create/logic/InsertGameInDB.js +++ b/src/routes/create/logic/InsertGameInDB.js @@ -2,27 +2,21 @@ import { supabase } from '$lib/supabase'; export async function createGame(questions, gamePin) { // Insert the game into the GAMES table - const { data: gameData, error: gameError } = await supabase - .from('games') - .insert({ - creator: 'anonymous', // Replace with actual creator if available - creationdate: new Date().toISOString(), - status: 'lobby', - GamePIN: gamePin - }) - .select('id') - .single(); + const { data: gameData, error: gameError } = await supabase.from('games').insert({ + creator: 'anonymous', + creationdate: new Date().toISOString(), + status: 'lobby', + gamepin: gamePin + }); if (gameError) { alert('Failed to create game: ' + gameError.message + '\n\nPlease try again.'); return; } - const gameid = gameData.id; - // Prepare questions and answers for batch insertion const questionsData = questions.map((q, index) => ({ - gameid: gameid, + gameid: gamePin, questionstext: q.name, correctanswer: q.correctAnswer })); @@ -53,6 +47,4 @@ export async function createGame(questions, gamePin) { alert('Failed to insert answers: ' + answersError.message + '\n\nPlease try again.'); return; } - - alert('Game created successfully!'); } diff --git a/src/routes/create/logic/StartGame.js b/src/routes/create/logic/StartGame.js index d24afe4..eaa080e 100644 --- a/src/routes/create/logic/StartGame.js +++ b/src/routes/create/logic/StartGame.js @@ -5,13 +5,14 @@ import { questions } from './GameCreateData.svelte.js'; export async function startGame() { if (questions.v.some((q) => q.name === '')) return alert('Please fill all questions'); if (questions.v.some((q) => q.answers.some((a) => a === ''))) return alert('Fill all options'); - if (questions.v.some((q) => q.correctAnswer === undefined)) return alert('Select correct answers'); + if (questions.v.some((q) => q.correctAnswer === undefined)) + return alert('Select correct answers'); const gamePin = Math.floor(Math.random() * 1000000) .toString() .padStart(6, '0'); - createGame($state.snapshot(questions), gamePin); + createGame(questions.v, gamePin); goto('/host/' + gamePin); } diff --git a/src/routes/host/[gamePin]/+page.js b/src/routes/host/[gamePin]/+page.js new file mode 100644 index 0000000..43d524c --- /dev/null +++ b/src/routes/host/[gamePin]/+page.js @@ -0,0 +1,5 @@ +export function load({ params }) { + return { + gamePin: params.gamePin + }; +} \ No newline at end of file diff --git a/src/routes/host/[gamePin]/+page.svelte b/src/routes/host/[gamePin]/+page.svelte new file mode 100644 index 0000000..5808a7b --- /dev/null +++ b/src/routes/host/[gamePin]/+page.svelte @@ -0,0 +1,82 @@ + + +
+
+

HOSTING

+

Game Pin:

+

+ {gamePin} +

+

Players Joined:

+

(Total Players: {players.length})

+
+ {#each players as player} + {player.name} + {/each} +
+ +
+
\ No newline at end of file diff --git a/src/routes/hostgame/[gamePin]/+page.svelte b/src/routes/hostgame/[gamePin]/+page.svelte deleted file mode 100644 index 1c47d33..0000000 --- a/src/routes/hostgame/[gamePin]/+page.svelte +++ /dev/null @@ -1,109 +0,0 @@ - - -
-
-

HOSTING

-
-

Question {currentQuestion + 1} of {totalQuetions} is beeing awnsered

-
-
-
-
-
-

{PeopleAwnseredQ} out of {Totalplayers} have awnsered the quetion

-
-
-
-
-
-
diff --git a/src/routes/join/+page.svelte b/src/routes/join/+page.svelte new file mode 100644 index 0000000..bf7df82 --- /dev/null +++ b/src/routes/join/+page.svelte @@ -0,0 +1,45 @@ + + +
+
+

Join a game here

+ + + + + + {#if Checking.v} + + {:else} + {/if} +
+
diff --git a/src/routes/join/logic/InsertPlayerInDB.js b/src/routes/join/logic/InsertPlayerInDB.js new file mode 100644 index 0000000..8669847 --- /dev/null +++ b/src/routes/join/logic/InsertPlayerInDB.js @@ -0,0 +1,14 @@ +import { supabase } from "$lib/supabase"; + +export async function addPlayer(name, gamePin) { + const { error } = await supabase.from("players").insert({ + gameid: gamePin, + score: 0, + playername: name, + }); + + if (error) { + alert("Failed to join game: " + error.message + "\n\nPlease try again."); + return; + } +} diff --git a/src/routes/join/logic/JoinGameData.svelte.js b/src/routes/join/logic/JoinGameData.svelte.js new file mode 100644 index 0000000..2ca2125 --- /dev/null +++ b/src/routes/join/logic/JoinGameData.svelte.js @@ -0,0 +1,3 @@ +export let Checking = $state({ + v: false, +}); diff --git a/src/routes/join/logic/joinGame.js b/src/routes/join/logic/joinGame.js new file mode 100644 index 0000000..9573e00 --- /dev/null +++ b/src/routes/join/logic/joinGame.js @@ -0,0 +1,20 @@ +import { goto } from '$app/navigation'; +import { addPlayer } from './InsertPlayerInDB.js'; +import { validateGamePin } from './validateGamePin.js'; +import { Checking} from "./JoinGameData.svelte.js" + +export async function joinGame(pin, name) { + Checking.v = true; + + if (!(await validateGamePin())) { + alert('Invalid game pin. Please try again.'); + Checking.v = false; + return; + } + + addPlayer(name, pin); + + goto('/play/' + pin, { + state: { name } + }); +} diff --git a/src/routes/join/logic/validateGamePin.js b/src/routes/join/logic/validateGamePin.js new file mode 100644 index 0000000..6801d24 --- /dev/null +++ b/src/routes/join/logic/validateGamePin.js @@ -0,0 +1,11 @@ +import { supabase } from '$lib/supabase'; + +export async function validateGamePin(pin) { + const { data, error } = await supabase + .from('games') + .select('gamepin') + .eq('gamepin', Number(pin)) + .maybeSingle(); + + return data !== null && !error; +} diff --git a/src/routes/play/[gamePin]/+page.svelte b/src/routes/play/[gamePin]/+page.svelte deleted file mode 100644 index a0a13c3..0000000 --- a/src/routes/play/[gamePin]/+page.svelte +++ /dev/null @@ -1,142 +0,0 @@ - - -
-
-
-

Question {currentQuestion + 1} of {question.length}

-
-
-
-
- {#if isWait != true} -

- Q{currentQuestion + 1}. {question[currentQuestion].name} -

-
- {#each question[currentQuestion].answers as answer, index} -
- - -
- {/each} -
- {#if Selected != null} - - {:else} - - {/if} - {:else}

- Please wait for everyone else to answer the question. -

{/if} -
-
diff --git a/src/routes/results/[gamePin]/+page.svelte b/src/routes/results/[gamePin]/+page.svelte deleted file mode 100644 index 972b002..0000000 --- a/src/routes/results/[gamePin]/+page.svelte +++ /dev/null @@ -1,71 +0,0 @@ - - -
-
-

Leaderboard

- - {#if players} - {#each players as player, i} - {#if name == player.name} -
-
- {i + 1} -
-
{player.name}
-
-
-
{player.score} points
-
-
-
- {:else}
-
- {i + 1} -
-
{player.name}
-
-
-
-
{player.score} points
-
- {/if} - {/each} - {/if} - - -
-
diff --git a/svelte.config.js b/svelte.config.js index 57a49bd..542d1ca 100644 --- a/svelte.config.js +++ b/svelte.config.js @@ -6,16 +6,16 @@ const config = { // adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list. // If your environment is not supported, or you settled on a specific environment, switch out the adapter. // See https://svelte.dev/docs/kit/adapters for more information about adapters. - adapter: adapter({ - // default options are shown. On some platforms - // these options are set automatically — see below - pages: 'build', - assets: 'build', - fallback: undefined, - precompress: false, - strict: true - }) + adapter: adapter() + + + + + + + + } }; -export default config; +export default config; \ No newline at end of file