bug fix repeat

This commit is contained in:
Ota Prokopec 2023-03-23 21:45:48 +01:00
parent 7cf0eb0eb4
commit 48e4f4d000
6 changed files with 68 additions and 29 deletions

View File

@ -75,10 +75,10 @@
path: '/scanner', path: '/scanner',
component: () => import('$routes/qrscanner/qrscanner.svelte'), component: () => import('$routes/qrscanner/qrscanner.svelte'),
}, },
{ /*{
path: '/test', path: '/test',
component: () => import('$routes/test.svelte'), component: () => import('$routes/test.svelte'),
}, },*/
{ {
path: '/map', path: '/map',
component: () => import('$root/src/routes/map/map.svelte'), component: () => import('$root/src/routes/map/map.svelte'),

View File

@ -10,9 +10,9 @@
export let center = null export let center = null
export let mapComponent = null export let mapComponent = null
export let user = { lat: 0, lng: 0 } export let userLocation = { lat: 0, lng: 0 }
$: console.log(user, center) $: console.log(userLocation, center)
let className = '' let className = ''
export { className as class } export { className as class }
@ -25,11 +25,11 @@
})()*/ })()*/
navigator.geolocation.getCurrentPosition((position: GeolocationPosition) => { navigator.geolocation.getCurrentPosition((position: GeolocationPosition) => {
if (!user) user = { lat: position.coords.latitude, lng: position.coords.longitude } if (!userLocation) userLocation = { lat: position.coords.latitude, lng: position.coords.longitude }
if (!center) center = { lat: position.coords.latitude, lng: position.coords.longitude } if (!center) center = { lat: position.coords.latitude, lng: position.coords.longitude }
}) //at mapbox this is required dont know why }) //at mapbox this is required dont know why
$: if (!center) center = user $: if (!center) center = userLocation
/* if (navigator.geolocation) { /* if (navigator.geolocation) {
navigator.geolocation.watchPosition(showPosition, () => dispatch('locationFailed')) navigator.geolocation.watchPosition(showPosition, () => dispatch('locationFailed'))
} else { } else {
@ -59,7 +59,7 @@
on:geolocate={(e) => { on:geolocate={(e) => {
// @ts-ignore // @ts-ignore
const { latitude, longitude } = e.detail.coords const { latitude, longitude } = e.detail.coords
user = { lat: latitude, lng: longitude } userLocation = { lat: latitude, lng: longitude }
}} }}
/> />
</Map> </Map>

View File

@ -6,15 +6,15 @@
export let lat = 0 export let lat = 0
export let lng = 0 export let lng = 0
export let user = { lat: 0, lng: 0 } export let userLocation = { lat: 0, lng: 0 }
export let popup: string | null = null export let popup: string | null = null
export let round = (1 / 110.574 / 1000) * 12 //cca 12m nutno pozměnit!!!!!!!!!! tento komentář nemazat export let round = (1 / 110.574 / 1000) * 12 //cca 12m nutno pozměnit!!!!!!!!!! tento komentář nemazat
let Mlat = [lat - round, lat + round] let Mlat = [lat - round, lat + round]
let Mlng = [lng - round, lng + round] let Mlng = [lng - round, lng + round]
$: isIn = user ? user.lat > Mlat[0] && user.lat < Mlat[1] && user.lng > Mlng[0] && user.lng < Mlng[1] : null $: isIn = userLocation ? userLocation.lat > Mlat[0] && userLocation.lat < Mlat[1] && userLocation.lng > Mlng[0] && userLocation.lng < Mlng[1] : null
$: isIn && dispatch('enter', { lat, lng, user }) $: isIn && dispatch('enter', { lat, lng, userLocation })
</script> </script>
<Marker popup={false} {lat} {lng}> <Marker popup={false} {lat} {lng}>

View File

@ -6,6 +6,8 @@ import { writable } from 'svelte/store'
import { CheckPoint, Experience } from '$lib/TStypes/experiences' import { CheckPoint, Experience } from '$lib/TStypes/experiences'
import collections from '$lib/collections' import collections from '$lib/collections'
export type AnswerState = 'wrong-firstTime' | 'wrong-secondTime' | 'correct' | 'not-control' | null
let user: { $id: string } let user: { $id: string }
userStore.subscribe((res) => (user = res)) userStore.subscribe((res) => (user = res))
@ -41,6 +43,7 @@ const getRating = async (expId: string) => {
const sum = documents.reduce((accumulator, rating) => accumulator + rating.ExpUserRating, 0) const sum = documents.reduce((accumulator, rating) => accumulator + rating.ExpUserRating, 0)
return sum / total || 0 return sum / total || 0
} }
// Fetch of answers to the checkpoints // Fetch of answers to the checkpoints
export const answer = async (experienceId: string, checkPointId: string, answer: any) => { export const answer = async (experienceId: string, checkPointId: string, answer: any) => {
try { try {
@ -54,17 +57,43 @@ export const answer = async (experienceId: string, checkPointId: string, answer:
if (CPType === 'CHECKBOX') correct = JSON.stringify(answer) === JSON.stringify(correctAnswer) if (CPType === 'CHECKBOX') correct = JSON.stringify(answer) === JSON.stringify(correctAnswer)
if (CPType === 'RADIO' || CPType === 'NUMBER' || CPType === 'TEXT') correct = answer === correctAnswer[0] if (CPType === 'RADIO' || CPType === 'NUMBER' || CPType === 'TEXT') correct = answer === correctAnswer[0]
collections.usersAnswers.createDocument({ //await saveAnswerIntoDatabase(experienceId, checkPointId, correct)
userId: user.$id,
checkPoint: checkPointId,
answer,
experience: experienceId,
}, [Permission.read(Role.user(user.$id))])
return correct return correct
} catch (error) { } catch (error) {
//operation only for sveltekit 401, 500, 403 //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) {
collections.usersAnswers.updateDocument(previousAttemptDocument.$id, {
correct,
attemptCount: 2,
})
} else {
await collections.usersAnswers.createDocument(
{
userId: user.$id,
checkPoint: checkPointId,
correct,
experience: experienceId,
attemptCount: 1,
},
[Permission.read(Role.user(user.$id)), Permission.update(Role.user(user.$id))],
)
}
}
}
export const getUserAnswer = async (userId, checkPointId) => {
return (await databases.listDocuments('63cef30d6da945dd4250', 'users-answers', [Query.equal('checkPoint', checkPointId), Query.equal('userId', userId)])).documents[0]
}
// Fetch of all experiences // Fetch of all experiences
export const getExperiences = async () => { export const getExperiences = async () => {
const experiences = (await databases.listDocuments('63cef30d6da945dd4250', '63cef4bd210fdf2e5888', [Query.equal('ExpApproved', true)])).documents const experiences = (await databases.listDocuments('63cef30d6da945dd4250', '63cef4bd210fdf2e5888', [Query.equal('ExpApproved', true)])).documents

View File

@ -12,7 +12,10 @@
import Info from './Info.svelte' import Info from './Info.svelte'
import { Experience } from '$lib/TStypes/experiences' import { Experience } from '$lib/TStypes/experiences'
import Layout from '../Components/Layout.svelte' import Layout from '../Components/Layout.svelte'
import { answer } from '$lib/utils/database/experience' import { answer, AnswerState, getUserAnswer } 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'
const components = { const components = {
TEXT: TextForm, TEXT: TextForm,
@ -24,8 +27,8 @@
INFO: Info, INFO: Info,
} }
export let control: 'wrong-firstTime' | 'wrong-secondTime' | 'correct' | 'not-control' | null = null export let control: AnswerState = null
let view: 'question' | 'map' | 'end' | 'start-map' = 'start-map' let view: 'question' | 'map' | 'end' | 'start' | 'start-map' = 'start-map'
export let gameData: Experience //data export let gameData: Experience //data
let client = { let client = {
@ -56,7 +59,7 @@
let [lat, lng] = [null, null] let [lat, lng] = [null, null]
$: if (client.pos < client.end) [lat, lng] = view === 'map' ? gameData.checkPoints[client.pos].CPLocation : gameData.ExpLocation $: if (client.pos < client.end) [lat, lng] = view === 'map' ? gameData.checkPoints[client.pos].CPLocation : gameData.ExpLocation
let user = { lat: 0, lng: 0 } let userLocation = { lat: 0, lng: 0 }
$: checkPoint = gameData.checkPoints[client.pos] $: checkPoint = gameData.checkPoints[client.pos]
let myAnswer: string | string[] let myAnswer: string | string[]
@ -76,14 +79,25 @@
</script> </script>
<input type="number" bind:value={client.pos} /> <input type="number" bind:value={client.pos} />
<button on:click={() => (view = view === 'start-map' ? 'map' : 'question')}>disappear map</button> <button on:click={() => (view = view === 'start-map' ? 'start' : 'question')}>disappear map</button>
{#if view === 'map' || view === 'start-map'} {#if view === 'map' || view === 'start-map'}
<Erantmap bind:user class="w-full h-full"> <Erantmap bind:userLocation class="w-full h-full">
<Marker on:enter={() => (view = view === 'start-map' ? 'map' : 'question')} {lat} {lng} {user} /> <Marker on:enter={() => (view = view === 'start-map' ? 'start' : 'question')} {lat} {lng} {userLocation} />
</Erantmap> </Erantmap>
{/if} {/if}
{#if view === 'start'}
<LayoutImg img={gameData.ExpImage}>
<div class="w-full h-auto flex flex-wrap flex-row gap-4 justify-center">
<div class="h-full w-full flex justify-self-center justify-center text-[32px] flex-wrap flex-col gap-4 items-center">
{@html gameData.ExpStart}
</div>
<Button class="w-80 mt-8 " on:click={() => (view = 'map')}>pokračovat</Button>
</div>
</LayoutImg>
{/if}
{#if view === 'question'} {#if view === 'question'}
<Layout imgSrc={'gameData.question.thumbnail'} on:submit={() => checkAnswer()} on:nextQuestion={nextQuestion} {control}> <Layout imgSrc={'gameData.question.thumbnail'} on:submit={() => checkAnswer()} on:nextQuestion={nextQuestion} {control}>
<span slot="title">{checkPoint.CPName}</span> <span slot="title">{checkPoint.CPName}</span>
@ -107,8 +121,5 @@
</Layout> </Layout>
{/if} {/if}
{#if view === 'end'} {#if view === 'end'}
<Finish {client} {gameData}> <Finish {client} {gameData} />
<span> Získali jste {client.points} / {client.possiblePointsToSeize} bodů</span>
<span />
</Finish>
{/if} {/if}

View File

@ -20,11 +20,10 @@
onMount(async () => { onMount(async () => {
try { try {
$data = await load(params.gameurl, 10, (preview) => { $data = await load(params.gameurl, 1, (preview) => {
$data = preview $data = preview
view = 'experience-preview' view = 'experience-preview'
}) })
view = 'experience-preview'
} catch (error) { } catch (error) {
navigate('/error') navigate('/error')
} }