mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: import file-graph package
* Update Dockerfile structure, copy quests, packages to sub folder
This commit is contained in:
1 parent
6c70a6eaea
commit
c3683ddaea
9 files changed
+64
-15
No files matched your search
@@ -1,7 +1,10 @@
|
||||
import minimist from 'minimist'
|
||||
import { FileGraph } from '@linux-odyssey/file-graph'
|
||||
import Quest from '../models/quest.js'
|
||||
import { pushToSession } from '../api/socket.js'
|
||||
|
||||
console.log(typeof FileGraph)
|
||||
|
||||
// 檢查 pattern 是否符合 input
|
||||
const checkMatch = (pattern, input) => {
|
||||
if (pattern.length === 0) return true
|
||||
@@ -11,6 +14,40 @@ const checkMatch = (pattern, input) => {
|
||||
})
|
||||
}
|
||||
|
||||
async function discoverHandler(session, commandInput, additionalData) {
|
||||
const graph = new FileGraph(session.graph)
|
||||
graph.discover(additionalData.discover)
|
||||
console.log('graph:', graph.toString())
|
||||
session.graph = graph.toJSON()
|
||||
await session.save()
|
||||
}
|
||||
|
||||
const pushGraph = (session, commandInput, additionalData) => {
|
||||
pushToSession(session.id, 'graph', {
|
||||
pwd: commandInput.pwd,
|
||||
discover: additionalData.discover,
|
||||
})
|
||||
}
|
||||
|
||||
const eventListenr = {
|
||||
add: [],
|
||||
remove: [],
|
||||
discover: [discoverHandler, pushGraph],
|
||||
pwd: [],
|
||||
}
|
||||
|
||||
function handleEvent(session, commandInput, additionalData) {
|
||||
const events = Object.keys(additionalData)
|
||||
events.forEach((event) => {
|
||||
const handlers = eventListenr[event]
|
||||
if (handlers) {
|
||||
handlers.forEach((handler) =>
|
||||
handler(session, commandInput, additionalData)
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function commandHandler(session, commandInput, additionalData) {
|
||||
const quest = await Quest.findById(session.quest)
|
||||
const stage = quest.stages.find((s) => s.id === session.progress)
|
||||
@@ -21,12 +58,8 @@ async function commandHandler(session, commandInput, additionalData) {
|
||||
|
||||
const argv = minimist(commandInput.command.split(' '))
|
||||
|
||||
if ('discover' in additionalData) {
|
||||
pushToSession(session.id, 'graph', {
|
||||
pwd: commandInput.pwd,
|
||||
discover: additionalData.discover,
|
||||
})
|
||||
} else if (argv._[0] === 'cd') {
|
||||
handleEvent(session, commandInput, additionalData)
|
||||
if (argv._[0] === 'cd') {
|
||||
pushToSession(session.id, 'graph', { pwd: commandInput.pwd })
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user