<?php
			//session_start();
			include('database.php');
			//include('ELO_code.php');
			DB_connexion(); 


			
/******************************************/
function incr_prior($prior)
{
	if ($prior<50) 
	{
		$prior=$prior+1;
	}
	return $prior;
}
/******************************************/
function incr_gcount($gcount) 
{
	$gcount=$gcount+1;
	return $gcount;
}
/******************************************/
function incr_wins($wins)
{
	$wins=$wins+1;
	return $wins;
}
/******************************************/
function incr_losses($losses)
{
	$losses=$losses+1;
	return $losses;
}
/******************************************/
function incr_draws($draws)
{
	$draws=$draws+1;
	return $draws;
}
/******************************************/			

function PWe($R,  $Ri)
{	
	if ($R - $Ri <= -400)
		return 0;
	else if ($R - $Ri >= 400)
		return 1;
	else
		return ( 0.5 + ($R - $Ri)/800);
}
/******************************************/			
	
function algo_elo($donnees, $option1, $option2, $indice) 
{	
	
				//variables
				$No_white = 0; //initial number of games played by white
				$Ns_white = 0;; //N star for white computed in step 2
				$Np_white = 0;; //N prime for white computed in step 2
				$Ro_white = 0;; //initial rating of white player
				$Rfinal_white = 0;

				$No_black = 0; //initial unmber of games played by black
				$Ns_black = 0; //N star for black computed in step 2
				$Np_black = 0; //N prime for black computed in step 2
				$Ro_black = 0; //initial rating of black player
				$Rfinal_black = 0;
		
				$timestamp_partie = $donnees[0];
				$J1 = $donnees[1];
				$J2 = $donnees[2];
				$whiteScore = $donnees[3];
				$blackScore = $donnees[4];
				
				
				$sql_w = "select * from classement where joueur = '$J1' ";
				$result = mysql_query($sql_w);
				while ($data = mysql_fetch_array($result))
				{
					$w_playerID = $data['id'];
					$w_joueur = $data['joueur'];
					$white_rating = $data['elo'];
					$white_initial = $data['initial'];
					$white_games = $data['parties'];
					$w_prior = $data['prior'];
					$w_bonus = $data['bonus'];
					$w_oldrating = $data['oldrating'];
					$white_games = $data['gcount'];
					$w_wins = $data['wins'];
					$w_losses = $data['losses'];
					$w_draws = $data['draws'];
				}	
				
				$sql_b = "select * from classement where joueur = '$J2' ";
				$result = mysql_query($sql_b);
				while ($data = mysql_fetch_array($result))
				{
					$b_playerID = $data['id'];
					$b_joueur = $data['joueur'];
					$black_rating = $data['elo'];
					$black_initial = $data['initial'];
					$black_games = $data['parties'];
					$b_prior = $data['prior'];
					$b_bonus = $data['bonus'];
					$b_oldrating = $data['oldrating'];
					$black_games = $data['gcount'];
					$b_wins = $data['wins'];
					$b_losses = $data['losses'];
					$b_draws = $data['draws'];
				}	
				
				
			/*	
				$w_playerID = 0;
				$w_joueur = "";
				$white_rating = 1000;
				$white_initial = 1000;
				$white_games = 0;
				$w_prior = 0;
				$w_bonus = 0;
				$w_oldrating = 0;
				$white_games = 0;
				$w_wins = 0;
				$w_losses = 0;
				$w_draws = 0;
				
				$b_playerID = 0;
				$b_joueur = "";
				$black_rating = 1050;
				$black_initial = 1050;
				$black_games = 0;
				$b_prior = 0;
				$b_bonus = 0;
				$b_oldrating = 0;
				$black_games = 0;
				$b_wins = 0;
				$b_losses = 0;
				$b_draws = 0;
			*/	
/******************************************************/
	/*				
				if($w_oldrating == 0)
				{	
					$w_oldrating = $white_initial;
				}
				else
				{
					$w_oldrating = $white_rating;
				}
				
				if($b_oldrating == 0)
				{	
					$b_oldrating = $black_initial;
				}
				else
				{
					$b_oldrating = $black_rating;
				}
				*/
/******************************************************/
				
				if($white_games == 0)
				{
					$No_white = 0;
					$Ro_white = $white_initial;
				}
				else
				{
					$No_white = $white_games;
					$Ro_white = $white_rating;
				}

				if($black_games == 0)
				{
					$No_black = 0;
					$Ro_black = $black_initial;
				}
				else
				{
					$No_black = $black_games;
					$Ro_black = $black_rating;
				}
			
/******************************************************/
				
				if ($Ro_white > 2200)
				{
					$Ns_white = 50;
				}
				else
				{
					$Ns_white = 50/ sqrt(1+pow((2200-$Ro_white),2)/100000 );
				}
	
				$Np_white = min($No_white, $Ns_white);
				if($No_white == 0)
				{
					$Np_white  = 1;
				}
				
				if($Ro_black > 2200)
				{
					$Ns_black = 50;
				}
				else
				{
					$Ns_black = 50/ sqrt(1+pow((2200-$Ro_black),2)/100000 );
				}
	
				$Np_black = min($No_black, $Ns_black);
				if($No_black == 0)
				{
					$Np_black  = 1;
				}
				
/******************************************************/
			
				if ($No_white < 8)
				{
					$We_white = PWe($Ro_white,$Ro_black);
				}
				else
				{
					$We_white = 1 / ( 1 + pow( 10, -1* ($Ro_white - $Ro_black) / 400) );
				}
					
				$K = 800 / ($Np_white + 1);
				$Rfinal_white = $Ro_white + $K * ( $whiteScore - $We_white );
				
				
				//make sure rating does not drop if winning
				if (($whiteScore == 1) && ($Rfinal_white < $Ro_white))
					$Rfinal_white = $Ro_white;
				//make sure rating does not climb if loosing
				if (($whiteScore == 0) && ($Rfinal_white > $Ro_white))
					$Rfinal_white = $Ro_white;

	
				if ($No_black < 8)
				{
					$We_black = PWe($Ro_black,$Ro_white);
				}	
				else
				{
					$We_black = 1 / ( 1 + pow( 10, -1* ($Ro_black - $Ro_white) / 400) );
				}
				
				$K = 800 / ($Np_black + 1);
				$Rfinal_black = $Ro_black + $K * ( $blackScore - $We_black );

				//make sure rating does not drop if winning
				if (($blackScore == 1) && ($Rfinal_black < $Ro_black))
					$Rfinal_black = $Ro_black;
				//make sure rating does not climb if loosing
				if (($blackScore == 0) && ($Rfinal_black > $Ro_black))
					$Rfinal_black = $Ro_black;
			
/******************************************************/
			
				$floor = 100;
				if($white_games > 25)
				{
					$sql = "SELECT * FROM evolution WHERE playerid = '$w_playerID' ORDER by rating desc ";
					$retid = mysql_query($sql);
					$row = mysql_fetch_array($retid);
					$white_highest = $row["rating"]; 
					//echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ " .$sql ."   " .$white_highest;
					//echo "<br>";
				
					//only apply regular floors to regualr ratings...
					$temp  = ( $white_highest - 200) / 100; //truncs!
					$floor = $temp * 100;
					if($floor > 2100)
					{
						$floor = 2100;
					}	
					if($floor < 1400)
					{
						$floor = 100;		
					}			
				}
				
				$wRating = $Rfinal_white;
				if($wRating < $floor)
				{
					$wRating =  $floor;
				}
/*				else
				{
					white.set_rating( wRating );
				}
				
				white.set_numGames( white.get_numGames() + 1);
				white.set_rawScore( white.get_rawScore() + whiteScore);
				if (  (white.get_numGames() > 25)  && ( white.get_rating() > white.get_highest() ) )
				white.set_highest( white.get_rating() );
*/
			
				if($black_games > 25)
				{
					$sql = "SELECT * FROM evolution WHERE playerid = '$b_playerID' ORDER by rating desc ";
					$retid = mysql_query($sql);
					$row = mysql_fetch_array($retid);
					$black_highest = $row["rating"]; 
					//echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ " .$sql ."   " .$black_highest;
					//echo "<br>";
					//only apply regular floors to regualr ratings...
					$temp  = ( $black_highest - 200) / 100; //truncs!
					$floor = $temp * 100;
					if($floor > 2100)
					{
						$floor = 2100;
					}	
					if($floor < 1400)
					{
						$floor = 100;	
					}	
				}
				
				$bRating = $Rfinal_black;
				if($bRating < $floor)
				{
					$bRating = $floor;
				}	
/*				else
				{
					black.set_rating( bRating );
				}
				
				black.set_numGames( black.get_numGames() + 1);
				black.set_rawScore( black.get_rawScore() + blackScore);
				if (  (black.get_numGames() > 25)
					&&(black.get_rating() > black.get_highest() ) )
						black.set_highest( black.get_rating() );
*/	
	
/******************************************************/
		
				$wRating = flooceil($wRating);
				$bRating = ceil($bRating);
				
				if($option1 == "statistique")
				{		
						$J1 = $donnees[1];
						$J2 = $donnees[2];
						$date_partie = date('d/m/Y', $timestamp_partie);
						//echo $date_partie;
						//echo '<br/>';
						echo "Partie " .$indice;
						echo '<br/>';
						echo $J1 ." +++++ " .$J2;
						echo '<br/>';
						
						if($whiteScore==1)
						{
								$esperance_white = round($We_white * 100, 2);
								$esperance_black = 100 - $esperance_white;
								//echo $esperance_white.' %' ."   " .$esperance_black.' %';
								//echo "<br>";
								$diff = $wRating - $white_rating;
								//echo "Blanc (J1) : gagne : passe de " .$white_rating  ." a " .$wRating   .' ('.$diff .')';
								//echo '<br/>';
								$diff = $bRating - $black_rating;
								//echo "Noir (J2) : perd : passe de " .$black_rating  ." a " .$bRating   .' ('.$diff .')';
								//echo '<br/>';
						}
						
						if($blackScore==1) 
						{
								$esperance_black = round($We_black * 100, 2);
								$esperance_white = 100 - $esperance_black; 
								//echo $esperance_white.' %' ."   " .$esperance_black.' %';
								//echo "<br>";
								$diff = $wRating - $white_rating;
								//echo "Blanc (J1) : perd : passe de " .$white_rating  ." a " .$wRating   .' ('.$diff .')';
								//echo '<br/>';
								$diff = $bRating - $black_rating;
								//echo "Noir (J2) : gagne : passe de " .$black_rating  ." a " .$bRating   .' ('.$diff .')';
								//echo '<br/>';

						}
						
						if($whiteScore==0.5) 
						{		echo "Match nul";
								echo "<br>";
								$esperance_black = round($We_black * 100, 2);
								$esperance_white = 100 - $esperance_black; 
								//echo $esperance_white.' %' ."   " .$esperance_black.' %';
								//echo "<br>";
								$diff = $wRating - $white_rating;
								//echo " Blanc (J1) : nul : passe de " .$white_rating  ." a " .$wRating   .' ('.$diff .')';
								//echo '<br/>';
								$diff = $bRating - $black_rating;
								//echo " Noir (J2) : nul : passe de " .$black_rating  ." a " .$bRating   .' ('.$diff .')';
								//echo '<br/>';

						}
						
						echo "------------------------------------";
						echo '<br/>';

				}
				
/*********************************************************************************************/		
			
				if($option2 == "sql")
				{
							
							$debug = false;
						
							if($whiteScore==1)
							{
								$w_wins=incr_wins($w_wins);
								$b_losses=incr_losses($b_losses);
								$partie_result = 1;
							}
							
							if($whiteScore == 0.5 && $blackScore == 0.5) 
							{
								$w_draws=incr_draws($w_draws);
								$b_draws=incr_draws($b_draws);
								$partie_result = 3;
							}
							
							if($blackScore==1) 
							{
								$w_losses=incr_losses($w_losses);
								$b_wins=incr_wins($b_wins);
								$partie_result = 2;
							}

							$b_gcount=incr_gcount($black_games);
							$w_gcount=incr_gcount($white_games);
							$w_prior=incr_prior($w_prior);
							$b_prior=incr_prior($b_prior);
						
							$sql_w="UPDATE classement SET `elo`=$wRating, `parties`= $w_gcount ,`prior`=$w_prior,
							`oldrating`=$w_oldrating,`gcount`=$w_gcount,`wins`=$w_wins,`losses`=$w_losses,`draws`=$w_draws
							WHERE `joueur`= '$J1' ";

							$sql_b="UPDATE classement SET `elo`=$bRating, `parties`= $b_gcount,`prior`=$b_prior,
							`oldrating`=$b_oldrating,`gcount`=$b_gcount,`wins`=$b_wins,`losses`=$b_losses,`draws`=$b_draws
							WHERE `joueur`= '$J2' ";
						
							if($debug == true)
							{
								echo $sql_w;
								echo "<br>";
								echo $sql_b;
								echo "<br>";
							}

							$result1 = mysql_query($sql_w);
							$result2 = mysql_query($sql_b);
										
							$sql_w="INSERT INTO `evolution` (playerID,rating,date) VALUES ($w_playerID,$wRating,'$timestamp_partie')";
							$sql_b="INSERT INTO `evolution` (playerID,rating,date) VALUES ($b_playerID,$bRating,'$timestamp_partie')";
						
							if($debug == true)
							{
								echo $sql_w;
								echo "<br>";
								echo $sql_b;
								echo "<br>";
							}	
						
							$result3 = mysql_query($sql_w);
							$result4 = mysql_query($sql_b);
							
							$sql="INSERT INTO parties (w_playerID,w_name,b_playerID,b_name,w_brating,w_arating,b_brating,b_arating,result,date) 
							VALUES ($w_playerID,'$w_joueur',$b_playerID,'$b_joueur',$w_oldrating,$wRating,$b_oldrating,$bRating,$partie_result,'$timestamp_partie')";
							
							$result5 = mysql_query($sql);

							if($debug == true)
							{				
								echo $sql;
								echo "<br>";
							}
					
				}	
	
	
		
		
}

