Update lottery.html

main
luoyonghuang 2 months ago
parent d9960ef64c
commit 2b465511d1

@ -64,12 +64,8 @@
<script> <script>
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
let name = urlParams.get('name'); let student_name = urlParams.get('student_name');
let student_id = urlParams.get('student_id');
// 如果 URL 中没有 name 参数,尝试从 localStorage 获取
if (!name) {
name = localStorage.getItem('currentUserName');
}
const pointsDisplay = document.querySelector('#pointsDisplay'); const pointsDisplay = document.querySelector('#pointsDisplay');
const startLotteryButton = document.querySelector('#startLotteryButton'); const startLotteryButton = document.querySelector('#startLotteryButton');
@ -82,54 +78,123 @@
return new Promise(resolve => setTimeout(resolve, d)); return new Promise(resolve => setTimeout(resolve, d));
} }
// 获取该用户的积分 // 全局变量,用于存储用户积分
let userPoints = parseInt(localStorage.getItem(name)) || 0; let userPoints = null;
pointsDisplay.textContent = `${name} 的当前积分: ${userPoints}`;
// 获取该用户的分数
const fetchStudentScore = async (student_id) => {
try {
const response = await fetch('http://localhost:3000/api/get-score', {
method: 'POST', // 使用 POST 方法
headers: {
'Content-Type': 'application/json', // 设置请求体的内容类型
},
body: JSON.stringify({ student_id }), // 将 student_id 放入请求体
});
if (!response.ok) {
alert('分数获取失败');
return null; // 返回 null 表示获取失败
}
// 直接返回响应体中的数字
const data = await response.json(); // 直接解析为 JSON
const score = data.score;
return score; // 返回数字
} catch (error) {
alert('网络请求失败');
return null; // 返回 null 表示获取失败
}
};
// 更新用户分数
async function updateScore(student_id, points) {
try {
const response = await fetch('http://localhost:3000/api/update-score', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ student_id, points }),
});
if (!response.ok) {
throw new Error('网络响应不正常');
}
// 使用 text() 解析响应,得到字符串形式的分数
const data = await response.text();
let score = Number(data); // 转换为数字
return score; // 返回数字
} catch (error) {
console.error('分数更新失败:', error);
return null; // 返回 null 表示更新失败
}
}
// 初始化函数,用于设置用户积分
async function init() {
userPoints = await fetchStudentScore(student_id); // 获取分数并存储到全局变量
pointsDisplay.textContent = `${student_name} 的当前积分: ${userPoints}`;
}
// 执行初始化函数
init();
// 设置返回按钮的 URL // 设置返回按钮的 URL
backButton.onclick = () => { backButton.onclick = () => {
loadingIndicator.style.display = 'block'; // 显示加载提示 loadingIndicator.style.display = 'block'; // 显示加载提示
window.location.href = `choice.html?name=${encodeURIComponent(name)}`; window.location.href = `choice.html?student_name=${encodeURIComponent(student_name)}&student_id=${encodeURIComponent(student_id)}`;
}; };
// 抽奖逻辑 // 开始抽奖
startLotteryButton.addEventListener('click', async () => { startLotteryButton.addEventListener('click', async () => {
if (userPoints >= 20) { if (userPoints >= 2) {
userPoints -= 20; // 每次抽奖消耗 20 分 userPoints = await updateScore(student_id, -2); // 积分-2
localStorage.setItem(name, userPoints); // 保存更新后的积分
pointsDisplay.textContent = `${name} 的当前积分: ${userPoints}`; pointsDisplay.textContent = `${student_name} 的当前积分: ${userPoints}`;
// 随机选择奖品 // 随机选择奖品
const prizes = ["跳过权", "再来一次", "什么也没有发生", "加30积分且跳过"]; const prizes = ["跳过权", "再来一次", "什么也没有发生", "加3积分且跳过"];
const randomPrize = prizes[Math.floor(Math.random() * prizes.length)]; const randomPrize = prizes[Math.floor(Math.random() * prizes.length)];
resultDisplay.textContent = `你赢得了: ${randomPrize}`; resultDisplay.textContent = `你赢得了: ${randomPrize}`;
// 等待 2 秒以展示结果 // 等待 2 秒以展示结果
await sleep(2000); await new Promise(resolve => setTimeout(resolve, 2000));
loadingIndicator.style.display = 'block'; // 显示加载提示 loadingIndicator.style.display = 'block'; // 显示加载提示
if (randomPrize === '跳过权') { if (randomPrize === '跳过权') {
await sleep(1000); await sleep(2000);
window.location.href = `select.html?name=${encodeURIComponent(name)}`; window.location.href = `select.html?student_name=${encodeURIComponent(student_name)}&student_id=${encodeURIComponent(student_id)}`;
} else if (randomPrize === '再来一次') { }
userPoints += 20;
localStorage.setItem(name, userPoints); else if (randomPrize === '再来一次') {
} else if (randomPrize === '什么也没有发生') { userPoints = await updateScore(student_id, 2); // 积分返回2
await sleep(1000); await sleep(2000);
window.location.href = `answer.html?name=${encodeURIComponent(name)}`; window.location.href = `lottery.html?student_name=${encodeURIComponent(student_name)}&student_id=${encodeURIComponent(student_id)}`;
} else if (randomPrize === '加30积分且跳过') {
userPoints += 30;
localStorage.setItem(name, userPoints);
await sleep(1000);
window.location.href = `select.html?name=${encodeURIComponent(name)}`;
} }
else if (randomPrize === '什么也没有发生') {
await sleep(2000);
window.location.href = `answer.html?student_name=${encodeURIComponent(student_name)}&student_id=${encodeURIComponent(student_id)}`;
}
else if (randomPrize === '加3积分且跳过') {
userPoints = await updateScore(student_id, 3); // 积分+3
await sleep(2000);
window.location.href = `select.html?student_name=${encodeURIComponent(student_name)}&student_id=${encodeURIComponent(student_id)}`;
}
} else { } else {
resultDisplay.textContent = "积分不足,无法抽奖!"; resultDisplay.textContent = "积分不足,无法抽奖!返回回答问题";
loadingIndicator.style.display = 'block'; // 显示加载提示 loadingIndicator.style.display = 'block'; // 显示加载提示
await sleep(1000); await sleep(2000);
window.location.href = `answer.html?name=${encodeURIComponent(name)}`; window.location.href = `answer.html?student_name=${encodeURIComponent(student_name)}&student_id=${encodeURIComponent(student_id)}`;
} }
}); });
</script> </script>
</body> </body>
</html> </html>

Loading…
Cancel
Save