-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
394 lines (348 loc) · 11.4 KB
/
app.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// wordlist and dictionary are included in the HTML
const guessGrid = document.querySelector('[data-guess-grid]');
const keyboard = document.querySelector('[data-keyboard]');
const alertContainer = document.querySelector('[data-alert-container]');
const modalContainer = document.querySelector('#modal-container');
const modalContent = document.querySelector('#modal-content');
const WORD_LENGTH = 5;
const DANCE_ANIMATION_DURATION = 1000;
const FLIP_ANIMATION_DELAY = 250;
const startingDate = new Date(2022, 0, 1);
const timeSinceDate = Date.now() - startingDate;
const wordIndex = Math.floor(timeSinceDate / 1000 / 3600 / 24) % wordlist.length;
const targetWord = wordlist[wordIndex];
console.log(`WWZ #${wordIndex} = %c${wordlist[wordIndex]}`, 'color: red; background: red; font-weight: bold;');
let outcomeString = '';
let guessCount = 0;
let gameOver = false;
// ----- Handle clicks for all objects -----
function handleClick(e) {
if (e.target.matches('[data-close-modal]')) {
closeModal();
return;
}
if (e.target.matches('[data-topic]')) {
openModal(e.target.dataset.topic);
return;
}
if (e.target.matches('[data-copy-link]')) {
copyTextToClipboard(e.target.dataset.copyLink);
}
// Ignore all other clicks after game ends
if (gameOver) return;
if (e.target.matches('[data-key]')) {
const key = e.target.dataset.key;
if(key === 'Z') {
console.error('Z clicked');
openModal('about');
return;
}
pressKey(key);
return;
}
if (e.target.matches('[data-enter]')) {
submitGuess();
return;
}
if (e.target.matches('[data-backspace]')) {
deleteLastLetter();
return;
}
}
// ----- Handle all keyboard inputs -----
function pressKey(key) {
// Adds the letter to the guess and update the display
const activeTiles = getActiveTiles();
if (activeTiles.length >= WORD_LENGTH) return;
const nextTile = guessGrid.querySelector(':not([data-letter])');
nextTile.dataset.letter = key.toLowerCase();
nextTile.textContent = key.toUpperCase();
nextTile.dataset.state = 'active';
}
function handleKeyPress(e) {
const key = e.key;
if (key === 'Enter') {
submitGuess();
return;
}
if (key === 'Backspace' || key === 'Delete') {
deleteLastLetter();
return;
}
if (key.match(/^[a-yA-Y]$/)) {
pressKey(key);
}
}
function submitGuess() {
// If a full row has been entered, check if it's correct
let submittedWord = '';
const activeTiles = [...getActiveTiles()];
if (activeTiles.length < WORD_LENGTH) {
showAlert('Not enough letters');
shakeTiles(activeTiles);
return;
}
for (let tile of activeTiles) {
submittedWord += tile.dataset.letter;
}
if (!dictionary.includes(submittedWord)) {
showAlert(`Not in wordlist`);
shakeTiles(activeTiles);
return;
}
stopInteraction();
const letterCount = {};
resetLetterCount(letterCount, targetWord);
const tileColors = getTileColors(letterCount, submittedWord, targetWord);
activeTiles.forEach((...params) => flipTile(...params, submittedWord, tileColors));
}
function deleteLastLetter() {
// Delete the last letter from the guess (current row only)
const activeTiles = getActiveTiles();
if (activeTiles.length === 0) return;
const lastTile = activeTiles[activeTiles.length - 1];
lastTile.removeAttribute('data-letter');
lastTile.textContent = '';
delete lastTile.dataset.state;
delete lastTile.dataset.letter;
}
// ----- Helper functions -----
function getActiveTiles() {
// Returns an array of all unsubmitted tiles on a line.
return guessGrid.querySelectorAll('[data-state="active"]');
}
function resetLetterCount(letterCount, word) {
for (let key in letterCount) { delete letterCount[key]; }
for (let letter of word) {
if (letter in letterCount) {
letterCount[letter]++;
} else {
letterCount[letter] = 1;
}
}
}
function getTileColors (letterCount, submittedWord, targetWord) {
let result = [];
let resultBoxes = [];
// First pass: find matching and incorrect letters
for (let i = 0; i < submittedWord.length; i++) {
const letter = submittedWord[i];
if (letter === targetWord[i]) {
result.push('match');
resultBoxes.push('🟦');
letterCount[letter]--;
} else if (!(letter in letterCount)) {
result.push('incorrect');
resultBoxes.push('⬛');
} else {
result.push('recheck');
resultBoxes.push('');
}
}
// Second pass: determine if remaining letters are in the wrong position
for (let i = 0; i < result.length; i++)
{
if(result[i] === 'recheck') {
const letter = submittedWord[i];
result[i] = (letterCount[letter] > 0) ? 'wrong-position' : 'incorrect';
resultBoxes[i] = (letterCount[letter] > 0) ? '🟨' : '⬛';
}
}
outcomeString += resultBoxes.join('');
outcomeString += '\n';
guessCount++;
return result;
}
function showAlert(message, duration = 1000) {
const alert = document.createElement('div');
alert.textContent = message;
alert.classList.add('alert');
alertContainer.prepend(alert);
setTimeout(() => {
alert.classList.add('hide');
// Wait for animation to finish
alert.addEventListener('transitionend', () => {
alert.remove();
});
}, duration);
}
function shakeTiles(tiles) {
for (let tile of tiles) {
tile.classList.add('shake');
tile.addEventListener('animationend', () => {
tile.classList.remove('shake');
}, { once: true });
}
}
// ----- Game Logic -----
function flipTile(tile, index, array, submittedWord, tileColors) {
const letter = tile.dataset.letter;
const key = keyboard.querySelector(`[data-key=${letter.toUpperCase()}]`);
setTimeout(() => {
tile.classList.add('flip');
}, index * FLIP_ANIMATION_DELAY);
tile.addEventListener('transitionend', () => {
tile.classList.remove('flip');
let status = tileColors[index];
key.classList.add(status);
tile.classList.add(status);
tile.dataset.state = status;
if (index === array.length - 1) {
tile.addEventListener('transitionend', () => {
startInteraction();
checkWinLose(submittedWord, array);
}, { once: true });
}
}, { once: true });
}
function checkWinLose(submittedWord, tiles) {
if (submittedWord === targetWord) {
showAlert(getWinToastText(), 5000);
danceTiles(tiles);
endGame(`Wordle Without Z\n#${wordIndex} (${guessCount}/6)\n${outcomeString}`);
return;
}
const remainingTiles = guessGrid.querySelectorAll(':not([data-letter])');
if (remainingTiles.length === 0) {
// showAlert(`${targetWord.toUpperCase()}`, null);
showAlert(`The word was ${targetWord.toUpperCase()}.`, 5000);
shakeTiles(tiles);
endGame(`Wordle Without Z\n#${wordIndex} (X/6)\n${outcomeString}`);
}
}
function getWinToastText() {
switch (guessCount) {
case 1:
return 'Unbelievable!! (Did you cheat?)';
case 2:
return 'Incredible!!';
case 3:
return 'Great Job!!';
case 4:
return 'Nice Work!';
case 5:
return 'You got it!';
case 6:
return 'Whew!';
default:
return `The word was ${targetWord.toUpperCase()}.`;
}
}
function danceTiles(tiles) {
for (let i = 0; i < tiles.length; i++) {
const tile = tiles[i];
setTimeout(() => {
tile.classList.add('dance');
tile.addEventListener('animationend', () => {
tile.classList.remove('dance');
}, { once: true });
}, (i * DANCE_ANIMATION_DURATION) / 5);
}
}
// ----- Modal functions -----
function openModal (topic) {
modalContent.innerHTML = `<div id="modal-snippet"><h3>Loading...</h3></div>`;
includeHTMLSnippet(document.getElementById('modal-snippet'), `${topic}.html`);
modalContainer.classList.add('show');
}
function closeModal() {
modalContainer.classList.remove('show');
}
function includeHTMLSnippet(targetElement, filename) {
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
targetElement.innerHTML =
this.responseText;
}
}
xhttp.open("GET", filename, true);
console.log(xhttp);
// FIXME: "The fetch event for http://.../links.html resulted in a network error response.
// an object that was not a Response was passed to respondWith()." (next line)
xhttp.send();
}
// Fallback function for browsers that don't support async clipboard API
// Only call if copyTextToClipboard() fails
function fallbackCopyTextToClipboard(text, displayToast) {
var textArea = document.createElement("textarea");
textArea.value = text;
// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Fallback: Copying text command was ' + msg);
if (displayToast) showAlert('Copied to clipboard', 1000);
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}
document.body.removeChild(textArea);
}
function copyTextToClipboard(text, displayToast = true) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text, displayToast);
return;
}
navigator.clipboard.writeText(text).then(function() {
console.log('Async: Copying to clipboard was successful!');
if (displayToast) showAlert('Copied');
}, function(err) {
console.error('Async: Could not copy text: ', err);
});
}
// ----- Interaction functions -----
function startInteraction() {
document.addEventListener('keydown', handleKeyPress);
document.addEventListener('click', handleClick);
}
function stopInteraction() {
document.removeEventListener('keydown', handleKeyPress);
document.removeEventListener('click', handleClick);
}
function endGame(shareString) {
gameOver = true;
document.removeEventListener('keydown', handleKeyPress);
copyTextToClipboard(shareString, false);
showAlert('Copying results to clipboard...', 2000);
console.log(shareString);
if (navigator.canShare && navigator.canShare()) console.log('%cNative sharing permitted', 'color: green');
if (navigator.canShare && !navigator.canShare()) console.log('Native sharing available, not permitted');
if (!navigator.canShare) console.log('%cNative sharing not supported. %cClipboard only.', 'color: red');
}
// ----- App installation functions -----
let bipEvent = null;
window.addEventListener("beforeinstallprompt", e => {
e.preventDefault();
bipEvent = e;
});
document.querySelector('.install-button').addEventListener('click', e => {
if (bipEvent) {
bipEvent.prompt();
console.log('Install clicked. User should install now.');
} else {
// Incompatible browser, PWA doesn't pass all criteria, or app already installed by user.
// TODO: Show user how to install the app.
alert(`To install the app, look for "Install" or "Add to Home Screen" in your browser's menu.`);
console.log('Install clicked. Unable to install.');
}
});
// ----- Adjust screen sizing for mobile -----
let vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty('--vh', `${vh}px`);
window.addEventListener('resize', () => {
let vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty('--vh', `${vh}px`);
});
// ----- Start the game -----
startInteraction();
// TODO:
// - Use pwabuilder.com to build the app for all online stores.
// - Test with https://developer.samsung.com/remotetestlab/
// - From iOS, use https://itest.nz/
// - Add screenshots for install dialog.