<?php
/**
 * Classe Bourse par Pierre-Olivier MERCIER
 * Dernière édition le 21 Juillet 2008
 * Copyright Halo-Battle Tous droits réservés
 */
class Bourse{
	var $bd;

	var $id;
	var $nom;
	var $taxeA = 1.1;
	var $achatM;
	var $achatC;
	var $taxeV = 1;
	var $venteM;
	var $venteC;
	var $actionsUser;
	var $user;

	/**
	 * Constructor
	 *
	 * @param String $nom Nom de l'action
	 * @param int $user ID du joueur à charger automatiquement
	 *
	 * @access protected
	 */
	function Bourse($nom = "", $user = 0){
		global $var___db, $config;
		$db = new bdd();
		$db->connexion();
		$this->bd = $db;

		if (!empty($nom)) {
			$this->loadAction($nom);
			if (!empty($user)) $this->loadUser($user);
		}
	}

	function loadAction($nom, $type = "nom"){
		global $table_bourse;
		$act = $this->bd->unique_query("SELECT * FROM $table_bourse WHERE $type = '$nom';");
		$this->id = $act['id'];
		$this->nom = $act['nom'];
		$this->achatM = $act['achatM'];
		$this->achatC = $act['achatC'];
		$this->venteM = $act['venteM'];
		$this->venteC = $act['venteC'];
	}

	function loadUser($user, $type = "id"){
		global $table_user;
		$act = $this->bd->unique_query("SELECT id, bourse FROM $table_user WHERE $type = '$user';");
		$this->user = $act['id'];
		$this->traitUser($act['bourse']);
	}

	function traitUser($start){
		$end = array();
		$start = explode(';', $start);
		$cnt = count($start);
		for($i = 0; $i < $cnt; $i++){
			$tmp = explode(':', $start[$i]);
			$end[$tmp[0]] = explode(',', $tmp[1]);
		}
		$this->actionsUser = $end;
	}

	function addAction($nb){
		$ret = array(floor($this->achatM * $nb * $this->taxeA), floor($this->achatC * $nb * $this->taxeA));

		$this->achatM *= pow(1.1, $nb);
		$this->achatC *= pow(1.1, $nb);
		$this->venteM *= pow(1.1, $nb);
		$this->venteC *= pow(1.1, $nb);

		for($i = 0; $i < $nb; $i++){
			$this->actionsUser[$this->id][] = time();
		}

		return $ret;
	}

	function delAction($nb){
		if ($this->action() < $nb) $nb = $this->action();

		$ret = array(floor($this->venteM * $nb / $this->taxeV), floor($this->venteC * $nb / $this->taxeV));

		$this->achatM /= pow(1.1, $nb);
		$this->achatC /= pow(1.1, $nb);
		$this->venteM /= pow(1.1, $nb);
		$this->venteC /= pow(1.1, $nb);

		for($i = 0; $i < $nb; $i++){
			unset($this->actionsUser[$this->id][$i]);
		}

		return $ret;
	}

	function actionIn24Hours(){
		$nb = 0;
		$cnt = count($this->actionsUser[$this->id]);
		for($i = 0; $i < $cnt; $i++){
			if ($this->actionsUser[$this->id][$i] > time() - 86400) $nb++;
		}
		return $nb;
	}

	function action(){
		return count($this->actionsUser[$this->id]);
	}

	function maj(){
		$this->majBourse();
		$this->majUser();
		$this->fileSave();
	}

	function majBourse(){
		global $table_bourse;

		$this->bd->query("UPDATE $table_bourse SET nom = '".$this->nom."', achatM = '".$this->achatM."', achatC = '".$this->achatC."', venteM = '".$this->venteM."', venteC = '".$this->venteC."' WHERE id = ".$this->id.";");
	}

	function majUser(){
		global $table_user;

		$champ = '';
		foreach($this->actionsUser as $key => $cell) {
			if (empty($champ)) $champ .= $key.':'.implode(',', $cell);
			else $champ .= ';'.$key.':'.implode(',', $cell);
		}

		$this->bd->query("UPDATE $table_user SET bourse = '$champ' WHERE id = ".$this->user.";");
	}


	function fileSave(){
		$fichier = fopen(_FCORE."hb_game/bourse/".$this->id.".".strftime('%Y%m%d').".bourse",'a+');
		fwrite($fichier, time().';'.$this->achatM.';'.$this->achatC.';'.$this->venteM.';'.$this->venteC);
		fclose($fichier);
	}


	function newGroupe($nom, $achatM, $achatC, $description = ""){
		global $table_bourse;

		$venteM = floor($achatM * pow(1.1, 5));
		$venteC = floor($achatC * pow(1.1, 5));

		$this->bd->query("INSERT INTO $table_bourse (nom, achatM, achatC, venteM, venteC, description) VALUES('$nom', '$achatM', '$achatC', '$venteM', '$venteC', '$description');");
	}

	function editGroupe($description){
		//TODO toute cette fonction !!
	}
}
?>