feat: handle discover in server

This commit is contained in:
wancat committed 2023-08-29 13:28:18 +08:00
1 parent 264d70fe3f
commit ee7cd083bd
2 files changed
+41 -24

No files matched your search

+26 -24
View File
@@ -16,37 +16,43 @@ export default class CommandHandler {
constructor(session, commandInput, additionalData) { constructor(session, commandInput, additionalData) {
this.session = session this.session = session
this.commandInput = commandInput this.commandInput = commandInput
this.argv = minimist(this.commandInput.command.split(' '))
this.additionalData = additionalData this.additionalData = additionalData
this.eventListenr = {
discover: [this.discoverHandler, this.pushGraph],
}
} }
handleEvent() { handleEvent() {
const events = Object.keys(this.additionalData) if (this.additionalData.discover) this.discoverHandler()
events.forEach((event) => { }
const handlers = this.eventListeners[event]
if (handlers) { handleCommand() {
handlers.forEach((handler) => handler()) 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() { async discoverHandler() {
const graph = new FileGraph(this.session.graph) const graph = new FileGraph(this.session.graph)
graph.discover(this.additionalData.discover) graph.discover(this.additionalData.discover)
console.log('graph:', graph.toString()) this.session.graph = graph
this.session.graph = graph.toJSON()
await this.session.save() await this.session.save()
} }
pushGraph() {
pushToSession(this.session.id, 'graph', {
pwd: this.commandInput.pwd,
discover: this.additionalData.discover,
})
}
async run() { async run() {
const quest = await Quest.findById(this.session.quest) const quest = await Quest.findById(this.session.quest)
const stage = quest.stages.find((s) => s.id === this.session.progress) const stage = quest.stages.find((s) => s.id === this.session.progress)
@@ -55,12 +61,8 @@ export default class CommandHandler {
return {} return {}
} }
const argv = minimist(this.commandInput.command.split(' '))
this.handleEvent() this.handleEvent()
if (argv._[0] === 'cd') { this.handleCommand()
pushToSession(this.session.id, 'graph', { pwd: this.commandInput.pwd })
}
const commandMatch = checkMatch( const commandMatch = checkMatch(
stage.condition.command, stage.condition.command,
+15
View File
@@ -1,4 +1,15 @@
import { model, Schema } from 'mongoose' 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( const Session = model(
'Session', 'Session',
@@ -28,6 +39,10 @@ const Session = model(
progress: String, progress: String,
completion: [String], completion: [String],
hints: [String], hints: [String],
graph: {
type: nodeSchema,
default: new FileNode({ path: '/', name: '/', type: 'folder' }),
},
}, },
{ timestamps: true } { timestamps: true }
) )