You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
5.7 KiB
156 lines
5.7 KiB
<?php
|
|
class UserModel
|
|
{
|
|
|
|
private EnigmeGateway $enigme_gateway;
|
|
private PartieGateway $partie_gateway;
|
|
private UtilisateurGateway $utilisateur_gateway;
|
|
private ResoudreGateway $resoudre_gateway;
|
|
private Nettoyage $nettoyage;
|
|
private Validation $validation;
|
|
|
|
function __construct()
|
|
{
|
|
try {
|
|
global $error, $view, $rep;
|
|
$this->enigme_gateway = new EnigmeGateway();
|
|
$this->partie_gateway = new PartieGateway();
|
|
$this->utilisateur_gateway = new UtilisateurGateway();
|
|
$this->resoudre_gateway = new ResoudreGateway();
|
|
$this->nettoyage = new Nettoyage();
|
|
$this->validation = new Validation();
|
|
} catch (Exception $e) {
|
|
$error = $e->getMessage();
|
|
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;
|
|
// }
|
|
// 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);
|
|
}
|
|
else{
|
|
$this->partie_gateway->addToPartie($mailUtilisateur, $idPartie);
|
|
$etat=$this->partie_gateway->getEtat($idPartie);
|
|
return array($idPartie, $etat);
|
|
}
|
|
}
|
|
|
|
public function findUserGroup() : int {
|
|
return $this->partie_gateway->findPartieInQueue();
|
|
}
|
|
|
|
public function launchGame()
|
|
{
|
|
$this->utilisateur_gateway->launchGame($_SESSION['utilisateur']);
|
|
}
|
|
|
|
public function logout()
|
|
{
|
|
session_unset();
|
|
session_destroy();
|
|
$_SESSION = array();
|
|
$_SESSION['role'] = 'visitor';
|
|
header('Location: index.php');
|
|
}
|
|
|
|
public function findUsersInQueue() : array
|
|
{
|
|
return $this->utilisateur_gateway->findUsersInQueue();
|
|
}
|
|
|
|
public function getEnigmeByOrdre(int $num) : Enigme
|
|
{
|
|
$tabEnigme = $this->enigme_gateway->findByOrdre($num);
|
|
if ($tabEnigme == null) {
|
|
$last = $this->enigme_gateway->findLastEnigmaByOrdre();
|
|
if ($last[0]->getOrdre() == ($num-1)){
|
|
return $this->enigme_gateway->findByOrdre($num-1)[0];
|
|
}
|
|
throw new Exception("Enigme non trouvée");
|
|
}
|
|
return $tabEnigme[0];
|
|
}
|
|
|
|
public function resoudreEnigmeSolo(Utilisateur $utilisateur, int $enigmeId){
|
|
if($this->resoudre_gateway->checkPartieIsAlreadyCreate($enigmeId)){
|
|
$partieId = $this->partie_gateway->findPartieByEnigmeId($enigmeId)->getIdPartie();
|
|
} else {
|
|
$enigme=$this->enigme_gateway->findById($enigmeId)[0];
|
|
$this->partie_gateway->createPartie($enigme, $utilisateur);
|
|
$partieId = $this->partie_gateway->findLastPartie()->getIdPartie();
|
|
}
|
|
$this->resoudre_gateway->resoudreEnigmeSolo($utilisateur, $enigmeId, $partieId);
|
|
}
|
|
public function checkEnigmeIsEnded(string $mailUtilisateur, int $enigmeId) : bool {
|
|
return $this->resoudre_gateway->checkEnigmeIsEnded($mailUtilisateur,$enigmeId);
|
|
}
|
|
|
|
public function enigmeEnded(string $mailUtilisateur, int $enigmeId){
|
|
$this->resoudre_gateway->enigmeEnded($mailUtilisateur,$enigmeId);
|
|
}
|
|
|
|
public function saveCode(string $mailUtilisateur, int $enigmeId,string $code ){
|
|
$this->resoudre_gateway->saveCode($mailUtilisateur, $enigmeId, $code);
|
|
}
|
|
|
|
public function getCode(string $mailUtilisateur, int $enigmeId){
|
|
return $this->resoudre_gateway->getCode($mailUtilisateur, $enigmeId);
|
|
}
|
|
|
|
public function getLastEnigmeEnded(string $mailUtilisateur) : Enigme{
|
|
$id=$this->resoudre_gateway->getLastEnigmeEnded($mailUtilisateur);
|
|
if ($id == 0){
|
|
return $this->enigme_gateway->findByOrdre(1)[0];
|
|
}
|
|
return $this->enigme_gateway->findById($id)[0];
|
|
}
|
|
|
|
public function getEtatPartie($idPartie){
|
|
return $this->partie_gateway->getEtat($idPartie);
|
|
}
|
|
|
|
public function getLesJoueurs($idPartie){
|
|
$lesMailJoeurs = $this->partie_gateway->getLesMailJoueurs($idPartie);
|
|
$lesJoueurs = array();
|
|
foreach ($lesMailJoeurs as $mail){
|
|
$lesJoueurs[] = $this->utilisateur_gateway->getUtilisateurByEmail($mail);
|
|
}
|
|
return $lesJoueurs;
|
|
}
|
|
} |