diff --git a/api/player.php b/api/player.php index 659b27a..b8b0d5f 100644 --- a/api/player.php +++ b/api/player.php @@ -1,6 +1,5 @@ 'Méthode non autorisée'], 405); } function getAllPlayers() { $db = getDB(); - $stmt = $db->query(" SELECT p.*, (SELECT GROUP_CONCAT(strength SEPARATOR '|') FROM player_strengths WHERE player_id = p.id) as strengths, @@ -45,26 +39,20 @@ function getAllPlayers() { FROM players p ORDER BY p.ranking ASC "); - $players = []; while ($row = $stmt->fetch()) { $row['strengths'] = $row['strengths'] ? explode('|', $row['strengths']) : []; $row['weaknesses'] = $row['weaknesses'] ? explode('|', $row['weaknesses']) : []; - - // Convertir les décimaux en float $row['clay_win_rate'] = (float)$row['clay_win_rate']; $row['hard_win_rate'] = (float)$row['hard_win_rate']; $row['grass_win_rate'] = (float)$row['grass_win_rate']; - $players[] = $row; } - jsonResponse(['success' => true, 'players' => $players]); } function getPlayer($id) { $db = getDB(); - $stmt = $db->prepare(" SELECT p.*, (SELECT GROUP_CONCAT(strength SEPARATOR '|') FROM player_strengths WHERE player_id = p.id) as strengths, @@ -74,57 +62,43 @@ function getPlayer($id) { "); $stmt->execute([$id]); $player = $stmt->fetch(); - if (!$player) { jsonResponse(['error' => 'Joueur non trouvé'], 404); } - $player['strengths'] = $player['strengths'] ? explode('|', $player['strengths']) : []; $player['weaknesses'] = $player['weaknesses'] ? explode('|', $player['weaknesses']) : []; - jsonResponse(['success' => true, 'player' => $player]); } function getMatchupAnalysis($player1Id, $player2Id) { $db = getDB(); - - // Récupérer les deux joueurs $stmt = $db->prepare("SELECT * FROM players WHERE id = ?"); $stmt->execute([$player1Id]); $player1 = $stmt->fetch(); - $stmt = $db->prepare("SELECT * FROM players WHERE id = ?"); $stmt->execute([$player2Id]); $player2 = $stmt->fetch(); - + if (!$player1 || !$player2) { jsonResponse(['error' => 'Un ou les deux joueurs n\'existent pas'], 404); } - - // Récupérer les forces et faiblesses $stmt = $db->prepare("SELECT strength FROM player_strengths WHERE player_id = ?"); $stmt->execute([$player1Id]); $player1['strengths'] = $stmt->fetchAll(PDO::FETCH_COLUMN); - $stmt = $db->prepare("SELECT weakness FROM player_weaknesses WHERE player_id = ?"); $stmt->execute([$player1Id]); $player1['weaknesses'] = $stmt->fetchAll(PDO::FETCH_COLUMN); - + $stmt = $db->prepare("SELECT strength FROM player_strengths WHERE player_id = ?"); $stmt->execute([$player2Id]); $player2['strengths'] = $stmt->fetchAll(PDO::FETCH_COLUMN); - $stmt = $db->prepare("SELECT weakness FROM player_weaknesses WHERE player_id = ?"); $stmt->execute([$player2Id]); $player2['weaknesses'] = $stmt->fetchAll(PDO::FETCH_COLUMN); - - // Calculer les probabilités + $probabilities = calculateWinProbability($player1, $player2, 'clay'); - - // Analyser le matchup $analysis = analyzeMatchup($player1, $player2); - - // Récupérer H2H si disponible + $stmt = $db->prepare(" SELECT * FROM head_to_head WHERE (player1_id = ? AND player2_id = ?) @@ -132,7 +106,7 @@ function getMatchupAnalysis($player1Id, $player2Id) { "); $stmt->execute([$player1Id, $player2Id, $player2Id, $player1Id]); $h2h = $stmt->fetch(); - + jsonResponse([ 'success' => true, 'player1' => $player1, @@ -146,29 +120,19 @@ function getMatchupAnalysis($player1Id, $player2Id) { function calculateWinProbability($player1, $player2, $surface = 'clay') { $prob1 = 50; $prob2 = 50; - - // Facteur ranking $rankingDiff = $player2['ranking'] - $player1['ranking']; $prob1 += $rankingDiff * 2; $prob2 -= $rankingDiff * 2; - - // Facteur surface $surfaceField = $surface . '_win_rate'; $p1Surface = $player1[$surfaceField] * 100; $p2Surface = $player2[$surfaceField] * 100; $surfaceDiff = $p1Surface - $p2Surface; $prob1 += $surfaceDiff * 0.5; $prob2 -= $surfaceDiff * 0.5; - - // Normalisation $total = $prob1 + $prob2; $prob1 = round(($prob1 / $total) * 100); - $prob2 = 100 - $prob1; - - // Limiter entre 10 et 90 $prob1 = max(10, min(90, $prob1)); $prob2 = 100 - $prob1; - return [ 'player1' => $prob1, 'player2' => $prob2 @@ -182,21 +146,16 @@ function analyzeMatchup($player1, $player2) { 'player2_advantages' => [], 'player2_disadvantages' => [] ]; - - // Analyser les forces foreach ($player1['strengths'] as $strength) { if (!in_array($strength, $player2['strengths']) && !in_array($strength, $player2['weaknesses'])) { $analysis['player1_advantages'][] = $strength; } } - foreach ($player2['strengths'] as $strength) { if (!in_array($strength, $player1['strengths']) && !in_array($strength, $player1['weaknesses'])) { $analysis['player2_advantages'][] = $strength; } } - - // Exploitation des faiblesses foreach ($player1['weaknesses'] as $weakness) { foreach ($player2['strengths'] as $strength) { if (stripos($strength, explode(' ', $weakness)[0]) !== false) { @@ -205,7 +164,6 @@ function analyzeMatchup($player1, $player2) { } } } - foreach ($player2['weaknesses'] as $weakness) { foreach ($player1['strengths'] as $strength) { if (stripos($strength, explode(' ', $weakness)[0]) !== false) { @@ -214,28 +172,23 @@ function analyzeMatchup($player1, $player2) { } } } - return $analysis; } function addPlayer() { $data = getJsonInput(); - $required = ['player_code', 'name', 'nationality', 'age', 'handedness', 'ranking', 'points']; foreach ($required as $field) { if (!isset($data[$field])) { jsonResponse(['error' => "Le champ $field est requis"], 400); } } - $db = getDB(); - $stmt = $db->prepare(" INSERT INTO players (player_code, name, nationality, age, handedness, photo_url, ranking, points, clay_win_rate, hard_win_rate, grass_win_rate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "); - $stmt->execute([ $data['player_code'], $data['name'], @@ -249,86 +202,66 @@ function addPlayer() { $data['hard_win_rate'] ?? 0.50, $data['grass_win_rate'] ?? 0.50 ]); - $playerId = $db->lastInsertId(); - - // Ajouter les forces + if (isset($data['strengths']) && is_array($data['strengths'])) { $stmt = $db->prepare("INSERT INTO player_strengths (player_id, strength) VALUES (?, ?)"); foreach ($data['strengths'] as $strength) { $stmt->execute([$playerId, $strength]); } } - - // Ajouter les faiblesses if (isset($data['weaknesses']) && is_array($data['weaknesses'])) { $stmt = $db->prepare("INSERT INTO player_weaknesses (player_id, weakness) VALUES (?, ?)"); foreach ($data['weaknesses'] as $weakness) { $stmt->execute([$playerId, $weakness]); } } - jsonResponse(['success' => true, 'message' => 'Joueur ajouté avec succès', 'player_id' => $playerId]); } function updatePlayer($id) { $data = getJsonInput(); $db = getDB(); - - // Vérifier que le joueur existe $stmt = $db->prepare("SELECT id FROM players WHERE id = ?"); $stmt->execute([$id]); if (!$stmt->fetch()) { jsonResponse(['error' => 'Joueur non trouvé'], 404); } - $db->beginTransaction(); - try { $fields = []; $values = []; - $allowedFields = ['name', 'nationality', 'age', 'handedness', 'photo_url', 'ranking', 'points', - 'clay_win_rate', 'hard_win_rate', 'grass_win_rate']; - + 'clay_win_rate', 'hard_win_rate', 'grass_win_rate']; foreach ($allowedFields as $field) { if (isset($data[$field])) { $fields[] = "$field = ?"; $values[] = $data[$field]; } } - if (!empty($fields)) { $values[] = $id; $stmt = $db->prepare("UPDATE players SET " . implode(', ', $fields) . " WHERE id = ?"); $stmt->execute($values); } - - // Mettre à jour les forces si fourni if (isset($data['strengths'])) { $stmt = $db->prepare("DELETE FROM player_strengths WHERE player_id = ?"); $stmt->execute([$id]); - $stmt = $db->prepare("INSERT INTO player_strengths (player_id, strength) VALUES (?, ?)"); foreach ($data['strengths'] as $strength) { $stmt->execute([$id, $strength]); } } - - // Mettre à jour les faiblesses si fourni if (isset($data['weaknesses'])) { $stmt = $db->prepare("DELETE FROM player_weaknesses WHERE player_id = ?"); $stmt->execute([$id]); - $stmt = $db->prepare("INSERT INTO player_weaknesses (player_id, weakness) VALUES (?, ?)"); foreach ($data['weaknesses'] as $weakness) { $stmt->execute([$id, $weakness]); } } - $db->commit(); jsonResponse(['success' => true, 'message' => 'Joueur mis à jour avec succès']); - } catch (Exception $e) { $db->rollBack(); jsonResponse(['error' => 'Erreur lors de la mise à jour: ' . $e->getMessage()], 500); @@ -337,14 +270,11 @@ function updatePlayer($id) { function deletePlayer($id) { $db = getDB(); - $stmt = $db->prepare("DELETE FROM players WHERE id = ?"); $stmt->execute([$id]); - if ($stmt->rowCount() === 0) { jsonResponse(['error' => 'Joueur non trouvé'], 404); } - jsonResponse(['success' => true, 'message' => 'Joueur supprimé avec succès']); } ?> \ No newline at end of file