From 6ac4dad9fab4bd4c733972c95bd6240f3911a53d Mon Sep 17 00:00:00 2001 From: RezHackXYZ Date: Mon, 2 Jun 2025 08:24:34 +0530 Subject: [PATCH] create easy backup --- .../create copy backup/+page.svelte | 26 ++++++ .../components/Questions/answers.svelte | 23 ++++++ .../components/Questions/question.svelte | 79 ++++++++++++++++++ .../components/buttons/DeleteQuestion.svelte | 20 +++++ .../buttons/GenerateOptionsUsingAI.svelte | 19 +++++ .../buttons/GenerateQuetionsUsingAI.svelte | 14 ++++ .../components/buttons/NewQuestion.svelte | 15 ++++ .../components/buttons/StartGame.svelte | 18 +++++ .../buttons/UseDemoQuestions.svelte | 15 ++++ .../components/buttons/WaitStartGame.svelte | 18 +++++ .../logic/GameCreateData.svelte.js | 38 +++++++++ .../logic/GenerateOptionsUsingAI.js | 43 ++++++++++ .../logic/GenerateQuestionsUsingAI.js | 41 ++++++++++ .../logic/InsertGameInDB.js | 80 +++++++++++++++++++ .../create copy backup/logic/StartGame.js | 17 ++++ .../create copy backup/logic/UpLoadFiles.js | 31 +++++++ 16 files changed, 497 insertions(+) create mode 100644 src/routes/kahootclone/create copy backup/+page.svelte create mode 100644 src/routes/kahootclone/create copy backup/components/Questions/answers.svelte create mode 100644 src/routes/kahootclone/create copy backup/components/Questions/question.svelte create mode 100644 src/routes/kahootclone/create copy backup/components/buttons/DeleteQuestion.svelte create mode 100644 src/routes/kahootclone/create copy backup/components/buttons/GenerateOptionsUsingAI.svelte create mode 100644 src/routes/kahootclone/create copy backup/components/buttons/GenerateQuetionsUsingAI.svelte create mode 100644 src/routes/kahootclone/create copy backup/components/buttons/NewQuestion.svelte create mode 100644 src/routes/kahootclone/create copy backup/components/buttons/StartGame.svelte create mode 100644 src/routes/kahootclone/create copy backup/components/buttons/UseDemoQuestions.svelte create mode 100644 src/routes/kahootclone/create copy backup/components/buttons/WaitStartGame.svelte create mode 100644 src/routes/kahootclone/create copy backup/logic/GameCreateData.svelte.js create mode 100644 src/routes/kahootclone/create copy backup/logic/GenerateOptionsUsingAI.js create mode 100644 src/routes/kahootclone/create copy backup/logic/GenerateQuestionsUsingAI.js create mode 100644 src/routes/kahootclone/create copy backup/logic/InsertGameInDB.js create mode 100644 src/routes/kahootclone/create copy backup/logic/StartGame.js create mode 100644 src/routes/kahootclone/create copy backup/logic/UpLoadFiles.js diff --git a/src/routes/kahootclone/create copy backup/+page.svelte b/src/routes/kahootclone/create copy backup/+page.svelte new file mode 100644 index 0000000..02e29a7 --- /dev/null +++ b/src/routes/kahootclone/create copy backup/+page.svelte @@ -0,0 +1,26 @@ + + +
+
+
+ {#each questions.v as question, index} + + {/each} +
+ + {#if Wait.v == false} + + {:else} + + {/if} +
+
+
diff --git a/src/routes/kahootclone/create copy backup/components/Questions/answers.svelte b/src/routes/kahootclone/create copy backup/components/Questions/answers.svelte new file mode 100644 index 0000000..dd8002e --- /dev/null +++ b/src/routes/kahootclone/create copy backup/components/Questions/answers.svelte @@ -0,0 +1,23 @@ + + +
+ + + +
diff --git a/src/routes/kahootclone/create copy backup/components/Questions/question.svelte b/src/routes/kahootclone/create copy backup/components/Questions/question.svelte new file mode 100644 index 0000000..e739168 --- /dev/null +++ b/src/routes/kahootclone/create copy backup/components/Questions/question.svelte @@ -0,0 +1,79 @@ + + +
+
+
+

Q{index + 1}.

+ + + + +
+ +
+ {#each questions.v[index].answers as _, answersIndex} + + {/each} +
+ + { + questions.v[index].media = await UpLoadFiles(files[0]); + }} + bind:files + accept="image/*,video/*" + /> +
+
diff --git a/src/routes/kahootclone/create copy backup/components/buttons/DeleteQuestion.svelte b/src/routes/kahootclone/create copy backup/components/buttons/DeleteQuestion.svelte new file mode 100644 index 0000000..7057ea9 --- /dev/null +++ b/src/routes/kahootclone/create copy backup/components/buttons/DeleteQuestion.svelte @@ -0,0 +1,20 @@ + + + diff --git a/src/routes/kahootclone/create copy backup/components/buttons/GenerateOptionsUsingAI.svelte b/src/routes/kahootclone/create copy backup/components/buttons/GenerateOptionsUsingAI.svelte new file mode 100644 index 0000000..b657a8c --- /dev/null +++ b/src/routes/kahootclone/create copy backup/components/buttons/GenerateOptionsUsingAI.svelte @@ -0,0 +1,19 @@ + + +{#if questions.v[index].answers.every((answer) => answer === "")} + +{/if} diff --git a/src/routes/kahootclone/create copy backup/components/buttons/GenerateQuetionsUsingAI.svelte b/src/routes/kahootclone/create copy backup/components/buttons/GenerateQuetionsUsingAI.svelte new file mode 100644 index 0000000..8932152 --- /dev/null +++ b/src/routes/kahootclone/create copy backup/components/buttons/GenerateQuetionsUsingAI.svelte @@ -0,0 +1,14 @@ + + +{#if questions.v.length === 0 || (questions.v.length === 1 && questions.v[0].name === "" && questions.v[0].answers.every((answer) => answer === "") && questions.v[0].correctAnswer === undefined)} + +{/if} diff --git a/src/routes/kahootclone/create copy backup/components/buttons/NewQuestion.svelte b/src/routes/kahootclone/create copy backup/components/buttons/NewQuestion.svelte new file mode 100644 index 0000000..63cc2d6 --- /dev/null +++ b/src/routes/kahootclone/create copy backup/components/buttons/NewQuestion.svelte @@ -0,0 +1,15 @@ + + + diff --git a/src/routes/kahootclone/create copy backup/components/buttons/StartGame.svelte b/src/routes/kahootclone/create copy backup/components/buttons/StartGame.svelte new file mode 100644 index 0000000..10f8d3a --- /dev/null +++ b/src/routes/kahootclone/create copy backup/components/buttons/StartGame.svelte @@ -0,0 +1,18 @@ + + + diff --git a/src/routes/kahootclone/create copy backup/components/buttons/UseDemoQuestions.svelte b/src/routes/kahootclone/create copy backup/components/buttons/UseDemoQuestions.svelte new file mode 100644 index 0000000..ead81cd --- /dev/null +++ b/src/routes/kahootclone/create copy backup/components/buttons/UseDemoQuestions.svelte @@ -0,0 +1,15 @@ + + + diff --git a/src/routes/kahootclone/create copy backup/components/buttons/WaitStartGame.svelte b/src/routes/kahootclone/create copy backup/components/buttons/WaitStartGame.svelte new file mode 100644 index 0000000..0102b86 --- /dev/null +++ b/src/routes/kahootclone/create copy backup/components/buttons/WaitStartGame.svelte @@ -0,0 +1,18 @@ + + + diff --git a/src/routes/kahootclone/create copy backup/logic/GameCreateData.svelte.js b/src/routes/kahootclone/create copy backup/logic/GameCreateData.svelte.js new file mode 100644 index 0000000..33a6316 --- /dev/null +++ b/src/routes/kahootclone/create copy backup/logic/GameCreateData.svelte.js @@ -0,0 +1,38 @@ + +import { DefaultQuestions } from "$lib/config.js"; +import toast from "svelte-5-french-toast"; + +export let Wait = $state({ v: false }); +export let questions = $state({ + v: [ + { + name: "", + answers: ["", "", "", ""], + correctAnswer: undefined, + timeLimit: 30, + }, + ], +}); + +export function SetQuestionsToDemoQuestions() { + questions.v = DefaultQuestions; +} + +export function AddQuestion() { + questions.v.push({ + name: "", + answers: ["", "", "", ""], + correctAnswer: undefined, + timeLimit: 30, + }); +} + +export function DeleteQuestion(index) { + if (questions.v.length > 1) { + if (confirm("Are you sure you want to delete this question? You cant undo this.")) { + questions.v.splice(index, 1); + } + } else { + toast.error("You need at least one question."); + } +} diff --git a/src/routes/kahootclone/create copy backup/logic/GenerateOptionsUsingAI.js b/src/routes/kahootclone/create copy backup/logic/GenerateOptionsUsingAI.js new file mode 100644 index 0000000..175c8d3 --- /dev/null +++ b/src/routes/kahootclone/create copy backup/logic/GenerateOptionsUsingAI.js @@ -0,0 +1,43 @@ +import { questions } from "./GameCreateData.svelte.js"; +import { AiPrompts } from "$lib/config.js"; +import toast from "svelte-5-french-toast"; + +export function GenerateOptionsUsingAI(index) { + if (!questions.v[index].name) { + toast.error("Please enter a question to generate options."); + return; + } + + const fetchOptions = async () => { + const response = await fetch("https://ai.hackclub.com/chat/completions", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + messages: [ + { + role: "user", + content: AiPrompts.GenerateOptionsUsingAI.replace( + "[question]", + questions.v[index].name, + ), + }, + ], + }), + }); + const data = await response.json(); + let question = questions.v[index].name; + questions.v[index] = JSON.parse(data.choices[0].message.content); + questions.v[index].name = question; + }; + + toast.promise( + fetchOptions(), + { + loading: "Generating options...", + success: "Options generated!", + error: (err) => "Error: " + (err?.message || err), + } + ); +} diff --git a/src/routes/kahootclone/create copy backup/logic/GenerateQuestionsUsingAI.js b/src/routes/kahootclone/create copy backup/logic/GenerateQuestionsUsingAI.js new file mode 100644 index 0000000..7c369f9 --- /dev/null +++ b/src/routes/kahootclone/create copy backup/logic/GenerateQuestionsUsingAI.js @@ -0,0 +1,41 @@ +import { questions } from "./GameCreateData.svelte.js"; +import { AiPrompts } from "$lib/config.js"; +import toast from "svelte-5-french-toast"; + +export function GenerateQuestionsUsingAI() { + let topic = window.prompt( + "What is the topic of the questions?\nand the number of questions in the topic?", + ); + + if (!topic) { + return; + } + + const fetchQuestions = async () => { + const response = await fetch("https://ai.hackclub.com/chat/completions", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + messages: [ + { + role: "user", + content: AiPrompts.GenerateQuestionsUsingAI.replace("[topic]", topic), + }, + ], + }), + }); + const data = await response.json(); + questions.v = JSON.parse(data.choices[0].message.content); + }; + + toast.promise( + fetchQuestions(), + { + loading: "Generating questions...", + success: "Questions added!", + error: (err) => "Error: " + err.message || err, + } + ); +} \ No newline at end of file diff --git a/src/routes/kahootclone/create copy backup/logic/InsertGameInDB.js b/src/routes/kahootclone/create copy backup/logic/InsertGameInDB.js new file mode 100644 index 0000000..8d4e525 --- /dev/null +++ b/src/routes/kahootclone/create copy backup/logic/InsertGameInDB.js @@ -0,0 +1,80 @@ +import { supabase } from "$lib/supabase"; +import toast from "svelte-5-french-toast"; +import { Wait } from "./GameCreateData.svelte.js"; + +export async function createGame(questions, gamePin) { + // Insert game + const insertGamePromise = supabase.from("games").insert({ + creator: "anonymous", + creationdate: new Date().toISOString(), + status: "lobby", + gamepin: gamePin, + }); + + const { data: gameData, error: gameError } = await toast.promise(insertGamePromise, { + loading: "Creating game...", + success: "Game created!", + error: (err) => + "Failed to create game: " + (err?.message || "Unknown error") + "\n\nPlease try again.", + }); + + if (gameError) { + Wait.v = false; + return; + } + + // Prepare questions and answers for batch insertion + const questionsData = questions.map((q) => ({ + gameid: gamePin, + questionstext: q.name, + correctanswer: q.correctAnswer, + timelimit: q.timeLimit, + media: q.media || null, + })); + + const insertQuestionsPromise = supabase.from("questions").insert(questionsData).select("id"); + + const { data: questionsResult, error: questionsError } = await toast.promise( + insertQuestionsPromise, + { + loading: "Inserting questions...", + success: "Questions inserted!", + error: (err) => + "Failed to insert questions: " + + (err?.message || "Unknown error") + + "\n\nPlease try again.", + }, + ); + + if (questionsError) { + Wait.v = false; + + return; + } + + const answersData = []; + questionsResult.forEach((question, index) => { + questions[index].answers.forEach((answer) => { + answersData.push({ + questionid: question.id, + content: answer, + }); + }); + }); + + const insertAnswersPromise = supabase.from("answers").insert(answersData); + + const { error: answersError } = await toast.promise(insertAnswersPromise, { + loading: "Inserting answers...", + success: "Answers inserted!", + error: (err) => + "Failed to insert answers: " + (err?.message || "Unknown error") + "\n\nPlease try again.", + }); + + if (answersError) { + Wait.v = false + return; + } + + window.location.href = `/kahootclone/host?gamepin=${gamePin}`; +} diff --git a/src/routes/kahootclone/create copy backup/logic/StartGame.js b/src/routes/kahootclone/create copy backup/logic/StartGame.js new file mode 100644 index 0000000..6060fac --- /dev/null +++ b/src/routes/kahootclone/create copy backup/logic/StartGame.js @@ -0,0 +1,17 @@ +import { createGame } from "./InsertGameInDB.js"; +import { questions,Wait } from "./GameCreateData.svelte.js"; +import toast from "svelte-5-french-toast"; + +export async function startGame() { + if (questions.v.some((q) => q.name === "")) return toast.error("Please fill all questions"); + if (questions.v.some((q) => q.answers.some((a) => a === ""))) return toast.error("Fill all options"); + if (questions.v.some((q) => q.correctAnswer === undefined)) + return toast.error("Select correct answers"); + + const gamePin = Math.floor(Math.random() * 1000000) + .toString() + .padStart(6, "0"); + + Wait.v = true; + + await createGame(questions.v, gamePin);} diff --git a/src/routes/kahootclone/create copy backup/logic/UpLoadFiles.js b/src/routes/kahootclone/create copy backup/logic/UpLoadFiles.js new file mode 100644 index 0000000..254d180 --- /dev/null +++ b/src/routes/kahootclone/create copy backup/logic/UpLoadFiles.js @@ -0,0 +1,31 @@ +import toast from "svelte-5-french-toast"; +import { supabase } from "$lib/supabase.js"; + +export async function UpLoadFiles(file) { + if (!file) { + toast.error("Please select a file to upload first."); + return; + } + + const fileExt = file.name.split(".").pop(); + const fileName = `${Date.now()}.${fileExt}`; + const filePath = `${fileName}`; + + const uploadPromise = supabase.storage.from("useruploadedcontent").upload(filePath, file); + + const result = await toast.promise(uploadPromise, { + loading: "Uploading...", + success: "Upload successful!", + error: (error) => `Upload failed. ${error.message} Please try again.`, + }); + + if (result.error) { + toast.error("Upload error:"+ result.error.message); + return; + } + + // Retrieve public URL + const { data: publicData } = supabase.storage.from("useruploadedcontent").getPublicUrl(filePath); + + return publicData.publicUrl; +}