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.

127 lines
3.8 KiB

const express = require('express');
const multer = require('multer');
const xlsx = require('xlsx');
const cors = require('cors');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const PORT = 3000;
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
let students = []; // 存储学生名单
let studentScores = {}; // 存储学生积分
let calledStudents = []; // 存储已点名学生的 ID
// 设置 multer 以处理文件上传
const upload = multer({ dest: 'uploads/' });
// 导入 Excel 文件中的学生名单
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];
const data = xlsx.utils.sheet_to_json(sheet);
students = data.map(student => ({
id: student['学号'],
name: student['姓名'],
score: 0 // 初始积分为 0
}));
// 初始化学生积分
students.forEach(student => {
studentScores[student.id] = student.score;
});
// 清空已点名的学生列表
calledStudents = [];
res.send({ message: '学生名单已导入', students });
});
// 随机点名
app.get('/rollcall', (req, res) => {
if (students.length === 0) {
return res.status(400).send({ message: '学生名单为空,请先导入名单' });
}
// 从所有学生中随机选择
const randomIndex = Math.floor(Math.random() * students.length);
const chosenStudent = students[randomIndex];
// 将选中的学生加入已点名列表
calledStudents.push(chosenStudent.id);
res.send(chosenStudent);
});
// 更新学生积分
app.post('/update-score', (req, res) => {
const { id, isInClass, isQuestionCorrect, isAnswerCorrect, score } = req.body;
// 检查学生 ID 是否存在
if (!studentScores.hasOwnProperty(id)) {
return res.status(400).send({ message: '学生不存在' });
}
let scoreChange = 0;
if (isInClass) {
scoreChange += 1; // 到达课堂
}
if (isQuestionCorrect) {
scoreChange += 0.5; // 准确重复问题
} else if (isQuestionCorrect === false) {
scoreChange -= 1; // 未准确重复问题
}
if (isAnswerCorrect && score) {
scoreChange += score; // 根据情况加分
}
studentScores[id] += scoreChange;
res.send({ message: '积分已更新', score: studentScores[id] });
});
// 幸运抽奖
app.post('/lottery', (req, res) => {
const { id } = req.body;
// 检查学生 ID 是否存在
if (!studentScores.hasOwnProperty(id)) {
return res.status(400).send({ message: '学生不存在' });
}
// 随机选择一个操作
const randomAction = Math.random();
let scoreChange = 0;
if (randomAction < 0.33) {
scoreChange = -0.5; // 扣0.5分
studentScores[id] += scoreChange;
return res.send({ message: '真无语,扣0.5分', score: studentScores[id] });
} else if (randomAction < 0.66) {
scoreChange = 1; // 加1分
studentScores[id] += scoreChange;
return res.send({ message: '不错不错,加1分', score: studentScores[id] });
} else {
scoreChange = 10; // 加10分
studentScores[id] += scoreChange;
return res.send({ message: '孩子你无敌了,加10分', score: studentScores[id] });
}
});
// 重置点名状态
app.post('/reset', (req, res) => {
calledStudents = []; // 清空已点名学生列表
res.send({ message: '点名状态已重置' });
});
app.listen(PORT, () => {
console.log(`服务器正在运行在 http://localhost:${PORT}`);
});