Skip to content

Commit

Permalink
created a numerical hierarchy for status (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelQuinones authored Oct 14, 2023
1 parent f0ed484 commit a66ce8d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/components/Keyboard/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import type { Guess, IKeyboardStore } from '$lib/types';
import type { CharStatus, Guess, IKeyboardStore } from '$lib/types';
import { writable } from 'svelte/store';
/**
* By converting the statuses into numbers,
* we can create a hierarchy that can be logically tested to determine the most correct status
*
* The idea being that the highest number is the most correct
*/
function translateHierarchy(status?: CharStatus) {
if (status === 'absent') return 1;
if (status === 'present') return 2;
if (status === 'correct') return 3;
return 0;
}

function createKeyboardStore() {
const init = { letterStatus: new Map(), disabled: false };
Expand All @@ -12,7 +24,11 @@ function createKeyboardStore() {
setLetterStatus: (guess: Guess) => {
update(({ letterStatus, ...rest }) => {
guess.letters.forEach((char, i) => {
if (letterStatus.get(char) !== 'correct') letterStatus.set(char, guess.statuses[i]);
const currentStatus = translateHierarchy(letterStatus.get(char));
const incomingStatus = translateHierarchy(guess.statuses[i]);
if (incomingStatus > currentStatus) {
letterStatus.set(char, guess.statuses[i]);
}
});
return { letterStatus, ...rest };
});
Expand Down

0 comments on commit a66ce8d

Please sign in to comment.