-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (45 loc) · 1.61 KB
/
index.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
const express = require('express');
const path = require('path');
const fileJson = require(`${path.join(__dirname, 'problems.json')}`)
const app = express();
const PORT = 3000;
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
// Endpoint to fetch problems from Codeforces API
app.post('/fetch-problems', async (req, res) => {
const { topic, minRating, maxRating, numProblems } = req.body;
try {
let response = await fetch('https://codeforces.com/api/problemset.problems');
let data;
try{
data = await response.json();
}catch(err){
data = fileJson;
}
if (data.status !== 'OK') {
return res.status(500).json({ error: 'Failed to fetch problems' });
}
const problems = data.result.problems.filter(problem =>
problem.tags.includes(topic) &&
problem.rating >= minRating &&
problem.rating <= maxRating
);
if (problems.length < numProblems) {
return res.status(400).json({ error: `Not enough problems available for the given criteria. Found only ${problems.length} problems.` });
}
const mashup = [];
for (let i = 0; i < numProblems; i++) {
const randomIndex = Math.floor(Math.random() * problems.length);
mashup.push(problems[randomIndex]);
problems.splice(randomIndex, 1);
}
mashup.sort((a, b) => a.rating - b.rating);
res.json(mashup);
} catch (error) {
console.error('Error fetching problems:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});