You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.0 KiB
74 lines
2.0 KiB
const express = require('express');
|
|
const multer = require('multer');
|
|
const XLSX = require('xlsx');
|
|
const path = require('path');
|
|
|
|
const app = express();
|
|
const upload = multer({ dest: 'uploads/' });
|
|
|
|
let students = [];
|
|
let scores = {};
|
|
|
|
// Middleware to serve static files
|
|
app.use(express.static('public'));
|
|
|
|
// Endpoint to upload Excel file
|
|
app.post('/upload', upload.single('file'), (req, res) => {
|
|
const workbook = XLSX.readFile(req.file.path);
|
|
const sheetName = workbook.SheetNames[0];
|
|
const sheet = workbook.Sheets[sheetName];
|
|
students = XLSX.utils.sheet_to_json(sheet);
|
|
|
|
// Initialize scores
|
|
students.forEach(student => {
|
|
scores[student.id] = { name: student.name, points: 0 };
|
|
});
|
|
|
|
res.json(students);
|
|
});
|
|
|
|
// Endpoint to randomly select a student
|
|
app.get('/random', (req, res) => {
|
|
if (students.length === 0) return res.status(400).send('No students available.');
|
|
|
|
// Calculate weights based on scores
|
|
const weights = students.map(student => {
|
|
return Math.max(1, 10 - scores[student.id].points); // Higher score -> lower weight
|
|
});
|
|
|
|
const totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
|
|
const randomNum = Math.random() * totalWeight;
|
|
let cumulativeWeight = 0;
|
|
|
|
for (let i = 0; i < students.length; i++) {
|
|
cumulativeWeight += weights[i];
|
|
if (randomNum < cumulativeWeight) {
|
|
return res.json(students[i]);
|
|
}
|
|
}
|
|
});
|
|
|
|
// Endpoint to update scores
|
|
app.post('/update-score', (req, res) => {
|
|
const { id, correctRepeat, correctAnswer } = req.body;
|
|
|
|
if (!scores[id]) return res.status(400).send('Invalid student ID.');
|
|
|
|
if (correctRepeat) {
|
|
scores[id].points += 0.5;
|
|
} else {
|
|
scores[id].points -= 1;
|
|
}
|
|
|
|
if (correctAnswer) {
|
|
scores[id].points += 1; // Adjust according to your scoring logic
|
|
}
|
|
|
|
res.json(scores[id]);
|
|
});
|
|
|
|
// Start the server
|
|
app.listen(3000, () => {
|
|
console.log('Server is running on http://localhost:3000');
|
|
});
|