mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: handle discover in server
This commit is contained in:
1 parent
264d70fe3f
commit
ee7cd083bd
2 files changed
+41
-24
No files matched your search
@@ -16,37 +16,43 @@ export default class CommandHandler {
|
||||
constructor(session, commandInput, additionalData) {
|
||||
this.session = session
|
||||
this.commandInput = commandInput
|
||||
this.argv = minimist(this.commandInput.command.split(' '))
|
||||
|
||||
this.additionalData = additionalData
|
||||
this.eventListenr = {
|
||||
discover: [this.discoverHandler, this.pushGraph],
|
||||
}
|
||||
}
|
||||
|
||||
handleEvent() {
|
||||
const events = Object.keys(this.additionalData)
|
||||
events.forEach((event) => {
|
||||
const handlers = this.eventListeners[event]
|
||||
if (handlers) {
|
||||
handlers.forEach((handler) => handler())
|
||||
}
|
||||
})
|
||||
if (this.additionalData.discover) this.discoverHandler()
|
||||
}
|
||||
|
||||
handleCommand() {
|
||||
const command = this.argv._[0]
|
||||
switch (command) {
|
||||
case 'cd':
|
||||
pushToSession(this.session.id, 'graph', {
|
||||
pwd: this.commandInput.pwd,
|
||||
})
|
||||
break
|
||||
|
||||
case 'ls':
|
||||
pushToSession(this.session.id, 'graph', {
|
||||
pwd: this.commandInput.pwd,
|
||||
discover: this.additionalData.discover,
|
||||
})
|
||||
break
|
||||
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
async discoverHandler() {
|
||||
const graph = new FileGraph(this.session.graph)
|
||||
graph.discover(this.additionalData.discover)
|
||||
console.log('graph:', graph.toString())
|
||||
this.session.graph = graph.toJSON()
|
||||
this.session.graph = graph
|
||||
await this.session.save()
|
||||
}
|
||||
|
||||
pushGraph() {
|
||||
pushToSession(this.session.id, 'graph', {
|
||||
pwd: this.commandInput.pwd,
|
||||
discover: this.additionalData.discover,
|
||||
})
|
||||
}
|
||||
|
||||
async run() {
|
||||
const quest = await Quest.findById(this.session.quest)
|
||||
const stage = quest.stages.find((s) => s.id === this.session.progress)
|
||||
@@ -55,12 +61,8 @@ export default class CommandHandler {
|
||||
return {}
|
||||
}
|
||||
|
||||
const argv = minimist(this.commandInput.command.split(' '))
|
||||
|
||||
this.handleEvent()
|
||||
if (argv._[0] === 'cd') {
|
||||
pushToSession(this.session.id, 'graph', { pwd: this.commandInput.pwd })
|
||||
}
|
||||
this.handleCommand()
|
||||
|
||||
const commandMatch = checkMatch(
|
||||
stage.condition.command,
|
||||
|
||||
@@ -1,4 +1,15 @@
|
||||
import { model, Schema } from 'mongoose'
|
||||
import { FileNode } from '@linux-odyssey/file-graph'
|
||||
|
||||
const nodeSchema = new Schema({
|
||||
path: String,
|
||||
type: String,
|
||||
discovered: Boolean,
|
||||
})
|
||||
|
||||
nodeSchema.add({
|
||||
children: [nodeSchema],
|
||||
})
|
||||
|
||||
const Session = model(
|
||||
'Session',
|
||||
@@ -28,6 +39,10 @@ const Session = model(
|
||||
progress: String,
|
||||
completion: [String],
|
||||
hints: [String],
|
||||
graph: {
|
||||
type: nodeSchema,
|
||||
default: new FileNode({ path: '/', name: '/', type: 'folder' }),
|
||||
},
|
||||
},
|
||||
{ timestamps: true }
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user