mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
refactor: seperate SessionHandler from commandHandler
This commit is contained in:
1 parent
a8147ecfa1
commit
97b0fb0f1d
3 files changed
+57
-49
No files matched your search
@@ -18,10 +18,10 @@ function removeFromSession(sessionId, event, callback) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function pushToSession(sessionId, ...args) {
|
export function pushToSession(sessionId, event, ...args) {
|
||||||
const callbacks = sessions.get(sessionId)
|
const callbacks = sessions.get(sessionId)
|
||||||
if (callbacks) {
|
if (callbacks) {
|
||||||
callbacks.forEach((callback) => callback(...args))
|
callbacks.forEach((callback) => callback(event, ...args))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,8 +68,8 @@ async function connectClient(socket) {
|
|||||||
stream.socket.write(message)
|
stream.socket.write(message)
|
||||||
})
|
})
|
||||||
|
|
||||||
const socketCallback = (event, data) => {
|
const socketCallback = (event, ...data) => {
|
||||||
socket.emit(event, data)
|
socket.emit(event, ...data)
|
||||||
}
|
}
|
||||||
|
|
||||||
listenToSession(session.id, socketCallback)
|
listenToSession(session.id, socketCallback)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import minimist from 'minimist'
|
|||||||
import { FileGraph } from '@linux-odyssey/file-graph'
|
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'
|
||||||
|
import SessionHandler from './sessionHandler.js'
|
||||||
|
|
||||||
// 檢查 pattern 是否符合 input
|
// 檢查 pattern 是否符合 input
|
||||||
const checkMatch = (pattern, input) => {
|
const checkMatch = (pattern, input) => {
|
||||||
@@ -12,9 +13,9 @@ const checkMatch = (pattern, input) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class CommandHandler {
|
export default class CommandHandler extends SessionHandler {
|
||||||
constructor(session, commandInput, additionalData) {
|
constructor(session, commandInput, additionalData) {
|
||||||
this.session = session
|
super(session)
|
||||||
this.commandInput = commandInput
|
this.commandInput = commandInput
|
||||||
this.argv = minimist(this.commandInput.command.split(' '))
|
this.argv = minimist(this.commandInput.command.split(' '))
|
||||||
|
|
||||||
@@ -52,23 +53,6 @@ export default class CommandHandler {
|
|||||||
this.session.graph = graph
|
this.session.graph = graph
|
||||||
}
|
}
|
||||||
|
|
||||||
getTask(stage) {
|
|
||||||
return this.session.tasks.find((t) => t.id === stage.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
isCompleted(stage) {
|
|
||||||
const task = this.getTask(stage)
|
|
||||||
return task ? task.completed : false
|
|
||||||
}
|
|
||||||
|
|
||||||
getStages() {
|
|
||||||
return this.quest.stages.filter(
|
|
||||||
(stage) =>
|
|
||||||
stage.requirements.every((r) => this.isCompleted({ id: r })) &&
|
|
||||||
(stage.repeatable || !this.isCompleted(stage))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
isMatch(stage) {
|
isMatch(stage) {
|
||||||
const keys = ['command', 'output', 'error', 'pwd']
|
const keys = ['command', 'output', 'error', 'pwd']
|
||||||
return keys.every((k) =>
|
return keys.every((k) =>
|
||||||
@@ -76,32 +60,6 @@ export default class CommandHandler {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
execute(stage) {
|
|
||||||
this.session.tasks
|
|
||||||
.filter((t) => t.id === stage.id)
|
|
||||||
.forEach((t) => {
|
|
||||||
t.completed = true
|
|
||||||
})
|
|
||||||
this.session.hints.push(...stage.hints)
|
|
||||||
if (stage.id === 'END') {
|
|
||||||
this.session.status = 'finished'
|
|
||||||
this.session.finishedAt = new Date()
|
|
||||||
}
|
|
||||||
const stages = this.getStages()
|
|
||||||
console.debug(stages)
|
|
||||||
const newTasks = stages
|
|
||||||
.filter((s) => s.task)
|
|
||||||
.map((s) => ({ id: s.id, name: s.task, completed: false }))
|
|
||||||
|
|
||||||
this.session.tasks.push(...newTasks)
|
|
||||||
pushToSession(this.session.id, 'tasks', this.session.tasks)
|
|
||||||
return {
|
|
||||||
responses: stage.responses,
|
|
||||||
hints: stage.hints,
|
|
||||||
tasks: newTasks,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async run() {
|
async run() {
|
||||||
this.quest = await Quest.findById(this.session.quest)
|
this.quest = await Quest.findById(this.session.quest)
|
||||||
const stages = this.getStages()
|
const stages = this.getStages()
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
import { pushToSession } from '../api/socket.js'
|
||||||
|
|
||||||
|
export default class SessionWrapper {
|
||||||
|
constructor(session) {
|
||||||
|
this.session = session
|
||||||
|
}
|
||||||
|
|
||||||
|
getTask(stage) {
|
||||||
|
return this.session.tasks.find((t) => t.id === stage.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
isCompleted(stage) {
|
||||||
|
const task = this.getTask(stage)
|
||||||
|
return task ? task.completed : false
|
||||||
|
}
|
||||||
|
|
||||||
|
getStages() {
|
||||||
|
return this.quest.stages.filter(
|
||||||
|
(stage) =>
|
||||||
|
stage.requirements.every((r) => this.isCompleted({ id: r })) &&
|
||||||
|
(stage.repeatable || !this.isCompleted(stage))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
execute(stage) {
|
||||||
|
this.session.tasks
|
||||||
|
.filter((t) => t.id === stage.id)
|
||||||
|
.forEach((t) => {
|
||||||
|
t.completed = true
|
||||||
|
})
|
||||||
|
this.session.hints.push(...stage.hints)
|
||||||
|
if (stage.id === 'END') {
|
||||||
|
this.session.status = 'finished'
|
||||||
|
this.session.finishedAt = new Date()
|
||||||
|
}
|
||||||
|
const stages = this.getStages()
|
||||||
|
console.debug(stages)
|
||||||
|
const newTasks = stages
|
||||||
|
.filter((s) => s.task)
|
||||||
|
.map((s) => ({ id: s.id, name: s.task, completed: false }))
|
||||||
|
|
||||||
|
this.session.tasks.push(...newTasks)
|
||||||
|
pushToSession(this.session.id, 'tasks', this.session.tasks)
|
||||||
|
return {
|
||||||
|
responses: stage.responses,
|
||||||
|
hints: stage.hints,
|
||||||
|
tasks: newTasks,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user