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