<?php

class JoueurInfos {
  public $id;
  public $nom;
  public $partie_id;
  public $plateau;
  public $statut;
  public $tresor;
  public $diamant;
  public $or;
  public $meeple;
  public $temple;
  public $dernier_coup;

  private $_exist;

  public function __construct() {
    $this->_exist = false;
  }

  public function init($id) {
    $this->id = $id;
    $this->refresh();
  }

  public function exist() {
    return $this->_exist;
  }

  public function creer($nom, $partie_id) {
    global $db, $__CONF;

    $db->req("INSERT INTO ".$__CONF['dbtablepre']."joueurs(nom, partie_id) VALUES ('".$nom."', ".$partie_id.");");
    return $db->reqFirst("SELECT id FROM ".$__CONF['dbtablepre']."joueurs WHERE partie_id=".$partie_id." ORDER BY id DESC LIMIT 1;")['id'];
  }

  public function refresh() {
    global $db, $__CONF;

    $req = $db->reqFirst("SELECT * FROM ".$__CONF['dbtablepre']."joueurs WHERE id=".$this->id.";");

    if(!$req) {
      $this->_exist = false;
      return;
    }

    $this->nom = $req['nom'];
    $this->partie_id = $req['partie_id'];
    $this->statut = $req['statut'];
    $this->diamant = $req['pepite_diamant'];
    $this->or = $req['pepite_or'];
    $this->dernier_coup = $req['dernier_coup'];

    for($i = 0 ; $i < 4 ; $i++) {
      $this->temple[$i] = $req['position_temple'.($i + 1)];
      $this->meeple[$i] = $req['position_meeple'.($i + 1)];
      $this->tresor[$i] = $req['tresor'.($i + 1)];
    }

    $this->plateau = explode(',', $req['plateau']);

    $this->_exist = true;
  }

  public function plateauTxt() {
    if(!$this->_exist) return false;

    $plateauTxt = '';
    for($i = 0 ; $i < count($this->plateau) ; $i++) {
      if($i > 0) $plateauTxt .= ',';
      $plateauTxt .= $this->plateau[$i];
    }
    return $plateauTxt;
  }
}

?>