diff --git a/WEB/Controller/UserController.php b/WEB/Controller/UserController.php index 6bc8b6fa..78e2b699 100644 --- a/WEB/Controller/UserController.php +++ b/WEB/Controller/UserController.php @@ -40,8 +40,11 @@ class UserController case "goToQueue": $this->goToQueue(); break; - case "queuing": - $this->queuing(); + case "waiting": + $this->waiting(); + break; + case "addToQueue": + $this->addToQueue(); break; case "logout": $this->logout(); @@ -211,32 +214,38 @@ class UserController require($rep . $vues['erreur']); } } - public function queuing(){ + public function waiting(){ try { global $rep, $vues, $error; $model = new UserModel(); - $utilisateur = $_SESSION['utilisateur']; - $reponse = $model->addToQueue($utilisateur->getEmail()); - $etat = $reponse[1]; - $idPartie = $reponse[0]; - $_SESSION['idPartie'] = $idPartie; - $lesJoueurs = $model->getLesJoueurs($idPartie); + $idPartie = $_SESSION['idPartie']; + $etat = $model->getEtatPartie($idPartie); + // $lesJoueurs = $model->getLesJoueurs($idPartie); if($etat == 1){ - $model->launchGame($idPartie); - header("Location: index.php?action=goToGame&idPartie=" . $idPartie . "&index=1"); + // $model->launchGame($idPartie); + $echo = "1"; + // header("Location: index.php?action=goToGame&idPartie=" . $idPartie . "&index=1"); } else{ - while ($etat != 1){ - sleep(5); - $etat = $model->getEtatPartie($idPartie); - } - header("Location: index.php?action=goToGame&idPartie=" . $idPartie . "&index=1"); + $echo = "0"; } } catch (Exception $e) { $error = $e->getMessage(); require($rep . $vues['erreur']); } } + public function addToQueue(){ + try { + global $rep, $vues, $error; + $model = new UserModel(); + $utilisateur = $_SESSION['utilisateur']; + $idPartie = $model->addToQueue($utilisateur->getEmail()); + $_SESSION['idPartie'] = $idPartie; + } catch (Exception $e) { + $error = $e->getMessage(); + require($rep . $vues['erreur']); + } + } public function goToGame() { diff --git a/WEB/Model/UserModel.php b/WEB/Model/UserModel.php index 9bf24411..27b6870e 100644 --- a/WEB/Model/UserModel.php +++ b/WEB/Model/UserModel.php @@ -25,7 +25,7 @@ class UserModel require($rep . $view['erreur']); } } - public function addToQueue($mailUtilisateur) : array{ + public function addToQueue($mailUtilisateur) : int{ $idPartie=$this->partie_gateway->findPartieInQueue(); if ($idPartie == 0){ $enigme= $this->enigme_gateway->getRandomEnigme(); @@ -40,7 +40,7 @@ class UserModel } $tpsMaxPartie = $this->calculTpsMaXPartie($idPartie); $_SESSION['tpsMaxPartie'] = $tpsMaxPartie; - return array($idPartie, $etat); + return $idPartie; } public function calculTpsMaXPartie( int $idPartie) : int { diff --git a/WEB/View/src/JS/Queu.js b/WEB/View/src/JS/Queu.js index d4f01f61..ffc22587 100644 --- a/WEB/View/src/JS/Queu.js +++ b/WEB/View/src/JS/Queu.js @@ -1,3 +1,21 @@ function displayWaiting(){ document.getElementById("waiting").innerHTML = "Waiting for Coders..."; + document.getElementsByClassName("btn").style.display = "none" +} + +function checkIsReady() { + var xhr = new XMLHttpRequest(); + xhr.open('POST', 'http://82.165.180.114/Scripted/WEB/index.php?action=waiting', true); + xhr.responseType = 'text'; + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + xhr.onload = function () { + if (xhr.responseText == "1") { + window.location.href = 'http://82.165.180.114/Scripted/WEB/index.php?action=goToGame&idPartie=" . $idPartie . "&index=1"' + } + }; + xhr.send(null); +} +function waiting(){ + displayWaiting(); + var countdownTimer = setInterval('checkIsReady()', 1000); } \ No newline at end of file diff --git a/WEB/View/src/pages/Multijoueur/FileAttente.php b/WEB/View/src/pages/Multijoueur/FileAttente.php index e0763f26..a540ea5f 100644 --- a/WEB/View/src/pages/Multijoueur/FileAttente.php +++ b/WEB/View/src/pages/Multijoueur/FileAttente.php @@ -59,7 +59,7 @@