350 lines
11 KiB
PHP
350 lines
11 KiB
PHP
<?php
|
|
require_once 'config.php';
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$action = $_GET['action'] ?? '';
|
|
$id = $_GET['id'] ?? null;
|
|
|
|
switch ($method) {
|
|
case 'GET':
|
|
if ($action === 'matchup' && isset($_GET['player1']) && isset($_GET['player2'])) {
|
|
getMatchupAnalysis($_GET['player1'], $_GET['player2']);
|
|
} elseif ($id) {
|
|
getPlayer($id);
|
|
} else {
|
|
getAllPlayers();
|
|
}
|
|
break;
|
|
|
|
case 'POST':
|
|
requireAdmin();
|
|
addPlayer();
|
|
break;
|
|
|
|
case 'PUT':
|
|
requireAdmin();
|
|
updatePlayer($id);
|
|
break;
|
|
|
|
case 'DELETE':
|
|
requireAdmin();
|
|
deletePlayer($id);
|
|
break;
|
|
|
|
default:
|
|
jsonResponse(['error' => '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,
|
|
(SELECT GROUP_CONCAT(weakness SEPARATOR '|') FROM player_weaknesses WHERE player_id = p.id) as weaknesses
|
|
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,
|
|
(SELECT GROUP_CONCAT(weakness SEPARATOR '|') FROM player_weaknesses WHERE player_id = p.id) as weaknesses
|
|
FROM players p
|
|
WHERE p.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 = ?)
|
|
OR (player1_id = ? AND player2_id = ?)
|
|
");
|
|
$stmt->execute([$player1Id, $player2Id, $player2Id, $player1Id]);
|
|
$h2h = $stmt->fetch();
|
|
|
|
jsonResponse([
|
|
'success' => true,
|
|
'player1' => $player1,
|
|
'player2' => $player2,
|
|
'probabilities' => $probabilities,
|
|
'analysis' => $analysis,
|
|
'head_to_head' => $h2h
|
|
]);
|
|
}
|
|
|
|
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
|
|
];
|
|
}
|
|
|
|
function analyzeMatchup($player1, $player2) {
|
|
$analysis = [
|
|
'player1_advantages' => [],
|
|
'player1_disadvantages' => [],
|
|
'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) {
|
|
$analysis['player2_advantages'][] = "Exploite: $weakness";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ($player2['weaknesses'] as $weakness) {
|
|
foreach ($player1['strengths'] as $strength) {
|
|
if (stripos($strength, explode(' ', $weakness)[0]) !== false) {
|
|
$analysis['player1_advantages'][] = "Exploite: $weakness";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
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'],
|
|
$data['nationality'],
|
|
$data['age'],
|
|
$data['handedness'],
|
|
$data['photo_url'] ?? null,
|
|
$data['ranking'],
|
|
$data['points'],
|
|
$data['clay_win_rate'] ?? 0.50,
|
|
$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'];
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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']);
|
|
}
|
|
?>
|