diff --git a/WEB/Controller/PartieGateway.php b/WEB/Controller/PartieGateway.php index 5252f0e6..6db232a2 100644 --- a/WEB/Controller/PartieGateway.php +++ b/WEB/Controller/PartieGateway.php @@ -17,15 +17,16 @@ class PartieGateway * @param array $listeJoueur */ - public function creerPartie(array $listeJoueur){ - $query = "SELECT * FROM Enigme"; + public function creerPartieMulti(array $listeJoueur){ + $query = "SELECT * FROM Enigme + WHERE points IS NOT NULL OR points != 0"; $this->con->executeQuery($query); $results = $this->con->getResults(); $query= "SELECT max(p.id) FROM PARTIE p;"; $this->con->executeQuery($query); - $max=$this->con->getResults()[0]; - $partie=PartieFactory::createPartie($max,$results); + $max=$this->con->getResults()[0]["max"]; + $partie=PartieFactory::createPartieMulti($max,$results); $query= "INSERT INTO Partie VALUES (:idPartie,:idEnigme)"; $this->con->executeQuery($query, array(':idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER))); foreach($partie->getListeEnigme() as $Enigme){ @@ -42,6 +43,30 @@ class PartieGateway } } + public function creerPartieSolo(Utilisateur $utilisateur){ + $query = "SELECT * FROM Enigme + WHERE points IS NULL OR points = 0"; + $this->con->executeQuery($query); + $results = $this->con->getResults(); + $query= "SELECT max(p.id) + FROM PARTIE p;"; + $this->con->executeQuery($query); + $max=$this->con->getResults()[0]["max"]; + $partie=PartieFactory::createPartieSolo($max,$results); + $query= "INSERT INTO Partie VALUES (:idPartie,:idEnigme)"; + $this->con->executeQuery($query, array(':idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER))); + foreach($partie->getListeEnigme() as $Enigme){ + $query= "INSERT INTO Contenir VALUES (:idPartie, :idEnigme)"; + $this->con->executeQuery($query, array( + ':idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER), + ':idEnigme' => array($Enigme->getIdEnigme(), SQLITE3_INTEGER))); + } + $query= "INSERT INTO Participer VALUES (:idPartie, :idJoueur, FALSE)"; + $this->con->executeQuery($query, array( + 'idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER), + 'idJoueur' => array($utilisateur->getEmail(), SQLITE3_INTEGER))); + } + public function delete(string $idPartie){ $query= "DELETE FROM Partie WHERE id = :idPartie"; $this->con->executeQuery($query, array(':idPartie' => array($idPartie, SQLITE3_INTEGER))); @@ -54,7 +79,7 @@ class PartieGateway $query= "SELECT max(p.id) FROM PARTIE p;"; $this->con->executeQuery($query); - $max=$this->con->getResults()[0]; + $max=$this->con->getResults()[0]["max"]; $listePartieHistory=array(); foreach($results as $row) { diff --git a/WEB/Controller/UtilisateurGateway.php b/WEB/Controller/UtilisateurGateway.php index 7e87d809..e1df5436 100644 --- a/WEB/Controller/UtilisateurGateway.php +++ b/WEB/Controller/UtilisateurGateway.php @@ -91,9 +91,9 @@ class UtilisateurGateway $this->con->executeQuery($query, array( ':emailUtilisateur' => array($emailUtilisateur,SQLITE3_TEXT) )); - $idPartie=$this->con->getResults()[0]; + $idPartie=$this->con->getResults()[0]["partie"]; - $query="SELECT e.id FROM Resoudre r, Contenir c1,Contenir c2, Enigme e WHERE + $query="SELECT e.id, c2.indexEnigme FROM Resoudre r, Contenir c1,Contenir c2, Enigme e WHERE r.id = :idPartie AND r.joueur=:idJoueur AND r.partie=:idPartie @@ -104,29 +104,94 @@ class UtilisateurGateway AND r.temps = (SELECT max(r.temps))"; $results=$this->con->getResults(); if(empty($results)) - $query="SELECT enigme FROM Contenir WHERE + { + $query="SELECT c.enigme, c.indexEnigme FROM Contenir c WHERE c.partie = :idPartie AND c.indexEnigme = 0"; $results=$this->con->getResults(); - $idEnigme=$results[0]; + } + $idEnigme=$results[0]["enigme"]; + $index=$results[0]["indexEnigme"]; $query="SELECT max(classement) FROM Enigme e,Partie p, Resoudre r - WHERE p.id=r.partie; + WHERE p.id=r.partie AND e.id=r.enigme"; $this->con->executeQuery($query); $results=$this->con->getResults(); if(empty($results)) $classement=1; else - $classement=$results[0]+1; + $classement=$results[0]["max"]+1; + + $query="SELECT * FROM Resoudre + WHERE r.joueur=:joueur + AND r.enigme=:idEnigme + AND r.partie=:idPartie"; + $this->con->executeQuery($query, array( + "joueur" => array($emailUtilisateur, SQLITE3_TEXT), + "enigme" => array($idEnigme, SQLITE3_INTEGER), + "partie" => array($idPartie, SQLITE3_INTEGER))); + $results=$this->con->getResults(); + if(empty($results)) + { + $query="INSERT INTO Resoudre VALUES (:joueur, :enigme,:partie,:classement,:index,:temps,TRUE)"; + $this->con->executeQuery($query, array( + "joueur" => array($emailUtilisateur, SQLITE3_TEXT), + "enigme" => array($idEnigme, SQLITE3_INTEGER), + "partie" => array($idPartie, SQLITE3_INTEGER), + "classement" => array($classement, SQLITE3_INTEGER), + "index" => array($index, SQLITE3_INTEGER), + "temps" => array($emailUtilisateur, SQLITE3_FLOAT))); + } + else + { + $query="UPDATE Resoudre + SET classement=:classement; + WHERE joueur=:joueur + AND enigme=:idEnigme + AND partie=:idPartie"; + $this->con->executeQuery($query, array( + "joueur" => array($emailUtilisateur, SQLITE3_TEXT), + "enigme" => array($idEnigme, SQLITE3_INTEGER), + "partie" => array($idPartie, SQLITE3_INTEGER), + "classement" => array($classement, SQLITE3_INTEGER))); + } + } + + public function passerEnigmeMulti(string $emailUtilisateur){ + $query="SELECT p.partie FROM Participer + WHERE p.joueur = :emailUtilisateur + and p.enCours = TRUE"; + $this->con->executeQuery($query, array( + ':emailUtilisateur' => array($emailUtilisateur,SQLITE3_TEXT) + )); + $idPartie=$this->con->getResults()[0]["partie"]; + + $query="SELECT e.id, c2.indexEnigme FROM Resoudre r, Contenir c1,Contenir c2, Enigme e WHERE + r.id = :idPartie + AND r.joueur=:idJoueur + AND r.partie=:idPartie + AND r.temps IS NOT NULL + AND c1.partie = r.partie + AND c2.index=c1.index + 1 + AND e.id = c1.enigme + AND r.temps = (SELECT max(r.temps))"; + $results=$this->con->getResults(); + if(empty($results)) + $query="SELECT c.enigme, c.indexEnigme FROM Contenir c WHERE + c.partie = :idPartie + AND c.indexEnigme = 0"; + $results=$this->con->getResults(); + $idEnigme=$results[0]["enigme"]; + $index=$results[0]["indexEnigme"]; $query="INSERT INTO Resoudre VALUES (:joueur, :enigme,:partie,:classement,:index,:temps,TRUE)"; $this->con->executeQuery($query, array( "joueur" => array($emailUtilisateur, SQLITE3_TEXT), - "enigme" => array($idEnigme, SQLITE3_TEXT), - "partie" => array($idPartie, SQLITE3_TEXT), - "classement" => array($classement, SQLITE3_INTEGER), - "index" => array(, SQLITE3_INTEGER), + "enigme" => array($idEnigme, SQLITE3_INTEGER), + "partie" => array($idPartie, SQLITE3_INTEGER), + "classement" => array(NULL, SQLITE3_NULL), + "index" => array($index, SQLITE3_INTEGER), "temps" => array($emailUtilisateur, SQLITE3_FLOAT))); } diff --git a/WEB/Factory/PartieFactory.php b/WEB/Factory/PartieFactory.php index f0f1d438..58f3bb53 100644 --- a/WEB/Factory/PartieFactory.php +++ b/WEB/Factory/PartieFactory.php @@ -2,7 +2,7 @@ include_once "EnigmeFactory.php"; include_once "../Model/Partie.php"; class PartieFactory{ - public static function createPartie($idMax,$resultsEnigme) : Partie + public static function createPartieMulti($idMax,$resultsEnigme) : Partie { $tempsResolutionPartie=0; $tabEnigme=array(); @@ -11,16 +11,31 @@ class PartieFactory{ while($tempsResolutionPartie <= 30) { $randomNumber=$tabIndex[array_rand($tabIndex)]; - $tabEnigme=EnigmeFactory::create($resultsEnigme[$randomNumber]); + $tabEnigme[]=EnigmeFactory::create($resultsEnigme[$randomNumber]); $TempsResolutionPartie+=$resultsEnigme[$randomNumber]['tempsDeResolution']; unset($tabIndex[$randomNumber]); } - $partie=new Partie($idMax,$resultsEnigme); + $partie=new Partie($idMax+1,$tabEnigme); + return $partie; + } + + public static function createPartieSolo($idMax,$resultsEnigme) : Partie + { + $tabEnigme=array(); + foreach($resultsEnigme as $row) + { + $tabEnigme[]=EnigmeFactory::create($row); + } + $partie=new Partie($idMax+1,$tabEnigme); return $partie; } public static function createPartieHistory($id,$resultsEnigme) : Partie { - $partie=new Partie($id,$resultsEnigme); + foreach($resultsEnigme as $row) + { + $tabEnigme[]=EnigmeFactory::create($row); + } + $partie=new Partie($id,$tabEnigme); return $partie; } } \ No newline at end of file