-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (118 loc) · 3.85 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
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
const express = require("express");
const app = express();
const cors = require("cors");
const problems = require("./problems.json");
app.use(cors());
// Endpoint do pobierania losowego zadania
app.get("/random-problem", (req, res) => {
try {
const random_problem =
problems[Math.floor(Math.random() * problems.length)];
res.json({ zadanie: random_problem });
} catch (error) {
res.status(500).json({ error: "Błąd serwera" });
}
});
function shuffleArray(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;
}
app.get("/random-problems", (req, res) => {
try {
let allProblems = shuffleArray(problems); // Shuffle the list of problems
const count = Math.floor(Math.random() * (10 - 5 + 1) + 5);
allProblems = allProblems.slice(0, count); // Randomly select a subset of problems
res.json({ zadania: allProblems });
} catch (error) {
res.status(500).json({ error: "Błąd serwera" });
}
});
app.get("/problems", (req, res) => {
try {
const typesParam = req.query.types;
const countParam = req.query.count;
const maturaParam = req.query.matura;
const difficultyParam = req.query.difficulty;
const tagsParam = req.query.tags;
problems.sort(() => Math.random() - 0.5);
if (maturaParam === true || maturaParam === "true") {
let easyProblems = problems.filter(
(problem) => problem.difficulty === "easy"
);
let mediumProblems = problems.filter(
(problem) => problem.difficulty === "medium"
);
let hardProblems = problems.filter(
(problem) => problem.difficulty === "hard"
);
const easyCount = 15;
const mediumCount = 10;
const hardCount = 6;
easyProblems = easyProblems.slice(0, easyCount);
mediumProblems = mediumProblems.slice(0, mediumCount);
hardProblems = hardProblems.slice(0, hardCount);
const maturaProblems = [
...easyProblems,
...mediumProblems,
...hardProblems,
];
return res.json({ zadania: maturaProblems });
} else {
let filteredProblems = problems;
if (typesParam && typesParam.toLowerCase() === "otwarte") {
filteredProblems = filteredProblems.filter(
(problem) => problem.type === "otwarte"
);
} else if (typesParam && typesParam.toLowerCase() === "zamkniete") {
filteredProblems = filteredProblems.filter(
(problem) => problem.type === "zamkniete"
);
}
if (difficultyParam) {
filteredProblems = filteredProblems.filter(
(problem) => problem.difficulty === difficultyParam
);
}
if (tagsParam) {
const tags = Array.isArray(tagsParam) ? tagsParam : [tagsParam];
filteredProblems = filteredProblems.filter((problem) =>
tags.some((tag) => problem.tags && problem.tags.includes(tag))
);
}
if (countParam) {
const count = parseInt(countParam);
filteredProblems = filteredProblems.slice(0, count);
}
res.json({ zadania: filteredProblems });
}
} catch (error) {
console.log(error);
res.status(500).json({ error: "Błąd serwera" });
}
});
app.get("/problems/:uid", (req, res) => {
try {
const uidArray = req.params.uid.split(",");
const selectedProblems = [];
uidArray.forEach((uid) => {
const problem = problems.find((p) => p.uid === uid);
if (problem) {
selectedProblems.push(problem);
}
});
res.json({ zadania: selectedProblems });
} catch (error) {
console.log(error);
res.status(500).json({ error: "Błąd serwera" });
}
});
app.get("/", (req, res) => {
res.send("Hello world");
});
const port = 3333;
app.listen(port, () => {
console.log(`Serwer działa na http://localhost:${port}`);
});