diff --git a/WEB/Controller/PartieGateway.php b/WEB/Controller/PartieGateway.php index 35d6f795..b927e860 100644 --- a/WEB/Controller/PartieGateway.php +++ b/WEB/Controller/PartieGateway.php @@ -161,7 +161,6 @@ class PartieGateway ) ); $partie = $this->findLastPartie(); - $tpsMaxPartie = 0; $query = "INSERT INTO Contenir VALUES (:partie,:idEnigme,1)"; foreach ($lesEnigmes as $enigme) { $this->con->executeQuery($query, array( @@ -169,9 +168,7 @@ class PartieGateway "idEnigme" => array($enigme->getIdEnigme(), SQLITE3_INTEGER) ) ); - $tpsMaxPartie += $enigme->getTempsDeResolution(); } - $_SESSION['tpsMaxPartie'] = $tpsMaxPartie; $query = "INSERT INTO Participer VALUES (:partie,:utilisateur,0)"; $this->con->executeQuery($query, array( "partie" => array($partie->getIdPartie(), SQLITE3_INTEGER), @@ -320,6 +317,20 @@ class PartieGateway $row = $results[0]; return $row['enigme']; } + + public function findAllEnigmeIdInPartie ($idPartie) : array{ + $query = "SELECT * FROM Contenir WHERE partie = :idPartie"; + $this->con->executeQuery($query, array( + "idPartie" => array($idPartie, SQLITE3_INTEGER) + ) + ); + $results = $this->con->getResults(); + $lesEnigmes = array(); + foreach ($results as $row){ + $lesEnigmes[] = $row['enigme']; + } + return $lesEnigmes; + } public function getDateDebut($idPartie) : DateTime{ $query = "SELECT dateDebut FROM Partie WHERE id = :idPartie"; $this->con->executeQuery($query, array( diff --git a/WEB/Controller/ResoudreGateway.php b/WEB/Controller/ResoudreGateway.php index addce10f..d80d4723 100644 --- a/WEB/Controller/ResoudreGateway.php +++ b/WEB/Controller/ResoudreGateway.php @@ -168,6 +168,34 @@ class ResoudreGateway "enigme" => array($enigmeId, SQLITE3_INTEGER), "ended" => array(1, SQLITE3_INTEGER))); } + public function enigmeMultiEnded(string $mailUtilisateur, int $enigmeId, int $tempDeResolution, $classement){ + $query="UPDATE Resoudre + SET ended=:ended + WHERE utilisateur=:utilisateur + AND enigme=:enigme"; + $this->con->executeQuery($query, array( + "utilisateur" => array($mailUtilisateur, SQLITE3_TEXT), + "enigme" => array($enigmeId, SQLITE3_INTEGER), + "ended" => array(1, SQLITE3_INTEGER))); + + $query="UPDATE Resoudre + SET temps=:temps + WHERE utilisateur=:utilisateur + AND enigme=:enigme"; + $this->con->executeQuery($query, array( + "utilisateur" => array($mailUtilisateur, SQLITE3_TEXT), + "enigme" => array($enigmeId, SQLITE3_INTEGER), + "temps" => array($tempDeResolution, SQLITE3_INTEGER))); + + $query="UPDATE Resoudre + SET classement=:classement + WHERE utilisateur=:utilisateur + AND enigme=:enigme"; + $this->con->executeQuery($query, array( + "utilisateur" => array($mailUtilisateur, SQLITE3_TEXT), + "enigme" => array($enigmeId, SQLITE3_INTEGER), + "classement" => array($classement, SQLITE3_INTEGER))); + } public function saveCode(string $mailUtilisateur, int $enigmeId, string $code){ $query="UPDATE Resoudre @@ -216,7 +244,6 @@ class ResoudreGateway WHERE utilisateur=:utilisateur AND ended=:ended ORDER BY ended DESC LIMIT 1"; - // $query = "SELECT * FROM Partie ORDER BY id DESC LIMIT 1"; $this->con->executeQuery($query, array( "utilisateur" => array($mailUtilisateur, SQLITE3_TEXT), "ended" => array(1, SQLITE3_INTEGER))); @@ -241,4 +268,29 @@ class ResoudreGateway } return $results[0]['max(indexEnigme)']; } + public function getMaxClassement($enigmeId, $idPartie) : int{ + $query="SELECT classement FROM Resoudre + WHERE enigme=:enigme + AND partie=:partie + ORDER BY classement DESC LIMIT 1"; + $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; + } + return $results[0]['classement']; + } + + public function getAllByPartieAndUtilisateur(string $mailUtilisateur, int $idPartie) : array{ + $query = "SELECT * FROM Resoudre + WHERE utilisateur=:utilisateur + AND partie=:partie"; + $this->con->executeQuery($query, array( + "utilisateur" => array($mailUtilisateur, SQLITE3_TEXT), + "partie" => array($idPartie, SQLITE3_INTEGER))); + $results=$this->con->getResults(); + return $results; + } } \ No newline at end of file diff --git a/WEB/Controller/UserController.php b/WEB/Controller/UserController.php index f61d6912..7982ea95 100644 --- a/WEB/Controller/UserController.php +++ b/WEB/Controller/UserController.php @@ -190,7 +190,7 @@ class UserController $index = $_REQUEST['index']; $enigme = $model->getEnigmebyPartieIdAndIndex($_SESSION['idPartie'],$index); $utilisateur=$_SESSION['utilisateur']; - $model->enigmeEnded($utilisateur->getEmail(),$enigme->getIdEnigme()); + $model->enigmeMultiEnded($utilisateur->getEmail(),$enigme->getIdEnigme()); $index = $index + 1; header("Location: index.php?action=goToGame&idPartie=" . $_SESSION['idPartie'] . "&index=". $index); } catch (Exception $e) { @@ -244,6 +244,7 @@ class UserController $lastIndex = $model->getLastIndex($idPartie); if($lastIndex != 0 && $index == $lastIndex + 1){ $dateDebut = $model->getDateDebut($idPartie); + $points = $model->getPointsAtTheEnd($utilisateur->getEmail(), $idPartie); require($rep . $vues['gameEnd']); } else{ diff --git a/WEB/Model/UserModel.php b/WEB/Model/UserModel.php index 09d2f6e5..81e53437 100644 --- a/WEB/Model/UserModel.php +++ b/WEB/Model/UserModel.php @@ -33,18 +33,28 @@ class UserModel $this->partie_gateway->createPartieMulti($lesEnigmes, $mailUtilisateur); $idPartie=$this->partie_gateway->findPartieInQueue(); $etat=$this->partie_gateway->getEtat($idPartie); - return array($idPartie, $etat); } else{ $this->partie_gateway->addToPartie($mailUtilisateur, $idPartie); $etat=$this->partie_gateway->getEtat($idPartie); - return array($idPartie, $etat); } + $tpsMaxPartie = $this->calculTpsMaXPartie($idPartie); + $_SESSION['tpsMaxPartie'] = $tpsMaxPartie; + return array($idPartie, $etat); } - public function findUserGroup() : int { - return $this->partie_gateway->findPartieInQueue(); + public function calculTpsMaXPartie( int $idPartie) : int { + $lesIdEnigmes = $this->partie_gateway->findAllEnigmeIdInPartie($idPartie); + foreach ($lesIdEnigmes as $idEnigme){ + $enigme = $this->enigme_gateway->findById($idEnigme)[0]; + $tpsMaxPartie += $enigme->getTempsDeResolution(); } + return $tpsMaxPartie; + } + + public function findUserGroup() : int { + return $this->partie_gateway->findPartieInQueue(); + } public function launchGame() { @@ -95,8 +105,20 @@ class UserModel return $this->resoudre_gateway->checkEnigmeIsEnded($mailUtilisateur,$enigmeId); } - public function enigmeEnded(string $mailUtilisateur, int $enigmeId){ - $this->resoudre_gateway->enigmeEnded($mailUtilisateur,$enigmeId); + 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){ + $classement = 1; + } + else{ + $classement = $result + 1; + } + $this->resoudre_gateway->enigmeMultiEnded($mailUtilisateur,$enigmeId, $tempDeResolution, $classement); } public function saveCode(string $mailUtilisateur, int $enigmeId,string $code ){ @@ -151,4 +173,21 @@ class UserModel public function getDateDebut($idPartie) : DateTime{ return $this->partie_gateway->getDateDebut($idPartie); } + + public function getPointsAtTheEnd(string $mailUtilisateur, int $idPartie) : int{ + $points = 0; + $result = $this->resoudre_gateway->getAllByPartieAndUtilisateur($mailUtilisateur, $idPartie); + foreach ($result as $row){ + if ($row['classement'] == 1){ + $points += $this->enigme_gateway->findById($row['enigme'])[0]->getPoints(); + } + else if ($row['classement'] == 2){ + $points += $this->enigme_gateway->findById($row['enigme'])[0]->getPoints() * 0.75; + } + else{ + $points += $this->enigme_gateway->findById($row['enigme'])[0]->getPoints() * 0.5; + } + } + return $points; + } } \ No newline at end of file diff --git a/WEB/View/src/pages/Multijoueur/GameEnd.php b/WEB/View/src/pages/Multijoueur/GameEnd.php index a3974740..d4e4a224 100644 --- a/WEB/View/src/pages/Multijoueur/GameEnd.php +++ b/WEB/View/src/pages/Multijoueur/GameEnd.php @@ -19,9 +19,10 @@

Vous avez terminer toute les énimges.

+

Votre score est de : points.

Temps restant avant la fin de la partie :

modify('+'. $_SESSION['tpsMaxPartie'] .'minutes'); + $end_time = $dateDebut->modify('+'. $_SESSION['tpsMaxPartie'] .'seconds'); $now = new DateTime(); $interval = $now->diff($end_time); $remaining_seconds = $interval->days * 24 * 60 * 60 + $interval->h * 60 * 60 + $interval->i * 60 + $interval->s; @@ -42,7 +43,7 @@ if (remainingSeconds < 10) { remainingSeconds = "0" + remainingSeconds; } - document.getElementById('countdown').innerHTML = days + " jours " + hours + " heures " + minutes + " minutes " + remainingSeconds + " secondes"; + document.getElementById('countdown').innerHTML = hours + " heures " + minutes + " minutes " + remainingSeconds + " secondes"; if (seconds == 0) { clearInterval(countdown); document.getElementById('countdown').innerHTML = "Expired";