<?php

	
	error_reporting(0);
	header( 'content-type: text/html; charset=utf-8' );
	$debug = false;		

/******************************************/
function incr_games($games)
{
	$games=$games+1;
	return $games;
}
/******************************************/
function specialrating($n,$rating,$op_rating,$r_coef) 
{
	if ($r_coef==.5) 
	{
		$r_coef=0;
	}
	elseif ($r_coef==0)
	{
		$r_coef=-1;
	}
	$newrating=($n*$rating+$op_rating+$r_coef*400)/($n+1);
	$rnewrating=round($newrating,0);
	return $rnewrating;
}
/******************************************/
function standardrating($n,$rating,$op_rating,$s) 
{
	$k=kcalculation($n,$rating);
	$e=winningexpactancy($rating,$op_rating);
	
	$newrating=$rating+$k*($s-$e);
	if ($rating<$newrating)
	{
		$roundAway=.4999;
	}
	else
	{
		$roundAway=-.4999;
	}
	$newrating=round($newrating+$roundAway,0);
	return $newrating;
}
/******************************************/
function kcalculation($myGameCount, $rating)
{
	if ($rating<2200) 
	{
		$myPriorGames=50/sqrt((1+(pow((2200-$rating),2))/100000));
	
	}
	elseif($rating>=2200) 
	{
		$myPriorGames=50;
	}
	
	if ($myPriorGames>$myGameCount)
	{
		$myPriorGames=$myGameCount;
	}

	$k=800/($myPriorGames+1);
	return $k;
}
/******************************************/
function winningexpactancy($rating,$op_rating)
{
	$we=1/(pow(10,(-($rating-$op_rating)/400))+1);
	return $we;
}
/******************************************/
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 traitement_classement_elo_un($donnees)
{
	
	$i = 0;
	$nb_ligne = count($donnees);
	for ($i=0; $i < $nb_ligne; $i++)
	{
				$timestamp_partie = $donnees[$i][0];
				$J1 = $donnees[$i][1];
				$J2 = $donnees[$i][2];
				$J1_score = $donnees[$i][3];
				$J2_score = $donnees[$i][4];
				
				//echo $J1 ." " .$J2 ." " .$J1_score ." " .$J2_score;
				//echo "<br>";

				$sql_w = "select * from classement where joueur = '$J1' ";
				//echo $sql_w;
				//echo "<br>";
				$result = mysql_query($sql_w);
				while ($data = mysql_fetch_array($result))
				{
					$w_playerID = $data['id'];
					$w_joueur = $data['joueur'];
					$w_rating = $data['elo'];
					$w_games = $data['parties'];
					$w_prior = $data['prior'];
					$w_bonus = $data['bonus'];
					$w_oldrating = $data['oldrating'];
					$w_gcount = $data['gcount'];
					$w_wins = $data['wins'];
					$w_losses = $data['losses'];
					$w_draws = $data['draws'];
				}	
				
				$sql_b = "select * from classement where joueur = '$J2' ";
				//echo $sql_b;
				//echo "<br>";
				$result = mysql_query($sql_b);
				while ($data = mysql_fetch_array($result))
				{
					$b_playerID = $data['id'];
					$b_joueur = $data['joueur'];
					$b_rating = $data['elo'];
					$b_games = $data['parties'];
					$b_prior = $data['prior'];
					$b_bonus = $data['bonus'];
					$b_oldrating = $data['oldrating'];
					$b_gcount = $data['gcount'];
					$b_wins = $data['wins'];
					$b_losses = $data['losses'];
					$b_draws = $data['draws'];
				}	
				
				if($J1_score==1) 
				{
					if($w_prior<=8) 
					{
						$new_w_rating=specialrating($w_prior,$w_rating,$b_rating,1);
					}
					if($w_prior>8)
					{
						$new_w_rating=standardrating($w_prior,$w_rating,$b_rating,1);
					}
					if($b_prior<=8)
					{
						$new_b_rating=specialrating($b_prior,$b_rating,$w_rating,0);
					}
					if($b_prior>8)
					{
						$new_b_rating=standardrating($b_prior,$b_rating,$w_rating,0);
					}
				}
				
				if(  $J1_score == "0.5" && $J2_score == "0.5"  ) 
				{
					if($w_prior<=8)
					{
						$new_w_rating=specialrating($w_prior,$w_rating,$b_rating,.5);
					}
					if($w_prior>8) 
					{
						$new_w_rating=standardrating($w_prior,$w_rating,$b_rating,.5);
					}
					if($b_prior<=8) 
					{
						$new_b_rating=specialrating($b_prior,$b_rating,$w_rating,.5);
					}
					if($b_prior>8) 
					{
						$new_b_rating=standardrating($b_prior,$b_rating,$w_rating,.5);
					}
				}
				
				if ($J2_score==1) 
				{
					if($w_prior<=8)
					{
						$new_w_rating=specialrating($w_prior,$w_rating,$b_rating,0);
					}
					if($w_prior>8)
					{
						$new_w_rating=standardrating($w_prior,$w_rating,$b_rating,0);
					}
					if($b_prior<=8) 
					{
						$new_b_rating=specialrating($b_prior,$b_rating,$w_rating,1);
					}
					if($b_prior>8)
					{
						$new_b_rating=standardrating($b_prior,$b_rating,$w_rating,1);
					}
				}
				
				
				if($J1_score==1)
				{
					$w_wins=incr_wins($w_wins);
					$b_losses=incr_losses($b_losses);
					$partie_result = 1;
				}
				
				if($J1_score == 0.5 && $J2_score == 0.5) 
				{
					$w_draws=incr_draws($w_draws);
					$b_draws=incr_draws($b_draws);
					$partie_result = 3;
				}
				
				if($J2_score==1) 
				{
					$w_losses=incr_losses($w_losses);
					$b_wins=incr_wins($b_wins);
					$partie_result = 2;
				}

				$b_gcount=incr_gcount($b_gcount);
				$w_gcount=incr_gcount($w_gcount);
				$w_prior=incr_prior($w_prior);
				$b_prior=incr_prior($b_prior);
				$w_games=incr_games($w_games);
				$b_games=incr_games($b_games);
				
				$sql_w="UPDATE classement SET `elo`=$new_w_rating, `parties`=$w_games,`prior`=$w_prior,
				`oldrating`=$w_rating,`gcount`=$w_gcount,`wins`=$w_wins,`losses`=$w_losses,`draws`=$w_draws
				WHERE `joueur`= '$J1' ";

				$sql_b="UPDATE classement SET `elo`=$new_b_rating, `parties`=$b_games,`prior`=$b_prior,
				`oldrating`=$b_rating,`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,$new_w_rating,'$timestamp_partie')";
				$sql_b="INSERT INTO `evolution` (playerID,rating,date) VALUES ($b_playerID,$new_b_rating,'$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_rating,$new_w_rating,$b_rating,$new_b_rating,$partie_result,'$timestamp_partie')";
				
				$result5 = mysql_query($sql);

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

/******************************************/
function estimation_elo_un($donnees)
{
	
				$J1 = $donnees[0];
				$J2 = $donnees[1];
				$J1_score = $donnees[2];
				$J2_score = $donnees[3];
				
				
				$sql_w = "select * from classement where joueur = '$J1' ";
				//echo $sql_w;
				//echo "<br>";
				$result = mysql_query($sql_w);
				while ($data = mysql_fetch_array($result))
				{
					$w_playerID = $data['id'];
					$w_joueur = $data['joueur'];
					$w_rating = $data['elo'];
					$w_games = $data['parties'];
					$w_prior = $data['prior'];
					$w_bonus = $data['bonus'];
					$w_oldrating = $data['oldrating'];
					$w_gcount = $data['gcount'];
					$w_wins = $data['wins'];
					$w_losses = $data['losses'];
					$w_draws = $data['draws'];
				}	
				
				$sql_b = "select * from classement where joueur = '$J2' ";
				//echo $sql_b;
				//echo "<br>";
				$result = mysql_query($sql_b);
				while ($data = mysql_fetch_array($result))
				{
					$b_playerID = $data['id'];
					$b_joueur = $data['joueur'];
					$b_rating = $data['elo'];
					$b_games = $data['parties'];
					$b_prior = $data['prior'];
					$b_bonus = $data['bonus'];
					$b_oldrating = $data['oldrating'];
					$b_gcount = $data['gcount'];
					$b_wins = $data['wins'];
					$b_losses = $data['losses'];
					$b_draws = $data['draws'];
				}	
				
				if($J1_score==1) 
				{
					if($w_prior<=8) 
					{
						$new_w_rating=specialrating($w_prior,$w_rating,$b_rating,1);
					}
					if($w_prior>8)
					{
						$new_w_rating=standardrating($w_prior,$w_rating,$b_rating,1);
					}
					if($b_prior<=8)
					{
						$new_b_rating=specialrating($b_prior,$b_rating,$w_rating,0);
					}
					if($b_prior>8)
					{
						$new_b_rating=standardrating($b_prior,$b_rating,$w_rating,0);
					}
				}
				
				if(  $J1_score == "0.5" && $J2_score == "0.5"  ) 
				{
					if($w_prior<=8)
					{
						$new_w_rating=specialrating($w_prior,$w_rating,$b_rating,.5);
					}
					if($w_prior>8) 
					{
						$new_w_rating=standardrating($w_prior,$w_rating,$b_rating,.5);
					}
					if($b_prior<=8) 
					{
						$new_b_rating=specialrating($b_prior,$b_rating,$w_rating,.5);
					}
					if($b_prior>8) 
					{
						$new_b_rating=standardrating($b_prior,$b_rating,$w_rating,.5);
					}
				}
				
				if ($J2_score==1) 
				{
					if($w_prior<=8)
					{
						$new_w_rating=specialrating($w_prior,$w_rating,$b_rating,0);
					}
					if($w_prior>8)
					{
						$new_w_rating=standardrating($w_prior,$w_rating,$b_rating,0);
					}
					if($b_prior<=8) 
					{
						$new_b_rating=specialrating($b_prior,$b_rating,$w_rating,1);
					}
					if($b_prior>8)
					{
						$new_b_rating=standardrating($b_prior,$b_rating,$w_rating,1);
					}
				}
				
				$val = $new_w_rating ."+" .$w_rating ."+" .$new_b_rating ."+" .$b_rating;
				
				return($val);	
				
	
}	
/******************************************/
function traitement_classement_elo_deux($donnees) 
{
	$white_initial = 1000;
	$black_initial = 1000;

	$nb_ligne = count($donnees);
	
	for ($i=0; $i < $nb_ligne ; $i++)
	{
	
				//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[$i][0];
				$J1 = $donnees[$i][1];
				$J2 = $donnees[$i][2];
				$whiteScore = $donnees[$i][3];
				$blackScore = $donnees[$i][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_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_w = "select * from classement where joueur = '$J2' ";
				$result = mysql_query($sql_w);
				while ($data = mysql_fetch_array($result))
				{
					$b_playerID = $data['id'];
					$b_joueur = $data['joueur'];
					$black_rating = $data['elo'];
					$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'];
				}	
				
				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 = ceil($wRating);
				$bRating = ceil($bRating);
		
/*********************************************************************************************/		
				
				if($debug == true)
				{		
						echo "<br>";
						echo "<------------------------------------->";
						echo "<br>";
						echo $J1 ." " .$J2 ." " .$whiteScore ." " .$blackScore;
						echo "<br>";
						echo "Avant le match " .$white_rating ."  " .$black_rating;	
						echo "<br>";
						echo "Après le match (arrondi supérieur ) :" .$wRating ."  " .$bRating;	
						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) :  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==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);
				//$w_games=incr_games($w_games);
				//$b_games=incr_games($b_games);
				
				//echo $w_gcount ."  "  .$w_wins  ."  " .$w_losses ."  " .$w_draws;
				//echo "<br>";
				
				
				$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>";
			    }
	
	}
		
		
}
/******************************************/

function estimation_elo_deux($donnees) 
{

				$white_initial = 1000;
				$black_initial = 1000;
	
				//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;
				
				$J1 = $donnees[0];
				$J2 = $donnees[1];
				$whiteScore = $donnees[2];
				$blackScore  = $donnees[3];
				
				$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_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_w = "select * from classement where joueur = '$J2' ";
				$result = mysql_query($sql_w);
				while ($data = mysql_fetch_array($result))
				{
					$b_playerID = $data['id'];
					$b_joueur = $data['joueur'];
					$black_rating = $data['elo'];
					$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'];
				}	
				
				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"]; 
				
					//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"]; 
					//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 = ceil($wRating);
				$bRating = ceil($bRating);
	
				$val = $wRating ."+" .$white_rating ."+" .$bRating ."+" .$black_rating;
				
				return($val);	
		
		
}
/******************************************/
function sauvegarde_partie($donnees)
{
	switch ($_SESSION['algorithme'])
			{
				case "un":
					$retour = traitement_classement_elo_un($donnees);
					break;
				case "deux":
					$retour = traitement_classement_elo_deux($donnees);
					break;
				default:
					echo "erreur dans le traitement";
			}
}