-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
79 lines (77 loc) · 3.55 KB
/
script.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
import {
tags,
localize_tags
} from "./modules/tags.js"
import {
shuffle
} from "./modules/shuffle.js"
import {
disableAll,
enableAll
} from "./modules/ui.js"
document.getElementById('choose').innerHTML += tags.map(arg =>
`<option value="${arg.value}" title="${arg.title}">${arg.name} | ${arg.value}</option>`).join('')
start.onclick = async function() {
disableAll()
document.getElementById("name").innerHTML = "Идёт поиск задач..."
document.getElementById("rating").innerHTML = "Рейтинг задачи: [загрузка]"
document.getElementById("tags").innerHTML = "Темы: [загрузка]"
console.log("Check inputs...")
let min_value = document.getElementById('min_num').value,
max_value = document.getElementById('max_num').value,
tag_value = document.getElementById('choose').value
if (+(min_value) < 0 || min_value == "") {
min_value = 0
}
if (+(max_value) > 3800 || max_value == "") {
max_value = 3800
}
if (+min_value > +max_value) {
document.getElementById("name").innerHTML = ("Минимум не может быть больше максимума.")
document.getElementById("rating").innerHTML = "-____-"
document.getElementById("tags").innerHTML = "-____-"
console.log("Min > Max? :hmm:")
enableAll()
return -1
}
let json, link = "https://codeforces.com/api/problemset.problems?tags="
if (tag_value != "Choose tag") {
link += tag_value
}
let response = await fetch(link)
if (response.ok) {
console.log("Yeah, Codeforces is working...")
document.getElementById("name").innerHTML = "Осталось совсем немного..."
json = await response.json()
let problems = []
for (let i = json.result.problems.length - 1; i >= 0; i--) {
if (min_value <= json.result.problems[i].rating && json.result.problems[i].rating <= max_value) {
problems.push(json.result.problems[i])
}
}
problems = shuffle(problems)
if (!problems.length) {
document.getElementById("name").innerHTML = "Нет задачи по Вашим параметрам."
document.getElementById("rating").innerHTML = "Рейтинг задачи: :("
document.getElementById("tags").innerHTML = "Темы: :("
console.log("Nothing... :sob:")
} else {
let res = problems[0]
console.log(res)
document.getElementById("name").innerHTML = `<a href="${"https://codeforces.com/problemset/problem/" + res.contestId + "/" + res.index}"target="_blank">${res.name}</a>`
document.getElementById("rating").innerHTML = "Рейтинг задачи: " + ((res.rating != undefined) ? res.rating : "Неизвестно")
if (!document.getElementById('doNotShowTags').checked && res.tags.length) {
document.getElementById("tags").innerHTML = "Темы: " + res.tags.map((arg) => localize_tags(arg)).join(", ")
} else if (!res.tags.length) {
document.getElementById("tags").innerHTML = "Не найдено тем этой задачи"
} else {
document.getElementById("tags").innerHTML = "Темы мы не показываем :D"
}
console.log("We did it!!!")
}
} else {
document.getElementById("name").innerHTML = "Ошибка HTTP: " + response.status
console.log("Codeforces is down... F")
}
enableAll()
}