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.
rollcall/3.0 pages rollcall.js

209 lines
5.5 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.

const db = wx.cloud.database();
const app = getApp();
Page({
data: {
students: [],
selectedStudent: null,
score: '',
questionRepeatedCorrectly: false,
additionalScore: 0,
doublePoints: false,
updatedScore: null
},
onLoad: function() {
this.getStudents();
},
// 获取学生列表
getStudents: function() {
const MAX_LIMIT = 20;
db.collection('students').count().then(res => {
const total = res.total;
const batchTimes = Math.ceil(total / MAX_LIMIT);
const tasks = [];
for (let i = 0; i < batchTimes; i++) {
const promise = db.collection('students').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get();
tasks.push(promise);
}
Promise.all(tasks).then(results => {
const students = results.reduce((acc, cur) => {
return acc.concat(cur.data);
}, []);
this.setData({
students: students
});
app.globalData.students = students;
console.log('Students retrieved:', students);
}).catch(err => {
console.error('Failed to get students:', err);
wx.showToast({
title: '获取学生信息失败',
icon: 'none'
});
});
}).catch(err => {
console.error('Failed to count students:', err);
wx.showToast({
title: '获取学生数量失败',
icon: 'none'
});
});
},
// 随机点名
randomRollCall: function() {
const students = this.data.students;
if (students.length === 0) {
wx.showToast({
title: '没有学生信息',
icon: 'none'
});
return;
}
const totalPoints = students.reduce((sum, student) => sum + Math.max(0, 10 - student.points), 0);
let randomPoint = Math.random() * totalPoints;
let cumulativePoint = 0;
let selectedStudent = null;
for (const student of students) {
cumulativePoint += Math.max(0, 10 - student.points);
if (randomPoint <= cumulativePoint) {
selectedStudent = student;
break;
}
}
if (selectedStudent) {
this.setData({
selectedStudent: selectedStudent
});
wx.showModal({
title: '点名成功',
content: `点到的同学是:${selectedStudent.name} (${selectedStudent.id})`,
showCancel: false
});
// 触发双倍积分事件
this.triggerDoublePointsEvent(selectedStudent);
// 初始积分更新:到达课堂 +1 分
this.updateInitialPoints(1);
} else {
wx.showToast({
title: '点名失败',
icon: 'none'
});
}
},
// 触发双倍积分事件
triggerDoublePointsEvent: function(student) {
const probability = 0.1; // 10%的概率触发双倍积分
if (Math.random() < probability) {
this.setData({
doublePoints: true
});
wx.showToast({
title: '双倍积分!',
icon: 'success'
});
} else {
this.setData({
doublePoints: false
});
}
},
// 输入评分
inputScore: function(e) {
this.setData({
additionalScore: parseFloat(e.detail.value)
});
},
// 初始积分更新函数
updateInitialPoints: function(baseScore) {
const student = this.data.selectedStudent;
student.points += baseScore;
this.saveStudentPoints(student);
},
// 更新积分
updatePoints: function() {
const student = this.data.selectedStudent;
let score = 1; // 基础到达课堂积分+1
console.log(`Initial score: ${score}`);
// 判断是否能准确重复所提问的问题
if (this.data.questionRepeatedCorrectly) {
score += 0.5;
console.log('Accurate repetition: +0.5');
// 如果准确回答问题,输入要加的分数
score += this.data.additionalScore;
console.log(`Accurate answer score: +${this.data.additionalScore}`);//调试
} else {
score -= 1;
console.log('Inaccurate repetition: -1');//调试
}
console.log(`Score before double points: ${score}`);//调试
// 如果触发双倍积分事件,积分加倍
if (this.data.doublePoints) {
score *= 2;
console.log('Double points applied');
}
console.log(`Final computed score: ${score}`);
student.points += score;
this.setData({ updatedScore: student.points });
this.saveStudentPoints(student);
},
// 学生未到
studentAbsent: function() {
const student = this.data.selectedStudent;
let score = -1; // 学生未到扣1分
student.points += score;
this.setData({ updatedScore: student.points });
this.saveStudentPoints(student);
},
// 保存学生积分
saveStudentPoints: function(student) {
db.collection('students').doc(student._id).update({
data: {
points: student.points
}
}).then(() => {
this.getStudents();
wx.showToast({
title: '积分更新成功',
icon: 'success'
});
}).catch(err => {
console.error('Failed to update student points:', err);
wx.showToast({
title: '积分更新失败',
icon: 'none'
});
});
},
// 设置回答问题的情况
setQuestionRepeatedCorrectlyTrue: function() {
this.setData({
questionRepeatedCorrectly: true
});
},
setQuestionRepeatedCorrectlyFalse: function() {
this.setData({
questionRepeatedCorrectly: false
});
}
});