Commits (5)
......@@ -17,7 +17,6 @@ class PartieSquadro {
// Constructeur : Crée une nouvelle partie avec un premier joueur
public function __construct(JoueurSquadro $playerOne) {
$this->joueurs[self::PLAYER_ONE] = $playerOne;
$this->joueurs[self::PLAYER_TWO] = null;
$this->joueurActif = self::PLAYER_ONE;
$this->plateau = new PlateauSquadro();
}
......@@ -59,12 +58,12 @@ class PartieSquadro {
// Convertit l'état de la partie en JSON
public function toJson(): string {
$joueursJson = array_map(function ($joueur) {
return $joueur->toJson();
}, $this->getJoueurs());
return json_encode([
'partieId' => $this->partieId,
'joueurs' => [
self::PLAYER_ONE => $this->joueurs[self::PLAYER_ONE]->getNomJoueur(),
self::PLAYER_TWO => ($this->joueurs[self::PLAYER_TWO] != null)?$this->joueurs[self::PLAYER_TWO]->getNomJoueur() : ""
],
'joueurs' => $joueursJson,
'joueurActif' => $this->joueurActif,
'gameStatus' => $this->gameStatus,
'plateau' => $this->plateau->toJson()
......@@ -78,11 +77,11 @@ class PartieSquadro {
*/
public static function fromJson(string $json): PartieSquadro {
$data = json_decode($json, true);
$joueurs = $data['joueurs'];
$partie = new PartieSquadro($joueurs[self::PLAYER_ONE]);
$joueursJson = $data['joueurs'];
$partie = new PartieSquadro(JoueurSquadro::fromJson($joueursJson[self::PLAYER_ONE]));
if (isset($joueurs[self::PLAYER_TWO])) {
$partie->addJoueur($joueurs[self::PLAYER_TWO]);
$partie->addJoueur(JoueurSquadro::fromJson($joueursJson[self::PLAYER_TWO]));
}
$partie->setPartieID($data['partieId']);
......
......@@ -288,12 +288,13 @@ class SquadroUIGenerator
</div>
<div class="card has-text-centered"> <header class = "card-header"><h2 class="card-header-title has-text-centered">Rejoindre une partie</h2> </header> ';
foreach ($allGames as $game) {
if ($game['gamestatus'] === 'constructed' && $game['playerone'] !== $_SESSION['player']->id) {
if ($game['gamestatus'] === 'initialized') {
$data = PartieSquadro::fromJson($game['json']);
$disabled = $data->getNomJoueurActif()===$player->getNomJoueur();
$playeroneName = $data->getJoueurs()[PartieSquadro::PLAYER_ONE]->getNomJoueur();
$html .= '<form class="card-content" action="traiteActionSquadro.php" method="post"> <span>Partie créée par: ' . $playeroneName . "</span>";
$html .= '<form class="card-content" action="traiteActionSquadro.php" method="post"> <span>Partie créée par: ' . $playeroneName . " </span>";
$html .= '<input type="hidden" name="partieid" value="' . $game['partieid'] . '>"</input>';
$html .= '<button class="button is-small" type="submit" name="action" value="rejoindrePartie"> > </button></form>';
$html .= '<button class="button is-small" type="submit" name="action" value="rejoindrePartie"'.($disabled ? "disabled":"").'> > </button></form>';
}
}
$html .= '</div> <div class="card has-text-centered"> <header class = "card-header"><h2 class="card-header-title"">Consulter une partie terminée</h2> </header> ';
......
......@@ -2,14 +2,15 @@
require_once 'ActionSquadro.php';
require_once 'SquadroUIGenerator.php';
require_once 'env/db.php';
require_once 'PDOSquadro.php';
require_once 'PartieSquadro.php';
require_once 'JoueurSquadro.php';
session_start();
if (!isset($_POST['action'])) {
$_SESSION['etat'] = 'erreur';
} else {
$action = $_POST['action'];
$plateau = $_SESSION['plateau'];
$actionSquadro = $_SESSION['actionSquadro'];
switch ($action) {
case "commencerPartie" :
if ($_SESSION['etat'] != 'accueil') {
......@@ -75,7 +76,6 @@ if (!isset($_POST['action'])) {
PDOSquadro::createPartieSquadro($_SESSION['player']->getNomJoueur(), $json);
$_SESSION['etat'] = 'home';
$_SESSION['partieCreer'] = true;
header('Location: index.php');
break;
......@@ -96,7 +96,7 @@ if (!isset($_POST['action'])) {
// Mettre à jour l'état de la session
$_SESSION['gameId'] = $gameId;
$_SESSION['etat'] = 'choixPiece';
$_SESSION['etat'] = 'home';
}
} else {
$_SESSION['etat'] = 'erreur';
......