-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.js
More file actions
430 lines (372 loc) · 13.3 KB
/
temp.js
File metadata and controls
430 lines (372 loc) · 13.3 KB
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
const f = async () => {
const res = await fetch(
"https://codeforces.com/api/problemset.problems"
).then(async (data) => await data.json());
const m = {};
let total = 0;
for (const prob of res.result.problems) {
if (prob.rating) {
if (!m[prob.rating]) {
m[prob.rating] = 1;
} else {
m[prob.rating]++;
}
total++;
}
}
};
f();
import connectDB from "@/config/database";
import { unwantedContests } from "@/constants/questions";
import Contest from "@/models/Contest";
import Team from "@/models/Team";
import User from "@/models/User";
export const POST = async (request) => {
//to round a given number to the nearest multiple of 100
const roundOff = (num) => {
const remainder = num % 100;
if (remainder >= 50) {
return ((100 - num) % 100) + num;
} else {
return num - (num % 100);
}
};
//just to check whether the codeforces API is working fine
try {
const data = await fetch(
"https://codeforces.com/api/user.info?handles=tourist"
).then(async (res) => await res.json());
} catch (error) {
console.log(error);
return new Response(
JSON.stringify({
message: "Codeforces API is currently down... Please try again later",
ok: false,
}),
{ status: 503 }
);
}
//actual logic for contest creation
try {
const data = await request.json();
await connectDB();
const {
codeforcesId1,
codeforcesId2,
codeforcesId3,
numQuestions,
lowerDifficulty,
upperDifficulty,
timeLimit,
shuffleOrder,
tags,
contestantType,
selectedTeam,
startsIn,
startYear,
} = data;
if (
!codeforcesId1 ||
!numQuestions ||
!lowerDifficulty ||
!upperDifficulty ||
!timeLimit ||
shuffleOrder == undefined ||
!tags ||
!contestantType ||
!startsIn ||
!startYear
) {
return new Response(
JSON.stringify({ message: "Fill all the fields", ok: false }),
{
status: 400,
}
);
}
// console.log(startYear)
if (!["2021", "2020", "2019", "2018"].includes(startYear)) {
return new Response(
JSON.stringify({ message: "Requested an invalid year", ok: false }),
{ status: 400 }
);
}
const uniqueHandles = new Set(
[codeforcesId1, codeforcesId2, codeforcesId3].filter(Boolean)
);
if (
uniqueHandles.size !==
[codeforcesId1, codeforcesId2, codeforcesId3].filter(Boolean).length
) {
return new Response(
JSON.stringify({
message: "Duplicate Codeforces handles are not allowed",
ok: false,
}),
{ status: 400 }
);
}
let contestants = [];
if (codeforcesId1 !== "") {
const user = await fetch(
`https://codeforces.com/api/user.info?handles=${codeforcesId1}&checkHistoricHandles=false`
).then(async (data) => await data.json());
contestants.push(user.result[0].handle);
}
if (codeforcesId2 !== "") {
const user = await fetch(
`https://codeforces.com/api/user.info?handles=${codeforcesId2}&checkHistoricHandles=false`
).then(async (data) => await data.json());
contestants.push(user.result[0].handle);
}
if (codeforcesId3 !== "") {
const user = await fetch(
`https://codeforces.com/api/user.info?handles=${codeforcesId3}&checkHistoricHandles=false`
).then(async (data) => await data.json());
contestants.push(user.result[0].handle);
}
if (contestantType === "Team" && contestants.length < 2) {
return new Response(
JSON.stringify({
message: "In team mode, provide at least 2 participants",
ok: false,
}),
{ status: 400 }
);
}
const total_questions = await fetch(
`https://codeforces.com/api/problemset.problems`
).then(async (data) => await data.json());
if (total_questions.status === "FAILED") {
return new Response(
JSON.stringify({ message: "Failed to fetch questions", ok: false }),
{ status: 500 }
);
}
const classified_questions = {};
total_questions.result.problems.forEach((problem) => {
if (!classified_questions[problem.rating]) {
classified_questions[problem.rating] = [];
}
classified_questions[problem.rating].push(problem);
});
//use a random number to avoid CF API bugs
let num = Math.floor(Math.random() * 1000) + 10000;
const user1_from_cf_submissions = await fetch(
`https://codeforces.com/api/user.status?handle=${codeforcesId1}&count=${num}`
).then(async (data) => await data.json());
if (user1_from_cf_submissions.status === "FAILED") {
return new Response(
JSON.stringify({
message: `Codeforces handle ${codeforcesId1} is invalid`,
ok: false,
}),
{ status: 400 }
);
}
//to store the common solved questions
const st = new Set();
user1_from_cf_submissions.result.forEach((problem) => {
st.add(`${problem.problem.contestId}${problem.problem.index}`);
});
if (codeforcesId2 !== "") {
num = Math.floor(Math.random() * 1000) + 10000;
const user2_from_cf_submissions = await fetch(
`https://codeforces.com/api/user.status?handle=${codeforcesId2}&count=${num}`
).then(async (data) => await data.json());
if (user2_from_cf_submissions.status === "FAILED") {
return new Response(
JSON.stringify({
message: `Codeforces handle ${codeforcesId2} is invalid`,
ok: false,
}),
{ status: 400 }
);
}
user2_from_cf_submissions.result.forEach((problem) => {
st.add(`${problem.problem.contestId}${problem.problem.index}`);
});
}
if (codeforcesId3 !== "") {
num = Math.floor(Math.random() * 1000) + 10000;
const user3_from_cf_submissions = await fetch(
`https://codeforces.com/api/user.status?handle=${codeforcesId3}&count=${num}`
).then(async (data) => await data.json());
if (user3_from_cf_submissions.status === "FAILED") {
return new Response(
JSON.stringify({
message: `Codeforces handle ${codeforcesId3} is invalid`,
ok: false,
}),
{ status: 400 }
);
}
user3_from_cf_submissions.result.forEach((problem) => {
st.add(`${problem.problem.contestId}${problem.problem.index}`);
});
}
//this list will be returned back to the client which contains all the questions fetched for the contest
let newList = [];
const startTime = Date.now();
const timeLimitMillis = 10000;
//signifies the range of the lower limit of the question rating, since we are considering only the range [rating_start , rating_start + delta(discussed below)], also rating_start will be incremented by delta in every iteration of the outer while loop
let rating_start = lowerDifficulty;
//to store the value by which the range of the question rating would increment
let delta = (upperDifficulty - lowerDifficulty) / numQuestions;
let count = 0;
//the outer while loop
while (newList.length < numQuestions) {
//calculate the upper limit of question rating to be found in this iteration of the outer while loop
const rating_end = Math.min(
upperDifficulty,
100 * Math.ceil((rating_start + delta) / 100)
);
//to terminate when it is taking too much time
if (Date.now() - startTime > timeLimitMillis) {
return new Response(
JSON.stringify({
message: "Time limit exceeded while fetching questions",
ok: false,
}),
{ status: 408 }
);
}
//to store the problem list and take the question having median rating to be added in the final list(namely -> newList)
let problem_list_for_this_range = [];
//we will select random questions in the range [rating_start , rating_end] for (temp_size) number of times.. this means higher the temp_size, higher will be the probability to converge towards the mean of the rating range, which will benefit us to avoid abrupt changes in the problem ratings in the final list (newList)
let temp_size = Math.floor(Math.random() * 5);
while (problem_list_for_this_range.length < temp_size) {
//to randomly select the question rating in the range [rating_start , rating_end]
const rating_of_question = Math.min(upperDifficulty , roundOff(Math.floor(Math.random() * delta)) + rating_start)
//randomly select an index for the rating
let index = Math.floor(
Math.random() * classified_questions[rating_of_question].length
);
let problem = classified_questions[rating_of_question][index];
while (
!(
!st.has(`${problem.contestId}${problem.index}`) &&
problem.rating <= upperDifficulty &&
problem.rating >= lowerDifficulty &&
problem.tags.some((tag) => tags.includes(tag)) &&
!newList.includes(problem) &&
!unwantedContests.includes(problem.contestId) &&
!problem_list_for_this_range.includes(problem)
) &&
count < 100000
) {
//keep updating the index
index = Math.floor(
Math.random() * classified_questions[rating_of_question].length
);
problem = classified_questions[rating_of_question][index];
//to keep a track of the number of iterations
count++;
}
//to handle the case when the user has solved most of the questions and we are unable to fetch questions for the chosen criteria
if (count >= 100000) {
return new Response(
JSON.stringify({
message:
"Majority of the matching questions solved. Please adjust criteria for new questions",
ok: false,
}),
{
status: 200,
}
);
}
if (startYear == "2021" && problem.contestId >= 1472) {
problem_list_for_this_range.push(problem);
} else if (startYear == "2020" && problem.contestId >= 1284) {
problem_list_for_this_range.push(problem);
} else if (startYear == "2019" && problem.contestId >= 1097) {
problem_list_for_this_range.push(problem);
} else if (startYear == "2018" && problem.contestId >= 912) {
problem_list_for_this_range.push(problem);
}
}
//insert the median element of the given rating range in the final list to have a better probability of converging to the average
problem_list_for_this_range.sort((a, b) => a.rating - b.rating);
newList.push(problem_list_for_this_range[Math.floor(problem_list_for_this_range.length / 2)])
// let index = Math.floor(Math.random() * 7000 + 500);
// const problem = total_questions.result.problems[index];
// if (
// !st.has(`${problem.contestId}${problem.index}`) &&
// problem.rating <= upperDifficulty &&
// problem.rating >= lowerDifficulty &&
// problem.tags.some((tag) => tags.includes(tag)) &&
// !newList.includes(problem) &&
// !unwantedContests.includes(problem.contestId)
// ) {
// if (startYear == "2021" && problem.contestId >= 1472) {
// newList.push(problem);
// } else if (startYear == "2020" && problem.contestId >= 1284) {
// newList.push(problem);
// } else if (startYear == "2019" && problem.contestId >= 1097) {
// newList.push(problem);
// } else if (startYear == "2018" && problem.contestId >= 912) {
// newList.push(problem);
// }
// }
// count++;
rating_start += delta
}
// if (count >= 100000) {
// return new Response(
// JSON.stringify({
// message:
// "Majority of the matching questions solved. Please adjust criteria for new questions",
// ok: false,
// }),
// {
// status: 200,
// }
// );
// }
if (!shuffleOrder) {
newList.sort((a, b) => a.rating - b.rating);
}
let users = [];
for (const contestant of contestants) {
const user = await User.findOne({ codeforcesId: contestant });
if (user) {
users.push(user);
}
}
let now = new Date();
if (startsIn != "Immediately") {
now = new Date(now.getTime() + startsIn * 60000);
}
const newDate = new Date(now.getTime() + timeLimit * 60000);
let teamId = selectedTeam;
if (!teamId || teamId === "" || teamId === "Select Team") {
teamId = undefined;
}
const contest = new Contest({
users,
problemList: newList,
contestants,
numberOfQuestions: numQuestions,
lowerLimit: lowerDifficulty,
upperLimit: upperDifficulty,
timeLimit,
timeStart: now.toISOString(),
timeEnding: newDate.toISOString(),
contestantType,
team: teamId ? teamId : undefined,
});
await contest.save();
const id = contest._id;
return new Response(
JSON.stringify({ message: "Contest created", id, ok: true }),
{ status: 200 }
);
} catch (error) {
console.log(error);
return new Response(
JSON.stringify({ message: "Failed to create a contest", ok: false }),
{ status: 500 }
);
}
};