make it backword compatible!
This commit is contained in:
parent
5b9054272f
commit
3b254c1aa1
3 changed files with 85 additions and 47 deletions
|
@ -2,7 +2,7 @@
|
||||||
import toast from "svelte-5-french-toast";
|
import toast from "svelte-5-french-toast";
|
||||||
|
|
||||||
import { DefaultQuestions } from "$lib/config.js";
|
import { DefaultQuestions } from "$lib/config.js";
|
||||||
import { QuestionsData } from "./create.svelte";
|
import { QuestionsData, wait } from "./create.svelte";
|
||||||
import { createGame } from "./createGame.js";
|
import { createGame } from "./createGame.js";
|
||||||
|
|
||||||
let userInput = "";
|
let userInput = "";
|
||||||
|
@ -87,7 +87,13 @@ The user's topic of interest is:
|
||||||
<button class="btn flex items-center gap-1" onclick={() => GenerateQuestionsUsingAI()}>
|
<button class="btn flex items-center gap-1" onclick={() => GenerateQuestionsUsingAI()}>
|
||||||
<i class="nf nf-md-cpu_64_bit"></i> Generate Questions Using AI
|
<i class="nf nf-md-cpu_64_bit"></i> Generate Questions Using AI
|
||||||
</button>
|
</button>
|
||||||
<button class="btn flex items-center gap-1" onclick={() => createGame()}>
|
{#if wait.v == true}
|
||||||
<i class="nf nf-md-play"></i> Start Game
|
<button class="btn dull flex cursor-not-allowed items-center gap-1" disabled>
|
||||||
</button>
|
<i class="nf nf-md-play"></i> Creating Game
|
||||||
|
</button>
|
||||||
|
{:else}
|
||||||
|
<button class="btn flex items-center gap-1" onclick={() => createGame()}>
|
||||||
|
<i class="nf nf-md-play"></i> Start Game
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -12,4 +12,4 @@ export let QuestionsData = $state({
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
export let wait = $state({ v: false });
|
|
@ -1,51 +1,83 @@
|
||||||
import { supabase } from "$lib/supabase.js";
|
import { supabase } from "$lib/supabase.js";
|
||||||
import { QuestionsData } from "./create.svelte.js";
|
import { QuestionsData, wait } from "./create.svelte.js";
|
||||||
import toast from "svelte-5-french-toast";
|
import toast from "svelte-5-french-toast";
|
||||||
|
|
||||||
export async function createGame() {
|
export async function createGame() {
|
||||||
let GamesData;
|
wait.v = true;
|
||||||
try {
|
const gamePin = Math.floor(Math.random() * 1000000)
|
||||||
GamesData = await toast.promise(
|
.toString()
|
||||||
(async () => {
|
.padStart(6, "0");
|
||||||
let { data, error } = await supabase.from("games").insert([{}]).select("id,gamepin");
|
|
||||||
if (error) {
|
|
||||||
throw new Error(error.message);
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
})(),
|
|
||||||
{
|
|
||||||
loading: "Adding Game...",
|
|
||||||
success: "Game added!",
|
|
||||||
error: (err) => `Could not add game.\nError: ${err.message}\n\n Please try again.`,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
await toast.promise(
|
const questionsData = QuestionsData.v.map((q) => ({
|
||||||
(async () => {
|
gameid: gamePin,
|
||||||
const { data, error } = await supabase
|
questionstext: q.questionText,
|
||||||
.from("questions")
|
correctanswer: q.CorrectOption.SingleAnswer,
|
||||||
.insert(
|
timeLimit: q.TimeLimit,
|
||||||
QuestionsData.v.map((_, index) => ({
|
media: q.hasMedia ? q.mediaURL : null,
|
||||||
...QuestionsData.v[index],
|
}));
|
||||||
gameid: GamesData[0].id,
|
|
||||||
})),
|
const insertGamePromise = supabase.from("games").insert({
|
||||||
)
|
creator: "anonymous",
|
||||||
.select();
|
creationdate: new Date().toISOString(),
|
||||||
if (error) {
|
status: "lobby",
|
||||||
throw new Error(error.message);
|
gamepin: gamePin,
|
||||||
}
|
});
|
||||||
return data;
|
|
||||||
})(),
|
const { data: gameData, error: gameError } = await toast.promise(insertGamePromise, {
|
||||||
{
|
loading: "Creating game...",
|
||||||
loading: "Adding Questions and Answers...",
|
success: "Game created!",
|
||||||
success: "Questions and Answers added!",
|
error: (err) =>
|
||||||
error: (err) =>
|
"Failed to create game: " + (err?.message || "Unknown error") + "\n\nPlease try again.",
|
||||||
`Could not add Questions and Answers.\nError: ${err.message}\n\n Please try again.`,
|
});
|
||||||
},
|
|
||||||
);
|
if (gameError) {
|
||||||
} catch (error) {
|
wait.v = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
toast.success(`Game created! Your game pin is: ${GamesData[0].gamepin}`);
|
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) => {
|
||||||
|
QuestionsData.v[index].options.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}`;
|
||||||
|
wait.v = false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue