diff --git a/WEB/Controller/EnigmeGateway.php b/WEB/Controller/EnigmeGateway.php index 796e5c66..e1c733a9 100644 --- a/WEB/Controller/EnigmeGateway.php +++ b/WEB/Controller/EnigmeGateway.php @@ -68,6 +68,16 @@ class EnigmeGateway return $tabEnigme; } + public function findEnigmaFromPartie(string $idPartie){ + $query = "SELECT * FROM Enigme e, Contenir c + WHERE c.partie=:idPartie"; + $this->con->executeQuery($query, array( + 'idPartie' => array($idPartie, SQLITE3_TEXT) + )); + $tabEnigme=EnigmeFactory::create($this->con->getResults()); + return $tabEnigme; + } + /* public function findById(string $idEnigme) : array { $query="SELECT * FROM Enigme WHERE idEnigme =:idEnigme"; @@ -86,7 +96,7 @@ class EnigmeGateway $results = $this->con->getResults(); $tabEnigme=EnigmeFactory::create($results); return $tabEnigme; - } + }*/ public function showAll(): void { diff --git a/WEB/Controller/PartieGateway.php b/WEB/Controller/PartieGateway.php index 7fb6498f..fd750a82 100644 --- a/WEB/Controller/PartieGateway.php +++ b/WEB/Controller/PartieGateway.php @@ -21,17 +21,27 @@ class PartieGateway /** * @param array $listeJoueur */ - - public function creerPartieMulti(array $listeJoueur){ - $query = "SELECT * FROM Enigme - WHERE points IS NOT NULL OR points != 0"; + public function partieInQueueExists() : bool{ + $query = "SELECT count(*) FROM PARTICIPER WHERE etat = 0"; $this->con->executeQuery($query); $results = $this->con->getResults(); - $query= "SELECT max(p.id) - FROM PARTIE p;"; + if ($results[0]['count'] == 0) + return false; + else + return true; + } + + public function findPartieMaxId() : int{ + $query = "SELECT max(id) FROM Partie"; $this->con->executeQuery($query); - $max=$this->con->getResults()[0]["max"]; - $partie=PartieFactory::createPartieMulti($max,$results); + $results=$this->con->getResults(); + if (empty($results)) + return 0; + else + return $results[0]['max']; + } + public function creerPartieMulti(int $max, array $tabEnigme) : Partie{ + $partie=PartieFactory::createPartieMulti($max+1,$tabEnigme); $query= "INSERT INTO Partie VALUES (:idPartie,:idEnigme)"; $this->con->executeQuery($query, array(':idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER))); foreach($partie->getListeEnigme() as $Enigme){ @@ -40,12 +50,17 @@ class PartieGateway ':idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER), ':idEnigme' => array($Enigme->getIdEnigme(), SQLITE3_INTEGER))); } - foreach($listeJoueur as $Joueur){ - $query= "INSERT INTO Participer VALUES (:idPartie, :idJoueur, TRUE)"; - $this->con->executeQuery($query, array( - ':idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER), - ':idEnigme' => array($Enigme->getIdEnigme(), SQLITE3_INTEGER))); - } + return $partie; + } + + public function findPartieInQueue(){ + $query = "SELECT id + FROM Partie p, Participer pa + WHERE pa.etat=0 + AND pa.partie=p.id"; + $this->con->executeQuery($query); + $results = $this->con->getResults(); + return $results[0]['id']; } public function creerPartieSolo(Utilisateur $utilisateur){ diff --git a/WEB/Controller/UtilisateurGateway.php b/WEB/Controller/UtilisateurGateway.php index 91b86efd..27a654d4 100644 --- a/WEB/Controller/UtilisateurGateway.php +++ b/WEB/Controller/UtilisateurGateway.php @@ -78,6 +78,23 @@ class UtilisateurGateway return $estAdmin; } + public function addToQueue(Utilisateur $utilisateur,Partie $partie){ + $query = "INSERT INTO Participer VALUES (:idUtilisateur,:idPartie)"; + $this->con->executeQuery($query,array( + 'idUtilisateur' => array($utilisateur->getEmail(),SQLITE3_TEXT), + 'idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER) + )); + } + + public function queueFilled(){ + $query = "SELECT count(*) FROM Participer WHERE etat=0"; + $this->con->executeQuery($query); + if ($this->con->getResults()[0]['count'] >= 4) + return True; + else + return False; + } + /*public function resoudreEnigmeMulti(string $emailUtilisateur) { $query="SELECT c.partie FROM Contenir diff --git a/WEB/Factory/PartieFactory.php b/WEB/Factory/PartieFactory.php index de23d7c0..6e9ff6d7 100644 --- a/WEB/Factory/PartieFactory.php +++ b/WEB/Factory/PartieFactory.php @@ -1,19 +1,19 @@ getTempsDeResolution(); unset($tabIndex[$randomNumber]); } - $partie=new Partie($idMax+1,$tabEnigme); + $partie=new Partie($newId,$tabEnigme); return $partie; } diff --git a/WEB/Model/UserModel.php b/WEB/Model/UserModel.php index 99a3607f..3c6afbd6 100644 --- a/WEB/Model/UserModel.php +++ b/WEB/Model/UserModel.php @@ -60,6 +60,20 @@ class UserModel $error = "404"; require($rep . $vues['erreur']); } + if ($this->partie_gateway->partieInQueueExists()) { + $tabEnigme = $this->enigme_gateway->findMultiEnigma(); + $idNewPartie = $this->partie_gateway->findPartieMaxId(); + $partie=$this->partie_gateway->creerPartieMulti($idNewPartie,$tabEnigme); + } + else{ + $IdPartieInQueue = $this->partie_gateway->findPartieInQueue(); + $tabEnigme = $this->enigme_gateway->findEnigmaFromPartie($Idpartie); + $partie = $this->partie_gateway->creerPartieMulti($IdPartieInQueue, $tabEnigme); + } + $this->utilisateur_gateway->addToQueue($_SESSION['utilisateur'],$partie); + if ($this->utilisateur_gateway->queueFilled()) + echo "Partie is launched" + // $this->launchGame(); } public function logout() diff --git a/WEB/Model/VisitorModel.php b/WEB/Model/VisitorModel.php index 23ffb231..5fa7839f 100644 --- a/WEB/Model/VisitorModel.php +++ b/WEB/Model/VisitorModel.php @@ -42,9 +42,10 @@ class VisitorModel throw (new Exception("Email déjà utilisé")); } $password = password_hash($_REQUEST['password'] . $sel, PASSWORD_DEFAULT); - $Utilisateur = new Utilisateur($_REQUEST['email'], $_REQUEST['username'], $password, false); - $this->utilisateur_gateway->insert($Utilisateur); + $utilisateur = new Utilisateur($_REQUEST['email'], $_REQUEST['username'], $password, false); + $this->utilisateur_gateway->insert($utilisateur); $_SESSION['role'] = 'user'; + $_SESSION['utilisateur'] = $utilisateur; require($rep . $vues['main']); } catch (PDOException $e) { $error = "Erreur de connexion à la base de données."; @@ -58,8 +59,8 @@ class VisitorModel { global $rep, $vues, $sel, $error; try { - $Utilisateur = $this->utilisateur_gateway->getUtilisateurByEmail($_REQUEST['email']); - if ($Utilisateur->getEmail() == "null") { + $utilisateur = $this->utilisateur_gateway->getUtilisateurByEmail($_REQUEST['email']); + if ($utilisateur->getEmail() == "null") { $error = "Utilisateur non trouvé."; throw new Exception("Utilisateur introuvable"); } @@ -74,7 +75,7 @@ class VisitorModel } else { $_SESSION['role'] = "user"; } - + $_SESSION['utilisateur'] = $utilisateur; $_SESSION['connected'] = 'true'; require($rep . $vues['main']); } catch (Exception $e) { diff --git a/WEB/bd.sql b/WEB/bd.sql index 853376cc..b22c665d 100644 --- a/WEB/bd.sql +++ b/WEB/bd.sql @@ -54,6 +54,6 @@ PRIMARY KEY(partie, enigme) CREATE TABLE Participer( partie int REFERENCES Partie(id), joueur varchar(50) REFERENCES Joueur(email), - etat int CHECK (etat IN (0,1,2)), + etat int CHECK (etat IN (0,1,2)), -- etat 0 = enAttente etat 1 = enCours etat 2 = fini PRIMARY KEY(partie, joueur) );