-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (62 loc) · 2.04 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
const express = require('express');
const fs = require('fs');
const { type } = require('os');
const app = express();
const port=3000;
//Hello Wolrd
let problems = [];
try {
const data = fs.readFileSync('cses_problems.json', 'utf8');//To read the JSON file
problems = JSON.parse(data);
} catch (err) {
console.error('Error reading problems file:', err);
}
//console.log(problems);
// Group problems by type
const problemsByType = problems.reduce((acc, problem) => {
const type = problem.type;
if (!acc[type]) acc[type] = [];
acc[type].push(problem);
return acc;
}, {});
let selectedTypes = [];
let selectedProblems = {};
let displayedProblems = {};
// Function to shuffle array
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));//To select a random number
[array[i], array[j]] = [array[j], array[i]];//Swap
}
}
// Select two random types and 5 unique problems from each
function selectRandomTypesAndProblems() {
const types = Object.keys(problemsByType);
// Select 2 random types
shuffleArray(types);//To mix the array and select the 2 types by slicing
selectedTypes = types.slice(0, 2);
selectedProblems = {};
selectedTypes.forEach(type => {
let availableProblems = problemsByType[type].filter(problem => {
return !displayedProblems[problem.problem];
});
// Shuffle and select 5 problems
shuffleArray(availableProblems);
selectedProblems[type] = availableProblems.slice(0, 5);
// Mark selected problems as displayed
selectedProblems[type].forEach(problem => {
displayedProblems[problem.problem] = type;
});
});
}
// Select initial types and problems and set interval for 10-second refresh
selectRandomTypesAndProblems();
setInterval(selectRandomTypesAndProblems, 24*60*60 * 1000);
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('index', { selectedTypes, selectedProblems });
});
app.listen(port, () => {
console.log("Server running on port 3000");
});