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