mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: add pending signal to status
This commit is contained in:
1 parent
aec0cf84ad
commit
d724f97403
11 files changed
+90
-30
No files matched your search
@@ -1,6 +1,8 @@
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
import { Command, Session } from '@linux-odyssey/models'
|
||||
import CommandHandler from '../../game/commandHandler.js'
|
||||
import { pushToSession } from '../socket.js'
|
||||
|
||||
const commandCompleteCallbacks = new Map()
|
||||
|
||||
export async function newCommand(req, res) {
|
||||
console.log('new command:', req.body)
|
||||
@@ -44,8 +46,35 @@ export async function newCommand(req, res) {
|
||||
if (response) {
|
||||
c.stage = response.stage
|
||||
await c.save()
|
||||
commandCompleteCallbacks.set(session.id, response.callback)
|
||||
pushToSession(session.id, 'status', 'pending')
|
||||
}
|
||||
|
||||
res.status(201).json(response)
|
||||
await session.save()
|
||||
}
|
||||
|
||||
export async function completedCommand(req, res) {
|
||||
const sessionId = req.user.session_id
|
||||
|
||||
const session = await Session.findById(sessionId)
|
||||
|
||||
if (!session) {
|
||||
res.status(404).json({ message: 'session not found' })
|
||||
return
|
||||
}
|
||||
|
||||
if (session.status === 'inactive') {
|
||||
res.status(400).json({ message: 'session is not active' })
|
||||
return
|
||||
}
|
||||
|
||||
const callback = commandCompleteCallbacks.get(session.id)
|
||||
if (!callback) {
|
||||
res.status(400).json({ message: 'no callback found' })
|
||||
return
|
||||
}
|
||||
callback()
|
||||
commandCompleteCallbacks.delete(session.id)
|
||||
res.json({ message: 'ok' })
|
||||
}
|
||||
@@ -44,6 +44,12 @@ export async function createSession(req, res) {
|
||||
return
|
||||
}
|
||||
|
||||
// deactive all active sessions
|
||||
await Session.updateMany(
|
||||
{ user: req.user._id, quest, status: 'active' },
|
||||
{ status: 'inactive' }
|
||||
)
|
||||
|
||||
try {
|
||||
const container = await createContainer(
|
||||
`quest-${quest.id}-${req.user.username}-${Date.now()}`
|
||||
|
||||
@@ -4,5 +4,6 @@ import * as commandController from '../controllers/commandController.js'
|
||||
const router = Router()
|
||||
|
||||
router.post('/', commandController.newCommand)
|
||||
router.post('/completed', commandController.completedCommand)
|
||||
|
||||
export default router
|
||||
@@ -19,6 +19,7 @@ router.use('/sessions', authRequired, sessions)
|
||||
router.use(
|
||||
'/commands',
|
||||
passport.authenticate('jwt', { session: false }),
|
||||
authRequired,
|
||||
commands
|
||||
)
|
||||
|
||||
|
||||
@@ -34,7 +34,6 @@ export default class SessionHandler {
|
||||
addNewTasks() {
|
||||
const newTasks = this.getNewTasks()
|
||||
this.session.tasks.push(...newTasks)
|
||||
pushToSession(this.session.id, 'tasks', this.session.tasks)
|
||||
}
|
||||
|
||||
execute(stage) {
|
||||
@@ -45,12 +44,10 @@ export default class SessionHandler {
|
||||
})
|
||||
|
||||
this.session.hints.push(...stage.hints)
|
||||
pushToSession(this.session.id, 'hints', stage.hints)
|
||||
|
||||
if (stage.id === 'END') {
|
||||
this.session.status = 'finished'
|
||||
this.session.finishedAt = new Date()
|
||||
pushToSession(this.session.id, 'status', 'finished')
|
||||
}
|
||||
|
||||
this.addNewTasks()
|
||||
@@ -58,6 +55,11 @@ export default class SessionHandler {
|
||||
return {
|
||||
responses: stage.responses,
|
||||
hints: stage.hints,
|
||||
callback: () => {
|
||||
pushToSession(this.session.id, 'tasks', this.session.tasks)
|
||||
pushToSession(this.session.id, 'hints', stage.hints)
|
||||
pushToSession(this.session.id, 'status', this.session.status)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user