diff --git a/api/predictions.php b/api/predictions.php index 59871f8..ecbe4b8 100644 --- a/api/predictions.php +++ b/api/predictions.php @@ -1,6 +1,5 @@ 'Méthode non autorisée'], 405); } @@ -27,11 +24,10 @@ switch ($method) { 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, + 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 @@ -43,46 +39,32 @@ function getUserPredictions() { 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 (?, ?, ?, ?) @@ -93,14 +75,11 @@ function makePrediction($user) { $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', + 'message' => 'Pronostic enregistré avec succès', 'points_earned' => POINTS_NEW_PREDICTION ]); } @@ -108,13 +87,10 @@ function makePrediction($user) { 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 @@ -123,15 +99,12 @@ function getUserStats() { "); $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' => [ @@ -145,10 +118,9 @@ function getUserStats() { function getLeaderboard() { $db = getDB(); - $stmt = $db->query(" - SELECT u.id, u.username, u.points, - COUNT(p.id) as total_predictions, + 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 @@ -157,7 +129,6 @@ function getLeaderboard() { ORDER BY u.points DESC, correct_predictions DESC LIMIT 50 "); - $leaderboard = []; $rank = 1; while ($row = $stmt->fetch()) { @@ -170,7 +141,6 @@ function getLeaderboard() { 'correct_predictions' => (int)$row['correct_predictions'] ]; } - jsonResponse(['success' => true, 'leaderboard' => $leaderboard]); } ?> \ No newline at end of file