65 lines
2.2 KiB
JavaScript
65 lines
2.2 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function() {
|
|
const refreshBtn = document.getElementById('refresh-btn');
|
|
const statsTable = document.getElementById('stats-table').getElementsByTagName('tbody')[0];
|
|
|
|
// 初始加载数据
|
|
loadStats();
|
|
|
|
// 刷新按钮点击事件
|
|
refreshBtn.addEventListener('click', loadStats);
|
|
|
|
// 加载统计数据
|
|
function loadStats() {
|
|
fetch('/api/stats')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
updateTable(data);
|
|
})
|
|
.catch(error => {
|
|
console.error('Error fetching stats:', error);
|
|
alert('Failed to load player stats. Check console for details.');
|
|
});
|
|
}
|
|
|
|
// 更新表格数据
|
|
function updateTable(statsData) {
|
|
const statsTable = document.getElementById('stats-table').getElementsByTagName('tbody')[0];
|
|
statsTable.innerHTML = '';
|
|
|
|
// 新数据格式是 { "玩家名": "统计信息", ... }
|
|
Object.entries(statsData).forEach(([playerName, statString]) => {
|
|
const row = statsTable.insertRow();
|
|
|
|
// 玩家名列
|
|
const nameCell = row.insertCell(0);
|
|
nameCell.textContent = playerName;
|
|
|
|
// 解析统计信息
|
|
const stats = {};
|
|
statString.split(" | ").forEach(part => {
|
|
const [label, value] = part.split(": ");
|
|
stats[label.trim()] = value;
|
|
});
|
|
|
|
// 总时长列
|
|
const totalCell = row.insertCell(1);
|
|
totalCell.textContent = stats["总时长"];
|
|
|
|
// 30天列
|
|
const thirtyCell = row.insertCell(2);
|
|
thirtyCell.textContent = stats["30天"];
|
|
|
|
// 7天列
|
|
const sevenCell = row.insertCell(3);
|
|
sevenCell.textContent = stats["7天"];
|
|
});
|
|
}
|
|
|
|
// 辅助函数:将"Xh Ym"格式的时间转换为分钟数
|
|
function parseTime(timeStr) {
|
|
const [hPart, mPart] = timeStr.split(' ');
|
|
const hours = parseInt(hPart.replace('h', ''));
|
|
const minutes = parseInt(mPart.replace('m', ''));
|
|
return hours * 60 + minutes;
|
|
}
|
|
}); |