<?php
/*
** CONSTANTS
*/
$cle_privee = "x0Cea9Zv+";
$MONTANT_CS1 = 200;
$MONTANT_CS2 = 200;

/*
** Classe Joker
*/
class Joker
{
 var $player;
 
 function Joker($PlayerId)
 {
	$this->player = db_fetch_array(main_db_query("SELECT * FROM Players WHERE Id=".$PlayerId));
 }
 
 function add($montant)
 {
 	main_db_query("UPDATE Players SET NbJokerDispo=NbJokerDispo+".$montant." WHERE Id=".$this->player['Id']);
 	main_db_query("INSERT INTO History (Date, PlayerId, NbJokerDepense) VALUES ('".date("Y-m-d H:i:s", my_local_time())."',".$this->player['Id'].",-".$montant.")");
	$this->player['NbJokerDispo'] += $montant;
 }
 
 function depense($montant, $cs, $matchid)
 {
 	global $TEMPS_PARI;
 	
 	if (($cs == "") && ($matchid == ""))
	{
	 if ($this->player['NbJokerDispo'] >= $montant)
	 {
 	  main_db_query("UPDATE Players SET NbJokerDispo=NbJokerDispo-".$montant." WHERE Id=".$this->player['Id']);
	  main_db_query("INSERT INTO History (Date, PlayerId, NbJokerDepense) VALUES ('".date("Y-m-d H:i:s", my_local_time())."',".$this->player['Id'].",".$montant.")");
	  $this->player['NbJokerDispo'] -= $montant;
	 }
	 else
	 	 return 1;
	 return 0;
	}
 	if ($this->player['NbJokerDispo'] >= $montant)
	{
		$paris = db_fetch_array(main_db_query("SELECT CS FROM Paris WHERE MatchId=".$matchid." AND PlayerId=".$this->player['Id']));
		$paris2 = db_fetch_array(main_db_query("SELECT CS FROM Paris2 WHERE MatchId=".$matchid." AND PlayerId=".$this->player['Id']));
		// si les 2 champs ne sont pas égaux, la base n'est pas consistante.
		if (($cs == 2) && ($paris2['CS'] != "") && ($paris['CS'] != $paris2['CS']))
			die("error in database, please contact administrator");
		// si le paris1 n'existait pas, on le cree
		if ($paris['CS'] == "")
		{
     main_db_query("INSERT INTO Paris (PlayerId, MatchId, Bet, Date, Amount, Done, Public, CS, TempsPari) VALUES (".$this->player['Id'].",".$matchid.",'0','".date("Y-m-d H:i:s", my_local_time())."',0,0,1,".$cs.",".$TEMPS_PARI.")");
		 // si on veut jouer un CS2, on cree directement un Paris2
		 if ($cs == 2)
		 		main_db_query("INSERT INTO Paris2 (PlayerId, MatchId, Bet, Date, Amount, Done, Public, CS, TempsPari) VALUES (".$this->player['Id'].",".$matchid.",'0','".date("Y-m-d H:i:s", my_local_time())."',0,0,1,".$cs.",".$TEMPS_PARI.")");
		}
		else
		{
		 // si on est ici, le Paris1 existe, on va donc le modifié :
		 // si CS == 0, alors on peut utiliser n'importe quel CS
		 switch ($paris['CS'])
		 {
		 	case 0:
			 break;
	   // si CS == 1, alors on peut uniquement utiliser CS2	
		  case 1:
			 if ($cs == 1)
			 	return 2;
			 else
			  $cs += $paris['CS'];
		   break;
		 // si CS == 2, alors on peut uniquement utiliser CS1
		  case 2:
			 if ($cs == 2)
			 	return 2;
			 else
			 	$cs += $paris['CS'];
		  break;
		 // si CS == 3, alors on a déjà utiliser les 2 CS !				
		  case 3:
		   return 2;
		 }
		 main_db_query("UPDATE Paris SET CS=".$cs." WHERE PlayerId=".$this->player['Id']." AND MatchId=".$matchid);
		 // si le CS2 n'existe pas et que l'on veut jouer un CS2, alors on le cree ou l'update
		 if (($cs & 2) == 2)
		 {
		 	if ($paris2['CS'] == "")
		 		main_db_query("INSERT INTO Paris2 (PlayerId, MatchId, Bet, Date, Amount, Done, Public, CS, TempsPari) VALUES (".$this->player['Id'].",".$matchid.",'0','".date("Y-m-d H:i:s", my_local_time())."',0,0,1,".$cs.",".$TEMPS_PARI.")");
		 	else
		 		main_db_query("UPDATE Paris2 SET CS=".$cs." WHERE PlayerId=".$this->player['Id']." AND MatchId=".$matchid);
		 }
		}
 		main_db_query("UPDATE Players SET NbJokerDispo=NbJokerDispo-".$montant." WHERE Id=".$this->player['Id']);
		main_db_query("INSERT INTO History (Date, PlayerId, NbJokerDepense) VALUES ('".date("Y-m-d H:i:s", my_local_time())."',".$this->player['Id'].",".$montant.")");
		$this->player['NbJokerDispo'] -= $montant;
	}								 
	else
		return 1;
	return 0;
 }
}
?>