Skip to content

Commit

Permalink
Fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Jan 24, 2022
1 parent 56dcc44 commit 41ea84b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
30 changes: 27 additions & 3 deletions app/components/board/state/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export function handleKeyDown(letter: Letter, keyEvent: KeyboardEvent) {

export function handleInput(letter: Letter, keyEvent: KeyboardEvent) {
if (isMobile()) {
// Hack for old android
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
keyEvent.key = keyEvent.data;

return handleKeyDown(letter, keyEvent);
Expand Down Expand Up @@ -103,9 +106,15 @@ export function guess(attempt: Attempt, { answer, all, onError, onWin }: GuessOp
return;
}

let answerOccurances = getLetterOccurances(answer);

// Are any letters in the correct position?
for (let letter of attempt.letters) {
if (answer.includes(letter.value)) {
let occurances = answerOccurances[letter.value];

if (occurances) {
answerOccurances[letter.value]--;

letter.isInAnswer = true;

if (answer.indexOf(letter.value) === word.indexOf(letter.value)) {
Expand All @@ -117,9 +126,14 @@ export function guess(attempt: Attempt, { answer, all, onError, onWin }: GuessOp
attempt.isFrozen = true;

if (word === answer) {
console.log('Congratulations');
console.debug('Congratulations');

let element = document.activeElement;

if (element instanceof HTMLElement) {
element.blur();
}

document.activeElement?.blur();
onWin();

return;
Expand All @@ -130,3 +144,13 @@ export function guess(attempt: Attempt, { answer, all, onError, onWin }: GuessOp
focusNext();
});
}

function getLetterOccurances(word: string) {
let result: Record<string, number> = {};

for (let letter of word.split('')) {
result[letter] = (result[letter] || 0) + 1;
}

return result;
}
4 changes: 2 additions & 2 deletions app/components/board/success.gts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
class="
absolute inset-0 grid gap-8 items-center
justify-center p-4
bg-[rgba(255,255,255,0.7)]
bg-[rgba(255,255,255,0.8)]
"
>
<h3 class="text-4xl">Yay 🎉</h3>

<p class="font-bold">You dit it!</p>
<p class="font-bold">You did it!</p>
</div>
{{/if}}
</template>

0 comments on commit 41ea84b

Please sign in to comment.