From fd862580bfd3993b3efdb247bd0df2358c2e2556 Mon Sep 17 00:00:00 2001 From: Szymon Tondowski Date: Thu, 30 May 2024 21:04:35 +0200 Subject: [PATCH] Better statuses --- src/api/getWordReport.ts | 2 + src/components/Keyboard/VirualKeyboard.tsx | 2 +- src/components/Words/Affix.tsx | 2 +- src/components/Words/Words.tsx | 6 +- src/store/selectors.ts | 72 +++++++++++++++++----- 5 files changed, 66 insertions(+), 18 deletions(-) diff --git a/src/api/getWordReport.ts b/src/api/getWordReport.ts index 376b9e885..8ec807ff0 100644 --- a/src/api/getWordReport.ts +++ b/src/api/getWordReport.ts @@ -114,12 +114,14 @@ const getFlatAffixes = (affixes: Affix[]) => { if (firstAffix.type === AffixStatus.Correct && firstAffix.isStart) { flatAffixes.start = firstAffix.text; } else { + // } else if (firstAffix.isStart) { flatAffixes.notStart.push(firstAffix.text[0]); } if (lastAffix.type === AffixStatus.Correct && lastAffix.isEnd) { flatAffixes.end = lastAffix.text; } else { + // } else if (lastAffix.isEnd) { flatAffixes.notEnd.push(lastAffix.text[lastAffix.text.length - 1]); } diff --git a/src/components/Keyboard/VirualKeyboard.tsx b/src/components/Keyboard/VirualKeyboard.tsx index 0cfd0351d..afe13fd4f 100644 --- a/src/components/Keyboard/VirualKeyboard.tsx +++ b/src/components/Keyboard/VirualKeyboard.tsx @@ -23,7 +23,7 @@ const VirualKeyboard = () => { const isSmallKeyboard = useSelector(state => state.app.isSmallKeyboard); const { keyLinesToUse, allowedKeys } = useSelector(selectGameLanguageKeyboardInfo); const isGameEnded = useSelector(selectIsGameEnded); - const type = useSelector(selectKeyboardState); + const { status: type } = useSelector(selectKeyboardState); const [isConfirmOpen, setIsConfirmOpen] = useState(false); const { vibrateKeyboard, vibrateKeyboardIncorrect } = useVibrate(); diff --git a/src/components/Words/Affix.tsx b/src/components/Words/Affix.tsx index 92433b4f3..0f33cf1d5 100644 --- a/src/components/Words/Affix.tsx +++ b/src/components/Words/Affix.tsx @@ -12,7 +12,7 @@ const Affix = ({ }: AffixType) => { const wordToSubmit = useSelector(selectWordToSubmit); const keyCapType = useSelector(selectLetterState(text)); - const keyboardState = useSelector(selectKeyboardState); + const { status: keyboardState } = useSelector(selectKeyboardState); const flatAffixes = useSelector(state => state.game.flatAffixes); const isKnownIncorrectTyped = keyCapType === AffixStatus.Incorrect; diff --git a/src/components/Words/Words.tsx b/src/components/Words/Words.tsx index baf5e92c1..ea87d034e 100644 --- a/src/components/Words/Words.tsx +++ b/src/components/Words/Words.tsx @@ -36,7 +36,7 @@ const Words = () => { const isProcessing = useSelector(selectIsProcessing); const wordToSubmit = useSelector(selectWordToSubmit); const wordStatus = useSelector(selectWordState(wordToSubmit)); - const keyboardStatus = useSelector(selectKeyboardState); + const { status: keyboardStatus, details } = useSelector(selectKeyboardState); const caretShift = useSelector(state => state.game.caretShift); const hasSpace = wordToSubmit.includes(' '); const isIncorrectType = [ @@ -49,6 +49,10 @@ const Words = () => { AffixStatus.IncorrectEnd, ].includes(keyboardStatus); + console.log({ + keyboardStatus, details, + }) + const { t } = useTranslation(); const submitGuess: WordType = useMemo(() => { diff --git a/src/store/selectors.ts b/src/store/selectors.ts index 5f9d5f1c4..fc2c99bde 100644 --- a/src/store/selectors.ts +++ b/src/store/selectors.ts @@ -305,12 +305,14 @@ export const selectKeyboardState = createSelector( selectFlatAffixes, (wordToGuess, wordToSubmit, incorrectLetter, positionLetters, flatAffixes) => { if (!wordToSubmit || !wordToSubmit.replaceAll(' ', '')) { - return AffixStatus.Unknown; + return { + status: AffixStatus.Unknown, + }; } const uniqueWordLetters = [...(new Set(wordToSubmit.split('')))].filter(letter => letter !== ' '); - const hasIncorrectLetterTyped = uniqueWordLetters.some((uniqueLetter) => { + const incorrectTyppedLetters = uniqueWordLetters.filter((uniqueLetter) => { const isIncorrect = incorrectLetter[uniqueLetter] > 0; if (!isIncorrect) { return false; @@ -326,15 +328,23 @@ export const selectKeyboardState = createSelector( return true; }); + const hasIncorrectLetterTyped = incorrectTyppedLetters.length > 0; if (hasIncorrectLetterTyped) { - return AffixStatus.Incorrect; + return { + status: AffixStatus.Incorrect, + details: incorrectTyppedLetters.join(', '), + }; } + // TODO: add info at the end if no special letter typed const hasWordToGuessSpecialCharacters = wordToGuess && getHasSpecialCharacters(wordToGuess); const hasWordToSubmitSpecialCharacters = wordToSubmit && getHasSpecialCharacters(wordToSubmit); const specialCharacterTypedWhenNotNeeded = !hasWordToGuessSpecialCharacters && hasWordToSubmitSpecialCharacters; if (specialCharacterTypedWhenNotNeeded) { - return AffixStatus.Incorrect; + return { + status: AffixStatus.Incorrect, + details: [], // TODO: add special + }; } const uniqueRequiredLetters = Object.keys(positionLetters); @@ -348,32 +358,64 @@ export const selectKeyboardState = createSelector( if (flatAffixes) { const isWrongStart = !wordToSubmit.startsWith(flatAffixes.start); if (isWrongStart) { - return AffixStatus.IncorrectStart; - } if (flatAffixes.notStart.includes(wordToSubmit[0])) { - return AffixStatus.IncorrectStart; + return { + status: AffixStatus.IncorrectStart, + details: flatAffixes.start, + }; + } + + if (flatAffixes.notStart.includes(wordToSubmit[0])) { + return { + status: AffixStatus.IncorrectStart, + details: wordToSubmit[0], + }; } const isWrongEnd = !wordToSubmit.endsWith(flatAffixes.end); if (isWrongEnd) { - return AffixStatus.IncorrectEnd; + return { + status: AffixStatus.IncorrectEnd, + details: flatAffixes.end, + }; + } + + if (flatAffixes.notEnd.includes(wordToSubmit[wordToSubmit.length - 1])) { + return { + status: AffixStatus.IncorrectEnd, + details: wordToSubmit[wordToSubmit.length - 1], + }; } - const isWrongMiddle = flatAffixes.middle.some(flatAffix => !wordToSubmit.includes(flatAffix)); + const wrongMiddles = flatAffixes.middle.filter(flatAffix => !wordToSubmit.includes(flatAffix)); + const isWrongMiddle = wrongMiddles.length > 0; if (isWrongMiddle) { - return AffixStatus.IncorrectMiddle; + return { + status: AffixStatus.IncorrectMiddle, + details: wrongMiddles.join(', '), + }; } - const isWrongOrder = !flatAffixes.correctOrders.some(order => getIsTextMatchingOrder(wordToSubmit, order)); - if (isWrongOrder) { - return AffixStatus.IncorrectOrder; + if (flatAffixes.correctOrders.length > 0) { + const wrongOrders = flatAffixes.correctOrders.filter(order => !getIsTextMatchingOrder(wordToSubmit, order)); + const isWrongOrder = wrongOrders.length > 0; + if (isWrongOrder) { + return { + status: AffixStatus.IncorrectOrder, + details: wrongOrders.join(', '), + }; + } } } - return AffixStatus.Correct; + return { + status: AffixStatus.Correct, + }; } // If not all known letters are typed, we know that the word is incorrect, but we don't display it. - return AffixStatus.Unknown; + return { + status: AffixStatus.Unknown, + }; }, );