-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
396 lines (358 loc) · 14.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
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
395
396
"use strict";
// A word guessing game inspired by Wordle
// Copyright (C) 2022 Amir Livne Bar-on
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
function cyrb53(str) {
let h1 = 0xdeadbeef, h2 = 0x41c6ce57;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1>>>16), 2246822507) ^ Math.imul(h2 ^ (h2>>>13), 3266489909);
h2 = Math.imul(h2 ^ (h2>>>16), 2246822507) ^ Math.imul(h1 ^ (h1>>>13), 3266489909);
return 4294967296 * (2097151 & h2) + (h1>>>0);
};
function get_date() {
return new Date().toLocaleDateString('he-IL', {timeZone: 'Asia/Jerusalem'});
}
const HEBREW_KEYMAP = {
'e': 'ק', 'ק': 'ק', 'r': 'ר', 'ר': 'ר', 't': 'א', 'א': 'א', 'y': 'ט', 'ט': 'ט',
'u': 'ו', 'ו': 'ו', 'i': 'נ', 'ן': 'נ', 'o': 'מ', 'ם': 'מ', 'p': 'פ', 'פ': 'פ',
'a': 'ש', 'ש': 'ש', 's': 'ד', 'ד': 'ד', 'd': 'ג', 'ג': 'ג', 'f': 'כ', 'כ': 'כ',
'g': 'ע', 'ע': 'ע', 'h': 'י', 'י': 'י', 'j': 'ח', 'ח': 'ח', 'k': 'ל', 'ל': 'ל',
'l': 'כ', 'ך': 'כ', ';': 'פ', 'ף': 'פ', 'z': 'ז', 'ז': 'ז', 'x': 'ס', 'ס': 'ס',
'c': 'ב', 'ב': 'ב', 'v': 'ה', 'ה': 'ה', 'b': 'נ', 'נ': 'נ', 'n': 'מ', 'מ': 'מ',
'm': 'צ', 'צ': 'צ', ',': 'ת', 'ת': 'ת', '.': 'צ', 'ץ': 'צ'
};
const FINAL_LETTERS = {'ך': 'כ', 'ם': 'מ', 'ן': 'נ', 'ף': 'פ', 'ץ': 'צ'};
const FINALED_LETTERS = {'כ': 'ך', 'מ': 'ם', 'נ': 'ן', 'פ': 'ף', 'צ': 'ץ'};
const today = get_date();
const word_of_the_day =
today === '15.1.2022' ? 'כימאי' :
today === '16.1.2022' ? 'מעטפת' :
today === '17.1.2022' ? 'נקיות' :
today === '18.1.2022' ? 'חיסון' :
today === '19.1.2022' ? 'שתיקה' :
today === '20.1.2022' ? 'מבואר' :
today === '21.1.2022' ? 'יתגלה' :
today === '22.1.2022' ? 'רשאים' :
today === '23.1.2022' ? 'הוביל' :
today === '24.1.2022' ? 'זינוק' :
FREQUENT_WORDS[cyrb53('meduyeket ' + today) % FREQUENT_WORDS.length];
let guesses = [];
function un_finalize(word) {
return Array.from(word).map(function(letter) {
if (FINAL_LETTERS.hasOwnProperty(letter))
return FINAL_LETTERS[letter];
else
return letter;
}).join('');
}
function get_matches(guess, truth) {
guess = un_finalize(guess);
truth = un_finalize(truth);
const not_exact_matches = [];
for (let i = 0; i < 5; i++)
if (guess[i] !== truth[i])
not_exact_matches.push(truth[i]);
const matches = [];
for (let i = 0; i < 5; i++) {
if (guess[i] === truth[i]) {
matches.push('exact');
continue;
}
const index = not_exact_matches.indexOf(guess[i]);
if (index === -1) {
matches.push('wrong');
} else {
not_exact_matches.splice(index, 1);
matches.push('other');
}
}
return matches;
}
function set_modal_state() {
switch (history.state) {
case 'help':
document.getElementById('modal').classList.remove('hidden');
document.getElementById('help-screen').classList.remove('hidden');
document.getElementById('help-screen').scrollTop = 0;
document.getElementById('success-screen').classList.add('hidden');
break;
case 'success':
document.getElementById('modal').classList.remove('hidden');
document.getElementById('help-screen').classList.add('hidden');
document.getElementById('success-screen').classList.remove('hidden');
document.getElementById('success-header').innerText =
guesses[guesses.length - 1] === word_of_the_day ? 'כל הכבוד!' : 'לא הצליח הפעם';
const RTL_MARK = '\u200f';
const rows = guesses.map(function(guess) {
return RTL_MARK + get_matches(guess, word_of_the_day).map(function(match) {
return {exact: '🟩', other: '🟨', wrong: '⬜'}[match];
}).join('');
});
document.getElementById('result').innerHTML = `מדויקת ${today} - ${guesses[guesses.length - 1] === word_of_the_day ? guesses.length : 'X'}/6\n\n` + rows.join('\n');
countdown();
break;
default:
document.getElementById('modal').classList.add('hidden');
}
}
function show_help() {
if (history.state !== 'help') {
if (history.state === 'success')
history.replaceState('help', '');
else
history.pushState('help', '');
}
set_modal_state();
}
function show_success_screen() {
if (history.state !== 'success') {
if (history.state === 'help')
history.replaceState('success', '');
else
history.pushState('success', '');
}
set_modal_state();
}
let showed_failure_popup = false;
function copy_result(event) {
event.stopPropagation();
console.log(event.target.id === 'result');
navigator.clipboard.writeText(document.getElementById('result').innerHTML)
.then(function() {popup('התוצאה הועתקה, אפשר להדביק עם Ctrl+V');})
.catch(function() {
if (!showed_failure_popup || event.target.id !== 'result') {
showed_failure_popup = true;
popup('לא עבד, נסו לסמן את הטקסט ולהעתיק ידנית');
}
});
}
function countdown() {
if (document.getElementById('modal').classList.contains('hidden'))
return;
if (document.getElementById('success-screen').classList.contains('hidden'))
return;
if (get_date() !== today) {
document.getElementById('countdown').innerText = '0:00:00';
return;
}
const time_str = new Date().toLocaleTimeString('he-IL', {timeZone: 'Asia/Jerusalem', hourCycle: 'h23'});
const [hours, minutes, seconds] = time_str.split(':').map(function(x) {return parseInt(x);});
const since_midnight = 3600 * hours + 60 * minutes + seconds;
const to_midnight = 3600 * 24 - since_midnight;
document.getElementById('countdown').innerText =
`${Math.trunc(to_midnight / 3600)}:${two_digits((to_midnight % 3600) / 60)}:${two_digits(to_midnight % 60)}`;
window.setTimeout(countdown, 1000 - new Date().getMilliseconds());
}
function two_digits(x) {
x = Math.trunc(x);
if (x < 10)
return '0' + x.toString();
else
return x.toString();
}
function hide_modal() {
if (history.state === 'help' || history.state === 'success')
history.back();
set_modal_state();
}
function popup(text) {
document.getElementById('popup').classList.remove('hidden');
document.getElementById('popup').innerText = text;
window.setTimeout(function() {
document.getElementById('popup').classList.add('hidden');
}, 1500);
}
function type_letter(letter) {
const row = guesses.length + 1;
for (let i = 1; i <= 5; i++) {
const elt = document.getElementById(`letter-${row}-${i}`);
if (elt.innerText === '') {
elt.classList.add('full');
if (i === 5 && FINALED_LETTERS.hasOwnProperty(letter)) {
let previous = '';
for (let j = 1; j <= 4; j++)
previous += document.getElementById(`letter-${row}-${j}`).innerText;
if (WORDS.indexOf(previous + letter) !== -1)
elt.innerText = letter;
else
elt.innerText = FINALED_LETTERS[letter];
}
else
elt.innerText = letter;
break;
}
}
}
function erase_letter() {
const row = guesses.length + 1;
for (let i = 5; i >= 1; i--) {
const elt = document.getElementById(`letter-${row}-${i}`);
if (elt.innerText !== '') {
elt.classList.remove('full');
elt.innerText = '';
break;
}
}
}
function make_guess() {
const row = guesses.length + 1;
let guess = '';
for (let i = 1; i <= 5; i++) {
const elt = document.getElementById(`letter-${row}-${i}`);
guess += elt.innerText;
}
if (guess.length < 5) {
const row_elt = document.getElementById(`guess-${row}`);
row_elt.classList.add('jiggle');
window.setTimeout(function() {row_elt.classList.remove('jiggle');}, 2000);
popup('אין מספיק אותיות');
return;
}
if (WORDS.indexOf(guess) === -1) {
const row_elt = document.getElementById(`guess-${row}`);
row_elt.classList.add('jiggle');
window.setTimeout(function() {row_elt.classList.remove('jiggle');}, 2000);
popup('לא ברשימת המילים');
return;
}
const matches = get_matches(guess, word_of_the_day);
for (let i = 1; i <= 5; i++) {
const elt = document.getElementById(`letter-${row}-${i}`);
elt.classList.remove('full');
elt.classList.add(matches[i-1]);
}
guesses.push(guess);
save_to_local_storage();
if (guess === word_of_the_day) {
add_result_to_local_storage();
const row_elt = document.getElementById(`guess-${row}`);
row_elt.classList.add('win');
const CONGRATULATIONS = ['גאוני', 'מדהים', 'נפלא', 'סחתיין', 'נהדר', 'מקסים'];
popup(CONGRATULATIONS[guesses.length - 1]);
window.setTimeout(show_success_screen, 3600);
} else {
window.setTimeout(set_keyboard_key_colors, 100);
if (guesses.length === 6) {
add_result_to_local_storage();
document.getElementById('popup').classList.remove('hidden');
document.getElementById('popup').innerText = word_of_the_day;
window.setTimeout(show_success_screen, 2000);
}
}
}
function set_keyboard_key_colors() {
let letter_states = {};
for (const guess of guesses) {
if (guess !== word_of_the_day) {
const matches = get_matches(guess, word_of_the_day);
for (let i = 0; i < 5; i++) {
let letter = guess[i];
if (FINAL_LETTERS.hasOwnProperty(letter))
letter = FINAL_LETTERS[letter];
if (matches[i] === 'exact')
letter_states[letter] = 'exact';
else if (matches[i] === 'other' && letter_states[letter] !== 'exact')
letter_states[letter] = 'other';
else if (matches[i] === 'wrong' && !letter_states.hasOwnProperty(letter))
letter_states[letter] = 'wrong';
}
}
}
for (const elt of document.getElementsByClassName('key'))
if (!elt.classList.contains('wide'))
elt.setAttribute('match', letter_states[elt.innerText]);
}
function handle_key(key) {
if (guesses.length === 6)
return;
if (guesses.length > 0 && guesses[guesses.length - 1] === word_of_the_day)
return;
else if (key === 'Backspace')
erase_letter();
else if (key === 'Enter')
make_guess();
else if (HEBREW_KEYMAP.hasOwnProperty(key))
type_letter(HEBREW_KEYMAP[key]);
}
function handle_on_screen_keyboard_click(event) {
if (event.currentTarget.classList.contains('wide'))
handle_key(event.currentTarget.getAttribute('value'));
else
handle_key(event.currentTarget.innerText);
}
function save_to_local_storage() {
localStorage.setItem('date', today);
localStorage.setItem('guesses', JSON.stringify(guesses));
}
function add_result_to_local_storage() {
let results = localStorage.getItem('results');
if (results)
results = JSON.parse(results);
else
results = [];
results.push(guesses[guesses.length - 1] === word_of_the_day ? guesses.length : 'X');
localStorage.setItem('results', JSON.stringify(results));
}
function load_from_local_storage() {
const date = localStorage.getItem('date');
if (!date) {
show_help();
return;
}
if (date !== today) {
localStorage.removeItem('date');
localStorage.removeItem('guesses');
return;
}
guesses = JSON.parse(localStorage.getItem('guesses'));
for (let i = 0; i < guesses.length; i++) {
const guess = guesses[i];
const matches = get_matches(guess, word_of_the_day);
for (let j = 0; j < 5; j++) {
const elt = document.getElementById(`letter-${i+1}-${j+1}`);
elt.classList.add(matches[j]);
elt.innerText = guess[j];
}
}
if (guesses[guesses.length - 1] === word_of_the_day || guesses.length === 6) {
window.setTimeout(show_success_screen, 500);
}
set_keyboard_key_colors();
}
document.addEventListener('DOMContentLoaded', function () {
load_from_local_storage();
save_to_local_storage();
document.getElementById('help-button').addEventListener('click', show_help);
document.getElementById('share-button').addEventListener('click', copy_result);
document.getElementById('modal').addEventListener('click', hide_modal);
document.body.addEventListener('keydown', function(event) {
if (event.ctrlKey || event.altKey)
return;
if (event.key === '?')
show_help();
else if (event.key === 'Escape')
hide_modal();
else
handle_key(event.key);
});
for (const elt of document.getElementsByClassName('key'))
elt.addEventListener('click', handle_on_screen_keyboard_click);
set_modal_state();
window.addEventListener('popstate', set_modal_state);
});