mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: refactor stage to multiple route design
This commit is contained in:
1 parent
52c6c6ab93
commit
e16b328892
6 files changed
+51
-32
No files matched your search
@@ -36,7 +36,6 @@ export async function newCommand(req, res) {
|
||||
}
|
||||
|
||||
session.lastActivityAt = new Date()
|
||||
await session.save()
|
||||
|
||||
const c = new Command({
|
||||
session,
|
||||
@@ -54,4 +53,5 @@ export async function newCommand(req, res) {
|
||||
console.log(response)
|
||||
|
||||
res.status(201).json(response)
|
||||
await session.save()
|
||||
}
|
||||
@@ -50,20 +50,15 @@ export default class CommandHandler {
|
||||
const graph = new FileGraph(this.session.graph)
|
||||
graph.discover(this.additionalData.discover)
|
||||
this.session.graph = graph
|
||||
await this.session.save()
|
||||
}
|
||||
|
||||
async run() {
|
||||
const quest = await Quest.findById(this.session.quest)
|
||||
const stage = quest.stages.find((s) => s.id === this.session.progress)
|
||||
if (!stage) {
|
||||
console.error('stage not found', this.session.progress)
|
||||
return {}
|
||||
}
|
||||
|
||||
this.handleEvent()
|
||||
this.handleCommand()
|
||||
getStages() {
|
||||
return this.quest.stages.filter((s) =>
|
||||
s.requirements.every((r) => this.session.completed.includes(r))
|
||||
)
|
||||
}
|
||||
|
||||
matchStage(stage) {
|
||||
const commandMatch = checkMatch(
|
||||
stage.condition.command,
|
||||
this.commandInput.command
|
||||
@@ -79,22 +74,39 @@ export default class CommandHandler {
|
||||
|
||||
if (!commandMatch || !outputMatch || !errorMatch) return {}
|
||||
|
||||
this.session.completion.push(this.session.progress)
|
||||
this.session.completed.push(stage.id)
|
||||
this.session.hints.push(...stage.hints)
|
||||
|
||||
this.session.progress = stage.next
|
||||
await this.session.save()
|
||||
|
||||
if (stage.next === 'END') {
|
||||
if (stage.id === 'END') {
|
||||
this.session.status = 'finished'
|
||||
this.session.finishedAt = new Date()
|
||||
await this.session.save()
|
||||
}
|
||||
|
||||
return {
|
||||
responses: stage.responses,
|
||||
hints: stage.hints,
|
||||
end: stage.next === 'END',
|
||||
}
|
||||
}
|
||||
|
||||
async run() {
|
||||
this.quest = await Quest.findById(this.session.quest)
|
||||
const stages = this.getStages()
|
||||
if (stages.length === 0) {
|
||||
console.error('stage not found', this.session.progress)
|
||||
return {}
|
||||
}
|
||||
|
||||
console.log('stages', stages)
|
||||
|
||||
this.handleEvent()
|
||||
this.handleCommand()
|
||||
|
||||
const response = stages.reduce(
|
||||
(r, s) => ({ ...r, ...this.matchStage(s) }),
|
||||
{}
|
||||
)
|
||||
|
||||
return {
|
||||
end: this.session.status === 'finished',
|
||||
...response,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,12 @@ const stageSchema = new Schema({
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
requirements: [
|
||||
{
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
condition: {
|
||||
command: {
|
||||
type: [String],
|
||||
|
||||
@@ -39,8 +39,7 @@ const Session = model(
|
||||
default: Date.now,
|
||||
required: true,
|
||||
},
|
||||
progress: String,
|
||||
completion: [String],
|
||||
completed: [String],
|
||||
hints: [String],
|
||||
graph: {
|
||||
type: nodeSchema,
|
||||
|
||||
+1
-1
@@ -392,7 +392,7 @@ components:
|
||||
type: string
|
||||
description: current stage id
|
||||
example: helloworld
|
||||
completion:
|
||||
completed:
|
||||
type: array
|
||||
description: list of completed stage ids
|
||||
items:
|
||||
|
||||
Reference in new issue
Block a user