<?php

class PartieInfos {
  public $id;
  public $nom;
  public $pioche;
  public $statut;
  public $tresor;
  public $wait;

  public $nbjoueurs;
  public $joueurs;

  private $_tresorDejaPris = [0, 0, 0, 0];

  private $_exist;

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

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

  public function creer($nom) {
    global $db;

    $db->req("INSERT INTO parties VALUES (NULL, '".$nom."', '', 0, '', '', '', '', 0);");
    return $db->reqFirst("SELECT id FROM parties ORDER BY id DESC LIMIT 1;")['id'];
  }

  public function nextTresor() {
    $nextTresor = [];
    for($i = 0 ; $i < count($this->tresor) ; $i++)
      $nextTresor[$i] = $this->tresor[$i][0];
    return $nextTresor;
  }

  public function gagneTresor($n) {
    $tresor = $this->tresor[$n][0];
    $this->tresor[$n] = substr($this->tresor[$n], 1);
    if($this->tresorDejaPris[$n] > 0)
      $tresor = $this->_tresorDejaPris[$n];
    else
      $this->_tresorDejaPris[$n] = $tresor;
    return $tresor;
  }

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

  public function refresh() {
    global $db;

    $req = $db->reqFirst('SELECT * FROM parties WHERE id='.$this->id.';');

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

    $this->nom = $req['nom'];
    $this->statut = $req['statut'];
    $this->wait = $req['wait'];

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

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

    $this->joueurs = [];
    $req = $db->req("SELECT id FROM joueurs WHERE partie_id=".$this->id." AND statut>0;");
    while( $req1 = $db->next($req) )
      $this->joueurs[] = $req1['id'];
    $this->nbjoueurs = count($this->joueurs);

    $this->_exist = true;
  }

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

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

  public function nouvelle() {
    global $db;

    if(!$this->_exist) return false;

    $this->pioche = [ '1o', '1o', '1d', '1d', '1', '1', '1', '1',         // |
                      '2o', '2o', '2d', '2d', '2', '2', '2', '2',         // -
                      '3', '3', '3', '3',                                 // +
                      '4d', '4', '5', '5', '6', '6', '7', '7',            // T
                      '8d', '8d', '9d', '9', '10d', '10', '11d', '11' ];  // L

    shuffle($this->pioche);

    $piocheTxt = '';
    $first = true;
    foreach($this->pioche as $p) {
      if($first) $first = false;
      else $piocheTxt .= ',';
      $piocheTxt .= $p;
    }

    if($this->nbjoueurs == 2) $tresors = '53';
    if($this->nbjoueurs == 3) $tresors = '532';
    if($this->nbjoueurs == 4) $tresors = '5432';
    if($this->nbjoueurs == 5) $tresors = '54322';
    if($this->nbjoueurs == 6) $tresors = '543322';
    if($this->nbjoueurs == 7) $tresors = '5443322';
    if($this->nbjoueurs == 8) $tresors = '55443322';
    if($this->nbjoueurs == 9) $tresors = '554433222';
    if($this->nbjoueurs == 10) $tresors = '5544333222';
    if($this->nbjoueurs == 11) $tresors = '55444333222';
    if($this->nbjoueurs >= 12) $tresors = '555444333222';


    $req = $db->req("UPDATE joueurs SET plateau=',,,,,,,,,,,,,,,,,,,,,,,,,,,,,', tresor1='0', tresor2='0', tresor3='0', tresor4='0', pepite_diamant='0', pepite_or='0', dernier_coup='', position_meeple1=0, position_temple1=0, position_meeple2=0, position_temple2=0, position_meeple3=0, position_temple3=0, position_meeple4=0, position_temple4=0 WHERE partie_id=".$this->id." AND statut>0;");
    if(!$req) return false;
 
    $req = $db->req("UPDATE parties SET pioche='".$piocheTxt."', statut=1, tresor1=".$tresors.", tresor2=".$tresors.", tresor3=".$tresors.", tresor4=".$tresors.", wait=0 WHERE id=".$this->id.";");
    if(!$req) return false;

    return true;
  }



}

?>