feat: check global exceptions

This commit is contained in:
Lin, Hung-Shin committed 2023-11-26 19:20:30 +08:00
1 parent 98a226f3ff
commit a7e646c28b
2 files changed
+38 -22

No files matched your search

+34 -16
View File
@@ -56,7 +56,7 @@ export default class CommandHandler extends SessionHandler {
this.session.graph = graph
} catch (e) {
logger.error('Update file graph failed', {
error: e,
error: e.message,
session: this.session.id,
graph: this.session.graph,
discover: this.params.discover,
@@ -122,9 +122,9 @@ export default class CommandHandler extends SessionHandler {
async run() {
try {
this.quest = await Quest.findById(this.session.quest)
const stages = this.getStages()
const stages = this.getUnlockedComponents(this.quest.stages)
if (stages.length === 0) {
logger.warn('stage not found', this.session.tasks)
logger.warn('stage not found', { session: this.session.id })
return {}
}
@@ -136,27 +136,45 @@ export default class CommandHandler extends SessionHandler {
)
const stage = stages.find((_, i) => matches[i])
if (!stage) {
for (const s of stages) {
if (stage) {
const response = this.execute(stage)
return {
stage: stage.id,
end: this.session.status === 'finished',
...response,
}
}
// stage exception
for (const s of stages) {
// eslint-disable-next-line no-await-in-loop
const exception = await this.checkException(s)
if (exception) {
return this.executeException(exception)
}
}
// global exception
console.log(this.quest.exceptions)
if (this.quest.exceptions.length > 0) {
const globalExceptions = this.getUnlockedComponents(
this.quest.exceptions
)
for (const exception of globalExceptions) {
// eslint-disable-next-line no-await-in-loop
const exception = await this.checkException(s)
if (exception) {
if (await this.isMatch(exception.condition)) {
return this.executeException(exception)
}
}
return {}
}
const response = this.execute(stage)
return {
stage: stage.id,
end: this.session.status === 'finished',
...response,
}
return {}
} catch (err) {
logger.error('CommandHandler error', {
error: err,
session: this.session.id,
error: err.message,
session: this.session._id,
quest: this.quest._id,
command: this.commandInput,
})
return {}
+4 -6
View File
@@ -19,16 +19,14 @@ export default class SessionHandler {
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))
getUnlockedComponents(components) {
return components.filter((component) =>
component.requirements.every((r) => this.isCompleted({ id: r }))
)
}
getNewTasks() {
const stages = this.getStages()
const stages = this.getUnlockedComponents(this.quest.stages)
return stages
.filter((s) => s.task)
.filter((s) => !this.session.tasks.some((t) => t.id === s.id))