diff --git a/src/lib/utils/database/experience.ts b/src/lib/utils/database/experience.ts index 9e3e59f..8028c69 100644 --- a/src/lib/utils/database/experience.ts +++ b/src/lib/utils/database/experience.ts @@ -2,7 +2,7 @@ import { databases, user as userStore } from '$lib/appwrite' import { Account, Models, Permission, Query, Role } from 'appwrite' import database from 'svelte-appwrite-client/src/lib/database' import { getLocationDataFromLatAndLong } from '../locations' -import { writable } from 'svelte/store' +import { Writable, writable } from 'svelte/store' import { CheckPoint, Experience } from '$lib/TStypes/experiences' import collections from '$lib/collections' @@ -37,6 +37,15 @@ export const load = async (pathName: string, previewQuestionsCount?: number, pre return experience } + +export const loadAsStore = (pathName: string, previewQuestionsCount?: number, preview?: Function) => { + const store = writable(null) + load(pathName, previewQuestionsCount, preview).then(experience => { + store.set(experience) + }) + return [store] +} + //Fetch of Rating const getRating = async (expId: string) => { const { documents, total } = await databases.listDocuments('63cef30d6da945dd4250', '63ee6353ebb174cf815d', [Query.equal('ExpID', expId)]) @@ -145,4 +154,12 @@ export const getUserProgressAsStore = (experienceId: string) => { return [store, loading] as const } +export const getExperienceIdByUrl = async(url: string) => { + return (await databases.listDocuments("63cef30d6da945dd4250", "63cef4bd210fdf2e5888", [Query.equal("ExpURL", url)])).documents[0].$id +} +export const getExperienceIdByUrlAsStore = (url: string): [Writable] => { + const store = writable(null) + getExperienceIdByUrl(url).then(exp => store.set(exp)) + return [store] +} // !pridat trideni podle kategorie! export const getExperiencesByCategory = async (category: string) => {} diff --git a/src/routes/game/experience.svelte b/src/routes/game/experience.svelte index 21a3dad..70fe047 100644 --- a/src/routes/game/experience.svelte +++ b/src/routes/game/experience.svelte @@ -12,20 +12,27 @@ import Renderer from './Forms/Renderer.svelte' import Marker from '../../lib/components/Map/Marker.svelte' import { onMount } from 'svelte' - import { getUserProgressAsStore, load } from '$lib/utils/database/experience' + import { getExperienceIdByUrl, getExperienceIdByUrlAsStore, getUserProgress, getUserProgressAsStore, load, loadAsStore } from '$lib/utils/database/experience' 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' + import { writable } from 'svelte/store' export let params: { gameurl: string } - let gameData: Experience // - $: [userProgress] = gameData ? getUserProgressAsStore(gameData?.$id) : [] + let gameData = writable(null) + let [id] = getExperienceIdByUrlAsStore(params.gameurl) + $: [userProgress] = $gameData ? getUserProgressAsStore($id) : [] + /* + $: [gameData] = loadAsStore(params.gameurl, $userProgress?.length + 4 || 8, (preview) => { + $gameData = preview + view = 'experience-preview' + })*/ onMount(async () => { try { - gameData = await load(params.gameurl, 2, (preview) => { - gameData = preview + $gameData = await load(params.gameurl, (await getUserProgress(await getExperienceIdByUrl(params.gameurl))).length || 8, (preview) => { + $gameData = preview view = 'experience-preview' }) } catch (error) { @@ -33,7 +40,7 @@ } }) - /*$: assets = gameData?.questions + /*$: assets = $gameData?.questions ?.filter((q) => q.thumbnail !== null) ?.slice(0, 8) ?.map((q) => q.thumbnail)*/ @@ -42,7 +49,7 @@ //is user already in game //const userInGame = JSON.parse(localStorage.getItem(location.pathname)) - //$: if (gameData && userInGame) view = 'game-play' + //$: if ($gameData && userInGame) view = 'game-play' {#if view === 'experience-loading'} @@ -51,27 +58,27 @@ {:else if view === 'experience-preview'} - +
- {gameData.ExpName} + {$gameData.ExpName}
- {gameData.rating} + {$gameData.rating} - {#await getLocationDataFromLatAndLong(gameData.ExpLocation[0], gameData.ExpLocation[1]) then { city }} + {#await getLocationDataFromLatAndLong($gameData.ExpLocation[0], $gameData.ExpLocation[1]) then { city }} {city} {/await}
- {#if gameData.ExpIntroduction} + {#if $gameData.ExpIntroduction}
- {@html gameData.ExpIntroduction} + {@html $gameData.ExpIntroduction}
{/if} @@ -84,25 +91,25 @@
- +
- - {#each gameData.checkPoints as { CPLocation: [lat, lng] }} + + {#each $gameData.checkPoints as { CPLocation: [lat, lng] }} {/each}
- {#if gameData.checkPoints.length} + {#if $gameData.checkPoints.length} {/if}
{:else if view === 'experience-play'} - + {/if}