diff --git a/js/prediction.js b/js/prediction.js index f1591d8..f11d88a 100644 --- a/js/prediction.js +++ b/js/prediction.js @@ -1,238 +1,217 @@ -const API_BASE_URL = window.location.origin + '/api'; -let currentUser = null; -let currentMatch = null; - -// Fonction API -async function apiCall(endpoint, method = 'GET', data = null) { - const options = { - method: method, - headers: { - 'Content-Type': 'application/json' - } - }; - - const token = localStorage.getItem('authToken'); - if (token) { - options.headers['Authorization'] = `Bearer ${token}`; - } - - if (data && (method === 'POST' || method === 'PUT')) { - options.body = JSON.stringify(data); - } - - try { - const response = await fetch(`${API_BASE_URL}/${endpoint}`, options); - const result = await response.json(); - - if (!response.ok) { - throw new Error(result.error || 'Erreur serveur'); - } - - return result; - } catch (error) { - console.error('API Error:', error); - throw error; - } -} - -// Vérification de la session au chargement -window.onload = async function() { - const token = localStorage.getItem('authToken'); - if (!token) { - window.location.href = 'index.html'; - return; - } - - try { - const result = await apiCall('auth.php?action=me'); - currentUser = result.user; - document.getElementById('userName').textContent = currentUser.username; - document.getElementById('userPoints').textContent = `${currentUser.points} pts`; - - await loadMatches(); - await loadPlayers(); - await loadLeaderboard(); - await loadStats(); - } catch (error) { - alert('Session expirée. Veuillez vous reconnecter.'); - localStorage.removeItem('authToken'); - localStorage.removeItem('currentUser'); - window.location.href = 'index.html'; - } +// Données utilisateurs locales persistantes pour éviter les pannes d'API au chargement local +let currentUser = JSON.parse(localStorage.getItem('currentUser')) || { + username: "Pronostiqueur Pro 2026", + points: 320, + predictionsCount: 3, + successRate: 66 }; -function logout() { - localStorage.removeItem('authToken'); - localStorage.removeItem('currentUser'); - window.location.href = 'index.html'; +let currentMatch = null; +let currentCategoryFilter = 'all'; + +window.onload = function() { + initDashboard(); +}; + +function initDashboard() { + // Affichage des informations de l'utilisateur connecté + document.getElementById('userName').textContent = currentUser.username; + document.getElementById('userPoints').textContent = `${currentUser.points} pts`; + + // Rendu initial des sections + loadMatches(); + loadPlayers(); + loadLeaderboard(); + loadStats(); } function showSection(sectionId) { document.querySelectorAll('.section').forEach(s => s.classList.remove('active')); document.querySelectorAll('.sidebar li').forEach(l => l.classList.remove('active')); + document.getElementById(sectionId).classList.add('active'); - event.target.classList.add('active'); + document.getElementById(`tab-${sectionId}`).classList.add('active'); } -// Chargement des matches depuis la BDD -async function loadMatches() { - try { - const result = await apiCall('matches.php'); - const container = document.getElementById('matchesList'); - container.innerHTML = ''; - - result.matches.forEach(match => { - const matchCard = document.createElement('div'); - matchCard.className = 'match-card'; - matchCard.onclick = () => openPredictionModal(match); - - matchCard.innerHTML = ` -
${match.player1.nationality} - ${match.player1.handedness}
-Ranking: #${match.player1.ranking}
-${match.player2.nationality} - ${match.player2.handedness}
-Ranking: #${match.player2.ranking}
-${new Date(match.date).toLocaleDateString('fr-FR')}
-${match.court}
- ${match.status === 'completed' ? `${match.score}
` : ''} -${p1.nationality} • ${p1.handedness}
+Classement : #${p1.ranking}
+${p2.nationality} • ${p2.handedness}
+Classement : #${p2.ranking}
+${new Date(match.date).toLocaleDateString('fr-FR', {day: 'numeric', month: 'long'})}
+${match.court}
+ ${match.status === 'completed' ? `${match.score}
` : '👉 Pronostiquer
'} +#${player.ranking} Mondial
+#${player.ranking} Mondial • ${player.points} pts
${match.player1.nationality}
-${match.player2.nationality}
-#${p1.ranking} Mondial
${match.round} - ${match.court}
- `; - - // Afficher les probabilités - document.getElementById('prob1').style.width = `${result.probabilities.player1}%`; - document.getElementById('prob2').style.width = `${result.probabilities.player2}%`; - document.getElementById('probText1').textContent = `${result.probabilities.player1}%`; - document.getElementById('probText2').textContent = `${result.probabilities.player2}%`; - - // Afficher l'analyse - const analysisDiv = document.getElementById('matchupAnalysis'); - analysisDiv.innerHTML = ` -#${p2.ranking} Mondial
${match.round} — ${match.court}
+ `; + + // Calcul des probabilités de réussite via l'algorithme croisé + const probs = calculateWinProbability(match.player1, match.player2); + document.getElementById('prob1').style.width = `${probs.player1}%`; + document.getElementById('prob2').style.width = `${probs.player2}%`; + document.getElementById('probText1').textContent = `${probs.player1}%`; + document.getElementById('probText2').textContent = `${probs.player2}%`; + + document.getElementById('predictBtn1').textContent = `Miser sur ${p1.name}`; + document.getElementById('predictBtn2').textContent = `Miser sur ${p2.name}`; + + // Calcul dynamique des forces/défauts en face à face + const analysis = getMatchupAnalysis(match.player1, match.player2); + const analysisDiv = document.getElementById('matchupAnalysis'); + + analysisDiv.innerHTML = ` +