diff --git a/packages/game/src/Quest.ts b/packages/game/src/Quest.ts index d074791..cb6eca3 100644 --- a/packages/game/src/Quest.ts +++ b/packages/game/src/Quest.ts @@ -42,6 +42,7 @@ export class Quest { completed: string[] ): Promise { const stages = this.getActiveStages(completed) + let catchAllException: string | null = null for (const stage of stages) { const satisfies = await stage.satisfies(command, this.checker) if (satisfies) { @@ -50,7 +51,9 @@ export class Quest { } for (const exception of stages.flatMap((stage) => stage.exceptions)) { const condition = new Condition(exception.condition) - if (await condition.satisfies(command, this.checker)) { + if (exception.catchAll && !catchAllException) { + catchAllException = exception.id + } else if (await condition.satisfies(command, this.checker)) { return exception.id } } @@ -60,7 +63,7 @@ export class Quest { return exception.id } } - return null + return catchAllException } getResponses(completed: string[]): IResponse[] { diff --git a/packages/game/test/exceptions.test.ts b/packages/game/test/exceptions.test.ts index 316328d..bc0215b 100644 --- a/packages/game/test/exceptions.test.ts +++ b/packages/game/test/exceptions.test.ts @@ -76,6 +76,17 @@ describe('exceptions', () => { expect(await session.runCommand({ command: 'ls' })).toBe('stage2') }) + it('should return the catch all exception', async () => { + const session = new Session( + { completedStages: [] }, + quest, + new MockFileChecker() + ) + expect(await session.runCommand({ command: 'what is this' })).toBe( + 'exception2' + ) + }) + it('should return the global exception', async () => { const session = new Session( { completedStages: [] }, @@ -83,7 +94,7 @@ describe('exceptions', () => { new MockFileChecker() ) expect(await session.runCommand({ command: 'rm' })).toBe('exception3') - expect(await session.runCommand({ command: 'mv' })).toBeNull() + expect(await session.runCommand({ command: 'mv' })).toBe('exception2') // catch all session.complete('stage1') expect(await session.runCommand({ command: 'mv' })).toBe('exception4') })