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.
178 lines
4.3 KiB
178 lines
4.3 KiB
2 months ago
|
const db = wx.cloud.database();
|
||
|
const app = getApp();
|
||
|
|
||
|
Page({
|
||
|
data: {
|
||
|
students: [],
|
||
|
selectedStudent: null,
|
||
|
score: ''
|
||
|
},
|
||
|
|
||
|
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);
|
||
|
|
||
|
// 更新积分
|
||
|
this.updatePoints(1); // 到达课堂积分+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)
|
||
|
});
|
||
|
},
|
||
|
|
||
|
// 更新积分
|
||
|
updatePoints: function() {
|
||
|
const student = this.data.selectedStudent;
|
||
|
let score = 1; // 到达课堂积分+1
|
||
|
|
||
|
// 判断是否能准确重复所提问的问题
|
||
|
if (this.data.questionRepeatedCorrectly) {
|
||
|
score += 0.5;
|
||
|
} else {
|
||
|
score -= 1;
|
||
|
}
|
||
|
|
||
|
// 如果准确回答问题,输入要加的分数
|
||
|
if (this.data.questionRepeatedCorrectly) {
|
||
|
score += this.data.additionalScore;
|
||
|
}
|
||
|
console.log('Updating student points:', student._id, student.points); // 调试
|
||
|
// 如果触发双倍积分事件,积分加倍
|
||
|
if (this.data.doublePoints) {
|
||
|
score *= 2;
|
||
|
}
|
||
|
|
||
|
student.points += score;
|
||
|
|
||
|
db.collection('students').doc(student._id).update({
|
||
|
data: {
|
||
|
points: student.points
|
||
|
}
|
||
|
}).then(() => {
|
||
|
console.log('Student points updated successfully'); // 调试
|
||
|
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
|
||
|
});
|
||
|
}
|
||
|
});
|