-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
515 lines (479 loc) · 17.9 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
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
async function Fetch(url) {
return fetch(url).then(function (response) {
return response.json();
}).then(function (data) {
return data;
}).catch(function (err) {
console.log('Fetch problem : ' + err.message);
});
}
function speakText(msg) {
let speech = new SpeechSynthesisUtterance();
speech.text = msg;
speech.lang = "en-US";
speech.volume = 1;
speech.rate = 1;
speech.pitch = 1;
window.speechSynthesis.speak(speech);
}
let msgEle = document.getElementById("msg");
let headerEle = document.getElementById("header");
let userEle = document.getElementById("user");
let recInfo = document.getElementById("inf");
let probSet = document.getElementById("prob");
let ratVal = document.getElementById("rat");
let myChart = document.getElementById("myChart");
let countChart = null;
async function createInfo() {
msg.innerHTML = '';
headerEle.innerHTML = '';
headerEle.innerHTML = 'Enter Handle Details';
var ip = document.createElement("input");
var btn1 = document.createElement("button");
var btn2 = document.createElement("button");
ip.classList.add("form-control");
ip.setAttribute('type', 'text');
ip.setAttribute('placeholder', 'Enter Handle');
ip.style.width = 'fit-content';
ip.setAttribute('id', 'inf');
ip.style.marginTop = '10px';
btn1.innerText = 'Get Details';
btn1.classList.add("btn");
btn1.classList.add("btn-primary");
btn1.style.width = 'fit-content';
btn2.innerText = 'Analysis';
btn2.classList.add("btn");
btn2.classList.add("btn-primary");
btn2.style.width = 'fit-content';
btn2.style.backgroundColor = 'red';
btn2.style.borderColor = 'red';
btn1.type = "button"
btn2.type = "button"
headerEle.appendChild(ip);
headerEle.appendChild(btn1);
headerEle.appendChild(btn2);
btn1.onclick = getInfo;
btn2.onclick = getAnalytics;
}
async function createSub() {
msg.innerHTML = '';
headerEle.innerHTML = '';
headerEle.innerHTML = 'Enter Codeforces ID';
var ip = document.createElement("input");
var btn = document.createElement("button");
ip.style.marginTop = '10px';
ip.classList.add("form-control");
ip.setAttribute('type', 'text');
ip.setAttribute('placeholder', 'Enter Handle');
ip.style.width = 'fit-content';
ip.setAttribute('id', 'user');
btn.innerText = 'Get Details';
btn.classList.add("btn");
btn.classList.add("btn-primary");
btn.style.width = 'fit-content';
btn.type = "button"
headerEle.appendChild(ip);
headerEle.appendChild(btn);
btn.onclick = getSubmission;
}
async function createProb() {
msg.innerHTML = '';
headerEle.innerHTML = '';
headerEle.innerHTML = 'Enter Problem Details';
var ip1 = document.createElement("input");
var ip2 = document.createElement("input");
var btn = document.createElement("button");
ip1.style.marginTop = '10px';
ip1.classList.add("form-control");
ip1.setAttribute('type', 'text');
ip1.setAttribute('placeholder', 'Enter Tags , separated');
ip1.style.width = 'fit-content';
ip1.setAttribute('id', 'prob');
ip2.classList.add("form-control");
ip2.setAttribute('type', 'text');
ip2.setAttribute('placeholder', 'Enter Rating');
ip2.style.width = 'fit-content';
ip2.setAttribute('id', 'rat');
btn.innerText = 'Fetch Problems';
btn.classList.add("btn");
btn.classList.add("btn-primary");
btn.style.width = 'fit-content';
btn.type = "button"
headerEle.appendChild(ip1);
headerEle.appendChild(ip2);
headerEle.appendChild(btn);
btn.onclick = getProblems;
}
async function getSubmission() {
let userEle = document.getElementById("user");
let user = userEle.value;
userEle.value = '';
let userInfoApi = `https://codeforces.com/api/user.info?handles=${user}`;
let userStatusApi = `https://codeforces.com/api/user.status?handle=${user}&from=1&count=1`;
let data = await Fetch(userInfoApi);
if (data.status === "FAILED") {
msgEle.innerText = "Invalid Username";
return;
}
if (countChart != null) {
countChart.destroy();
}
let firstName = data.result[0].firstName || "sir";
let prvSubmissionId = null;
msgEle.innerHTML = '';
msgEle.innerHTML = `Waiting for <a href="https://codeforces.com/profile/${user}" target="_blank">your</a> submission ...`;
let x = await Fetch(userStatusApi);
setInterval(async () => {
let data = await Fetch(userStatusApi);
if (data.status == "OK") {
data = data.result[0];
if (data.verdict == "TESTING") return;
if (data.id != prvSubmissionId) {
if (prvSubmissionId) {
if (data.verdict == "OK") {
speakText(`well done ${firstName}, AC on problem ${data.problem.index}`);
}
else {
speakText(`hey ${firstName}, ${data.verdict.split("_").join(" ")} on test ${data.passedTestCount + 1}, problem ${data.problem.index}`);
}
}
prvSubmissionId = data.id;
}
}
}, 15000);
}
async function getContestDetails() {
let y = await Fetch(`https://codeforces.com/api/contest.list`);
console.log(y);
let cnt = 0;
const month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
msg.innerHTML = '';
headerEle.innerHTML = 'Upcoming Contests - ';
if (countChart != null) {
countChart.destroy();
}
let table = document.createElement('table')
let thead = document.createElement('thead')
let tbody = document.createElement('tbody')
table.appendChild(thead);
table.appendChild(tbody);
msgEle.appendChild(table);
let heading1 = document.createElement('th')
let heading2 = document.createElement('th')
let row = document.createElement('tr')
for (var i = 8; i >= 0; i--) {
let str = y.result[i].name;
//pre-check
if (str.indexOf("#TBA") != -1 || y.result[i].phase == "FINISHED") continue;
//include only contests that have not yet happened
cnt++;
if (cnt == 1) {
speakText(`Some Upcoming contests are`);
heading1.innerText = "Contest Name"
heading2.innerText = "Date"
heading1.style.paddingBottom = 0;
heading2.style.paddingBottom = 0;
row.appendChild(heading1)
row.appendChild(heading2)
thead.appendChild(row)
}
var timestamp = y.result[i].startTimeSeconds;
var d = new Date(timestamp * 1000);
let name = month[d.getMonth()];
let date = d.getDate();
let year = d.getFullYear();
var div = document.createElement("div");
let a_row = document.createElement('tr')
let data1 = document.createElement('td')
let data2 = document.createElement('td')
data1.style.paddingTop = 1;
data1.style.paddingBottom = 0;
data2.style.paddingTop = 1;
data2.style.paddingBottom = 0;
a_row.appendChild(data1)
a_row.appendChild(data2)
tbody.appendChild(a_row)
data1.innerHTML = `<a href="https://codeforces.com/contests">${y.result[i].name}</a>`;
data2.innerHTML = date + ' ' + name + ' ' + year;
table.classList.add("table");
}
if (cnt == 0) {
var div = document.createElement("div");
div.innerHTML = 'No Upcoming Contests As Of Now .... ';
msgEle.appendChild(div);
}
}
async function getInfo() {
let recInfo = document.getElementById("inf");
let rec = recInfo.value;
recInfo.value = '';
console.log(rec);
let userInfoApi = `https://codeforces.com/api/user.info?handles=${rec}`;
let data = await Fetch(userInfoApi);
console.log(data);
if (data.status === "FAILED") {
msgEle.innerText = "Invalid Username";
return;
}
msgEle.innerText = "";
if (countChart != null) {
countChart.destroy();
}
let str = data.result[0].firstName + ' ' + data.result[0].lastName;
let name = str;
let country = data.result[0].country;
let handle = data.result[0].handle;
let currRating = data.result[0].rating;
let maxRating = data.result[0].maxRating;
let currRank = data.result[0].rank;
let maxRank = data.result[0].maxRank;
let lastOnline = data.result[0].lastOnlineTimeSeconds;
let d = new Date(lastOnline * 1000);
d = d.toLocaleString();
let key = ["Name:", "Country:", "Handle:", "CurrRating:", "MaxRating:", "CurrRank:", "MaxRank:", "LastOnline:"];
let val = [name, country, handle, currRating, maxRating, currRank, maxRank, d];
// console.log(val);
speakText(`The Details Of User Are `);
let table = document.createElement('table')
let thead = document.createElement('thead')
let tbody = document.createElement('tbody')
table.appendChild(thead);
table.appendChild(tbody);
msgEle.appendChild(table);
let row = document.createElement('tr')
let heading1 = document.createElement('th')
let heading2 = document.createElement('th')
row.appendChild(heading1)
row.appendChild(heading2)
thead.appendChild(row)
heading1.innerText = "Paramter"
heading2.innerText = "Value"
heading1.style.paddingBottom = 0;
heading2.style.paddingBottom = 0;
for (var i = 0; i < 8; i++) {
let a_row = document.createElement('tr')
let data1 = document.createElement('td')
let data2 = document.createElement('td')
data1.style.paddingTop = 1;
data1.style.paddingBottom = 0;
data2.style.paddingTop = 1;
data2.style.paddingBottom = 0;
a_row.appendChild(data1)
a_row.appendChild(data2)
tbody.appendChild(a_row)
data1.innerHTML = key[i];
data2.innerHTML = val[i];
table.classList.add("table");
}
}
function HashCheck(x) {
min = Math.ceil(1);
max = Math.floor(10);
var store = Math.floor(Math.random() * (max - min + 1) + min);
return x % store == 0;
}
async function getProblems() {
let probSet = document.getElementById("prob");
let ratVal = document.getElementById("rat");
let probs = probSet.value;
let value = ratVal.value;
probSet.value = ratVal.value = '';
let total = String(probs);
let res = '';
for (var i = 0; i < total.length; i++) {
if (total[i] == ',') {
res += ';';
}
else if (total[i] == ' ') res += '%20'
else res += total[i];
}
msgEle.innerHTML = '';
if (countChart != null) {
countChart.destroy();
}
let url = `https://codeforces.com/api/problemset.problems?tags=${res}`;
let data = await Fetch(url);
if (data.status === "FAILED") {
msgEle.innerText = "Check Details Once";
return;
}
let store = {}; //creating object to store key-value pairs
let contestNo = [], Id = [];
let cnt = 1;
for (var i = 0; i < 100000 && i < Object.keys(data.result.problems).length; i++) {
if (Object.keys(store).length == 10) break;
if (data.result.problems[i].rating == value && HashCheck(value)) {
store[cnt++] = data.result.problems[i].name;
contestNo.push(data.result.problems[i].contestId);
Id.push(data.result.problems[i].index);
}
}
if (Object.keys(store).length == 0) {
msgEle.innerHTML = "Check Details Once";
return;
}
cnt = 0;
let table = document.createElement('table')
let thead = document.createElement('thead')
let tbody = document.createElement('tbody')
table.appendChild(thead);
table.appendChild(tbody);
msgEle.appendChild(table);
let heading1 = document.createElement('th')
let heading2 = document.createElement('th')
let row = document.createElement('tr')
row.appendChild(heading1)
row.appendChild(heading2)
thead.appendChild(row)
heading1.innerText = "Sl No"
heading2.innerText = "Problem Name"
heading1.style.paddingBottom = 0;
heading2.style.paddingBottom = 0;
console.log(store);
speakText(`Here are some of the selected problems`);
for (var key of Object.keys(store)) {
var div = document.createElement("div");
var num1 = contestNo[cnt];
var num2 = Id[cnt];
cnt++;
let a_row = document.createElement('tr')
let data1 = document.createElement('td')
let data2 = document.createElement('td')
data1.style.paddingTop = 1;
data1.style.paddingBottom = 0;
data2.style.paddingTop = 1;
data2.style.paddingBottom = 0;
a_row.appendChild(data1)
a_row.appendChild(data2)
tbody.appendChild(a_row)
var link = `https://codeforces.com/problemset/problem/${num1}/${num2}`;
data1.innerHTML = `${key}`;
data2.innerHTML = `<a href="${link}">${store[key]}</a>`;
table.classList.add("table");
}
}
async function getAnalytics() {
let recInfo = document.getElementById("inf");
let rec = recInfo.value;
recInfo.value = '';
let userSubmissions = `https://codeforces.com/api/user.status?handle=${rec}`;
let data = await Fetch(userSubmissions);
if (data.status === "FAILED") {
msgEle.innerText = "Invalid Username";
return;
}
const key = [800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300, 2400];
const val = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
const count = [];
let store = new Map; //creating object to store key-value pairs
let map = new Map();
for (var i = 800; i <= 2400; i += 100) {
store.set(i, 0);
}
for (var i = 0; i < Object.keys(data.result).length; i++) {
var k1 = data.result[i].problem.contestId;
var k2 = data.result[i].problem.index;
var check = `${k1}${k2}`;
if (data.result[i].problem.rating != '' && data.result[i].verdict == "OK" && !map.has(check)) {
var x = store.get(data.result[i].problem.rating);
store.set(data.result[i].problem.rating, x + 1);
map.set(check, 1);
}
}
store.delete(undefined);
for (var values of store.values()) {
count.push(values);
}
//chart js part
msgEle.innerHTML = '';
if (countChart != null) {
countChart.destroy();
}
let mc = myChart.getContext('2d');
countChart = new Chart(mc,
{
type: 'bar',
data: {
labels: ['800', '900', '1000', '1100', '1200', '1300', '1400', '1500', '1600', '1700', '1800', '1900', '2000', '2100', '2200', '2300', '2400'],
datasets: [{
label: 'Problem Rating Count',
data: [
count[0],
count[1],
count[2],
count[3],
count[4],
count[5],
count[6],
count[7],
count[8],
count[9],
count[10],
count[11],
count[12],
count[13],
count[14],
count[15],
count[16]
],
//backgroundColor setting
backgroundColor: [
'rgba(255,99,132,0.6)',
'rgba(54,162,235,0.6)',
'rgba(255,206,86,0.6)',
'rgba(75,192,192,0.6)',
'rgba(153,102,255,0.6)',
'rgba(255,159,64,0.6)',
'rgba(255,99,132,0.6)',
'rgba(255,99,132,0.6)',
'rgba(54,162,235,0.6)',
'rgba(255,206,86,0.6)',
'rgba(75,192,192,0.6)',
'rgba(153,102,255,0.6)',
'rgba(255,159,64,0.6)',
'rgba(255,99,132,0.6)',
'rgba(153,102,255,0.6)',
'rgba(255,159,64,0.6)'
],
borderWidth: 1,
borderColor: '#777',
hoverBorderWidth: 3,
hoverBorderColor: '#000'
}]
},
options: {
plugins: {
title: {
display: true,
text: `Rating Distribution for User: ${rec}`,
font: {
size: 30
}
},
legend: {
display: false
}
},
scales: {
x: {
title: {
display: true,
text: 'Problem Ratings',
font: {
size: 15
}
}
},
y: {
title: {
display: true,
text: 'Count of Problems',
font: {
size: 15
}
}
}
}
}
});
}