From 95622685feded039814e50cad0c94a6fce5e9017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Garnier?= Date: Fri, 13 Jan 2023 11:08:42 +0100 Subject: [PATCH] Calcul du classement --- WEB/Controller/ResoudreGateway.php | 13 +++++++----- WEB/Model/UserModel.php | 32 +++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/WEB/Controller/ResoudreGateway.php b/WEB/Controller/ResoudreGateway.php index beedb8a7..18098a11 100644 --- a/WEB/Controller/ResoudreGateway.php +++ b/WEB/Controller/ResoudreGateway.php @@ -291,19 +291,22 @@ class ResoudreGateway } return $results[0]['max(indexEnigme)']; } - public function getMaxClassement($enigmeId, $idPartie) : int{ + public function getClassement($enigmeId, $idPartie) : array{ $query="SELECT classement FROM Resoudre WHERE enigme=:enigme AND partie=:partie - ORDER BY classement DESC LIMIT 1"; + ORDER BY classement DESC"; $this->con->executeQuery($query, array( "enigme" => array($enigmeId, SQLITE3_INTEGER), "partie" => array($idPartie, SQLITE3_INTEGER))); $results=$this->con->getResults(); - if (empty($results) || $results[0]['classement'] == null) { - return 0; + // if (empty($results) || $results[0]['classement'] == null) { + // return 0; + // } + foreach ($results as $row){ + $classment[] = $row['classement']; } - return $results[0]['classement']; + return $classment; } public function getAllByPartieAndUtilisateur(string $mailUtilisateur, int $idPartie) : array{ diff --git a/WEB/Model/UserModel.php b/WEB/Model/UserModel.php index 7b679d5f..7214d28f 100644 --- a/WEB/Model/UserModel.php +++ b/WEB/Model/UserModel.php @@ -118,18 +118,44 @@ class UserModel public function enigmeEnded(string $mailUtilisateur, int $enigmeId){ $this->resoudre_gateway->enigmeEnded($mailUtilisateur,$enigmeId); } + + public function largest_consecutive_number($list) { + sort($list); + $largest = 0; + $missing = 0; + for ($i = 0; $i < count($list) - 1; $i++) { + if ($list[$i] + 1 != $list[$i + 1]) { + if ($list[$i] > $largest) { + $largest = $list[$i]; + } + if ($missing == 0) { + $missing = $list[$i] + 1; + } + } else { + if ($list[$i] > $largest) { + $largest = $list[$i]; + } + } + } + if ($missing != 0) { + return $missing; + } else { + return $largest + 1; + } + } + public function enigmeMultiEnded(string $mailUtilisateur, int $enigmeId){ $idPartie = $_SESSION['idPartie']; $dateDebut = $this->partie_gateway->getDateDebut($idPartie); $now = new DateTime(); $interval = $now->diff($dateDebut); $tempDeResolution = $interval->days * 24 * 60 * 60 + $interval->h * 60 * 60 + $interval->i * 60 + $interval->s; - $result = $this->resoudre_gateway->getMaxClassement($enigmeId, $idPartie); - if ($result == 0){ + $leClassement = $this->resoudre_gateway->getClassement($enigmeId, $idPartie); + if (empty($leClassement)){ $classement = 1; } else{ - $classement = $result + 1; + $classement = $this->largest_consecutive_number($leClassement); } $this->resoudre_gateway->enigmeMultiEnded($mailUtilisateur,$enigmeId, $tempDeResolution, $classement); }