-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameHandler.mjs
61 lines (50 loc) · 1.66 KB
/
gameHandler.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as db from './dbHandler.mjs';
let dailyWord;
makeDailyWord();
async function makeDailyWord() {
// Gets todays date then creates an ID for it
const utcDate = (new Date());
let date = new Date(utcDate);
date.setTime(date.getTime() + 3600000); // Add one hour because utc time is wrong
date = date.toISOString().split('T')[0];
const year = date.split('-')[0];
const month = date.split('-')[1];
const day = date.split('-')[2];
const dayId = ((year * 365) + (month * 31) + day) % 2309; // 2309 is the amount of words in the database
const word = await db.findWord(dayId);
dailyWord = word.word;
}
export function checkWord(word) {
makeDailyWord();
const wordArr = word.split('');
const colors = [];
wordArr.forEach((letter, index) => {
colors.push(getTileStatus(letter, index, wordArr));
});
return colors;
}
// Takes a letter, the index of that letter, and the array of the currentWord being registered
function getTileStatus(typedLetter, index, wordArr) {
const letterInThatPosition = dailyWord.charAt(index);
const isCorrectPosition = typedLetter === letterInThatPosition;
const isCorrectLetter = dailyWord.includes(typedLetter);
// IF correct position, return correct
if (isCorrectPosition) {
return 'correct';
}
// Make sure only one square is "present" for multiple present letters
let firstLetter = true;
wordArr.forEach((countedLetter, i) => {
if (countedLetter === typedLetter) {
if (i < index) {
firstLetter = false;
}
}
});
// If the letter is in the word, return yellow
if (isCorrectLetter && firstLetter) {
return 'present';
}
// Otherwise, return grey
return 'absent';
}