From 188e91efb06f9e99ef53a28acfd0de72e056eaa5 Mon Sep 17 00:00:00 2001 From: Ota Prokopec Date: Thu, 9 Mar 2023 20:51:28 +0100 Subject: [PATCH 1/8] game fix / loading empty games / locatinRe(fail) --- src/App.svelte | 7 +- src/lib/components/Map/Erantmap.svelte | 5 +- src/lib/components/Map/LocationRequest.svelte | 27 +++++ src/lib/utils/database/game.ts | 18 ++-- src/routes/game/game.svelte | 102 +++++++++--------- src/routes/map.svelte | 9 +- 6 files changed, 99 insertions(+), 69 deletions(-) create mode 100644 src/lib/components/Map/LocationRequest.svelte diff --git a/src/App.svelte b/src/App.svelte index 4915821..9bd58cb 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -7,12 +7,11 @@ import Routes from './__routes.svelte' /** import CookiesPopUp from '$lib/components/Cookies/CookiesPopUp.svelte' */ import { navigate } from '$lib/router' + import LocationRequest from '$lib/components/Map/LocationRequest.svelte' let isMounted = false $: isReady = $localeLoading === false && $authLoading === false && isMounted - $: console.log($user) - $: if (isReady && !$user) { console.log('change') @@ -37,7 +36,9 @@
- + + + {#if isReady} diff --git a/src/lib/components/Map/Erantmap.svelte b/src/lib/components/Map/Erantmap.svelte index 074e70d..0b7fa10 100644 --- a/src/lib/components/Map/Erantmap.svelte +++ b/src/lib/components/Map/Erantmap.svelte @@ -3,7 +3,8 @@ import Map from './Map.svelte' import { navigate } from 'svelte-routing' import NavigationBarLayout from '../Layouts/NavigationBarLayout.svelte' - import { onMount } from 'svelte' + import { createEventDispatcher, onMount } from 'svelte' + const dispatch = createEventDispatcher() export let center = null @@ -14,7 +15,7 @@ export { className as class } if (navigator.geolocation) { - navigator.geolocation.getCurrentPosition(showPosition) + navigator.geolocation.getCurrentPosition(showPosition, () => dispatch('locationFailed')) } else { alert("Can't load your location!") } diff --git a/src/lib/components/Map/LocationRequest.svelte b/src/lib/components/Map/LocationRequest.svelte new file mode 100644 index 0000000..f2a9158 --- /dev/null +++ b/src/lib/components/Map/LocationRequest.svelte @@ -0,0 +1,27 @@ + + +{#if state === 'not-granded'} + + This is a info alert +
+
Please provide Erant your location
+
+ +
+
+
+{/if} diff --git a/src/lib/utils/database/game.ts b/src/lib/utils/database/game.ts index 02ef7d8..365c00d 100644 --- a/src/lib/utils/database/game.ts +++ b/src/lib/utils/database/game.ts @@ -11,12 +11,14 @@ export const load = async (pathName: string, previewQuestionsCount?: number, pre const rating = await getRating(game.$id) for (const checkPointId of checkPointsIds) { - checkPoints.push(await databases.getDocument('63cef30d6da945dd4250', '63cef84d908acf805758', checkPointId)) - if (checkPointsIds.indexOf(checkPointId) === previewQuestionsCount - 1) { - game['rating'] = rating - game['checkPoints'] = checkPoints - preview(game) - } + try { + checkPoints.push(await databases.getDocument('63cef30d6da945dd4250', '63cef84d908acf805758', checkPointId)) + if (checkPointsIds.indexOf(checkPointId) === previewQuestionsCount - 1) { + game['rating'] = rating + game['checkPoints'] = checkPoints + preview(game) + } + } catch (error) {} } game['rating'] = rating @@ -25,9 +27,9 @@ export const load = async (pathName: string, previewQuestionsCount?: number, pre } const getRating = async (expId: string) => { - const { documents } = await databases.listDocuments('63cef30d6da945dd4250', '63ee6353ebb174cf815d', [Query.equal('ExpID', expId)]) + const { documents, total } = await databases.listDocuments('63cef30d6da945dd4250', '63ee6353ebb174cf815d', [Query.equal('ExpID', expId)]) const sum = documents.reduce((accumulator, rating) => accumulator + rating.ExpUserRating, 0) - return sum / documents.length + return sum / total || 0 } export const answer = async (checkPointId: string, answer: any) => { diff --git a/src/routes/game/game.svelte b/src/routes/game/game.svelte index ba41bf1..b4143ed 100644 --- a/src/routes/game/game.svelte +++ b/src/routes/game/game.svelte @@ -18,18 +18,20 @@ import { onMount } from 'svelte' import { load } from '$lib/utils/database/game' import { getLocationDataFromLatAndLong } from '$lib/utils/locations' + import { navigate } from '$lib/router' export let params: { gameurl: string } - //const [gameData] = collections.expiriences.getDocument([Query.equal('ExpURL', params.gameurl)]) - - let loading: boolean = true - onMount(async () => { - $data = await load(params.gameurl, 5, (preview) => { - $data = preview - loading = false - }) + try { + $data = await load(params.gameurl, 5, (preview) => { + $data = preview + view = 'game-preview' + }) + view = 'game-preview' + } catch (error) { + navigate('/error') + } }) $: console.log($data) @@ -38,9 +40,7 @@ ?.slice(0, 8) ?.map((q) => q.thumbnail) - let view: 'game-play' | 'game-loading' = 'game-loading' - - $: console.log($data?.ExpImage) + let view: 'game-play' | 'game-loading' | 'game-preview' = 'game-loading' //is user already in game //const userInGame = JSON.parse(localStorage.getItem(location.pathname)) @@ -48,62 +48,60 @@ {#if view === 'game-loading'} - {#if loading} -

- Hra se načítá... - -

- {:else if $data} - -
- {$data.ExpName} -
- - - {$data.rating} - - - - {#await getLocationDataFromLatAndLong($data.ExpLocation[0], $data.ExpLocation[1]) then { city }} - {city} - {/await} - -
+

+ Hra se načítá... + +

+{:else if view === 'game-preview'} + +
+ {$data.ExpName} +
+ + + {$data.rating} + + + + {#await getLocationDataFromLatAndLong($data.ExpLocation[0], $data.ExpLocation[1]) then { city }} + {city} + {/await} +
+
+ {#if $data.ExpIntroduction}
{@html $data.ExpIntroduction}
+ {/if} -
-
-
- -
-
-
- +
- - {#each $data.checkPoints as { CPLocation: [lat, lng] }} - - {/each} - +
+
+
+
+ + {#each $data.checkPoints as { CPLocation: [lat, lng] }} + + {/each} + +
+
+ + {#if $data.checkPoints.length} -
- {:else} - - {/if} + {/if} + {:else if view === 'game-play'} - {#if !loading} - - {/if} + {/if} \ No newline at end of file diff --git a/src/lib/components/Layouts/NavigationBarLayout.svelte b/src/lib/components/Layouts/NavigationBarLayout.svelte index 2240d86..07e474f 100644 --- a/src/lib/components/Layouts/NavigationBarLayout.svelte +++ b/src/lib/components/Layouts/NavigationBarLayout.svelte @@ -44,7 +44,7 @@