joueurs[self::PLAYER_ONE] = $playerOne; $this->joueurActif = self::PLAYER_ONE; $this->plateau = new PlateauSquadro(); } // Ajoute un deuxième joueur à la partie public function addJoueur(JoueurSquadro $player): void { if (!isset($this->joueurs[self::PLAYER_TWO])) { $this->joueurs[self::PLAYER_TWO] = $player; $this->gameStatus = 'waitingForPlayer'; // La partie peut commencer } else { throw new Exception("La partie a déjà deux joueurs !"); } } // Récupère le joueur actif public function getJoueurActif(): JoueurSquadro { return $this->joueurs[$this->joueurActif]; } // Retourne le nom du joueur actif public function getNomJoueurActif(): string { return $this->joueurs[$this->joueurActif]->getNomJoueur(); } // Retourne l'ID de la partie public function getPartieID(): int { return $this->partieId; } // Définit l'ID de la partie public function setPartieID(int $id): void { $this->partieId = $id; } // Retourne la liste des joueurs public function getJoueurs(): array { return $this->joueurs; } // Convertit l'état de la partie en JSON public function toJson(): string { return json_encode([ 'partieId' => $this->partieId, 'joueurs' => [ self::PLAYER_ONE => $this->joueurs[self::PLAYER_ONE]->getNomJoueur(), self::PLAYER_TWO => $this->joueurs[self::PLAYER_TWO]->getNomJoueur() ?? null ], 'joueurActif' => $this->joueurActif, 'gameStatus' => $this->gameStatus, 'plateau' => $this->plateau->toJson() ]); } // Restaure une partie à partir d'un JSON /** * @throws Exception */ public static function fromJson(string $json): PartieSquadro { $data = json_decode($json, true); $joueurs = $data['joueurs']; $partie = new PartieSquadro($joueurs[self::PLAYER_ONE]); if (isset($joueurs[self::PLAYER_TWO])) { $partie->addJoueur($joueurs[self::PLAYER_TWO]); } $partie->setPartieID($data['partieId']); $partie->joueurActif = $data['joueurActif']; $partie->gameStatus = $data['gameStatus']; $partie->plateau = PlateauSquadro::fromJson($data['plateau']); return $partie; } // Convertit l'objet en une représentation textuelle public function __toString(): string { return "Partie ID: " . $this->partieId . " | Joueur Actif: " . $this->getNomJoueurActif(); } }