PartieSquadro.php 3,18 ko
Newer Older
<?php

require_once 'JoueurSquadro.php'; // Assurez-vous d'avoir la classe JoueurSquadro
require_once 'PlateauSquadro.php'; // Assurez-vous d'avoir la classe PlateauSquadro

class PartieSquadro {
    // Définition des constantes pour identifier les joueurs
    public const PLAYER_ONE = 0;
    public const PLAYER_TWO = 1;

    private ?int $partieId = 0;
    private array $joueurs = [];
    public int $joueurActif;
    public string $gameStatus = 'initialized';
    public PlateauSquadro $plateau;

    // 🔹 Constructeur : Crée une nouvelle partie avec un premier joueur
    public function __construct(JoueurSquadro $playerOne) {
        $this->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]->getNom();
    }

    // 🔹 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]->getNom(),
                self::PLAYER_TWO => $this->joueurs[self::PLAYER_TWO]->getNom() ?? null
            ],
            'joueurActif' => $this->joueurActif,
            'gameStatus' => $this->gameStatus,
            'plateau' => $this->plateau->toJson()
        ]);
    }

    // 🔹 Restaure une partie à partir d'un JSON
    public static function fromJson(string $json, array $joueurs): PartieSquadro {
        $data = json_decode($json, true);
        $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();
    }
}