Skip to content

Commit

Permalink
Order checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Deykun committed May 26, 2024
1 parent 0e1d68a commit 3f9d82e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/api/getWordReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const getFlatAffixes = (affixes: Affix[]) => {
start: '',
notStart: [],
middle: [],
correctOrders: [],
notEnd: [],
end: '',
};
Expand Down Expand Up @@ -127,6 +128,11 @@ const getFlatAffixes = (affixes: Affix[]) => {
&& affix.isEnd !== true
&& affix.text.length > 1).map(affix => affix.text);

const order = affixes.filter(affix => affix.type === AffixStatus.Correct).map(({ text }) => text);
if (order.length >= 2) {
flatAffixes.correctOrders = [order];
}

return flatAffixes;
};

Expand Down Expand Up @@ -211,6 +217,7 @@ export const getWordReportForMultipleWords = async (
start: '',
notStart: [],
middle: [],
correctOrders: [],
notEnd: [],
end: '',
},
Expand Down
2 changes: 2 additions & 0 deletions src/api/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ export const mergeFlatAffixes = (flatAffixesA: FlatAffixes, flatAffixesB: FlatAf
}
});

flatAffixesResult.correctOrders = [...flatAffixesResult.correctOrders, ...flatAffixesB.correctOrders];

console.log(flatAffixesResult);

return flatAffixesResult;
Expand Down
5 changes: 5 additions & 0 deletions src/components/Words/Words.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const Words = () => {
].includes(wordStatus) || [
AffixStatus.IncorrectStart,
AffixStatus.IncorrectMiddle,
AffixStatus.IncorrectOrder,
AffixStatus.IncorrectEnd,
].includes(keyboardStatus);

Expand Down Expand Up @@ -83,6 +84,10 @@ const Words = () => {
return 'game.youCanUseIncorrectMiddle';
}

if (keyboardStatus === AffixStatus.IncorrectOrder) {
return 'game.youCanUseIncorrectOrder';
}

if (keyboardStatus === AffixStatus.IncorrectEnd) {
return 'game.youCanUseIncorrectEnd';
}
Expand Down
5 changes: 3 additions & 2 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@
"game.itSpecialCharacters": "special",
"game.plSpecialCharacter": "special",
"game.plSpecialCharacters": "special",
"game.youCanUseIncorrectLetters": "Your word <strong>can't</strong> win, but you can use it while searching for the winning word.",
"game.youCanUseLettersTypedTooManyTimes": "You can use this word, but <strong>can't</strong> win with it.<br />(a correct letter used too many times)",
"game.youCanUseIncorrectLetters": "Your word <strong>can't</strong> win, but you can use it while searching for the winning word.<br />(an incorrect typped)",
"game.youCanUseLettersTypedTooManyTimes": "You can use this word, but <strong>can't</strong> win with it.<br />(a correct letter used too many times)",
"game.youCanUseSpace": "Words <strong>don't</strong> have spaces, but you can use them (they will be removed).",
"game.youCanUseIncorrectStart": "You can use this word, but <strong>can't</strong> win with it.<br />(incorrect start of the word)",
"game.youCanUseIncorrectEnd": "You can use this word, but <strong>can't</strong> win with it.<br />(incorrect ending of the word)",
"game.youCanUseIncorrectMiddle": "You can use this word, but <strong>can't</strong> win with it.<br />(some letters should be adjacent to each other)",
"game.youCanUseIncorrectOrder": "You can use this word, but <strong>can't</strong> win with it.<br />(some letters do not match the known order)",
"game.spacesRemoved": "The spaces were removed.",
"game.isNotInDictionary": "Word not found in the dictionary.",
"game.wordAlreadyUsed": "The word had already been used.",
Expand Down
2 changes: 1 addition & 1 deletion src/locales/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"game.frSpecialCharacters": "specialne",
"game.plSpecialCharacter": "polski",
"game.plSpecialCharacters": "polskich",
"game.youCanUseIncorrectLetters": "Twoje słowo <strong>nie może</strong> być hasłem, ale możesz go użyć szukając hasła.",
"game.youCanUseIncorrectLetters": "Twoje słowo <strong>nie może</strong> być hasłem, ale możesz go użyć szukając hasła.<br />(niepoprawna litera użyta w słowie)",
"game.youCanUseLettersTypedTooManyTimes": "Możesz użyć tego słowa, ale <strong>nie może</strong> ono być hasłem.<br />(poprawna litera użyta zbyt wiele razy)",
"game.youCanUseSpace": "Hasła <strong>nie mają</strong> spacji, ale możesz jej używać (zostanie usunięta).",
"game.spacesRemoved": "Usunięto spacje.",
Expand Down
2 changes: 2 additions & 0 deletions src/store/gameSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const initialState: RootGameState = {
start: '',
notStart: [],
middle: [],
correctOrders: [],
notEnd: [],
end: '',
},
Expand All @@ -95,6 +96,7 @@ const resetGame = (state: RootGameState, wordToGuess: string) => {
start: '',
notStart: [],
middle: [],
correctOrders: [],
notEnd: [],
end: '',
};
Expand Down
30 changes: 28 additions & 2 deletions src/store/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,29 @@ export const selectWordState = (word: string) => createSelector(
},
);

const getIsTextMatchingOrder = (text: string, order: string[]) => {
const {
isMatching,
} = order.reduce((stack, subtext) => {
if (!stack.restString.includes(subtext)) {
return {
restString: '',
isMatching: false,
};
}

const [, ...rest] = stack.restString.split(subtext);
stack.restString = rest.join('');

return stack;
}, {
restString: text,
isMatching: true,
});

return isMatching;
};

export const selectKeyboardState = createSelector(
selectWordToGuess,
selectWordToSubmit,
Expand Down Expand Up @@ -322,8 +345,6 @@ export const selectKeyboardState = createSelector(
});

if (allKnownLettersAreTyped) {
console.log(flatAffixes);

if (flatAffixes) {
const isWrongStart = !wordToSubmit.startsWith(flatAffixes.start);
if (isWrongStart) {
Expand All @@ -341,6 +362,11 @@ export const selectKeyboardState = createSelector(
if (isWrongMiddle) {
return AffixStatus.IncorrectMiddle;
}

const isWrongOrder = !flatAffixes.correctOrders.some(order => getIsTextMatchingOrder(wordToSubmit, order));
if (isWrongOrder) {
return AffixStatus.IncorrectOrder;
}
}

return AffixStatus.Correct;
Expand Down
2 changes: 2 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export enum AffixStatus {
IncorrectStart = 'incorrect-start',
IncorrectEnd = 'incorrect-end',
IncorrectMiddle = 'incorrect-middle',
IncorrectOrder = 'incorrect-order',
}

export type Affix = {
Expand Down Expand Up @@ -257,6 +258,7 @@ export type FlatAffixes = {
start: string,
notStart: string[],
middle: string[],
correctOrders: string[][],
notEnd: string[],
end: string,
};
Expand Down

0 comments on commit 3f9d82e

Please sign in to comment.