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.
128 lines
4.6 KiB
128 lines
4.6 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';
|
|
var_dump($this->utilisateur_gateway->isAlreadyInQueue($_SESSION['utilisateur']));
|
|
if ($this->utilisateur_gateway->isAlreadyInqueue($_SESSION['utilisateur'])) {
|
|
var_dump($this->utilisateur_gateway->queueFilled());
|
|
if($this->utilisateur_gateway->queueFilled())
|
|
{
|
|
$_REQUEST['action']='launchGame';
|
|
var_dump($_REQUEST['action']);
|
|
}
|
|
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($_SESSION['utilisateur'], $partie);
|
|
}
|
|
|
|
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)+1;
|
|
if ($id-1 == 0){
|
|
return $this->enigme_gateway->findByOrdre(1)[0];
|
|
}
|
|
return $this->enigme_gateway->findById($id)[0];
|
|
}
|
|
} |