Actualiser api/predictions.php
This commit is contained in:
+9
-39
@@ -1,6 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once 'config.php';
|
require_once 'config.php';
|
||||||
|
|
||||||
$method = $_SERVER['REQUEST_METHOD'];
|
$method = $_SERVER['REQUEST_METHOD'];
|
||||||
$action = $_GET['action'] ?? '';
|
$action = $_GET['action'] ?? '';
|
||||||
|
|
||||||
@@ -14,12 +13,10 @@ switch ($method) {
|
|||||||
getUserPredictions();
|
getUserPredictions();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'POST':
|
case 'POST':
|
||||||
$user = requireAuth();
|
$user = requireAuth();
|
||||||
makePrediction($user);
|
makePrediction($user);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
jsonResponse(['error' => 'Méthode non autorisée'], 405);
|
jsonResponse(['error' => 'Méthode non autorisée'], 405);
|
||||||
}
|
}
|
||||||
@@ -27,11 +24,10 @@ switch ($method) {
|
|||||||
function getUserPredictions() {
|
function getUserPredictions() {
|
||||||
$user = requireAuth();
|
$user = requireAuth();
|
||||||
$db = getDB();
|
$db = getDB();
|
||||||
|
|
||||||
$stmt = $db->prepare("
|
$stmt = $db->prepare("
|
||||||
SELECT p.*, m.match_date, m.round, m.status,
|
SELECT p.*, m.match_date, m.round, m.status,
|
||||||
p1.name as p1_name, p2.name as p2_name,
|
p1.name as p1_name, p2.name as p2_name,
|
||||||
pw.name as predicted_winner_name,
|
pw.name as predicted_winner_name,
|
||||||
mw.name as actual_winner_name
|
mw.name as actual_winner_name
|
||||||
FROM predictions p
|
FROM predictions p
|
||||||
JOIN matches m ON p.match_id = m.id
|
JOIN matches m ON p.match_id = m.id
|
||||||
@@ -43,46 +39,32 @@ function getUserPredictions() {
|
|||||||
ORDER BY m.match_date DESC
|
ORDER BY m.match_date DESC
|
||||||
");
|
");
|
||||||
$stmt->execute([$user['id']]);
|
$stmt->execute([$user['id']]);
|
||||||
|
|
||||||
jsonResponse(['success' => true, 'predictions' => $stmt->fetchAll()]);
|
jsonResponse(['success' => true, 'predictions' => $stmt->fetchAll()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function makePrediction($user) {
|
function makePrediction($user) {
|
||||||
$data = getJsonInput();
|
$data = getJsonInput();
|
||||||
|
|
||||||
if (!isset($data['match_id']) || !isset($data['predicted_winner_id'])) {
|
if (!isset($data['match_id']) || !isset($data['predicted_winner_id'])) {
|
||||||
jsonResponse(['error' => 'match_id et predicted_winner_id requis'], 400);
|
jsonResponse(['error' => 'match_id et predicted_winner_id requis'], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$db = getDB();
|
$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 = $db->prepare("SELECT id, status, player1_id, player2_id 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'] !== 'upcoming') {
|
if ($match['status'] !== 'upcoming') {
|
||||||
jsonResponse(['error' => 'Ce match est déjà terminé ou annulé'], 400);
|
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']) {
|
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);
|
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 = $db->prepare("SELECT id FROM predictions WHERE user_id = ? AND match_id = ?");
|
||||||
$stmt->execute([$user['id'], $data['match_id']]);
|
$stmt->execute([$user['id'], $data['match_id']]);
|
||||||
|
|
||||||
if ($stmt->fetch()) {
|
if ($stmt->fetch()) {
|
||||||
jsonResponse(['error' => 'Vous avez déjà fait un pronostic pour ce match'], 409);
|
jsonResponse(['error' => 'Vous avez déjà fait un pronostic pour ce match'], 409);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Créer le pronostic
|
|
||||||
$stmt = $db->prepare("
|
$stmt = $db->prepare("
|
||||||
INSERT INTO predictions (user_id, match_id, predicted_winner_id, points_earned)
|
INSERT INTO predictions (user_id, match_id, predicted_winner_id, points_earned)
|
||||||
VALUES (?, ?, ?, ?)
|
VALUES (?, ?, ?, ?)
|
||||||
@@ -93,14 +75,11 @@ function makePrediction($user) {
|
|||||||
$data['predicted_winner_id'],
|
$data['predicted_winner_id'],
|
||||||
POINTS_NEW_PREDICTION
|
POINTS_NEW_PREDICTION
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Ajouter les points à l'utilisateur
|
|
||||||
$stmt = $db->prepare("UPDATE users SET points = points + ? WHERE id = ?");
|
$stmt = $db->prepare("UPDATE users SET points = points + ? WHERE id = ?");
|
||||||
$stmt->execute([POINTS_NEW_PREDICTION, $user['id']]);
|
$stmt->execute([POINTS_NEW_PREDICTION, $user['id']]);
|
||||||
|
|
||||||
jsonResponse([
|
jsonResponse([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'message' => 'Pronostic enregistré avec succès',
|
'message' => 'Pronostic enregistré avec succès',
|
||||||
'points_earned' => POINTS_NEW_PREDICTION
|
'points_earned' => POINTS_NEW_PREDICTION
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@@ -108,13 +87,10 @@ function makePrediction($user) {
|
|||||||
function getUserStats() {
|
function getUserStats() {
|
||||||
$user = requireAuth();
|
$user = requireAuth();
|
||||||
$db = getDB();
|
$db = getDB();
|
||||||
|
|
||||||
// Total de pronostics
|
|
||||||
$stmt = $db->prepare("SELECT COUNT(*) as total FROM predictions WHERE user_id = ?");
|
$stmt = $db->prepare("SELECT COUNT(*) as total FROM predictions WHERE user_id = ?");
|
||||||
$stmt->execute([$user['id']]);
|
$stmt->execute([$user['id']]);
|
||||||
$total = $stmt->fetch()['total'];
|
$total = $stmt->fetch()['total'];
|
||||||
|
|
||||||
// Pronostics corrects
|
|
||||||
$stmt = $db->prepare("
|
$stmt = $db->prepare("
|
||||||
SELECT COUNT(*) as correct
|
SELECT COUNT(*) as correct
|
||||||
FROM predictions p
|
FROM predictions p
|
||||||
@@ -123,15 +99,12 @@ function getUserStats() {
|
|||||||
");
|
");
|
||||||
$stmt->execute([$user['id']]);
|
$stmt->execute([$user['id']]);
|
||||||
$correct = $stmt->fetch()['correct'];
|
$correct = $stmt->fetch()['correct'];
|
||||||
|
|
||||||
// Taux de réussite
|
|
||||||
$rate = $total > 0 ? round(($correct / $total) * 100) : 0;
|
$rate = $total > 0 ? round(($correct / $total) * 100) : 0;
|
||||||
|
|
||||||
// Points totaux
|
|
||||||
$stmt = $db->prepare("SELECT points FROM users WHERE id = ?");
|
$stmt = $db->prepare("SELECT points FROM users WHERE id = ?");
|
||||||
$stmt->execute([$user['id']]);
|
$stmt->execute([$user['id']]);
|
||||||
$points = $stmt->fetch()['points'];
|
$points = $stmt->fetch()['points'];
|
||||||
|
|
||||||
jsonResponse([
|
jsonResponse([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'stats' => [
|
'stats' => [
|
||||||
@@ -145,10 +118,9 @@ function getUserStats() {
|
|||||||
|
|
||||||
function getLeaderboard() {
|
function getLeaderboard() {
|
||||||
$db = getDB();
|
$db = getDB();
|
||||||
|
|
||||||
$stmt = $db->query("
|
$stmt = $db->query("
|
||||||
SELECT u.id, u.username, u.points,
|
SELECT u.id, u.username, u.points,
|
||||||
COUNT(p.id) as total_predictions,
|
COUNT(p.id) as total_predictions,
|
||||||
SUM(CASE WHEN p.is_correct = 1 THEN 1 ELSE 0 END) as correct_predictions
|
SUM(CASE WHEN p.is_correct = 1 THEN 1 ELSE 0 END) as correct_predictions
|
||||||
FROM users u
|
FROM users u
|
||||||
LEFT JOIN predictions p ON u.id = p.user_id
|
LEFT JOIN predictions p ON u.id = p.user_id
|
||||||
@@ -157,7 +129,6 @@ function getLeaderboard() {
|
|||||||
ORDER BY u.points DESC, correct_predictions DESC
|
ORDER BY u.points DESC, correct_predictions DESC
|
||||||
LIMIT 50
|
LIMIT 50
|
||||||
");
|
");
|
||||||
|
|
||||||
$leaderboard = [];
|
$leaderboard = [];
|
||||||
$rank = 1;
|
$rank = 1;
|
||||||
while ($row = $stmt->fetch()) {
|
while ($row = $stmt->fetch()) {
|
||||||
@@ -170,7 +141,6 @@ function getLeaderboard() {
|
|||||||
'correct_predictions' => (int)$row['correct_predictions']
|
'correct_predictions' => (int)$row['correct_predictions']
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(['success' => true, 'leaderboard' => $leaderboard]);
|
jsonResponse(['success' => true, 'leaderboard' => $leaderboard]);
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
Reference in New Issue
Block a user