From a7e646c28b19a30d44b4f5f837d9dae2a1dcc626 Mon Sep 17 00:00:00 2001 From: "Lin, Hung-Shin" Date: Sun, 26 Nov 2023 19:20:30 +0800 Subject: [PATCH] feat: check global exceptions --- server/src/game/commandHandler.js | 50 +++++++++++++++++++++---------- server/src/game/sessionHandler.js | 10 +++---- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/server/src/game/commandHandler.js b/server/src/game/commandHandler.js index f45a839..9120b0a 100644 --- a/server/src/game/commandHandler.js +++ b/server/src/game/commandHandler.js @@ -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 {} diff --git a/server/src/game/sessionHandler.js b/server/src/game/sessionHandler.js index 2312c4c..a9b89f0 100644 --- a/server/src/game/sessionHandler.js +++ b/server/src/game/sessionHandler.js @@ -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))