mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: add commandHandler
This commit is contained in:
1 parent
0de4b6ac9d
commit
094332da53
7 files changed
+66
-6
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import Quest from '../models/quest.js'
|
||||
|
||||
// 檢查 pattern 是否符合 input
|
||||
const checkMatch = (pattern, input) => {
|
||||
if (pattern.length === 0) return true
|
||||
return pattern.some((p) => {
|
||||
const regex = new RegExp(p)
|
||||
return regex.test(input)
|
||||
})
|
||||
}
|
||||
|
||||
const commandHandler = async (session, commandInput) => {
|
||||
const quest = await Quest.findById(session.quest)
|
||||
const stage = quest.stages.find((s) => s.id === session.progress)
|
||||
if (!stage) {
|
||||
console.error('stage not found', session.progress)
|
||||
return {}
|
||||
}
|
||||
|
||||
console.log(stage.condition)
|
||||
|
||||
const commandMatch = checkMatch(stage.condition.command, commandInput.command)
|
||||
const outputMatch = checkMatch(stage.condition.output, commandInput.output)
|
||||
const errorMatch = checkMatch(stage.condition.error, commandInput.output)
|
||||
|
||||
console.log(commandMatch, outputMatch, errorMatch)
|
||||
|
||||
if (!commandMatch || !outputMatch || !errorMatch) return {}
|
||||
|
||||
session.completion.push(session.progress)
|
||||
|
||||
const nextStage = quest.stages.find((s) => s.id === stage.next)
|
||||
session.progress = nextStage.id
|
||||
await session.save()
|
||||
|
||||
return {
|
||||
responses: stage.responses,
|
||||
hint: stage.hint,
|
||||
}
|
||||
}
|
||||
|
||||
export default commandHandler
|
||||
Reference in new issue
Block a user