55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
import { supabase } from "$lib/supabase";
|
|
import toast from "svelte-5-french-toast";
|
|
|
|
|
|
export async function createGame(questions, gamePin) {
|
|
const { data: gameData, error: gameError } = await supabase.from("games").insert({
|
|
creator: "anonymous",
|
|
creationdate: new Date().toISOString(),
|
|
status: "lobby",
|
|
gamepin: gamePin,
|
|
});
|
|
|
|
if (gameError) {
|
|
toast.error("Failed to create game: " + gameError.message + "\n\nPlease try again.");
|
|
return;
|
|
}
|
|
|
|
// Prepare questions and answers for batch insertion
|
|
const questionsData = questions.map((q, index) => ({
|
|
gameid: gamePin,
|
|
questionstext: q.name,
|
|
correctanswer: q.correctAnswer,
|
|
timelimit: q.timeLimit,
|
|
media: q.media || null,
|
|
}));
|
|
|
|
const { data: questionsResult, error: questionsError } = await supabase
|
|
.from("questions")
|
|
.insert(questionsData)
|
|
.select("id");
|
|
|
|
if (questionsError) {
|
|
toast.error("Failed to insert questions: " + questionsError.message + "\n\nPlease try again.");
|
|
return;
|
|
}
|
|
|
|
const answersData = [];
|
|
questionsResult.forEach((question, index) => {
|
|
questions[index].answers.forEach((answer, answerIndex) => {
|
|
answersData.push({
|
|
questionid: question.id,
|
|
content: answer,
|
|
});
|
|
});
|
|
});
|
|
|
|
const { error: answersError } = await supabase.from("answers").insert(answersData);
|
|
|
|
if (answersError) {
|
|
toast.error("Failed to insert answers: " + answersError.message + "\n\nPlease try again.");
|
|
return;
|
|
}
|
|
|
|
window.location.href = `/kahootclone/host?gamepin=${gamePin}`;
|
|
}
|