Merge pull request #81 from Erant-s-r-o/otaprokopecFIX

fix
This commit is contained in:
Ota-Prokopec 2023-03-26 19:05:23 +02:00 committed by GitHub
commit 2012b043a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 29 deletions

View File

@ -1,5 +1,5 @@
import { databases, user as userStore } from '$lib/appwrite'
import { Account, Permission, Query, Role } from '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'
@ -57,17 +57,17 @@ export const answer = async (experienceId: string, checkPointId: string, answer:
if (CPType === 'CHECKBOX') correct = JSON.stringify(answer) === JSON.stringify(correctAnswer)
if (CPType === 'RADIO' || CPType === 'NUMBER' || CPType === 'TEXT') correct = answer === correctAnswer[0]
//await saveAnswerIntoDatabase(experienceId, checkPointId, correct)
await saveAnswerIntoDatabase(experienceId, checkPointId, correct)
return correct
} catch (error) {
throw new Error(error)
//operation only for sveltekit 401, 500, 403
}
}
const saveAnswerIntoDatabase = async (experienceId: string, checkPointId: string, correct: boolean) => {
const previousAttemptDocument = await getUserAnswer(user.$id, checkPointId)
console.log(previousAttemptDocument)
if (!(previousAttemptDocument?.attemptCount === 2)) {
if (previousAttemptDocument) {
@ -87,6 +87,10 @@ const saveAnswerIntoDatabase = async (experienceId: string, checkPointId: string
[Permission.read(Role.user(user.$id)), Permission.update(Role.user(user.$id))],
)
}
} else {
const err = new Error('User has already answered.')
err['code'] = '409'
throw err
}
}
@ -126,4 +130,16 @@ export const getExperiencesAsStore = () => {
return [store, loading] as const
}
export const getUserProgress = async (experienceId: string) => {
const { documents } = await databases.listDocuments('63cef30d6da945dd4250', 'users-answers', [Query.equal('userId', user.$id), Query.equal('experience', experienceId)])
return documents
}
export const getUserProgressAsStore = (experienceId: string) => {
const store = writable<Models.Document[]>([])
getUserProgress(experienceId).then((documents) => {
store.set(documents)
})
return [store] as const
}
// !pridat trideni podle kategorie! export const getExperiencesByCategory = async (category: string) => {}

View File

@ -16,6 +16,7 @@
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'
const components = {
TEXT: TextForm,
@ -30,10 +31,11 @@
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[]
let client = {
//user data about game
pos: 0,
pos: userProgress.length,
end: gameData.checkPoints.length - 1, //kolik otázek
points: 0, //body
possiblePointsToSeize: gameData.checkPoints.length * 2,
@ -63,9 +65,12 @@
$: checkPoint = gameData.checkPoints[client.pos]
let myAnswer: string | string[]
let clear: boolean = false
const checkAnswer = async () => {
try {
const res = await answer(gameData.$id, checkPoint.$id, myAnswer)
if (res) {
control = 'correct'
client.points += 2
@ -74,8 +79,10 @@
clear = true
} else control = 'wrong-secondTime'
setTimeout(() => (clear = false), 400)
} catch (error) {
console.log(error)
}
}
let clear: boolean = false
</script>
<input type="number" bind:value={client.pos} />

View File

@ -12,16 +12,20 @@
import Renderer from './Forms/Renderer.svelte'
import Marker from '../../lib/components/Map/Marker.svelte'
import { onMount } from 'svelte'
import { load } from '$lib/utils/database/experience'
import { getUserProgressAsStore, load } from '$lib/utils/database/experience'
import { getLocationDataFromLatAndLong } from '$lib/utils/locations'
import { navigate } from '$lib/router'
import { Experience } from '$lib/TStypes/experiences'
export let params: { gameurl: string }
let gameData: Experience //
$: [userProgress] = gameData ? getUserProgressAsStore(gameData?.$id) : []
$: console.log($userProgress)
onMount(async () => {
try {
$data = await load(params.gameurl, 1, (preview) => {
$data = preview
gameData = await load(params.gameurl, 2, (preview) => {
gameData = preview
view = 'experience-preview'
})
} catch (error) {
@ -29,7 +33,7 @@
}
})
/*$: assets = $data?.questions
/*$: assets = gameData?.questions
?.filter((q) => q.thumbnail !== null)
?.slice(0, 8)
?.map((q) => q.thumbnail)*/
@ -38,7 +42,7 @@
//is user already in game
//const userInGame = JSON.parse(localStorage.getItem(location.pathname))
//$: if ($data && userInGame) view = 'game-play'
//$: if (gameData && userInGame) view = 'game-play'
</script>
{#if view === 'experience-loading'}
@ -47,27 +51,27 @@
<Loading />
</h1>
{:else if view === 'experience-preview'}
<Overlay shareData={{ url: window.location.href }} img={$data.ExpImage}>
<Overlay shareData={{ url: window.location.href }} img={gameData.ExpImage}>
<div>
<span class="text-[36px]">{$data.ExpName}</span>
<span class="text-[36px]">{gameData.ExpName}</span>
<div class="bubbles">
<Bubble background="blue">
<span slot="icon"><IconStar /></span>
<span> {$data.rating} </span>
<span> {'gameData.rating'} </span>
</Bubble>
<Bubble background="white">
<span slot="icon"><IconPoint /></span>
{#await getLocationDataFromLatAndLong($data.ExpLocation[0], $data.ExpLocation[1]) then { city }}
{#await getLocationDataFromLatAndLong(gameData.ExpLocation[0], gameData.ExpLocation[1]) then { city }}
<span>{city}</span>
{/await}
</Bubble>
</div>
</div>
{#if $data.ExpIntroduction}
{#if gameData.ExpIntroduction}
<Section title="Popis">
<span>
{@html $data.ExpIntroduction}
{@html gameData.ExpIntroduction}
</span>
</Section>
{/if}
@ -82,20 +86,20 @@
<div class="w-full relative">
<div class="px-4 m-auto" style="max-width: var(--max-viewport-width);">
<Map radius class="w-full h-44" center={{ lng: $data.ExpLocation[1], lat: $data.ExpLocation[0] }}>
{#each $data.checkPoints as { CPLocation: [lat, lng] }}
<Map radius class="w-full h-44" center={{ lng: gameData.ExpLocation[1], lat: gameData.ExpLocation[0] }}>
{#each gameData.checkPoints as { CPLocation: [lat, lng] }}
<Marker {lat} {lng} />
{/each}
</Map>
</div>
</div>
{#if $data.checkPoints.length}
{#if gameData.checkPoints.length}
<Button on:click={() => (view = 'experience-play')} primary>Hrát</Button>
{/if}
</Overlay>
{:else if view === 'experience-play'}
<Renderer gameData={$data} />
<Renderer {gameData} {userProgress} />
{/if}
<style>