experience totals fix, countPointsFix-realtime

This commit is contained in:
Ota Prokopec 2023-04-25 21:40:11 +02:00
parent 3a6488c5d1
commit b73eb5d020
3 changed files with 47 additions and 10 deletions

View File

@ -54,3 +54,11 @@ export interface ExperienceDocument extends Models.Document {
export interface CPAnswerDocument extends Models.Document {
CPAnswer: string[]
}
export interface UsersAnswersDocument extends Models.Document {
userId: string
checkPoint: string
experience: string
correct: boolean
attemptCount: number
}

View File

@ -3,7 +3,7 @@ import { Account, Models, Permission, Query, Role } from 'appwrite'
import database from 'svelte-appwrite-client/src/lib/database'
import { getLocationDataFromLatAndLong } from '../locations'
import { Writable, writable } from 'svelte/store'
import { CPAnswerDocument, CheckPoint, Experience } from '$lib/TStypes/experiences'
import { CPAnswerDocument, CheckPoint, Experience, UsersAnswersDocument } from '$lib/TStypes/experiences'
import collections from '$lib/collections'
export type AnswerState = 'wrong-firstTime' | 'wrong-secondTime' | 'correct' | 'not-control' | null
@ -141,11 +141,36 @@ export const getExperiencesAsStore = () => {
}
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
const { documents, total } = await databases.listDocuments<Models.Document & UsersAnswersDocument>('63cef30d6da945dd4250', 'users-answers', [
Query.equal('userId', user.$id),
Query.equal('experience', experienceId),
Query.limit(25),
Query.offset(0),
])
console.log({ total })
const allOtherDocuments = (
await Promise.all(
new Array(Math.ceil((total - 25) / 25) > 0 ? Math.ceil((total - 25) / 25) : 0)
.fill(null)
.map((_n, i) =>
databases.listDocuments<Models.Document & UsersAnswersDocument>('63cef30d6da945dd4250', 'users-answers', [
Query.equal('userId', user.$id),
Query.equal('experience', experienceId),
Query.limit(25),
Query.offset((i + 1) * 25),
]),
),
)
).flatMap((list) => list.documents)
console.log({ allOtherDocuments })
return [...documents, ...allOtherDocuments]
}
export const getUserProgressAsStore = (experienceId: string) => {
const store = writable<Models.Document[]>([])
const store = writable<(Models.Document & UsersAnswersDocument)[]>([])
const loading = writable<boolean>(true)
getUserProgress(experienceId).then((documents) => {
store.set(documents)

View File

@ -13,7 +13,7 @@
import { Experience } from '$lib/TStypes/experiences'
import Layout from '../Components/Layout.svelte'
import { answer, AnswerState, getUserAnswer, getUserProgressAsStore } from '$lib/utils/database/experience'
import { user } from '$lib/appwrite'
import client, { user } from '$lib/appwrite'
import Button from '$lib/components/Buttons/Button.svelte'
import LayoutImg from '$lib/components/Layouts/LayoutImg.svelte'
import { Models } from 'appwrite'
@ -29,23 +29,27 @@
INFO: Info,
}
$: console.log(gameData.checkPoints[client.pos], gameData.checkPoints, client.pos)
export let control: AnswerState = null
let view: 'question' | 'map' | 'end' | 'start' | 'start-map' = 'start-map'
export let gameData: Experience //data
$: [userProgress, userProgressLoading] = gameData ? getUserProgressAsStore(gameData?.$id) : []
$: [userProgress, userProgressLoading] = getUserProgressAsStore(gameData.$id)
let client = {
//user data about game
pos: 0,
end: gameData.ExpCPsID.length - 1, //kolik otázek
points: 0, //body
possiblePointsToSeize: gameData.checkPoints.length * 2,
points: 0,
possiblePointsToSeize: new Array(gameData.checkPoints.length).fill(2).reduce((accumulator, currentValue, index) => {
if (gameData.checkPoints[index].CPType !== 'INFO') return accumulator + currentValue
else return accumulator
}),
}
$: if (client.pos < $userProgress?.length - 1 + 1) {
// nastaví na continue
client.pos = $userProgress?.length - 1 + 1
client.points = $userProgress?.map((i) => (i.correct ? 2 : 0))?.reduce((accumulator, currentValue) => accumulator + currentValue)
view = 'map'
}