/******************************************/			

	/*
			$J1_nom = "un";
			$J2_nom ="deux";  
						
			$elo1 = 1000;
			$elo2 = 1050;	
			
			echo '
					<h3>cote initiale</h3>
					Blanc : '.$elo1.'<br>
					Noir : '.$elo2.'<br>
				 ';

			echo "----------------------------------------------------------";
			echo "<br/>";
			$donnees[0] = "rien";
			$donnees[1] = $J1_nom;
			$donnees[2] = $J2_nom;
			$donnees[3] = 1;
			$donnees[4] = 0;
			
			$retour = algo_elo($donnees, "statistique", "sql" );
			
			echo "----------------------------------------------------------";
			echo "<br/>";
			$donnees[0] = "rien";
			$donnees[1] = $J1_nom;
			$donnees[2] = $J2_nom;
			$donnees[3] = 1;
			$donnees[4] = 0;
			
			$retour = algo_elo($donnees, "statistique", "sql" );
			
			
			echo "----------------------------------------------------------";
			echo "<br/>";
			$donnees[0] = "rien";
			$donnees[1] = $J1_nom;
			$donnees[2] = $J2_nom;
			$donnees[3] = 0;
			$donnees[4] = 1;
			
			$retour = algo_elo($donnees, "statistique", "sql" );
			
			
			
		/*	echo $retour;
			echo '<br/>';
			
			$pieces = explode("+", $retour);
			$diff = $pieces[0] - $pieces[1];
			echo "Blanc (J1) :  gagne ,  passe de " .$pieces[1]  ." à " .$pieces[0]   .' ('.$diff .')';
			echo '<br/>';
			$diff = $pieces[2] - $pieces[3];
			echo ' Noir (J2) : perd , passe de '  .$pieces[3] ." à " .$pieces[2]  .' ('.$diff .')';
			echo '<br/>';
			
			echo "----------------------------------------------------------";
			echo "<br/>";*/
		
	/*		$donnees[0] = $J1_nom;
			$donnees[1] = $J2_nom;
			$donnees[2] = 0;
			$donnees[3] = 1;
						
	//		$retour = estimation_elo($donnees, True);
	/*			
			echo $retour;
			echo '<br/>';
			
			$pieces = explode("+", $retour);
			$diff = $pieces[0] - $pieces[1];
			echo "Blanc (J1) :  perd ,  passe de " .$pieces[1]  ." à " .$pieces[0]   .' ('.$diff .')';
			echo '<br/>';
			$diff = $pieces[2] - $pieces[3];
			echo ' Noir (J2) : gagne , passe de '  .$pieces[3] ." à " .$pieces[2]  .' ('.$diff .')';
			echo '<br/>';
		
			echo "----------------------------------------------------------";
			echo "<br/>";
	*/
		
	/*		$donnees[0] = $J1_nom;
			$donnees[1] = $J2_nom;
			$donnees[2] = 0.5;
			$donnees[3] = 0.5;
			
	//		$retour = estimation_elo($donnees, True);
	/*		echo $retour;
			echo '<br/>';
			
			$pieces = explode("+", $retour);
			$diff = $pieces[0] - $pieces[1];
			echo "Blanc (J1) :  nul ,  passe de " .$pieces[1]  ." à " .$pieces[0]   .' ('.$diff .')';
			echo '<br/>';
			$diff = $pieces[2] - $pieces[3];
			echo ' Noir (J2) : nul , passe de '  .$pieces[3] ." à " .$pieces[2]  .' ('.$diff .')';
			echo '<br/>';
		
			echo "----------------------------------------------------------";
			echo "<br/>";
			echo "<br/>";
	*/
	
			
?>	