230 lines
8.0 KiB
JavaScript
230 lines
8.0 KiB
JavaScript
// Vérification admin
|
|
window.onload = function() {
|
|
const user = JSON.parse(localStorage.getItem('currentUser'));
|
|
if (!user || user.role !== 'admin') {
|
|
window.location.href = 'index.html';
|
|
}
|
|
document.getElementById('adminName').textContent = user.username;
|
|
loadAdminData();
|
|
};
|
|
|
|
function logout() {
|
|
localStorage.removeItem('currentUser');
|
|
window.location.href = 'index.html';
|
|
}
|
|
|
|
function showAdminSection(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');
|
|
|
|
if (sectionId === 'matches') loadMatchesTable();
|
|
if (sectionId === 'users') loadUsersTable();
|
|
if (sectionId === 'results') loadResultsDropdown();
|
|
}
|
|
|
|
function loadAdminData() {
|
|
document.getElementById('totalMatches').textContent = tournamentMatches.length;
|
|
document.getElementById('totalPlayers').textContent = Object.keys(playersData).length;
|
|
document.getElementById('totalUsers').textContent = users.length;
|
|
|
|
const allPredictions = JSON.parse(localStorage.getItem('userPredictions') || '[]');
|
|
document.getElementById('totalPredictions').textContent = allPredictions.length;
|
|
|
|
// Populate player selects
|
|
const playerSelects = ['matchPlayer1', 'matchPlayer2'];
|
|
playerSelects.forEach(selectId => {
|
|
const select = document.getElementById(selectId);
|
|
select.innerHTML = '';
|
|
Object.values(playersData).forEach(player => {
|
|
const option = document.createElement('option');
|
|
option.value = player.id;
|
|
option.textContent = player.name;
|
|
select.appendChild(option);
|
|
});
|
|
});
|
|
}
|
|
|
|
function loadMatchesTable() {
|
|
const tbody = document.getElementById('matchesTable');
|
|
tbody.innerHTML = '';
|
|
|
|
tournamentMatches.forEach(match => {
|
|
const p1 = playersData[match.player1];
|
|
const p2 = playersData[match.player2];
|
|
|
|
const row = document.createElement('tr');
|
|
row.innerHTML = `
|
|
<td>${match.round}</td>
|
|
<td>${p1.name}</td>
|
|
<td>${p2.name}</td>
|
|
<td>${new Date(match.date).toLocaleDateString('fr-FR')}</td>
|
|
<td>${match.status === 'completed' ? 'Terminé' : 'À venir'}</td>
|
|
<td>
|
|
<button class="btn-danger" onclick="deleteMatch(${match.id})">Supprimer</button>
|
|
</td>
|
|
`;
|
|
tbody.appendChild(row);
|
|
});
|
|
}
|
|
|
|
function loadUsersTable() {
|
|
const tbody = document.getElementById('usersTable');
|
|
tbody.innerHTML = '';
|
|
|
|
users.forEach(user => {
|
|
const row = document.createElement('tr');
|
|
row.innerHTML = `
|
|
<td>${user.id}</td>
|
|
<td>${user.username}</td>
|
|
<td>${user.email}</td>
|
|
<td>${user.role}</td>
|
|
<td>${user.points || 0}</td>
|
|
<td>
|
|
<button class="btn-danger" onclick="deleteUser(${user.id})">Supprimer</button>
|
|
</td>
|
|
`;
|
|
tbody.appendChild(row);
|
|
});
|
|
}
|
|
|
|
function addMatch() {
|
|
const newMatch = {
|
|
id: Date.now(),
|
|
round: document.getElementById('matchRound').value,
|
|
player1: document.getElementById('matchPlayer1').value,
|
|
player2: document.getElementById('matchPlayer2').value,
|
|
date: document.getElementById('matchDate').value,
|
|
court: document.getElementById('matchCourt').value,
|
|
status: 'upcoming'
|
|
};
|
|
|
|
tournamentMatches.push(newMatch);
|
|
alert('Match ajouté avec succès!');
|
|
loadAdminData();
|
|
loadMatchesTable();
|
|
}
|
|
|
|
function deleteMatch(id) {
|
|
if (confirm('Êtes-vous sûr de vouloir supprimer ce match?')) {
|
|
const index = tournamentMatches.findIndex(m => m.id === id);
|
|
if (index > -1) {
|
|
tournamentMatches.splice(index, 1);
|
|
loadMatchesTable();
|
|
loadAdminData();
|
|
}
|
|
}
|
|
}
|
|
|
|
function addPlayer() {
|
|
const newPlayer = {
|
|
id: document.getElementById('playerName').value.toLowerCase().replace(' ', ''),
|
|
name: document.getElementById('playerName').value,
|
|
nationality: document.getElementById('playerNationality').value,
|
|
age: parseInt(document.getElementById('playerAge').value),
|
|
handedness: document.getElementById('playerHandedness').value,
|
|
ranking: parseInt(document.getElementById('playerRanking').value),
|
|
photo: 'https://www.atptour.com/-/media/tennis/players/head-shot/2024/default.png',
|
|
strengths: ['Nouveau joueur'],
|
|
weaknesses: ['À définir'],
|
|
surfaceStats: {
|
|
clay: { winRate: 0.5, titles: 0 },
|
|
hard: { winRate: 0.5, titles: 0 },
|
|
grass: { winRate: 0.5, titles: 0 }
|
|
},
|
|
recentForm: ['W', 'L', 'W', 'L', 'W']
|
|
};
|
|
|
|
playersData[newPlayer.id] = newPlayer;
|
|
alert('Joueur ajouté avec succès!');
|
|
loadAdminData();
|
|
}
|
|
|
|
function deleteUser(id) {
|
|
if (confirm('Êtes-vous sûr de vouloir supprimer cet utilisateur?')) {
|
|
const index = users.findIndex(u => u.id === id);
|
|
if (index > -1) {
|
|
users.splice(index, 1);
|
|
localStorage.setItem('users', JSON.stringify(users));
|
|
loadUsersTable();
|
|
loadAdminData();
|
|
}
|
|
}
|
|
}
|
|
|
|
function loadResultsDropdown() {
|
|
const select = document.getElementById('resultMatch');
|
|
select.innerHTML = '';
|
|
|
|
tournamentMatches.filter(m => m.status === 'upcoming').forEach(match => {
|
|
const p1 = playersData[match.player1];
|
|
const p2 = playersData[match.player2];
|
|
const option = document.createElement('option');
|
|
option.value = match.id;
|
|
option.textContent = `${match.round}: ${p1.name} vs ${p2.name}`;
|
|
option.dataset.p1 = match.player1;
|
|
option.dataset.p2 = match.player2;
|
|
select.appendChild(option);
|
|
});
|
|
|
|
updateWinnerSelect();
|
|
|
|
select.addEventListener('change', updateWinnerSelect);
|
|
}
|
|
|
|
function updateWinnerSelect() {
|
|
const select = document.getElementById('resultMatch');
|
|
const winnerSelect = document.getElementById('resultWinner');
|
|
const selectedOption = select.options[select.selectedIndex];
|
|
|
|
winnerSelect.innerHTML = '';
|
|
if (selectedOption.dataset.p1) {
|
|
const p1 = playersData[selectedOption.dataset.p1];
|
|
const p2 = playersData[selectedOption.dataset.p2];
|
|
|
|
winnerSelect.innerHTML = `
|
|
<option value="${p1.id}">${p1.name}</option>
|
|
<option value="${p2.id}">${p2.name}</option>
|
|
`;
|
|
}
|
|
}
|
|
|
|
function updateResult() {
|
|
const matchId = parseInt(document.getElementById('resultMatch').value);
|
|
const winner = document.getElementById('resultWinner').value;
|
|
const score = document.getElementById('resultScore').value;
|
|
|
|
const match = tournamentMatches.find(m => m.id === matchId);
|
|
if (match) {
|
|
match.status = 'completed';
|
|
match.winner = winner;
|
|
match.score = score;
|
|
|
|
// Calculer les points pour les utilisateurs
|
|
calculateUserPoints(matchId, winner);
|
|
|
|
alert('Résultat mis à jour avec succès!');
|
|
loadAdminData();
|
|
}
|
|
}
|
|
|
|
function calculateUserPoints(matchId, winner) {
|
|
const allPredictions = JSON.parse(localStorage.getItem('userPredictions') || '[]');
|
|
const matchPredictions = allPredictions.filter(p => p.matchId === matchId);
|
|
|
|
matchPredictions.forEach(pred => {
|
|
const user = users.find(u => u.id === pred.userId);
|
|
if (user && pred.predictedWinner === winner) {
|
|
user.points = (user.points || 0) + 50; // 50 points pour un pronostic juste
|
|
|
|
// Mettre à jour localStorage
|
|
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
|
if (currentUser && currentUser.id === user.id) {
|
|
localStorage.setItem('currentUser', JSON.stringify(user));
|
|
}
|
|
}
|
|
});
|
|
|
|
localStorage.setItem('users', JSON.stringify(users));
|
|
} |