diff --git a/src/lib/utils/game.ts b/src/lib/utils/database/game.ts similarity index 55% rename from src/lib/utils/game.ts rename to src/lib/utils/database/game.ts index 7800b15..8abc5f8 100644 --- a/src/lib/utils/game.ts +++ b/src/lib/utils/database/game.ts @@ -1,6 +1,7 @@ import { databases } from '$lib/appwrite' import { Query } from 'appwrite' import database from 'svelte-appwrite-client/src/lib/database' +import { getLocationDataFromLatAndLong } from '../locations' export const load = async (pathName: string) => { const checkPoints = [] @@ -11,10 +12,20 @@ export const load = async (pathName: string) => { for (const checkPointId of checkPointsIds) { checkPoints.push(await databases.getDocument('63cef30d6da945dd4250', '63cef84d908acf805758', checkPointId)) } + const rating = await getRating(game.$id) + console.log(rating) + + game['rating'] = rating game['checkPoints'] = checkPoints return game } +const getRating = async (expId: string) => { + const { documents } = await databases.listDocuments('63cef30d6da945dd4250', '63ee6353ebb174cf815d', [Query.equal('ExpID', expId)]) + const sum = documents.reduce((accumulator, rating) => accumulator + rating.ExpUserRating, 0) + return sum / documents.length +} + export const answer = async (checkPointId: string, answer: any) => { const checkPoint = await databases.getDocument('63cef30d6da945dd4250', '63cef84d908acf805758', checkPointId) console.log({ checkPoint }) @@ -24,3 +35,16 @@ export const answer = async (checkPointId: string, answer: any) => { if (CPType === 'CHECKBOX') return JSON.stringify(answer) === JSON.stringify(correctAnswer) if (CPType === 'RADIO' || CPType === 'NUMBER' || CPType === 'TEXT') return answer === correctAnswer[0] } + +export const getExpiriences = async () => { + const expiriences = (await databases.listDocuments('63cef30d6da945dd4250', '63cef4bd210fdf2e5888')).documents + let items: Array<{ location: string; name: string; link: string }> = [] + for (const expirience of expiriences) { + items.push({ + location: (await getLocationDataFromLatAndLong(expirience.ExpLocation[0], expirience.ExpLocation[1])).city, + name: expirience.ExpName, + link: `${expirience.ExpURL}`, + }) + } + return items +} diff --git a/src/lib/utils/locations.ts b/src/lib/utils/locations.ts new file mode 100644 index 0000000..0082a11 --- /dev/null +++ b/src/lib/utils/locations.ts @@ -0,0 +1,8 @@ +export const getLocationDataFromLatAndLong = async (latitude: number, longitude: number) => { + const response = await fetch(`https://nominatim.openstreetmap.org/reverse?lat=${latitude}&lon=${longitude}&format=json`) + const data = await response.json() + return { + city: data.address.town || data.address.city || data.address.village, + state: data.address.country, + } +} diff --git a/src/routes/game/Forms/MultiChoiceForm.svelte b/src/routes/game/Forms/MultiChoiceForm.svelte index 09d20c6..5e89dfd 100644 --- a/src/routes/game/Forms/MultiChoiceForm.svelte +++ b/src/routes/game/Forms/MultiChoiceForm.svelte @@ -2,7 +2,7 @@ import Layout from '../Components/Layout.svelte' import CheckBox from '../../../lib/components/Inputs/Checkbox.svelte' import { checkPoint } from '$lib/TStypes/expiriences' - import { answer } from '$lib/utils/game' + import { answer } from '$lib/utils/database/game' export let data: { name: string; checkPoint: checkPoint } const { checkPoint, name } = data diff --git a/src/routes/game/Forms/NumberForm.svelte b/src/routes/game/Forms/NumberForm.svelte index fd890ea..b86146e 100644 --- a/src/routes/game/Forms/NumberForm.svelte +++ b/src/routes/game/Forms/NumberForm.svelte @@ -2,7 +2,7 @@ import Layout from '../Components/Layout.svelte' import Input from '../../../lib/components/Inputs/Input.svelte' import { checkPoint } from '$lib/TStypes/expiriences' - import { answer } from '$lib/utils/game' + import { answer } from '$lib/utils/database/game' export let data: { name: string; checkPoint: checkPoint } const { checkPoint, name } = data diff --git a/src/routes/game/Forms/Renderer.svelte b/src/routes/game/Forms/Renderer.svelte index e6b9a5b..49cc6e2 100644 --- a/src/routes/game/Forms/Renderer.svelte +++ b/src/routes/game/Forms/Renderer.svelte @@ -21,7 +21,7 @@ } let control = null // if true => spravne if false spatne - let view: 'question' | 'map' = 'question' + let view: 'question' | 'map' = 'map' export let gameData: any = {} //data $: console.log(gameData) @@ -54,6 +54,8 @@ let user = { lat: null, lng: null } + $: console.log(page, gameData.checkPoints[clientAnswers.pos].CPType) + /* //set user to localstorage $: if (clientAnswers.pos !== clientAnswers.end && clientAnswers.pos !== 0) { //nastaví diff --git a/src/routes/game/Forms/SingleChoiceForm.svelte b/src/routes/game/Forms/SingleChoiceForm.svelte index 73bc3cf..e364d50 100644 --- a/src/routes/game/Forms/SingleChoiceForm.svelte +++ b/src/routes/game/Forms/SingleChoiceForm.svelte @@ -2,7 +2,7 @@ import Radio from '../../../lib/components/Inputs/Radio.svelte' import Layout from '../Components/Layout.svelte' import { checkPoint } from '$lib/TStypes/expiriences' - import { answer } from '$lib/utils/game' + import { answer } from '$lib/utils/database/game' export let data: { name: string; checkPoint: checkPoint } const { checkPoint, name } = data diff --git a/src/routes/game/Forms/TextForm.svelte b/src/routes/game/Forms/TextForm.svelte index 945a06f..ae6104e 100644 --- a/src/routes/game/Forms/TextForm.svelte +++ b/src/routes/game/Forms/TextForm.svelte @@ -2,7 +2,7 @@ import Layout from '../Components/Layout.svelte' import Input from '../../../lib/components/Inputs/Input.svelte' import { checkPoint } from '$lib/TStypes/expiriences' - import { answer } from '$lib/utils/game' + import { answer } from '$lib/utils/database/game' export let data: { name: string; checkPoint: checkPoint } const { checkPoint, name } = data diff --git a/src/routes/game/game.svelte b/src/routes/game/game.svelte index e665f22..5a562b6 100644 --- a/src/routes/game/game.svelte +++ b/src/routes/game/game.svelte @@ -16,7 +16,8 @@ import collections from '$lib/collections' import { Query } from 'appwrite' import { onMount } from 'svelte' - import { load } from '$lib/utils/game' + import { load } from '$lib/utils/database/game' + import { getLocationDataFromLatAndLong } from '$lib/utils/locations' export let params: { gameurl: string } @@ -28,13 +29,14 @@ $data = await load(params.gameurl) loading = false }) + $: console.log($data) $: assets = $data?.questions ?.filter((q) => q.thumbnail !== null) ?.slice(0, 8) ?.map((q) => q.thumbnail) - let view: 'game-play' | 'game-loading' = 'game-play' + let view: 'game-play' | 'game-loading' = 'game-loading' //is user already in game //const userInGame = JSON.parse(localStorage.getItem(location.pathname)) @@ -54,11 +56,13 @@