diff --git a/WEB/Controller/EnigmeGateway.php b/WEB/Controller/EnigmeGateway.php index f9545f15..67a8dd85 100644 --- a/WEB/Controller/EnigmeGateway.php +++ b/WEB/Controller/EnigmeGateway.php @@ -169,6 +169,15 @@ class EnigmeGateway $tabEnigme=EnigmeFactory::create($results); return $tabEnigme; } + + public function getRandomEnigme() : Enigme + { + $query = "SELECT * FROM Enigme WHERE ordre = 0 ORDER BY RANDOM() LIMIT 1"; + $this->con->executequery($query); + $results = $this->con->getResults(); + $tabEnigme=EnigmeFactory::create($results); + return $tabEnigme[0]; + } public function showAll(): void { $query = "SELECT * FROM Enigme"; diff --git a/WEB/Controller/PartieGateway.php b/WEB/Controller/PartieGateway.php index 8de62459..2c6d1a72 100644 --- a/WEB/Controller/PartieGateway.php +++ b/WEB/Controller/PartieGateway.php @@ -9,9 +9,9 @@ class PartieGateway public function __construct() { global $dsn, $rep, $vues, $error; - try{ - $con = new Connection($dsn); - $this->con = $con; + try { + $con = new Connection($dsn); + $this->con = $con; } catch (Exception $e) { $error = $e->getMessage(); require($rep . $vues['erreur']); @@ -21,7 +21,8 @@ class PartieGateway /** * @param array $listeUtilisateur */ - public function partieInQueueExists() : bool{ + public function partieInQueueExists(): bool + { $query = "SELECT count(*) FROM PARTICIPER WHERE etat = 0"; $this->con->executeQuery($query); $results = $this->con->getResults(); @@ -31,174 +32,263 @@ class PartieGateway return true; } - public function findPartieMaxId() : int{ + public function findPartieMaxId(): int + { $query = "SELECT max(id) FROM Partie"; $this->con->executeQuery($query); - $results=$this->con->getResults(); - if ($results[0]['max(id)']==NULL) + $results = $this->con->getResults(); + if ($results[0]['max(id)'] == NULL) return 0; else return $results[0]['max(id)']; } - public function creerPartieMulti(int $max, array $tabEnigme) : Partie{ - $partie=PartieFactory::createPartieMulti($max+1,$tabEnigme); - $query= "INSERT INTO Partie VALUES (:idPartie,CURRENT_DATE)"; - $this->con->executeQuery($query, array(':idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER))); - if (count($partie->getListeEnigme()) != 0) { - 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) - ) - ); - } - } - return $partie; - } + // public function creerPartieMulti(int $max, array $tabEnigme) : Partie{ + // $partie=PartieFactory::createPartieMulti($max+1,$tabEnigme); + // $query= "INSERT INTO Partie VALUES (:idPartie,CURRENT_DATE)"; + // $this->con->executeQuery($query, array(':idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER))); + // if (count($partie->getListeEnigme()) != 0) { + // 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) + // ) + // ); + // } + // } + // return $partie; + // } - public function rejoindrePartieMulti(int $idPartieInQueue,array $tabEnigme) : Partie{ - $partie = PartieFactory::createPartieMulti($idPartieInQueue,$tabEnigme); + public function rejoindrePartieMulti(int $idPartieInQueue, array $tabEnigme): Partie + { + $partie = PartieFactory::createPartieMulti($idPartieInQueue, $tabEnigme); return $partie; } - public function findPartieInQueue(){ - $query = "SELECT p.id - FROM Partie p, Participer pa - WHERE pa.etat=0 - AND pa.partie=p.id + public function findPartieInQueue() + { + $query = "SELECT partie + FROM Participer + WHERE etat=0 LIMIT 1"; $this->con->executeQuery($query); $results = $this->con->getResults(); - return $results[0]['id']; + if (empty($results)) { + return 0; + } + return $results[0]['partie']; } - public function creerPartieSolo(Utilisateur $utilisateur){ + 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) + $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)"; + $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)"; + 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))); + ':idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER), + ':idEnigme' => array($Enigme->getIdEnigme(), SQLITE3_INTEGER) + ) + ); } - foreach($partie->getListeEnigme() as $Enigme){ - $query= "INSERT INTO Contenir VALUES (:idPartie, :idEnigme)"; + 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))); + ':idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER), + ':idEnigme' => array($Enigme->getIdEnigme(), SQLITE3_INTEGER) + ) + ); } - $query= "INSERT INTO Participer VALUES (:idPartie, :idUtilisateur, TRUE)"; + $query = "INSERT INTO Participer VALUES (:idPartie, :idUtilisateur, TRUE)"; $this->con->executeQuery($query, array( - 'idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER), - 'idUtilisateur' => array($utilisateur->getEmail(), SQLITE3_INTEGER))); + 'idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER), + 'idUtilisateur' => array($utilisateur->getEmail(), SQLITE3_INTEGER) + ) + ); } - public function findLastPartie() : Partie + public function findLastPartie(): Partie { $query = "SELECT * FROM Partie ORDER BY id DESC LIMIT 1"; $this->con->executequery($query); $results = $this->con->getResults(); $row = $results[0]; - $partie=new Partie($row['id'],array()); + $partie = new Partie($row['id'], array()); $date = new DateTime($row['dateDebut']); $partie->setDatePartie($date); return $partie; } - public function createPartie(Enigme $enigme, Utilisateur $utilisateur){ - $query="INSERT INTO Partie VALUES (NULL,:date)"; + public function createPartie(Enigme $enigme, Utilisateur $utilisateur) + { + $query = "INSERT INTO Partie VALUES (NULL,:date)"; + $currentDate = date('Y-m-d H:i:s'); + $this->con->executeQuery($query, array( + "date" => array($currentDate, SQLITE3_TEXT) + ) + ); + $partie = $this->findLastPartie(); + $query = "INSERT INTO Contenir VALUES (:partie,:idEnigme,NULL)"; + $this->con->executeQuery($query, array( + "partie" => array($partie->getIdPartie(), SQLITE3_INTEGER), + "idEnigme" => array($enigme->getIdEnigme(), SQLITE3_INTEGER) + ) + ); + $query = "INSERT INTO Participer VALUES (:partie,:utilisateur,NULL)"; + $this->con->executeQuery($query, array( + "partie" => array($partie->getIdPartie(), SQLITE3_INTEGER), + "utilisateur" => array($utilisateur->getEmail(), SQLITE3_TEXT) + ) + ); + } + + public function createPartieMulti(Enigme $enigme, string $mailUtilisateur) + { + $query = "INSERT INTO Partie VALUES (NULL,:date)"; $currentDate = date('Y-m-d H:i:s'); $this->con->executeQuery($query, array( - "date" => array($currentDate, SQLITE3_TEXT))); + "date" => array($currentDate, SQLITE3_TEXT) + ) + ); $partie = $this->findLastPartie(); - $query="INSERT INTO Contenir VALUES (:partie,:idEnigme,NULL)"; + $query = "INSERT INTO Contenir VALUES (:partie,:idEnigme,1)"; $this->con->executeQuery($query, array( "partie" => array($partie->getIdPartie(), SQLITE3_INTEGER), - "idEnigme" => array($enigme->getIdEnigme(), SQLITE3_INTEGER))); - $query="INSERT INTO Participer VALUES (:partie,:utilisateur,NULL)"; + "idEnigme" => array($enigme->getIdEnigme(), SQLITE3_INTEGER) + ) + ); + $query = "INSERT INTO Participer VALUES (:partie,:utilisateur,0)"; $this->con->executeQuery($query, array( "partie" => array($partie->getIdPartie(), SQLITE3_INTEGER), - "utilisateur" => array($utilisateur->getEmail(), SQLITE3_TEXT))); + "utilisateur" => array($mailUtilisateur, SQLITE3_TEXT) + ) + ); } - public function delete(int $idPartie){ - $query= "DELETE FROM Contenir WHERE partie = :enigmeId"; + public function delete(int $idPartie) + { + $query = "DELETE FROM Contenir WHERE partie = :enigmeId"; $this->con->executeQuery($query, array(':enigmeId' => array($idPartie, SQLITE3_INTEGER))); - $query= "DELETE FROM Participer WHERE partie = :enigmeId"; + $query = "DELETE FROM Participer WHERE partie = :enigmeId"; $this->con->executeQuery($query, array(':enigmeId' => array($idPartie, SQLITE3_INTEGER))); - $query= "DELETE FROM Partie WHERE id = :idPartie"; + $query = "DELETE FROM Partie WHERE id = :idPartie"; $this->con->executeQuery($query, array(':idPartie' => array($idPartie, SQLITE3_INTEGER))); } - public function deleteByEnigme(int $enigmeId){ - $query="SELECT partie FROM Contenir WHERE enigme = :enigmeId"; + public function deleteByEnigme(int $enigmeId) + { + $query = "SELECT partie FROM Contenir WHERE enigme = :enigmeId"; $this->con->executeQuery($query, array(':enigmeId' => array($enigmeId, SQLITE3_INTEGER))); $results = $this->con->getResults(); - $query= "DELETE FROM Contenir WHERE enigme = :enigmeId"; + $query = "DELETE FROM Contenir WHERE enigme = :enigmeId"; $this->con->executeQuery($query, array(':enigmeId' => array($enigmeId, SQLITE3_INTEGER))); - foreach($results as $row){ - $query="DELETE FROM Participer WHERE partie = :partieId"; + foreach ($results as $row) { + $query = "DELETE FROM Participer WHERE partie = :partieId"; $this->con->executeQuery($query, array(':partieId' => array($row['partie'], SQLITE3_INTEGER))); - $query= "DELETE FROM Partie WHERE id = :partieId"; + $query = "DELETE FROM Partie WHERE id = :partieId"; $this->con->executeQuery($query, array(':partieId' => array($row['partie'], SQLITE3_INTEGER))); } } - public function findPartieHistory() : array{ - $query="SELECT * FROM Partie"; + public function findPartieHistory(): array + { + $query = "SELECT * FROM Partie"; $this->con->executeQuery($query); $results = $this->con->getResults(); - $query= "SELECT max(p.id) + $query = "SELECT max(p.id) FROM PARTIE p;"; $this->con->executeQuery($query); - $max=$this->con->getResults()[0]["max"]; - $listePartieHistory=array(); - foreach($results as $row) - { + $max = $this->con->getResults()[0]["max"]; + $listePartieHistory = array(); + foreach ($results as $row) { $query = "SELECT e.* FROM Enigme e,Contenir c,Partie p WHERE p.id = c.partie AND c.enigme = e.id AND c.enCours = false; AND p.partie = :idPartie"; - $this->con->executeQuery($query,array( - "idPartie" => array($row["idPartie"],SQLITE3_INTEGER) - )); - $historiquePartie=$this->con->getResults(); - $listePartieHistory[]=PartieFactory::createPartieHistory($row["idPartie"],$historiquePartie); + $this->con->executeQuery($query, array( + "idPartie" => array($row["idPartie"], SQLITE3_INTEGER) + ) + ); + $historiquePartie = $this->con->getResults(); + $listePartieHistory[] = PartieFactory::createPartieHistory($row["idPartie"], $historiquePartie); } return $listePartieHistory; } - public function findPartieByEnigmeId(int $enigmeId) : Partie{ + public function findPartieByEnigmeId(int $enigmeId): Partie + { $query = "SELECT * FROM Partie p, Resoudre r WHERE r.enigme = :enigmeId AND r.partie = p.id"; $this->con->executeQuery($query, array( ':enigmeId' => array($enigmeId, SQLITE3_INTEGER) - )); + ) + ); $results = $this->con->getResults(); $row = $results[0]; - $partie=new Partie($row['id'],array()); + $partie = new Partie($row['id'], array()); $date = new DateTime($row['dateDebut']); $partie->setDatePartie($date); return $partie; } - public function showAll() : void{ - $query= "SELECT * FROM Partie"; + public function addToPartie(string $mailUtilisateur, int $idPartie) + { + global $playerNumberPerGame; + $player = $this->getPlayerNumber($idPartie); + if ($player == $playerNumberPerGame) { + throw new Exception("La partie est pleine"); + } + if ($player + 1 == $playerNumberPerGame) { + $query = "INSERT INTO Participer VALUES (:partie,:utilisateur,1)"; + $this->con->executeQuery($query, array( + "partie" => array($idPartie, SQLITE3_INTEGER), + "utilisateur" => array($mailUtilisateur, SQLITE3_TEXT))); + } + else { + $query = "INSERT INTO Participer VALUES (:partie,:utilisateur,0)"; + $this->con->executeQuery($query, array( + "partie" => array($idPartie, SQLITE3_INTEGER), + "utilisateur" => array($mailUtilisateur, SQLITE3_TEXT))); + } + } + public function getPlayerNumber(int $idPartie): int + { + $query = "SELECT count(*) FROM Participer WHERE partie = :idPartie"; + $this->con->executeQuery($query, array( + "idPartie" => array($idPartie, SQLITE3_INTEGER) + ) + ); + $results = $this->con->getResults(); + $row = $results[0]; + return $row['count(*)']; + } + public function getEtat($idPartie){ + $query = "SELECT etat FROM Participer WHERE partie = :idPartie"; + $this->con->executeQuery($query, array( + "idPartie" => array($idPartie, SQLITE3_INTEGER) + ) + ); + $results = $this->con->getResults(); + $row = $results[0]; + return $row['etat']; + } + public function showAll(): void + { + $query = "SELECT * FROM Partie"; $this->con->executeQuery($query); - $results=$this->con->getResults(); + $results = $this->con->getResults(); foreach ($results as $row) { echo $row['idPartie'] . '
'; } diff --git a/WEB/Controller/UserController.php b/WEB/Controller/UserController.php index 1df09f7a..e2384ebb 100644 --- a/WEB/Controller/UserController.php +++ b/WEB/Controller/UserController.php @@ -169,13 +169,24 @@ class UserController public function goToQueue() { try { - global $rep, $vues, $error; //$serverAdress,$port; + global $rep, $vues, $error; $model = new UserModel(); - // $model->addToQueue(); - require($rep . $vues['queue']); - $client = Client::getInstance(); - // new Queuing(); - $client->Queuing(); + $utilisateur = $_SESSION['utilisateur']; + $reponse = $model->addToQueue($utilisateur->getEmail()); + $etat = $reponse[1]; + $idPartie = $reponse[0]; + if($etat == 1){ + // header("Location: index.php?action=launchGame"); + header("Location: index.php"); + } + else{ + echo "Vous ĂȘtes dans la fille d'attente\n"; + require($rep . $vues['queue']); + // while ($etat == 0){ + // $etat = $model->getEtatPartie($idPartie); + // } + // header("Location: index.php"); + } } catch (Exception $e) { $error = $e->getMessage(); require($rep . $vues['erreur']); diff --git a/WEB/Model/UserModel.php b/WEB/Model/UserModel.php index b178ccb4..ddc9219f 100644 --- a/WEB/Model/UserModel.php +++ b/WEB/Model/UserModel.php @@ -24,35 +24,51 @@ class UserModel require($rep . $view['erreur']); } } - public function addToQueue() - { - echo '1'; - $utilisateur = $_SESSION['utilisateur']; - if ($this->utilisateur_gateway->isAlreadyInqueue($utilisateur)) { - var_dump($this->utilisateur_gateway->queueFilled()); - if($this->utilisateur_gateway->queueFilled()) - { - header('Location: index.php?action=launchGame'); - } - return; + // public function addToQueue() + // { + // echo '1'; + // $utilisateur = $_SESSION['utilisateur']; + // if ($this->utilisateur_gateway->isAlreadyInqueue($utilisateur)) { + // var_dump($this->utilisateur_gateway->queueFilled()); + // if($this->utilisateur_gateway->queueFilled()) + // { + // header('Location: index.php?action=launchGame'); + // } + // return; + // } + // echo '2'; + // if (!$this->partie_gateway->partieInQueueExists()) { + // echo '3'; + // $tabEnigme = $this->enigme_gateway->findMultiEnigma(); + // $idNewPartie = $this->partie_gateway->findPartieMaxId(); + // $partie = $this->partie_gateway->creerPartieMulti($idNewPartie, $tabEnigme); + // } else { + // echo '4'; + // $idPartieInQueue = $this->partie_gateway->findPartieInQueue(); + // echo '5'; + // $tabEnigme = $this->enigme_gateway->findEnigmaFromPartie($idPartieInQueue); + // echo '6'; + // $partie = $this->partie_gateway->rejoindrePartieMulti($idPartieInQueue, $tabEnigme); + // echo '7'; + // } + // echo '8'; + // $this->utilisateur_gateway->addToQueue($utilisateur, $partie); + // } + + public function addToQueue($mailUtilisateur) : array{ + $idPartie=$this->partie_gateway->findPartieInQueue(); + if ($idPartie == 0){ + $enigme= $this->enigme_gateway->getRandomEnigme(); + $this->partie_gateway->createPartieMulti($enigme, $mailUtilisateur); + $idPartie=$this->partie_gateway->findPartieInQueue(); + $etat=$this->partie_gateway->getEtat($idPartie); + return array($idPartie, $etat); } - echo '2'; - if (!$this->partie_gateway->partieInQueueExists()) { - echo '3'; - $tabEnigme = $this->enigme_gateway->findMultiEnigma(); - $idNewPartie = $this->partie_gateway->findPartieMaxId(); - $partie = $this->partie_gateway->creerPartieMulti($idNewPartie, $tabEnigme); - } else { - echo '4'; - $idPartieInQueue = $this->partie_gateway->findPartieInQueue(); - echo '5'; - $tabEnigme = $this->enigme_gateway->findEnigmaFromPartie($idPartieInQueue); - echo '6'; - $partie = $this->partie_gateway->rejoindrePartieMulti($idPartieInQueue, $tabEnigme); - echo '7'; + else{ + $this->partie_gateway->addToPartie($mailUtilisateur, $idPartie); + $etat=$this->partie_gateway->getEtat($idPartie); + return array($idPartie, $etat); } - echo '8'; - $this->utilisateur_gateway->addToQueue($utilisateur, $partie); } public function findUserGroup() : int { @@ -124,4 +140,8 @@ class UserModel } return $this->enigme_gateway->findById($id)[0]; } + + public function getEtatPartie($idPartie){ + return $this->partie_gateway->getEtat($idPartie); + } } \ No newline at end of file