feat: finishSession and allow user command after finished

This commit is contained in:
wancat authored and Lin, Hung-Shin committed 2023-11-14 10:44:52 +08:00
1 parent 45f12acbd9
commit f49fb0e4da
3 files changed
+30 -5

No files matched your search

-5
View File
@@ -49,11 +49,6 @@ export default class SessionHandler {
this.session.hints.push(...stage.hints)
if (stage.id === 'END') {
this.session.status = 'finished'
this.session.finishedAt = new Date()
}
this.addNewTasks()
return {
+20
View File
@@ -79,3 +79,23 @@ export async function getOrCreateActiveSession(user, questId) {
}
return createNewSession(user, questId)
}
export async function finishSession(session) {
session.status = 'finished'
await session.save()
const userProfile = await UserProfile.findOne({ user: session.user })
if (!userProfile) {
throw new Error(`UserProfile ${session.user} not found`)
}
const progress = userProfile.progress.get(session.quest)
if (!progress) {
throw new Error(`Progress ${session.quest} not found`)
}
if (!progress.completed) {
progress.finishedAt = new Date()
progress.completed = true
}
await userProfile.save()
}