mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
refactor: refactor commandHandler to class
This commit is contained in:
1 parent
c3683ddaea
commit
3b8264e39e
2 files changed
+58
-49
No files matched your search
@@ -2,7 +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'
|
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)
|
||||||
@@ -48,7 +48,9 @@ export async function newCommand(req, res) {
|
|||||||
|
|
||||||
await c.save()
|
await c.save()
|
||||||
|
|
||||||
const response = await commandHandler(session, c, additionalData)
|
const commandHandler = new CommandHandler(session, c, additionalData)
|
||||||
|
|
||||||
|
const response = await commandHandler.run()
|
||||||
console.log(response)
|
console.log(response)
|
||||||
|
|
||||||
res.status(201).json(response)
|
res.status(201).json(response)
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ import { FileGraph } from '@linux-odyssey/file-graph'
|
|||||||
import Quest from '../models/quest.js'
|
import Quest from '../models/quest.js'
|
||||||
import { pushToSession } from '../api/socket.js'
|
import { pushToSession } from '../api/socket.js'
|
||||||
|
|
||||||
console.log(typeof FileGraph)
|
|
||||||
|
|
||||||
// 檢查 pattern 是否符合 input
|
// 檢查 pattern 是否符合 input
|
||||||
const checkMatch = (pattern, input) => {
|
const checkMatch = (pattern, input) => {
|
||||||
if (pattern.length === 0) return true
|
if (pattern.length === 0) return true
|
||||||
@@ -14,71 +12,81 @@ const checkMatch = (pattern, input) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function discoverHandler(session, commandInput, additionalData) {
|
export default class CommandHandler {
|
||||||
const graph = new FileGraph(session.graph)
|
constructor(session, commandInput, additionalData) {
|
||||||
graph.discover(additionalData.discover)
|
this.session = session
|
||||||
console.log('graph:', graph.toString())
|
this.commandInput = commandInput
|
||||||
session.graph = graph.toJSON()
|
this.additionalData = additionalData
|
||||||
await session.save()
|
this.eventListenr = {
|
||||||
|
discover: [this.discoverHandler, this.pushGraph],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const pushGraph = (session, commandInput, additionalData) => {
|
handleEvent() {
|
||||||
pushToSession(session.id, 'graph', {
|
const events = Object.keys(this.additionalData)
|
||||||
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) => {
|
events.forEach((event) => {
|
||||||
const handlers = eventListenr[event]
|
const handlers = this.eventListeners[event]
|
||||||
if (handlers) {
|
if (handlers) {
|
||||||
handlers.forEach((handler) =>
|
handlers.forEach((handler) => handler())
|
||||||
handler(session, commandInput, additionalData)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function commandHandler(session, commandInput, additionalData) {
|
async discoverHandler() {
|
||||||
const quest = await Quest.findById(session.quest)
|
const graph = new FileGraph(this.session.graph)
|
||||||
const stage = quest.stages.find((s) => s.id === session.progress)
|
graph.discover(this.additionalData.discover)
|
||||||
|
console.log('graph:', graph.toString())
|
||||||
|
this.session.graph = graph.toJSON()
|
||||||
|
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)
|
||||||
if (!stage) {
|
if (!stage) {
|
||||||
console.error('stage not found', session.progress)
|
console.error('stage not found', this.session.progress)
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const argv = minimist(commandInput.command.split(' '))
|
const argv = minimist(this.commandInput.command.split(' '))
|
||||||
|
|
||||||
handleEvent(session, commandInput, additionalData)
|
this.handleEvent()
|
||||||
if (argv._[0] === 'cd') {
|
if (argv._[0] === 'cd') {
|
||||||
pushToSession(session.id, 'graph', { pwd: commandInput.pwd })
|
pushToSession(this.session.id, 'graph', { pwd: this.commandInput.pwd })
|
||||||
}
|
}
|
||||||
|
|
||||||
const commandMatch = checkMatch(stage.condition.command, commandInput.command)
|
const commandMatch = checkMatch(
|
||||||
const outputMatch = checkMatch(stage.condition.output, commandInput.output)
|
stage.condition.command,
|
||||||
const errorMatch = checkMatch(stage.condition.error, commandInput.output)
|
this.commandInput.command
|
||||||
|
)
|
||||||
|
const outputMatch = checkMatch(
|
||||||
|
stage.condition.output,
|
||||||
|
this.commandInput.output
|
||||||
|
)
|
||||||
|
const errorMatch = checkMatch(
|
||||||
|
stage.condition.error,
|
||||||
|
this.commandInput.output
|
||||||
|
)
|
||||||
|
|
||||||
if (!commandMatch || !outputMatch || !errorMatch) return {}
|
if (!commandMatch || !outputMatch || !errorMatch) return {}
|
||||||
|
|
||||||
session.completion.push(session.progress)
|
this.session.completion.push(this.session.progress)
|
||||||
session.hints.push(...stage.hints)
|
this.session.hints.push(...stage.hints)
|
||||||
|
|
||||||
session.progress = stage.next
|
this.session.progress = stage.next
|
||||||
await session.save()
|
await this.session.save()
|
||||||
|
|
||||||
if (stage.next === 'END') {
|
if (stage.next === 'END') {
|
||||||
session.status = 'finished'
|
this.session.status = 'finished'
|
||||||
session.finishedAt = new Date()
|
this.session.finishedAt = new Date()
|
||||||
await session.save()
|
await this.session.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -87,5 +95,4 @@ async function commandHandler(session, commandInput, additionalData) {
|
|||||||
end: stage.next === 'END',
|
end: stage.next === 'END',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
export default commandHandler
|
|
||||||
Reference in new issue
Block a user