From ee7cd083bd487add71129caa44ad387bebe388af Mon Sep 17 00:00:00 2001 From: wancat Date: Tue, 29 Aug 2023 13:28:18 +0800 Subject: [PATCH] feat: handle discover in server --- server/src/game/commandHandler.js | 50 ++++++++++++++++--------------- server/src/models/session.js | 15 ++++++++++ 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/server/src/game/commandHandler.js b/server/src/game/commandHandler.js index 39da0f2..62eb626 100644 --- a/server/src/game/commandHandler.js +++ b/server/src/game/commandHandler.js @@ -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, diff --git a/server/src/models/session.js b/server/src/models/session.js index 1ae266c..d56880d 100644 --- a/server/src/models/session.js +++ b/server/src/models/session.js @@ -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 } )