Skip to content

Commit

Permalink
Better statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
Deykun committed May 30, 2024
1 parent 3f9d82e commit fd86258
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/api/getWordReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/Keyboard/VirualKeyboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/components/Words/Affix.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/components/Words/Words.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -49,6 +49,10 @@ const Words = () => {
AffixStatus.IncorrectEnd,
].includes(keyboardStatus);

console.log({
keyboardStatus, details,
})

const { t } = useTranslation();

const submitGuess: WordType = useMemo(() => {
Expand Down
72 changes: 57 additions & 15 deletions src/store/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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,
};
},
);

Expand Down

0 comments on commit fd86258

Please sign in to comment.