'Méthode non autorisée'], 405); } function getUserPredictions() { $user = requireAuth(); $db = getDB(); $stmt = $db->prepare(" SELECT p.*, m.match_date, m.round, m.status, p1.name as p1_name, p2.name as p2_name, pw.name as predicted_winner_name, mw.name as actual_winner_name FROM predictions p JOIN matches m ON p.match_id = m.id JOIN players p1 ON m.player1_id = p1.id JOIN players p2 ON m.player2_id = p2.id JOIN players pw ON p.predicted_winner_id = pw.id LEFT JOIN players mw ON m.winner_id = mw.id WHERE p.user_id = ? ORDER BY m.match_date DESC "); $stmt->execute([$user['id']]); jsonResponse(['success' => true, 'predictions' => $stmt->fetchAll()]); } function makePrediction($user) { $data = getJsonInput(); if (!isset($data['match_id']) || !isset($data['predicted_winner_id'])) { jsonResponse(['error' => 'match_id et predicted_winner_id requis'], 400); } $db = getDB(); // Vérifier que le match existe et est à venir $stmt = $db->prepare("SELECT id, status, player1_id, player2_id FROM matches WHERE id = ?"); $stmt->execute([$data['match_id']]); $match = $stmt->fetch(); if (!$match) { jsonResponse(['error' => 'Match non trouvé'], 404); } if ($match['status'] !== 'upcoming') { jsonResponse(['error' => 'Ce match est déjà terminé ou annulé'], 400); } // Vérifier que le gagnant prédit est l'un des deux joueurs if ($data['predicted_winner_id'] != $match['player1_id'] && $data['predicted_winner_id'] != $match['player2_id']) { jsonResponse(['error' => 'Le gagnant prédit doit être l\'un des deux joueurs du match'], 400); } // Vérifier si l'utilisateur a déjà fait un pronostic pour ce match $stmt = $db->prepare("SELECT id FROM predictions WHERE user_id = ? AND match_id = ?"); $stmt->execute([$user['id'], $data['match_id']]); if ($stmt->fetch()) { jsonResponse(['error' => 'Vous avez déjà fait un pronostic pour ce match'], 409); } // Créer le pronostic $stmt = $db->prepare(" INSERT INTO predictions (user_id, match_id, predicted_winner_id, points_earned) VALUES (?, ?, ?, ?) "); $stmt->execute([ $user['id'], $data['match_id'], $data['predicted_winner_id'], POINTS_NEW_PREDICTION ]); // Ajouter les points à l'utilisateur $stmt = $db->prepare("UPDATE users SET points = points + ? WHERE id = ?"); $stmt->execute([POINTS_NEW_PREDICTION, $user['id']]); jsonResponse([ 'success' => true, 'message' => 'Pronostic enregistré avec succès', 'points_earned' => POINTS_NEW_PREDICTION ]); } function getUserStats() { $user = requireAuth(); $db = getDB(); // Total de pronostics $stmt = $db->prepare("SELECT COUNT(*) as total FROM predictions WHERE user_id = ?"); $stmt->execute([$user['id']]); $total = $stmt->fetch()['total']; // Pronostics corrects $stmt = $db->prepare(" SELECT COUNT(*) as correct FROM predictions p JOIN matches m ON p.match_id = m.id WHERE p.user_id = ? AND p.is_correct = 1 "); $stmt->execute([$user['id']]); $correct = $stmt->fetch()['correct']; // Taux de réussite $rate = $total > 0 ? round(($correct / $total) * 100) : 0; // Points totaux $stmt = $db->prepare("SELECT points FROM users WHERE id = ?"); $stmt->execute([$user['id']]); $points = $stmt->fetch()['points']; jsonResponse([ 'success' => true, 'stats' => [ 'total_predictions' => (int)$total, 'correct_predictions' => (int)$correct, 'success_rate' => $rate, 'total_points' => (int)$points ] ]); } function getLeaderboard() { $db = getDB(); $stmt = $db->query(" SELECT u.id, u.username, u.points, COUNT(p.id) as total_predictions, SUM(CASE WHEN p.is_correct = 1 THEN 1 ELSE 0 END) as correct_predictions FROM users u LEFT JOIN predictions p ON u.id = p.user_id WHERE u.role = 'user' GROUP BY u.id ORDER BY u.points DESC, correct_predictions DESC LIMIT 50 "); $leaderboard = []; $rank = 1; while ($row = $stmt->fetch()) { $leaderboard[] = [ 'rank' => $rank++, 'user_id' => $row['id'], 'username' => $row['username'], 'points' => (int)$row['points'], 'total_predictions' => (int)$row['total_predictions'], 'correct_predictions' => (int)$row['correct_predictions'] ]; } jsonResponse(['success' => true, 'leaderboard' => $leaderboard]); } ?>