From b2ca2ab33be6d9e8ffdc6d65dc4932f683bafb88 Mon Sep 17 00:00:00 2001 From: Maiko Tan Date: Wed, 27 Sep 2023 11:03:05 +0800 Subject: [PATCH] fix(core): state transformation should be put outside --- packages/core/src/index.ts | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index f56ec4b..d45ff79 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -72,32 +72,20 @@ export function defineVariation char === input[i])) { + } else if (currentWord.some((char) => char === input[i])) { unitResults.push({ type: 'bad-position', char }) } else { unitResults.push({ type: 'incorrect', char }) } } if (unitResults.every((result) => result.type === 'correct')) { - // game ended - sessionState.delete(`${session.guildId}.${session.channelId}`) - variation.onGameEnd?.(session, ctx) - state.state = undefined return { unitResults, type: 'correct' } } else { - sessionState.set(`${session.guildId}.${session.channelId}`, { - state: Wordle.GameState.Active, - currentWord: state.currentWord, - guessedWords: [...guessedWords, { unitResults, type: 'incorrect' }], - guessedCount: guessedCount + 1, - }) return { unitResults, type: 'incorrect' } } } @@ -154,10 +142,22 @@ export function defineVariation= variation.guessCount ?? 6) { + if (result.type === 'correct') { + // game ended + sessionState.delete(`${session.guildId}.${session.channelId}`) + variation.onGameEnd?.(session, ctx) + state.state = undefined + } else if (state.guessedCount >= variation.guessCount ?? 6) { await session.send(session?.text('wordle.messages.game-over', [command.name, state.currentWord.join('')])) variation.onGameEnd?.(session, ctx) sessionState.delete(`${session.guildId}.${session.channelId}`) + } else { + sessionState.set(`${session.guildId}.${session.channelId}`, { + state: Wordle.GameState.Active, + currentWord: state.currentWord, + guessedWords: [...(state.guessedWords ?? []), { unitResults: result.unitResults, type: 'incorrect' }], + guessedCount: state.guessedCount + 1, + }) } } else { return session?.text('wordle.messages.no-input') @@ -183,13 +183,13 @@ export function defineVariation { switch (unit.type) { case 'correct': - line += (`[${unit.char}]`) + line += `[${unit.char}]` break case 'bad-position': - line += (`(${unit.char})`) + line += `(${unit.char})` break case 'incorrect': - line += (` ${unit.char} `) + line += ` ${unit.char} ` break } })