feat: add initial task on session creation

This commit is contained in:
wancat committed 2023-09-01 17:16:32 +08:00
1 parent 97b0fb0f1d
commit 0ea44e1f71
7 files changed
+59 -28

No files matched your search

@@ -1,6 +1,7 @@
import Session from '../../models/session.js'
import Quest from '../../models/quest.js'
import { createContainer } from '../../containers/docker.js'
import SessionHandler from '../../game/sessionHandler.js'
export async function getSessionList(req, res) {
try {
@@ -45,20 +46,22 @@ export async function createSession(req, res) {
}
try {
const progress = quest.stages[0].id
const container = await createContainer(
`quest-${quest.id}-${req.user.username}-${Date.now()}`
)
const newSession = new Session({
user: req.user,
quest,
progress,
containerId: container.id,
})
const session = await newSession.save()
res.status(201).json(session)
const session = new SessionHandler(newSession)
session.addNewTasks()
await newSession.save()
res.status(201).json(newSession)
} catch (err) {
console.error(err)
res.status(400).json({ message: err.message })
}
}
-1
View File
@@ -75,7 +75,6 @@ export default class CommandHandler extends SessionHandler {
.filter((s) => this.isMatch(s))
.reduce((r, s) => ({ ...r, ...this.execute(s) }), {})
console.log(this.session)
return {
end: this.session.status === 'finished',
...response,
+21 -9
View File
@@ -1,8 +1,9 @@
import { pushToSession } from '../api/socket.js'
export default class SessionWrapper {
export default class SessionHandler {
constructor(session) {
this.session = session
this.quest = session.quest
}
getTask(stage) {
@@ -22,29 +23,40 @@ export default class SessionWrapper {
)
}
getNewTasks() {
const stages = this.getStages()
return stages
.filter((s) => s.task)
.filter((s) => !this.session.tasks.some((t) => t.id === s.id))
.map((s) => ({ id: s.id, name: s.task, completed: false }))
}
addNewTasks() {
const newTasks = this.getNewTasks()
this.session.tasks.push(...newTasks)
pushToSession(this.session.id, 'tasks', this.session.tasks)
}
execute(stage) {
this.session.tasks
.filter((t) => t.id === stage.id)
.forEach((t) => {
t.completed = true
})
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()
}
const stages = this.getStages()
console.debug(stages)
const newTasks = stages
.filter((s) => s.task)
.map((s) => ({ id: s.id, name: s.task, completed: false }))
this.session.tasks.push(...newTasks)
pushToSession(this.session.id, 'tasks', this.session.tasks)
this.addNewTasks()
return {
responses: stage.responses,
hints: stage.hints,
tasks: newTasks,
}
}
}