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