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

@@ -1,6 +1,7 @@
import { Command, Session } from '@linux-odyssey/models'
import CommandHandler from '../../game/commandHandler.js'
import { pushToSession } from '../socket.js'
import { finishSession } from '../../game/sessionManager.js'
const commandCompleteCallbacks = new Map()
@@ -21,6 +22,11 @@ export async function newCommand(req, res) {
return
}
if (session.status === 'finished') {
// Allow commands to be sent to finished sessions
res.status(200).end()
return
}
if (session.status !== 'active') {
res.status(400).json({ message: 'session is not active' })
return
@@ -45,6 +51,10 @@ export async function newCommand(req, res) {
if (response) {
c.stage = response.stage
await c.save()
if (response.stage === 'END') {
await finishSession(session)
}
commandCompleteCallbacks.set(session.id, response.callback)
pushToSession(session.id, 'status', 'pending')
}
-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()
}