Initial commit
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
|
||||
// Vérifier les droits admin
|
||||
$user = requireAdmin();
|
||||
|
||||
if ($method !== 'POST') {
|
||||
jsonResponse(['error' => 'Méthode non autorisée'], 405);
|
||||
}
|
||||
|
||||
$data = getJsonInput();
|
||||
|
||||
if (!isset($data['match_id']) || !isset($data['winner_id']) || !isset($data['score'])) {
|
||||
jsonResponse(['error' => 'match_id, winner_id et score requis'], 400);
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
|
||||
// Vérifier que le match existe
|
||||
$stmt = $db->prepare("SELECT id, player1_id, player2_id, status FROM matches WHERE id = ?");
|
||||
$stmt->execute([$data['match_id']]);
|
||||
$match = $stmt->fetch();
|
||||
|
||||
if (!$match) {
|
||||
jsonResponse(['error' => 'Match non trouvé'], 404);
|
||||
}
|
||||
|
||||
if ($match['status'] === 'completed') {
|
||||
jsonResponse(['error' => 'Ce match a déjà un résultat'], 400);
|
||||
}
|
||||
|
||||
// Vérifier que le gagnant est l'un des deux joueurs
|
||||
if ($data['winner_id'] != $match['player1_id'] && $data['winner_id'] != $match['player2_id']) {
|
||||
jsonResponse(['error' => 'Le gagnant doit être l\'un des deux joueurs du match'], 400);
|
||||
}
|
||||
|
||||
$db->beginTransaction();
|
||||
|
||||
try {
|
||||
// Mettre à jour le match
|
||||
$stmt = $db->prepare("
|
||||
UPDATE matches
|
||||
SET status = 'completed', winner_id = ?, score = ?
|
||||
WHERE id = ?
|
||||
");
|
||||
$stmt->execute([$data['winner_id'], $data['score'], $data['match_id']]);
|
||||
|
||||
// Mettre à jour les pronostics
|
||||
$stmt = $db->prepare("
|
||||
UPDATE predictions
|
||||
SET is_correct = CASE
|
||||
WHEN predicted_winner_id = ? THEN 1
|
||||
ELSE 0
|
||||
END
|
||||
WHERE match_id = ?
|
||||
");
|
||||
$stmt->execute([$data['winner_id'], $data['match_id']]);
|
||||
|
||||
// Ajouter les points bonus pour les pronostics corrects
|
||||
$stmt = $db->prepare("
|
||||
UPDATE users u
|
||||
JOIN predictions p ON u.id = p.user_id
|
||||
SET u.points = u.points + ?
|
||||
WHERE p.match_id = ? AND p.predicted_winner_id = ?
|
||||
");
|
||||
$stmt->execute([POINTS_CORRECT_PREDICTION, $data['match_id'], $data['winner_id']]);
|
||||
|
||||
$db->commit();
|
||||
|
||||
// Compter les pronostics corrects
|
||||
$stmt = $db->prepare("
|
||||
SELECT COUNT(*) as correct_count
|
||||
FROM predictions
|
||||
WHERE match_id = ? AND predicted_winner_id = ?
|
||||
");
|
||||
$stmt->execute([$data['match_id'], $data['winner_id']]);
|
||||
$correctCount = $stmt->fetch()['correct_count'];
|
||||
|
||||
jsonResponse([
|
||||
'success' => true,
|
||||
'message' => 'Résultat enregistré avec succès',
|
||||
'correct_predictions' => (int)$correctCount,
|
||||
'points_distributed' => $correctCount * POINTS_CORRECT_PREDICTION
|
||||
]);
|
||||
|
||||
} catch (Exception $e) {
|
||||
$db->rollBack();
|
||||
jsonResponse(['error' => 'Erreur lors de l\'enregistrement du résultat: ' . $e->getMessage()], 500);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user