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.
Scripted/WEB/Controller/PartieGateway.php

119 lines
4.7 KiB

<?php
class PartieGateway
{
private Connection $con;
/**
* @param Connection $con
*/
public function __construct()
{
global $dsn, $rep, $vues, $error;
try{
$con = new Connection($dsn);
$this->con = $con;
} catch (Exception $e) {
$error = $e->getMessage();
require($rep . $vues['erreur']);
}
}
/**
* @param array $listeJoueur
*/
public function creerPartieMulti(array $listeJoueur){
$query = "SELECT * FROM Enigme
WHERE points IS NOT NULL OR points != 0";
$this->con->executeQuery($query);
$results = $this->con->getResults();
$query= "SELECT max(p.id)
FROM PARTIE p;";
$this->con->executeQuery($query);
$max=$this->con->getResults()[0]["max"];
$partie=PartieFactory::createPartieMulti($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)";
$this->con->executeQuery($query, array(
':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)));
}
}
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)
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)";
$this->con->executeQuery($query, array(':idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER)));
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)));
}
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)));
}
$query= "INSERT INTO Participer VALUES (:idPartie, :idJoueur, TRUE)";
$this->con->executeQuery($query, array(
'idPartie' => array($partie->getIdPartie(), SQLITE3_INTEGER),
'idJoueur' => array($utilisateur->getEmail(), SQLITE3_INTEGER)));
}
public function delete(string $idPartie){
$query= "DELETE FROM Partie WHERE id = :idPartie";
$this->con->executeQuery($query, array(':idPartie' => array($idPartie, SQLITE3_INTEGER)));
}
public function findPartieHistory() : array{
$query="SELECT * FROM Partie";
$this->con->executeQuery($query);
$results = $this->con->getResults();
$query= "SELECT max(p.id)
FROM PARTIE p;";
$this->con->executeQuery($query);
$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);
}
return $listePartieHistory;
}
public function showAll() : void{
$query= "SELECT * FROM Partie";
$this->con->executeQuery($query);
$results=$this->con->getResults();
foreach ($results as $row) {
echo $row['idPartie'] . '</br>';
}
}
}