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
+37 -21

No files matched your search

+33 -15
View File
@@ -56,7 +56,7 @@ export default class CommandHandler extends SessionHandler {
this.session.graph = graph this.session.graph = graph
} catch (e) { } catch (e) {
logger.error('Update file graph failed', { logger.error('Update file graph failed', {
error: e, error: e.message,
session: this.session.id, session: this.session.id,
graph: this.session.graph, graph: this.session.graph,
discover: this.params.discover, discover: this.params.discover,
@@ -122,9 +122,9 @@ export default class CommandHandler extends SessionHandler {
async run() { async run() {
try { try {
this.quest = await Quest.findById(this.session.quest) this.quest = await Quest.findById(this.session.quest)
const stages = this.getStages() const stages = this.getUnlockedComponents(this.quest.stages)
if (stages.length === 0) { if (stages.length === 0) {
logger.warn('stage not found', this.session.tasks) logger.warn('stage not found', { session: this.session.id })
return {} return {}
} }
@@ -136,16 +136,7 @@ export default class CommandHandler extends SessionHandler {
) )
const stage = stages.find((_, i) => matches[i]) const stage = stages.find((_, i) => matches[i])
if (!stage) { if (stage) {
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)
}
}
return {}
}
const response = this.execute(stage) const response = this.execute(stage)
return { return {
@@ -153,10 +144,37 @@ export default class CommandHandler extends SessionHandler {
end: this.session.status === 'finished', end: this.session.status === 'finished',
...response, ...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
if (await this.isMatch(exception.condition)) {
return this.executeException(exception)
}
}
}
return {}
} catch (err) { } catch (err) {
logger.error('CommandHandler error', { logger.error('CommandHandler error', {
error: err, error: err.message,
session: this.session.id, session: this.session._id,
quest: this.quest._id,
command: this.commandInput, command: this.commandInput,
}) })
return {} return {}
+4 -6
View File
@@ -19,16 +19,14 @@ export default class SessionHandler {
return task ? task.completed : false return task ? task.completed : false
} }
getStages() { getUnlockedComponents(components) {
return this.quest.stages.filter( return components.filter((component) =>
(stage) => component.requirements.every((r) => this.isCompleted({ id: r }))
stage.requirements.every((r) => this.isCompleted({ id: r })) &&
(stage.repeatable || !this.isCompleted(stage))
) )
} }
getNewTasks() { getNewTasks() {
const stages = this.getStages() const stages = this.getUnlockedComponents(this.quest.stages)
return stages return stages
.filter((s) => s.task) .filter((s) => s.task)
.filter((s) => !this.session.tasks.some((t) => t.id === s.id)) .filter((s) => !this.session.tasks.some((t) => t.id === s.id))