<?php


/*http://www.antoine-augusti.fr/blog/2012/06/maths-et-code-le-classement-elo/*/

function valeur_k($elo)
{
if ($elo < 1000)
{
$k = 80;
}
 
if ($elo >= 1000 AND $elo < 2000)
{
$k = 50;
}
 
if ($elo >= 2000 AND $elo <= 2400)
{
$k = 30;
}
 
if ($elo > 2400)
{
$k = 20;
}
 
return $k;
}

function calcul_cote_J1($elo1, $elo2)
{
	$exp = ($elo2 - $elo1) / 400;
	$cote1 = (1/ (1 + pow(10,$exp)));
	echo '<br/>';
	//echo "++++Côte de J1 " . number_format($cote1, 2) * 100 ."%";
	$cote1_pourcentage = number_format($cote1 * 100, 2);
	//echo "++++Côte de J1 " .number_format($cote1 * 100, 2); 
	echo "++++Côte de J1 " .$cote1_pourcentage ; 
	return ($cote1);
}

function calcul_cote_J2($elo1, $elo2)
{
	$exp = ($elo1 - $elo2) / 400;
	$cote2 = (1/ (1 + pow(10,$exp)));
	echo '<br/>';
	//echo "++++Côte de J2 " . number_format($cote2, 2) * 100 ."%";
	$cote2_pourcentage = number_format($cote2 * 100, 2);
	//echo "++++Côte de J2 " .number_format($cote2 * 100, 2)
	echo "++++Côte de J2 " .$cote2_pourcentage ;
	return ($cote2);
}


function J1_gagne($elo1, $elo2, $cote_J1, $cote_J2, $k)
{	
	
	echo "Si J1 gagne ";
	$new_cote_J1 = ceil($elo1 + $k * (1 - $cote_J1 ));
	$new_cote_J2 = floor($elo2 + $k * (0 - $cote_J2 ));
	
	
	$difference_cote_J1 = $new_cote_J1 - $elo1;
	$difference_cote_J2 = $new_cote_J2 - $elo2;
	
	
	echo '<br/>';
	echo "Nouvelle côte de J1 " .$new_cote_J1 .":    (variation : $difference_cote_J1 )";
	echo '<br/>';
	echo "Nouvelle côte de J2 " .$new_cote_J2 .":    (variation : $difference_cote_J2 )";
	/*return ($cote1);*/
	return array($new_cote_J1, $new_cote_J2);
}


function J1_perd($elo1, $elo2, $cote_J1, $cote_J2, $k)
{	
	echo '<br/>';
	echo "Si J1 perd ";
	$new_cote_J1 = ceil($elo1 + $k * (0 - $cote_J1 ));
	$new_cote_J2 = floor($elo2 + $k * (1 - $cote_J2 ));
	
	$difference_cote_J1 = $new_cote_J1 - $elo1;
	$difference_cote_J2 = $new_cote_J2 - $elo2;
	
	echo '<br/>';
	echo "Nouvelle côte de J1 " .$new_cote_J1 .":    (variation : $difference_cote_J1 )";
	echo '<br/>';
	echo "Nouvelle côte de J2 " .$new_cote_J2 .":    (variation : $difference_cote_J2 )";
	/*return ($cote1);*/

}


function calculation($ancien_classement, $k, $score, $proba)
{
	$nouveau_classement = $ancien_classement + $k * ( $score - $proba);
	return ($nouveau_classement);
}

function f1($elo1, $elo2, $score, $k)
{	
	$exp = ($elo1 - $elo2); 
	echo $exp;
	echo '<br/>';
	echo '<br/>';
	echo '<br/>';
	
	$exp = (-1) * $exp / 400;
	
	/*$exp = -1 * $exp;*/
	$proba1 = (1/ (1 + pow(10,$exp)));
	$proba2 = 1 - $proba1;
	
	$proba1 = number_format($proba1,2) ; 
	$proba2 = number_format($proba2,2) ; 
	
	$proba1_pourcent = $proba1 * 100 ; 
	$proba2_pourcent = $proba2 * 100 ;  
	
	echo "Proba pour J1 de gagner " .$proba1_pourcent ."%";
	echo '<br/>';
	echo "Proba pour J2 de ganger " .$proba2_pourcent ."%";
	
		
	$nouveau_J1 = calculation($elo1, $k, $score, $proba1);
	$nouveau_J2 = calculation($elo2, $k, $score, $proba2);
	
	echo '<br/>';
	echo "+++++ J1 " .$nouveau_J1;
	echo '<br/>';
	echo "++++ J2 " .$nouveau_J2;
	echo '<br/>';
	echo '<br/>';
}	
	$k = 15;
	$score = 0.5;
	$elo1 = 1800;
	$elo2 = 2005;
	f1($elo1, $elo2, $score, $k);
	
	$k = 15;
	$score = 1;
	$elo1 = 1000;
	$elo2 = 1800;
	f1($elo1, $elo2, $score, $k);
	
	$k = 16;
	$score = 1;
	$elo1 = 2300;
	$elo2 = 1800;
	f1($elo1, $elo2, $score, $k);

	$k = 16;
	$score = 1;
	$elo1 = 2300;
	$elo2 = 1700;
	
	echo "J1 initale " .$elo1 ;
	echo '<br/>';
	echo "J2 initale " .$elo2 ;
	echo '<br/>';
	
	$cote_J1 = calcul_cote_J1($elo1, $elo2);
	$cote_J2 = calcul_cote_J2($elo1, $elo2);
	
	echo '<br/>';
	
	list($a, $b) = J1_gagne($elo1, $elo2, $cote_J1, $cote_J2, $k);
		
	J1_perd($elo1, $elo2, $cote_J1, $cote_J2, $k);
	
	
	
?>	
	