-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnections.js
305 lines (257 loc) · 7.53 KB
/
connections.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
const FIRST_CONNECTIONS_DATE = '06-12-2023';
var todaysGameIndex = 0; // today's game's index
var currentGameIndex = 0; // current game's index
var groups; // initialized in click events
var dateArray = [];
// reset the helper wrapper for debugging purposes
let helperWrapper = document.querySelector('#nyt-games-helper');
if (helperWrapper) {
helperWrapper.remove();
}
let intervalId;
let status;
initBoardGroups(); // fetch JSON with today's board data
attachCustomDiv(); // async init of our link bar
document.querySelector('head').insertAdjacentHTML('beforeend',`
<style>
#top-text {
display: none;
}
#nyt-games-helper {
text-align: center;
}
#nyt-games-helper a {
text-decoration: underline;
padding: 10px;
display: inline-block;
}
#status-wrapper {
padding: 10px;
display: block;
}
#status {
border-radius: 10px;
padding: 7px;
background: none;
transition: all .4s easeOutExpo;
}
#status.updated {
background: #eee;
}
</style>`);
function attachCustomDiv() {
intervalId = setInterval(() => {
let board = document.querySelector('#pz-game-root > article > section:last-of-type');
if (board) {
clearTimeout(intervalId);
init();
}
}, 500);
}
function init() {
document.querySelector('#pz-game-root > article > section:last-of-type').insertAdjacentHTML('afterend', '<div id="nyt-games-helper"></div>');
helperWrapper = document.querySelector('#nyt-games-helper');
helperWrapper.insertAdjacentHTML('beforeend', '<a href="#" id="free-guess">Free guess</a>');
helperWrapper.insertAdjacentHTML('beforeend', '<a href="#" id="reveal-group">Reveal next group</a>');
helperWrapper.insertAdjacentHTML('beforeend', '<a href="#" id="random-group">Reveal random group</a>');
helperWrapper.insertAdjacentHTML('beforeend', '<div id="status-wrapper"><span id="status"></span></div>');
status = document.querySelector('#status');
/**
* Make a free guess with the current selection.
*/
helperWrapper.querySelector('#free-guess').addEventListener('click', (event) => {
event.preventDefault();
let selected = getSelectedItems();
let selectedElements = document.querySelectorAll('input[checked]');
let selectedCells = [];
selectedElements.forEach((element) => {
selectedCells.push(element.parentNode);
});
if (selected.length < 4) {
updateStatus('Select 4 items to make a guess.');
return;
}
for(let i=0; i<groups.length; i++) {
let cards = [];
groups[i].cards.forEach((card) => {
cards.push(card.content);
});
cards.sort();
if (equalArrays(cards, selected)) {
updateStatus('Current selection is a match!');
// class can be found via animations.css
animate(selectedCells, 'solved-pulse', 500);
return;
} else {
// check how many match
let overlap = arrayIntersect(cards, selected);
if (overlap.length === 3) {
let item = arrayDiff(cards, selected);
updateStatus('3 overlap - ' + item);
let element = Array.from(
document.querySelectorAll('input[checked]')
).find(el => {
return el.value.trim() == item
});
animate([element.parentNode], 'short-bounce', 500);
return;
}
}
};
// output the overlap message
animate(selectedCells, 'invalid-shake', 500);
updateStatus('No significant overlap');
});
/**
* Output the next easiest group
*/
helperWrapper.querySelector('#reveal-group').addEventListener('click', (event) => {
event.preventDefault();
let answerBanner0 = getAnswerBannerWithText(groups[0].title);
let answerBanner1 = getAnswerBannerWithText(groups[1].title);
let answerBanner2 = getAnswerBannerWithText(groups[2].title);
let answerBanner3 = getAnswerBannerWithText(groups[3].title);
let nextGroup = '';
if (!answerBanner0) {
nextGroup = groups[0].title;
} else if (!answerBanner1) {
nextGroup = groups[1].title;
} else if (!answerBanner2) {
nextGroup = groups[2].title;
} else if (!answerBanner3) {
nextGroup = groups[3].title;
}
updateStatus('Next group: ' + nextGroup);
});
/**
* Output a random unsolved group
*/
helperWrapper.querySelector('#random-group').addEventListener('click', (event) => {
event.preventDefault();
let answerBanner0 = getAnswerBannerWithText(groups[0].title);
let answerBanner1 = getAnswerBannerWithText(groups[1].title);
let answerBanner2 = getAnswerBannerWithText(groups[2].title);
let answerBanner3 = getAnswerBannerWithText(groups[3].title);
let possibleKeys = [];
if (!answerBanner0) {
possibleKeys.push(groups[0].title);
}
if (!answerBanner1) {
possibleKeys.push(groups[1].title);
}
if (!answerBanner2) {
possibleKeys.push(groups[2].title);
}
if (!answerBanner3) {
possibleKeys.push(groups[3].title);
}
let randomGroup = possibleKeys[Math.floor(Math.random() * possibleKeys.length)];
updateStatus('Random group: ' + randomGroup);
});
}
/**
* Initialize today's board groups by fetching the same JSON their JS uses
*/
function initBoardGroups() {
let today = new Date();
let dd = String(today.getDate()).padStart(2, '0');
let mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
let yyyy = today.getFullYear();
let dateString = yyyy + '-' + mm + '-' + dd;
fetch('https://www.nytimes.com/svc/connections/v2/'+dateString+'.json')
.then((res) => res.json())
.then((res) => {
groups = res.categories;
});
}
function getBoardData() {
let today = new Date();
let dd = String(today.getDate()).padStart(2, '0');
let mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
let yyyy = today.getFullYear();
let dateString = yyyy + '-' + mm + '-' + dd;
fetch('https://www.nytimes.com/svc/connections/v2/'+dateString+'.json')
.then((res) => res.json())
.then((res) => {
return res.categories;
});
}
function getAnswerBannerWithText(text) {
for (const a of document.querySelectorAll("h3[class^='SolvedCategory']")) {
if (a.textContent.includes(text)) {
return a;
}
}
}
/**
* Check if 2 arrays have the same values in the same order
*
* @param arr1 array
* @param arr2 array
* @return bool
*/
function equalArrays(arr1, arr2) {
return JSON.stringify(arr1) == JSON.stringify(arr2);
}
/**
* Get the intersection of values between 2 arrays
*
* @param arr1 array
* @param arr2 array
* @return array of intersecting items
*/
function arrayIntersect(arr1, arr2) {
const setA = new Set(arr1);
return arr2.filter(value => setA.has(value));
}
/**
* Get the difference of values between 2 arrays
*
* @param arr1 array
* @param arr2 array
* @return array of intersecting items
*/
function arrayDiff(arr1, arr2) {
const setA = new Set(arr1);
return arr2.filter(value => !setA.has(value));
}
/**
* Get all currently selected items in alphabetical order
*
* @return array of strings
*/
function getSelectedItems() {
// get the 4 selections, sort alphabetically
let selected = document.querySelectorAll('input[checked]');
let items = [];
selected.forEach((item) => {
items.push(item.value);
});
items.sort();
return items;
}
/**
* Update the status text and toggle the .updated class to animate the background
*/
function updateStatus(text) {
status.innerText = text;
status.classList.add('updated');
setTimeout(() => {
status.classList.remove('updated');
}, 300);
}
/**
* Toggle an animation class for a group of elements
*
* @param elements to toggle the class on
* @param cssClass to toggle
* @param duration How long until the class should be removed
*/
function animate(elements, cssClass, duration) {
elements.forEach((element) => {
element.classList.add(cssClass);
setTimeout(() => {
element.classList.remove(cssClass);
}, duration);
});
}