feat: add commandHandler

This commit is contained in:
wancat committed 2023-08-08 20:38:27 +08:00
1 parent 0de4b6ac9d
commit 094332da53
7 files changed
+66 -6

No files matched your search

+1 -1
View File
@@ -95,4 +95,4 @@ async function main() {
}
}
main()
main().then(exit)
+9 -1
View File
@@ -5,7 +5,7 @@ introduction: |
「輸入 echo Hello World!」
stages:
- name: helloworld
- id: helloworld
condition:
command:
- echo Hello World!
@@ -19,3 +19,11 @@ stages:
- 例如,你可以輸入 `ls` 來查看目前的目錄下有哪些檔案。
hint:
- 使用 `ls` 指令來查看目前的目錄下有哪些檔案。
next: ls
- id: ls
condition:
command:
- ls
responses:
- NOTHING
next: END
@@ -2,6 +2,7 @@
import Command from '../../models/command.js'
import Session from '../../models/session.js'
import { verifySessionJWT } from '../../utils/auth.js'
import commandHandler from '../../game/commandHandler.js'
export async function newCommand(req, res) {
console.log('new command:', req.body)
@@ -38,7 +39,8 @@ export async function newCommand(req, res) {
await c.save()
res.status(201).json({
responses: ['Hello, world!', 'How are you?'],
})
const response = await commandHandler(session, c)
console.log(response)
res.status(201).json(response)
}
@@ -23,9 +23,11 @@ export async function createSession(req, res) {
}
try {
const progress = quest.stages[0].id
const newSession = new Session({
user: req.user,
quest,
progress,
containerId: `quest-${quest.id}-${req.user.username}-${Date.now()}`,
})
+42
View File
@@ -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
+5 -1
View File
@@ -1,7 +1,7 @@
import { model, Schema } from 'mongoose'
const stageSchema = new Schema({
name: {
id: {
type: String,
required: true,
},
@@ -27,6 +27,10 @@ const stageSchema = new Schema({
type: [String],
required: true,
},
next: {
type: String,
required: true,
},
})
const Quest = model(
+2
View File
@@ -14,6 +14,8 @@ const Session = model(
},
containerId: String,
finishedAt: Date,
progress: String,
completion: [String],
},
{ timestamps: true }
)