mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
feat: finishSession and allow user command after finished
This commit is contained in:
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 { Command, Session } from '@linux-odyssey/models'
|
||||||
import CommandHandler from '../../game/commandHandler.js'
|
import CommandHandler from '../../game/commandHandler.js'
|
||||||
import { pushToSession } from '../socket.js'
|
import { pushToSession } from '../socket.js'
|
||||||
|
import { finishSession } from '../../game/sessionManager.js'
|
||||||
|
|
||||||
const commandCompleteCallbacks = new Map()
|
const commandCompleteCallbacks = new Map()
|
||||||
|
|
||||||
@@ -21,6 +22,11 @@ export async function newCommand(req, res) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (session.status === 'finished') {
|
||||||
|
// Allow commands to be sent to finished sessions
|
||||||
|
res.status(200).end()
|
||||||
|
return
|
||||||
|
}
|
||||||
if (session.status !== 'active') {
|
if (session.status !== 'active') {
|
||||||
res.status(400).json({ message: 'session is not active' })
|
res.status(400).json({ message: 'session is not active' })
|
||||||
return
|
return
|
||||||
@@ -45,6 +51,10 @@ export async function newCommand(req, res) {
|
|||||||
if (response) {
|
if (response) {
|
||||||
c.stage = response.stage
|
c.stage = response.stage
|
||||||
await c.save()
|
await c.save()
|
||||||
|
|
||||||
|
if (response.stage === 'END') {
|
||||||
|
await finishSession(session)
|
||||||
|
}
|
||||||
commandCompleteCallbacks.set(session.id, response.callback)
|
commandCompleteCallbacks.set(session.id, response.callback)
|
||||||
pushToSession(session.id, 'status', 'pending')
|
pushToSession(session.id, 'status', 'pending')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,11 +49,6 @@ export default class SessionHandler {
|
|||||||
|
|
||||||
this.session.hints.push(...stage.hints)
|
this.session.hints.push(...stage.hints)
|
||||||
|
|
||||||
if (stage.id === 'END') {
|
|
||||||
this.session.status = 'finished'
|
|
||||||
this.session.finishedAt = new Date()
|
|
||||||
}
|
|
||||||
|
|
||||||
this.addNewTasks()
|
this.addNewTasks()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -79,3 +79,23 @@ export async function getOrCreateActiveSession(user, questId) {
|
|||||||
}
|
}
|
||||||
return createNewSession(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()
|
||||||
|
}
|
||||||
Reference in new issue
Block a user