-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
560 lines (465 loc) · 14.9 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
const tags_list = [
"2-sat",
"binary search",
"bitmasks",
"brute force",
"chinese remainder theorem ",
"combinatorics",
"constructive algorithms",
"data structures",
"dfs and similar",
"divide and conquer",
"dp",
"dsu",
"expression parsing",
"fft",
"flows",
"games",
"geometry",
"graph matchings",
"graphs",
"greedy",
"hashing",
"implementation",
"interactive",
"math",
"matrices",
"meet-in-the-middle",
"number theory",
"probabilities",
"schedules",
"shortest paths",
"sortings",
"string suffix structures ",
"strings",
"ternary search",
"trees",
"two pointers",
];
function toggleHelp() {
let helpList = document.getElementById("help-list");
if (helpList.style.display === "none") {
helpList.style.display = "block";
} else {
helpList.style.display = "none";
}
}
document.getElementById("expand-help").addEventListener("click", toggleHelp);
function disableBtn(button) {
button.disabled = true;
}
function enableBtn(button) {
button.disabled = false;
}
const green = "rgb(51, 172, 113)";
const blue = "rgb(0, 188, 212)";
function tags() {
let needed_html = "";
tags_list.forEach((tag) => {
needed_html += `<button class="tag-btn">${tag}</button>`;
});
return needed_html;
}
function dummy_data() {
let [from, to, problems_cnt] =
document.getElementsByClassName("another-class");
from.value = "800";
to.value = "800";
problems_cnt.value = "2";
add_handle("Mohab_Yaser");
document.getElementById("handles").value =
"Mohab_Yaser,Mostafa__Fouad,JOE002";
let btns = document.getElementsByClassName("tag-btn");
btns[21].style.backgroundColor = green;
btns[23].style.backgroundColor = green;
btns[26].style.backgroundColor = green;
let problem_container = `
<div class="problem">
<button class="code-btn tooltip">
Copy Code
</button>
<button class="tags-btn tooltip">
Click to see tags
</button>
<button class="rate-btn tooltip">
Click to see rating
</button>
<a target="_blank" href="https://codeforces.com/problemset/problem/">
Go to problem
</a>
</div>`;
document.getElementsByClassName("problems-container")[0].innerHTML +=
problem_container;
document.getElementsByClassName("problems-container")[0].innerHTML +=
problem_container;
document.getElementsByClassName("problems-container")[0].innerHTML +=
problem_container;
document.getElementsByClassName("problems-container")[0].innerHTML +=
problem_container;
document.getElementsByClassName("problems-container")[0].innerHTML +=
problem_container;
document.getElementsByClassName("problems-container")[0].innerHTML +=
problem_container;
document.getElementsByClassName("problems-container")[0].innerHTML +=
problem_container;
document.getElementsByClassName("problems-container")[0].innerHTML +=
problem_container;
document.getElementsByClassName("problems-container")[0].innerHTML +=
problem_container;
document.getElementsByClassName("problems-container")[0].innerHTML +=
problem_container;
}
document
.getElementsByClassName("storage-reset")[0]
.addEventListener("click", () => {
localStorage.clear();
});
document
.getElementsByClassName("storage-save")[0]
.addEventListener("click", () => {
let tags_btns = document.getElementsByClassName("tag-btn");
let tags = "";
for (let i = 0; i < tags_btns.length; i++) {
let tag = tags_btns[i];
if (tag.style.backgroundColor === green) {
tags += i.toString() + ",";
}
}
tags = tags.slice(0, -1);
let [from, to, problems_cnt] =
document.getElementsByClassName("another-class");
from = from.value;
to = to.value;
problems_cnt = problems_cnt.value;
let found_handles = document.getElementsByClassName(
"accepted-handle tooltip"
);
let handles = "";
for (let i = 0; i < found_handles.length; i++) {
handles += found_handles[i].innerText + ",";
}
handles = handles.slice(0, -1);
localStorage.setItem("tags", tags);
localStorage.setItem("from", from);
localStorage.setItem("to", to);
localStorage.setItem("problems_cnt", problems_cnt);
localStorage.setItem("handles", handles);
});
function load_from_local_storage() {
let tags_btns = document.getElementsByClassName("tag-btn");
let tags = localStorage.getItem("tags");
if (tags === null) return;
tags = localStorage.getItem("tags").split(",");
if (tags.length > 1) {
for (let i = 0; i < tags.length; i++) {
tags_btns[+tags[i]].style.backgroundColor = green;
}
}
let [from, to, problems_cnt] =
document.getElementsByClassName("another-class");
from.value = localStorage.getItem("from");
to.value = localStorage.getItem("to");
problems_cnt.value = localStorage.getItem("problems_cnt");
let handles = localStorage.getItem("handles").trim().split(",");
for (let i = 0; i < handles.length; i++) {
if (handles[i] != "") add_handle(handles[i]);
}
}
window.addEventListener("load", () => {
document.getElementsByClassName("tags-container")[0].innerHTML = tags();
let btns = document.getElementsByClassName("tag-btn");
for (let i = 0; i < btns.length; i++) {
let element = btns[i];
element.style.backgroundColor = blue;
element.addEventListener("click", () => {
if (element.style.backgroundColor == blue) {
element.style.backgroundColor = green;
} else {
element.style.backgroundColor = blue;
}
});
}
load_from_local_storage();
// dummy_data();
});
function http_request(url) {
let response = fetch(url)
.then((res) => {
if (!res.ok) return false;
else return res.json();
})
.then((json) => json);
return response;
}
async function valid_handle(handle) {
let verdict = await http_request(
`https://codeforces.com/api/user.info?handles=${handle}`
);
return verdict;
}
function add_handle(handle) {
if (document.getElementsByClassName("accepted-handle tooltip").length === 0)
document.getElementsByClassName("accepted-handles")[0].style.display =
"block";
document.getElementsByClassName(
"accepted-handles"
)[0].innerHTML += `<div class="accepted-handle tooltip">
${handle}
</div>`;
document.getElementById("handles").value = "";
let acc_handles = document.getElementsByClassName("accepted-handle tooltip");
for (let i = 0; i < acc_handles.length; i++) {
acc_handles[i].addEventListener("click", (element) => {
element.srcElement.remove();
if (
document.getElementsByClassName("accepted-handle tooltip").length === 0
)
document.getElementsByClassName("accepted-handles")[0].style.display =
"none";
});
}
}
function already_entered(handle) {
let already_entered_handles = document.getElementsByClassName(
"accepted-handle tooltip"
);
for (let i = 0; i < already_entered_handles.length; i++) {
if (already_entered_handles[i].innerText == handle) return true;
}
return false;
}
document
.getElementsByClassName("add-handle-btn")[0]
.addEventListener("click", async () => {
disableBtn(document.getElementsByClassName("add-handle-btn")[0]);
let handles = document.getElementById("handles").value;
handles = handles.split(",");
handles = new Set(handles);
handles.forEach(async (handle) => {
handle = handle.trim();
if (handle === "")
Swal.fire({
icon: "error",
title: "Oops...",
text: "Please Enter a handle!",
});
else {
const valid = await valid_handle(handle);
if (!valid) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Please Enter a valid handle!",
});
} else if (already_entered(handle)) {
Swal.fire({
icon: "error",
title: "Oops...",
text: `You have already entered the handle ${handle} before`,
});
} else add_handle(handle);
}
});
enableBtn(document.getElementsByClassName("add-handle-btn")[0]);
});
function validate_input() {
let [from, to, problems_cnt] =
document.getElementsByClassName("another-class");
from = +from.value;
to = +to.value;
problems_cnt = +problems_cnt.value;
if (from % 100 !== 0 || to % 100 !== 0 || from > to) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Please Enter a valid ratings!",
});
return false;
} else if (from > 3500 || from < 800 || to > 3500 || to < 800) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Please Enter a valid rating boundaries (800 - 3500)!",
});
return false;
} else if (problems_cnt < 1 || problems_cnt > 50) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Enter a valid number of problems (1 - 50)!",
});
return false;
} else return true;
}
function valid_problem(problem, min, max, problems_out_of_scope, chosen_tags) {
if (problems_out_of_scope.has(problem["name"])) return false;
let rate = problem["rating"] !== undefined ? problem["rating"] : 0;
if (rate < min || rate > max) return false;
problem["tags"].forEach((tag) => {
if (!chosen_tags.has(tag)) return false;
});
let len = problem["tags"].length;
if (len === 0) return false;
for (let i = 0; i < len; i++) {
let tag = problem["tags"][i];
if (!chosen_tags.has(tag)) return false;
}
return true;
}
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
async function get_problems() {
let not_solved_by = [];
let acc_handles = document.getElementsByClassName("accepted-handle tooltip");
for (let i = 0; i < acc_handles.length; i++) {
not_solved_by.push(acc_handles[i].innerText);
}
let problems_out_of_scope = new Set();
for (let handle of not_solved_by) {
let url = `https://codeforces.com/api/user.status?handle=${handle}`;
let submissions = await http_request(url);
submissions["result"].forEach((submission) => {
if (submission["verdict"] === "OK") {
problems_out_of_scope.add(submission["problem"]["name"]);
}
});
}
let chosen_tags = new Set();
let all_tags = document.getElementsByClassName("tag-btn");
for (let i = 0; i < all_tags.length; i++) {
if (all_tags[i].style.backgroundColor === green)
chosen_tags.add(all_tags[i].innerText);
}
if (chosen_tags.size === 0) chosen_tags = new Set(tags_list);
let problemset = await http_request(
"https://codeforces.com/api/problemset.problems"
);
let available_problems = [];
let [from, to, problems_cnt] =
document.getElementsByClassName("another-class");
from = +from.value;
to = +to.value;
problems_cnt = +problems_cnt.value;
let min = Math.max(800, from);
let max = Math.min(3500, to);
problemset["result"]["problems"].forEach((problem) => {
if (valid_problem(problem, min, max, problems_out_of_scope, chosen_tags))
available_problems.push(problem);
});
available_problems = shuffle(available_problems);
if (available_problems.length === 0) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "There are not sufficient problems on the site with these criteria",
});
return false;
}
let final_problems = available_problems.slice(
0,
Math.min(problems_cnt, available_problems.length)
);
if (available_problems.length < problems_cnt) {
Swal.fire({
icon: "warning",
title: "Limited Results",
text: `Only ${available_problems.length} problems are available, but you requested ${problems_cnt} problems. Please adjust your filters or check back later.`,
});
}
return final_problems;
}
function view_problems(problems) {
document.getElementsByClassName("problems-container")[0].style.display =
"grid";
let showed_tags = [];
let showed_ratings = [];
let showed_codes = [];
for (let i = 0; i < problems.length; i++) {
let problem_container = `
<div class="problem">
<button class="code-btn tooltip">
Copy Code
</button>
<button class="tags-btn tooltip">
Click to see tags
</button>
<button class="rate-btn tooltip">
Click to see rating
</button>
<a target="_blank" href="https://codeforces.com/problemset/problem/${problems[i]["contestId"]}/${problems[i]["index"]}">
Go to problem
</a>
</div>`;
showed_tags.push(problems[i]["tags"]);
showed_ratings.push(problems[i]["rating"]);
showed_codes.push(`${problems[i]["contestId"]}${problems[i]["index"]}`);
document.getElementsByClassName("problems-container")[0].innerHTML +=
problem_container;
}
let tags_btns = document.getElementsByClassName("tags-btn tooltip");
for (let i = 0; i < tags_btns.length; i++) {
let btn = tags_btns[i];
btn.addEventListener("click", () => {
Swal.fire({
title: showed_tags[i],
});
});
}
let ratings_btns = document.getElementsByClassName("rate-btn tooltip");
for (let i = 0; i < ratings_btns.length; i++) {
let btn = ratings_btns[i];
btn.addEventListener("click", () => {
Swal.fire(showed_ratings[i].toString());
});
}
let codes_btns = document.getElementsByClassName("code-btn tooltip");
for (let i = 0; i < codes_btns.length; i++) {
let btn = codes_btns[i];
btn.addEventListener("click", () => {
// Check if clipboard API is supported [requires HTTPS]
if (navigator.clipboard) {
navigator.clipboard.writeText(showed_codes[i]);
}
// Fallback is the solution for browsers without clipboard API or insecure connections
else {
const textArea = document.createElement("textarea");
textArea.value = showed_codes[i];
document.body.appendChild(textArea);
textArea.select();
document.execCommand("copy");
document.body.removeChild(textArea);
}
Swal.fire({
title: "Code Copied",
icon: "success",
});
});
}
}
function remove_old_problems() {
document.getElementsByClassName("problems-container")[0].innerHTML = "";
document.getElementsByClassName("problems-container")[0].style.display =
"none";
}
document
.getElementsByClassName("gen-btn")[0]
.addEventListener("click", async () => {
disableBtn(document.getElementsByClassName("gen-btn")[0]);
if (validate_input() === true) {
remove_old_problems();
let problems = await get_problems();
if (problems !== false) await view_problems(problems);
}
enableBtn(document.getElementsByClassName("gen-btn")[0]);
});
document.querySelector("#github span").addEventListener("click", () => {
window.open("https://github.com/Mohab96", "_blank");
});
document.querySelector("#github img").addEventListener("click", () => {
window.open("https://github.com/Mohab96", "_blank");
});