experience totals fix, countPointsFix-realtime
This commit is contained in:
parent
3a6488c5d1
commit
b73eb5d020
|
@ -54,3 +54,11 @@ export interface ExperienceDocument extends Models.Document {
|
||||||
export interface CPAnswerDocument extends Models.Document {
|
export interface CPAnswerDocument extends Models.Document {
|
||||||
CPAnswer: string[]
|
CPAnswer: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface UsersAnswersDocument extends Models.Document {
|
||||||
|
userId: string
|
||||||
|
checkPoint: string
|
||||||
|
experience: string
|
||||||
|
correct: boolean
|
||||||
|
attemptCount: number
|
||||||
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { Account, Models, Permission, Query, Role } from 'appwrite'
|
||||||
import database from 'svelte-appwrite-client/src/lib/database'
|
import database from 'svelte-appwrite-client/src/lib/database'
|
||||||
import { getLocationDataFromLatAndLong } from '../locations'
|
import { getLocationDataFromLatAndLong } from '../locations'
|
||||||
import { Writable, writable } from 'svelte/store'
|
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'
|
import collections from '$lib/collections'
|
||||||
|
|
||||||
export type AnswerState = 'wrong-firstTime' | 'wrong-secondTime' | 'correct' | 'not-control' | null
|
export type AnswerState = 'wrong-firstTime' | 'wrong-secondTime' | 'correct' | 'not-control' | null
|
||||||
|
@ -141,11 +141,36 @@ export const getExperiencesAsStore = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getUserProgress = async (experienceId: string) => {
|
export const getUserProgress = async (experienceId: string) => {
|
||||||
const { documents } = await databases.listDocuments('63cef30d6da945dd4250', 'users-answers', [Query.equal('userId', user.$id), Query.equal('experience', experienceId)])
|
const { documents, total } = await databases.listDocuments<Models.Document & UsersAnswersDocument>('63cef30d6da945dd4250', 'users-answers', [
|
||||||
return documents
|
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) => {
|
export const getUserProgressAsStore = (experienceId: string) => {
|
||||||
const store = writable<Models.Document[]>([])
|
const store = writable<(Models.Document & UsersAnswersDocument)[]>([])
|
||||||
const loading = writable<boolean>(true)
|
const loading = writable<boolean>(true)
|
||||||
getUserProgress(experienceId).then((documents) => {
|
getUserProgress(experienceId).then((documents) => {
|
||||||
store.set(documents)
|
store.set(documents)
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
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, AnswerState, getUserAnswer, getUserProgressAsStore } from '$lib/utils/database/experience'
|
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 Button from '$lib/components/Buttons/Button.svelte'
|
||||||
import LayoutImg from '$lib/components/Layouts/LayoutImg.svelte'
|
import LayoutImg from '$lib/components/Layouts/LayoutImg.svelte'
|
||||||
import { Models } from 'appwrite'
|
import { Models } from 'appwrite'
|
||||||
|
@ -29,23 +29,27 @@
|
||||||
INFO: Info,
|
INFO: Info,
|
||||||
}
|
}
|
||||||
|
|
||||||
$: console.log(gameData.checkPoints[client.pos], gameData.checkPoints, client.pos)
|
|
||||||
|
|
||||||
export let control: AnswerState = null
|
export let control: AnswerState = null
|
||||||
let view: 'question' | 'map' | 'end' | 'start' | 'start-map' = 'start-map'
|
let view: 'question' | 'map' | 'end' | 'start' | 'start-map' = 'start-map'
|
||||||
export let gameData: Experience //data
|
export let gameData: Experience //data
|
||||||
$: [userProgress, userProgressLoading] = gameData ? getUserProgressAsStore(gameData?.$id) : []
|
$: [userProgress, userProgressLoading] = getUserProgressAsStore(gameData.$id)
|
||||||
|
|
||||||
let client = {
|
let client = {
|
||||||
//user data about game
|
//user data about game
|
||||||
pos: 0,
|
pos: 0,
|
||||||
end: gameData.ExpCPsID.length - 1, //kolik otázek
|
end: gameData.ExpCPsID.length - 1, //kolik otázek
|
||||||
points: 0, //body
|
points: 0,
|
||||||
possiblePointsToSeize: gameData.checkPoints.length * 2,
|
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) {
|
$: if (client.pos < $userProgress?.length - 1 + 1) {
|
||||||
// nastaví na continue
|
// nastaví na continue
|
||||||
client.pos = $userProgress?.length - 1 + 1
|
client.pos = $userProgress?.length - 1 + 1
|
||||||
|
client.points = $userProgress?.map((i) => (i.correct ? 2 : 0))?.reduce((accumulator, currentValue) => accumulator + currentValue)
|
||||||
|
|
||||||
view = 'map'
|
view = 'map'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue