// API基础路径
const API_BASE = '';
// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', () => {
loadOverviewStats();
loadUpcomingMatches();
loadFinishedMatches();
loadAlgorithms();
initTabs();
});
// 初始化标签切换
function initTabs() {
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.addEventListener('click', () => {
// 移除所有活动状态
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
// 添加当前活动状态
btn.classList.add('active');
const tabId = btn.dataset.tab + '-tab';
document.getElementById(tabId).classList.add('active');
});
});
}
// 加载总体统计
async function loadOverviewStats() {
try {
const response = await fetch(`${API_BASE}/api/stats/overview`);
const result = await response.json();
if (result.success) {
document.getElementById('total-matches').textContent = result.data.total_matches;
document.getElementById('upcoming-matches').textContent = result.data.upcoming_matches;
document.getElementById('active-algorithms').textContent = result.data.active_algorithms;
document.getElementById('overall-accuracy').textContent = result.data.overall_accuracy.toFixed(1) + '%';
}
} catch (error) {
console.error('加载统计数据失败:', error);
}
}
// 加载即将进行的比赛
async function loadUpcomingMatches() {
try {
const response = await fetch(`${API_BASE}/api/matches?status=upcoming`);
const result = await response.json();
if (result.success) {
const container = document.getElementById('upcoming-matches-list');
if (result.data.length === 0) {
container.innerHTML = '
暂无即将进行的比赛
';
return;
}
// 获取每场比赛的预测
for (const match of result.data) {
const predictions = await getMatchPredictions(match.id);
match.predictions = predictions;
}
container.innerHTML = result.data.map(match => renderMatchCard(match, false)).join('');
}
} catch (error) {
console.error('加载比赛失败:', error);
}
}
// 加载已完成的比赛
async function loadFinishedMatches() {
try {
const response = await fetch(`${API_BASE}/api/matches?status=finished`);
const result = await response.json();
if (result.success) {
const container = document.getElementById('finished-matches-list');
if (result.data.length === 0) {
container.innerHTML = '暂无已完成的比赛
';
return;
}
// 获取每场比赛的预测
for (const match of result.data) {
const predictions = await getMatchPredictions(match.id);
match.predictions = predictions;
}
container.innerHTML = result.data.map(match => renderMatchCard(match, true)).join('');
}
} catch (error) {
console.error('加载比赛失败:', error);
}
}
// 获取比赛预测
async function getMatchPredictions(matchId) {
try {
const response = await fetch(`${API_BASE}/api/matches/${matchId}`);
const result = await response.json();
return result.success ? result.data.predictions : [];
} catch (error) {
console.error('获取预测失败:', error);
return [];
}
}
// 渲染比赛卡片
function renderMatchCard(match, isFinished) {
const matchTime = match.match_time ? new Date(match.match_time).toLocaleString('zh-CN') : '待定';
let predictionsHtml = '';
if (match.predictions && match.predictions.length > 0) {
predictionsHtml = `
预测结果
${match.predictions.map(pred => {
let resultText = '';
let resultClass = '';
if (isFinished && pred.is_correct !== null) {
resultText = pred.is_correct ? '✓ 正确' : '✗ 错误';
resultClass = pred.is_correct ? 'correct' : 'wrong';
}
return `
${pred.algorithm_name}
${pred.predicted_home_score !== null ? `${pred.predicted_home_score} : ${pred.predicted_away_score}` : ''}
${pred.predicted_winner ? `(${getWinnerText(pred.predicted_winner, match.home_team, match.away_team)})` : ''}
${resultText ? `${resultText}` : ''}
`;
}).join('')}
`;
}
return `
${isFinished && match.home_score !== null ? `
${match.home_score}
:
${match.away_score}
` : ''}
${predictionsHtml}
`;
}
// 获取胜负文本
function getWinnerText(winner, homeTeam, awayTeam) {
if (winner === 'home') return homeTeam + '胜';
if (winner === 'away') return awayTeam + '胜';
if (winner === 'draw') return '平局';
return winner;
}
// 加载算法排行
async function loadAlgorithms() {
try {
const response = await fetch(`${API_BASE}/api/algorithms`);
const result = await response.json();
if (result.success) {
const container = document.getElementById('algorithms-list');
if (result.data.length === 0) {
container.innerHTML = '暂无算法数据
';
return;
}
container.innerHTML = result.data.map(algo => `
${algo.name}
${algo.description || '暂无描述'}
${(algo.accuracy || 0).toFixed(1)}%
准确率
${algo.total_predictions || 0}
预测数
${algo.correct_predictions || 0}
正确数
`).join('');
}
} catch (error) {
console.error('加载算法失败:', error);
}
}
// 显示比赛详情
async function showMatchDetail(matchId) {
try {
const response = await fetch(`${API_BASE}/api/matches/${matchId}`);
const result = await response.json();
if (result.success) {
const match = result.data.match;
const predictions = result.data.predictions;
const matchTime = match.match_time ? new Date(match.match_time).toLocaleString('zh-CN') : '待定';
let predictionsHtml = '';
if (predictions.length > 0) {
predictionsHtml = `
预测详情
${predictions.map(pred => `
${pred.algorithm_name}
准确率: ${(pred.algorithm_accuracy || 0).toFixed(1)}%
${pred.predicted_home_score !== null ?
`预测比分: ${pred.predicted_home_score} : ${pred.predicted_away_score}` :
''}
${pred.predicted_winner ?
`
预测胜负: ${getWinnerText(pred.predicted_winner, match.home_team, match.away_team)}` :
''}
${pred.confidence ?
`
置信度: ${(pred.confidence * 100).toFixed(0)}%` :
''}
${match.status === 'finished' && pred.is_correct !== null ? `
${pred.is_correct ? '✓ 预测正确' : '✗ 预测错误'}
` : ''}
`).join('')}
`;
}
document.getElementById('modal-title').textContent =
`${match.home_team} vs ${match.away_team}`;
document.getElementById('modal-body').innerHTML = `
${match.status === 'finished' ? '已完成' : '即将进行'}
${match.round_name || '小组赛'} | ${matchTime}
${match.status === 'finished' && match.home_score !== null ? `
${match.home_score}
:
${match.away_score}
` : ''}
${predictionsHtml}
`;
document.getElementById('match-modal').style.display = 'block';
}
} catch (error) {
console.error('加载比赛详情失败:', error);
}
}
// 显示算法详情
async function showAlgorithmDetail(algoId) {
try {
const response = await fetch(`${API_BASE}/api/algorithms/${algoId}`);
const result = await response.json();
if (result.success) {
const algo = result.data.algorithm;
const history = result.data.history;
let historyHtml = '';
if (history.length > 0) {
historyHtml = `
预测历史
${history.map(h => `
${h.home_team} vs ${h.away_team}
${h.match_time ? new Date(h.match_time).toLocaleDateString('zh-CN') : '待定'}
预测: ${h.predicted_home_score} : ${h.predicted_away_score}
${h.home_score !== null ? ` | 实际: ${h.home_score} : ${h.away_score}` : ''}
${h.is_correct !== null ? `
${h.is_correct ? '正确' : '错误'}
` : ''}
`).join('')}
`;
}
document.getElementById('modal-title').textContent = algo.name;
document.getElementById('modal-body').innerHTML = `
${algo.description || '暂无描述'}
${(algo.accuracy || 0).toFixed(1)}%
准确率
${algo.total_predictions || 0}
总预测数
${algo.correct_predictions || 0}
正确预测
${historyHtml}
`;
document.getElementById('match-modal').style.display = 'block';
}
} catch (error) {
console.error('加载算法详情失败:', error);
}
}
// 关闭弹窗
document.querySelectorAll('.close').forEach(btn => {
btn.addEventListener('click', () => {
btn.parentElement.parentElement.style.display = 'none';
});
});
// 点击弹窗外部关闭
window.addEventListener('click', (e) => {
if (e.target.classList.contains('modal')) {
e.target.style.display = 'none';
}
});