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.

131 lines
4.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

document.addEventListener('DOMContentLoaded', function () {
let students = [];
let intervalId;
let isRunning = false;
let audio = new Audio('path/to/your/music.mp3'); // 自定义音乐文件路径
// 读取Excel文件
document.getElementById('file-upload').addEventListener('change', function (e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function (event) {
const data = new Uint8Array(event.target.result);
const workbook = XLSX.read(data, { type: 'array' });
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
const json = XLSX.utils.sheet_to_json(worksheet);
students = json.map(row => ({
name: row['姓名'],
points: row['积分'] ? parseFloat(row['积分']) : 0
}));
document.querySelector('.upload-button').innerText = file.name; // 显示文件名在按钮上
};
reader.readAsArrayBuffer(file);
});
// 随机点名
function startRandomName() {
if (students.length === 0) return;
isRunning = true;
intervalId = setInterval(() => {
const selectedStudent = weightedRandomSelection();
document.getElementById('nameDisplay').innerText = selectedStudent.name;
}, 100);
audio.play(); // 开始播放音乐
}
function stopRandomName() {
clearInterval(intervalId);
isRunning = false;
const selectedName = document.getElementById('nameDisplay').innerText;
const selectedStudent = students.find(student => student.name === selectedName);
if (selectedStudent) {
handleStudentAttendance(selectedStudent);
}
audio.pause(); // 停止播放音乐
audio.currentTime = 0; // 重置音乐播放时间
}
// 处理学生到场与表现
function handleStudentAttendance(student) {
const isPresent = confirm(`${student.name} 是否到场?`);
if (!isPresent) {
student.points -= 1;
alert(`${student.name} 未到场扣1分。当前积分${student.points}`);
} else {
const answeredCorrectly = confirm(`${student.name} 是否准确回答问题?`);
if (!answeredCorrectly) {
student.points += 0.5;
alert(`${student.name} 回答错误默认得分0.5。当前积分:${student.points}`);
} else {
const score = parseFloat(prompt(`${student.name} 回答问题得分0.5-3分`));
if (!isNaN(score) && score >= 0.5 && score <= 3) {
student.points += score;
alert(`${student.name} 回答正确,得分:${score}。当前积分:${student.points}`);
} else {
student.points += 0.5;
alert(`${student.name} 输入无效默认得分0.5。当前积分:${student.points}`);
}
triggerRandomEvents(student);
}
}
}
// 触发随机事件
function triggerRandomEvents(student) {
const randomEvent = Math.random();
if (randomEvent < 0.5) { // 50%的触发几率
student.points *= 2;
alert(`${student.name} 触发双倍积分!当前积分:${student.points}`);
} else if (randomEvent < 0.2) {
alert(`${student.name} 触发点名转移权!下次可以选择不回答问题。`);
}
}
// 开始/结束按钮点击事件
window.clickButton = function (button) {
if (isRunning) {
stopRandomName();
button.innerText = '开始';
} else {
startRandomName();
button.innerText = '结束';
}
};
// 权重随机选择与疯狂星期四加成
function weightedRandomSelection() {
const totalWeight = students.reduce((sum, student) => sum + (1 / (student.points + 1)), 0);
let random = Math.random() * totalWeight;
for (const student of students) {
random -= (1 / (student.points + 1));
if (random <= 0) {
return student;
}
}
return students[students.length - 1];
}
function applyThursdayBonus() {
const today = new Date();
if (today.getDay() === 4) { // 星期四
students.forEach(student => {
if (student.points % 50 === 0) {
student.points *= 2;
}
});
}
}
applyThursdayBonus();
module.exports = {
startRandomName,
stopRandomName,
handleStudentAttendance,
weightedRandomSelection,
applyThursdayBonus
};
});