From 59f98fadadf6a229dcab39b6a546d748cec093c6 Mon Sep 17 00:00:00 2001 From: Ota Prokopec Date: Sun, 26 Mar 2023 20:03:30 +0200 Subject: [PATCH] continuing in experience --- src/lib/TStypes/experiences.ts | 1 + src/lib/components/erant/Progressbar.svelte | 20 +++++++ src/lib/utils/database/experience.ts | 9 ++- src/routes/game/Components/Layout.svelte | 10 +++- src/routes/game/Forms/Renderer.svelte | 61 ++++++++++++++------- src/routes/game/experience.svelte | 11 ++-- 6 files changed, 85 insertions(+), 27 deletions(-) create mode 100644 src/lib/components/erant/Progressbar.svelte diff --git a/src/lib/TStypes/experiences.ts b/src/lib/TStypes/experiences.ts index eb8c246..a740e9e 100644 --- a/src/lib/TStypes/experiences.ts +++ b/src/lib/TStypes/experiences.ts @@ -39,4 +39,5 @@ export type Experience = { ExpURL: string UserID: string checkPoints: Array + rating: number } diff --git a/src/lib/components/erant/Progressbar.svelte b/src/lib/components/erant/Progressbar.svelte new file mode 100644 index 0000000..a91735b --- /dev/null +++ b/src/lib/components/erant/Progressbar.svelte @@ -0,0 +1,20 @@ + + +
+ {#if showWrittenProgress} + {progress}/{max} + {/if} + +
diff --git a/src/lib/utils/database/experience.ts b/src/lib/utils/database/experience.ts index d9c0972..9e3e59f 100644 --- a/src/lib/utils/database/experience.ts +++ b/src/lib/utils/database/experience.ts @@ -50,18 +50,19 @@ export const answer = async (experienceId: string, checkPointId: string, answer: const checkPoint = await databases.getDocument('63cef30d6da945dd4250', '63cef84d908acf805758', checkPointId) console.log({ checkPoint }) const { CPType, CPAnswerID } = checkPoint - const correctAnswer = (await databases.getDocument('63cef30d6da945dd4250', '63dd5c2b764061e40025', CPAnswerID)).CPAnswer + const correctAnswer = CPType !== 'INFO' ? (await databases.getDocument('63cef30d6da945dd4250', '63dd5c2b764061e40025', CPAnswerID)).CPAnswer : true let correct: boolean = false if (CPType === 'CHECKBOX') correct = JSON.stringify(answer) === JSON.stringify(correctAnswer) if (CPType === 'RADIO' || CPType === 'NUMBER' || CPType === 'TEXT') correct = answer === correctAnswer[0] - + if (CPType === 'INFO') correct = false await saveAnswerIntoDatabase(experienceId, checkPointId, correct) return correct } catch (error) { throw new Error(error) + //operation only for sveltekit 401, 500, 403 } } @@ -136,10 +137,12 @@ export const getUserProgress = async (experienceId: string) => { } export const getUserProgressAsStore = (experienceId: string) => { const store = writable([]) + const loading = writable(true) getUserProgress(experienceId).then((documents) => { store.set(documents) + loading.set(false) }) - return [store] as const + return [store, loading] as const } // !pridat trideni podle kategorie! export const getExperiencesByCategory = async (category: string) => {} diff --git a/src/routes/game/Components/Layout.svelte b/src/routes/game/Components/Layout.svelte index 7226f95..238b06e 100644 --- a/src/routes/game/Components/Layout.svelte +++ b/src/routes/game/Components/Layout.svelte @@ -3,10 +3,12 @@ import Button from '$lib/components/Buttons/Button.svelte' import Image from '$lib/components/Common/Image.svelte' import { createEventDispatcher } from 'svelte' + import Loading from '$lib/components/Common/Loading.svelte' const dispatch = createEventDispatcher() export let control: 'wrong-firstTime' | 'wrong-secondTime' | 'correct' | 'not-control' | null export let imgSrc + export let loading: boolean
@@ -43,7 +45,13 @@ {#if control === 'not-control' || control === 'correct' || control === 'wrong-secondTime'} {:else if control === 'wrong-firstTime' || control === null} - + {/if}
diff --git a/src/routes/game/Forms/Renderer.svelte b/src/routes/game/Forms/Renderer.svelte index a25ec80..76810cf 100644 --- a/src/routes/game/Forms/Renderer.svelte +++ b/src/routes/game/Forms/Renderer.svelte @@ -12,11 +12,12 @@ import Info from './Info.svelte' import { Experience } from '$lib/TStypes/experiences' import Layout from '../Components/Layout.svelte' - import { answer, AnswerState, getUserAnswer } from '$lib/utils/database/experience' + import { answer, AnswerState, getUserAnswer, getUserProgressAsStore } from '$lib/utils/database/experience' import { user } from '$lib/appwrite' import Button from '$lib/components/Buttons/Button.svelte' import LayoutImg from '$lib/components/Layouts/LayoutImg.svelte' import { Models } from 'appwrite' + import { Writable } from 'svelte/store' const components = { TEXT: TextForm, @@ -31,17 +32,24 @@ export let control: AnswerState = null let view: 'question' | 'map' | 'end' | 'start' | 'start-map' = 'start-map' export let gameData: Experience //data - export let userProgress: Models.Document[] + $: [userProgress, userProgressLoading] = gameData ? getUserProgressAsStore(gameData?.$id) : [] let client = { //user data about game - pos: userProgress.length, - end: gameData.checkPoints.length - 1, //kolik otázek + pos: 0, + end: gameData.ExpCPsID.length - 1, //kolik otázek points: 0, //body possiblePointsToSeize: gameData.checkPoints.length * 2, } + $: if (client.pos < $userProgress?.length - 1 + 1) { + // nastaví na continue + client.pos = $userProgress?.length - 1 + 1 + view = 'map' + } - $: if (gameData.checkPoints[client.pos].CPType === 'INFO') control = 'not-control' + $: if (gameData.checkPoints[client.pos].CPType === 'INFO' && !$userProgressLoading) control = 'not-control' + + $: console.log(control) const nextQuestion = () => { //další otázka @@ -53,35 +61,41 @@ } } - //let answers - //$: if (client.pos < client.end) answers = parseQuestion(gameData.questions[client.pos].answer, gameData.questions[client.pos].type) //delete + $: checkPoint = gameData.checkPoints[client.pos] + $: checkPointType = checkPoint.CPType + let page = null - $: page = view === 'question' ? components[gameData.checkPoints[client.pos].CPType] : null + $: page = view === 'question' ? components[checkPointType] : null let [lat, lng] = [null, null] $: if (client.pos < client.end) [lat, lng] = view === 'map' ? gameData.checkPoints[client.pos].CPLocation : gameData.ExpLocation let userLocation = { lat: 0, lng: 0 } - $: checkPoint = gameData.checkPoints[client.pos] let myAnswer: string | string[] let clear: boolean = false + let answerLoading = false const checkAnswer = async () => { + answerLoading = true try { const res = await answer(gameData.$id, checkPoint.$id, myAnswer) - - if (res) { - control = 'correct' - client.points += 2 - } else if (control === null) { - control = 'wrong-firstTime' - clear = true - } else control = 'wrong-secondTime' - setTimeout(() => (clear = false), 400) + if (checkPointType === 'INFO') { + nextQuestion() + } else { + if (res) { + control = 'correct' + client.points += 2 + } else if (control === null) { + control = 'wrong-firstTime' + clear = true + } else control = 'wrong-secondTime' + setTimeout(() => (clear = false), 400) + } } catch (error) { console.log(error) } + answerLoading = false } @@ -106,7 +120,16 @@ {/if} {#if view === 'question'} - checkAnswer()} on:nextQuestion={nextQuestion} {control}> + checkAnswer()} + on:nextQuestion={() => { + if (checkPointType === 'INFO') checkAnswer() + else nextQuestion() + }} + {control} + > {checkPoint.CPName} {@html checkPoint.CPText} diff --git a/src/routes/game/experience.svelte b/src/routes/game/experience.svelte index 98f0fe2..21a3dad 100644 --- a/src/routes/game/experience.svelte +++ b/src/routes/game/experience.svelte @@ -16,11 +16,11 @@ import { getLocationDataFromLatAndLong } from '$lib/utils/locations' import { navigate } from '$lib/router' import { Experience } from '$lib/TStypes/experiences' + import Progressbar from '$lib/components/erant/Progressbar.svelte' export let params: { gameurl: string } let gameData: Experience // $: [userProgress] = gameData ? getUserProgressAsStore(gameData?.$id) : [] - $: console.log($userProgress) onMount(async () => { try { @@ -57,7 +57,7 @@
- {'gameData.rating'} + {gameData.rating} @@ -83,6 +83,9 @@
+
+ +
@@ -95,11 +98,11 @@
{#if gameData.checkPoints.length} - + {/if} {:else if view === 'experience-play'} - + {/if}