-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
249 lines (218 loc) · 10.1 KB
/
main.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Phrase data and generation function
import { phraseObject } from "./phrases.js";
import { resetPuzzle, startingGuessCount, guessAmount, money, dollarAmount, correctLetter, puzzleAnswerLettersArr, removeAlert, letterGuessDelete, puzzleElement, categoryElement, hintElement, bankrollElement, makeAGuessBtn, keyboardLettersArr, enterLetter, checkHighlight, potentialPurchaseElement, increaseCurrentGuessesBtn, revealHintBtn, backspaceBtn, enterBtn, checkPotentialPurchase } from "./functions.js";
/*=================================
GENERATED CONTENT FROM DAILY PHRASE
=================================*/
// add starting bankroll
bankrollElement.innerHTML = "$ " + money;
// setup initial puzzle
resetPuzzle(Object.values(phraseObject), puzzleElement, categoryElement, hintElement, bankrollElement);
let today = new Date();
console.log(today);
console.log(Date.now());
// Reset puzzle once per day
let dayInMilliseconds = 800000;
setInterval(resetPuzzle, dayInMilliseconds, Object.values(phraseObject), puzzleElement, categoryElement, hintElement, bankrollElement, makeAGuessBtn, keyboardLettersArr);
/*===============
USER INPUT ROUTES
===============*/
// click enter btn
enterBtn.addEventListener("click", enterLetter);
// various key events
document.addEventListener("keydown", function (event) {
// remove hint possiblity
if(!hintElement.classList.contains("visible")) {
revealHintBtn.classList.remove("hint-focus");
potentialPurchaseElement.innerHTML = "";
}
// remove add guess possibility
if(increaseCurrentGuessesBtn.classList.contains("guess-focus-default") || increaseCurrentGuessesBtn.classList.contains("guess-focus-one") || increaseCurrentGuessesBtn.classList.contains("guess-focus-zero")) {
increaseCurrentGuessesBtn.classList.remove("guess-focus-default");
increaseCurrentGuessesBtn.classList.remove("guess-focus-one");
increaseCurrentGuessesBtn.classList.remove("guess-focus-zero");
potentialPurchaseElement.innerHTML = "";
}
event.preventDefault();
if (event.key == "Enter") {
enterLetter();
}
else if (event.key == "Backspace") {
letterGuessDelete("delete");
}
else {
let letter = event.key;
if (/^[a-z]$/i.test(letter)) {
letterGuessDelete("letter", "keyboard", letter);
}
}
});
// guess mode toggle
makeAGuessBtn.addEventListener("click", function() {
// remove hint possiblity
if(!hintElement.classList.contains("visible")) {
revealHintBtn.classList.remove("hint-focus");
potentialPurchaseElement.innerHTML = "";
}
// remove add guess possibility
if(increaseCurrentGuessesBtn.classList.contains("guess-focus-default") || increaseCurrentGuessesBtn.classList.contains("guess-focus-one") || increaseCurrentGuessesBtn.classList.contains("guess-focus-zero")) {
increaseCurrentGuessesBtn.classList.remove("guess-focus-default");
increaseCurrentGuessesBtn.classList.remove("guess-focus-one");
increaseCurrentGuessesBtn.classList.remove("guess-focus-zero");
potentialPurchaseElement.innerHTML = "";
}
letterGuessDelete("guess");
});
// adding letters with click
for (let element of keyboardLettersArr) {
element.addEventListener("click", function() {
// remove hint possiblity
if(!hintElement.classList.contains("visible")) {
revealHintBtn.classList.remove("hint-focus");
potentialPurchaseElement.innerHTML = "";
}
// remove add guess possibility
if(increaseCurrentGuessesBtn.classList.contains("guess-focus-default") || increaseCurrentGuessesBtn.classList.contains("guess-focus-one") || increaseCurrentGuessesBtn.classList.contains("guess-focus-zero")) {
increaseCurrentGuessesBtn.classList.remove("guess-focus-default");
increaseCurrentGuessesBtn.classList.remove("guess-focus-one");
increaseCurrentGuessesBtn.classList.remove("guess-focus-zero");
potentialPurchaseElement.innerHTML = "";
}
letterGuessDelete("letter", element);
});
}
// deleting letters in guess mode
backspaceBtn.addEventListener("click", function() {
letterGuessDelete("delete");
});
/*===========================
ADDITONAL GUESS FUNCTIONALITY
===========================*/
increaseCurrentGuessesBtn.addEventListener("click", function() {
if(makeAGuessBtn.classList.contains("guess-mode")){
// remove current guesses
makeAGuessBtn.classList.remove("guess-mode");
makeAGuessBtn.classList.remove("guess-highlight");
for(let i = 0; i < puzzleAnswerLettersArr.length; i++) {
if (puzzleAnswerLettersArr[i].lastElementChild.classList.contains("added-guess-input")) {
puzzleAnswerLettersArr[i].removeChild(puzzleAnswerLettersArr[i].lastElementChild);
}
// remove highlighted box
if (puzzleAnswerLettersArr[i].classList.contains("guess-mode-current-guess-box")) {
puzzleAnswerLettersArr[i].classList.remove("guess-mode-current-guess-box");
}
}
}
// remove highlights from other entry possibilities
checkHighlight(keyboardLettersArr);
// remove hint possiblity
if(!hintElement.classList.contains("visible")) {
revealHintBtn.classList.remove("hint-focus");
potentialPurchaseElement.innerHTML = "";
}
if (startingGuessCount >= 2) {
increaseCurrentGuessesBtn.classList.toggle("guess-focus-default");
}
else if (startingGuessCount === 1) {
increaseCurrentGuessesBtn.classList.toggle("guess-focus-one");
}
else if (startingGuessCount === 0) {
increaseCurrentGuessesBtn.classList.toggle("guess-focus-zero");
}
if(increaseCurrentGuessesBtn.classList.contains("guess-focus-default") || increaseCurrentGuessesBtn.classList.contains("guess-focus-one") || increaseCurrentGuessesBtn.classList.contains("guess-focus-zero")) {
potentialPurchaseElement.innerHTML = "-150";
}
else {
potentialPurchaseElement.innerHTML = "";
}
});
/*===========================
HINT SETTINGS BUTTONS & STATS
===========================*/
revealHintBtn.addEventListener("click", function() {
// exit if hint provided
if (hintElement.classList.contains("visible")){
return;
}
if(makeAGuessBtn.classList.contains("guess-mode")){
// remove current guesses
makeAGuessBtn.classList.remove("guess-mode");
makeAGuessBtn.classList.remove("guess-highlight");
for(let i = 0; i < puzzleAnswerLettersArr.length; i++) {
if (puzzleAnswerLettersArr[i].lastElementChild.classList.contains("added-guess-input")) {
puzzleAnswerLettersArr[i].removeChild(puzzleAnswerLettersArr[i].lastElementChild);
}
// remove highlighted box
if (puzzleAnswerLettersArr[i].classList.contains("guess-mode-current-guess-box")) {
puzzleAnswerLettersArr[i].classList.remove("guess-mode-current-guess-box");
}
}
}
// remove add guess possibility
if(increaseCurrentGuessesBtn.classList.contains("guess-focus-default") || increaseCurrentGuessesBtn.classList.contains("guess-focus-one") || increaseCurrentGuessesBtn.classList.contains("guess-focus-zero")) {
increaseCurrentGuessesBtn.classList.remove("guess-focus-default");
increaseCurrentGuessesBtn.classList.remove("guess-focus-one");
increaseCurrentGuessesBtn.classList.remove("guess-focus-zero");
potentialPurchaseElement.innerHTML = "";
}
// remove highlights from other entry possibilities
checkHighlight(keyboardLettersArr);
revealHintBtn.classList.toggle("hint-focus");
checkPotentialPurchase(150);
if (revealHintBtn.classList.contains("hint-focus")) {
potentialPurchaseElement.innerHTML = "-150";
}
else {
potentialPurchaseElement.innerHTML = "";
}
// make visible
});
// toggle dark and light mode
let toggleTheme = document.querySelector(".theme-btn");
let storedTheme = localStorage.getItem('theme') || (window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");
if (storedTheme) {
document.documentElement.setAttribute('data-theme', storedTheme)
if (storedTheme === "light") {
document.querySelector(".moon").style.display = "none";
document.querySelector(".sun").style.display = "inline-block";
}
}
toggleTheme.onclick = function() {
let currentTheme = document.documentElement.getAttribute("data-theme");
let targetTheme = "light";
if (currentTheme === "light") {
targetTheme = "dark";
document.querySelector(".moon").style.display = "inline-block";
document.querySelector(".sun").style.display = "none";
}
else {
document.querySelector(".moon").style.display = "none";
document.querySelector(".sun").style.display = "inline-block";
}
document.documentElement.setAttribute('data-theme', targetTheme)
localStorage.setItem('theme', targetTheme);
};
// toggle how to play
let toggleHowToPlay = document.querySelector(".how-to-play-btn");
toggleHowToPlay.onclick = function() {
document.querySelector(".overlay").classList.toggle("game-play");
setTimeout(function() {
document.querySelector(".overlay").classList.toggle("fade");
}, 20);
setTimeout(function() {
document.querySelector(".overlay-content").classList.toggle("translate");
}, 10);
}
let closeHowToPlay = document.querySelector(".close-settings");
closeHowToPlay.onclick = function() {
document.querySelector(".overlay").classList.toggle("fade");
document.querySelector(".overlay").classList.toggle("game-play");
document.querySelector(".overlay-content").classList.toggle("translate");
}
// toggle game-stats popup
let toggleGameStats = document.querySelector(".close-stats");
toggleGameStats.onclick = function() {
document.querySelector(".overlay-game-win").classList.toggle("game-play");
document.querySelector(".overlay-game-win").classList.toggle("fade");
document.querySelector(".game-stats-overlay").classList.toggle("translate");
}