<?

$Bonus[0] = -470; 	$Bonus[1] = -470; 	$Bonus[2] = -470; 	$Bonus[3] = -470; 
$Bonus[4] = -470; 	$Bonus[5] = -470; 	$Bonus[6] = -444; 	$Bonus[7] = -422; 	
$Bonus[8] = -401; 	$Bonus[9] = -383; 	$Bonus[10] = -366; 	$Bonus[11] = -351; 
$Bonus[12] = -336; 	$Bonus[13] = -322; 	$Bonus[14] = -309; 	$Bonus[15] = -296; 
$Bonus[16] = -284; 	$Bonus[17] = -273; 	$Bonus[18] = -262; 	$Bonus[19] = -251; 
$Bonus[20] = -240; 	$Bonus[21] = -230; 	$Bonus[22] = -220; 	$Bonus[23] = -211; 
$Bonus[24] = -202; 	$Bonus[25] = -193; 	$Bonus[26] = -184; 	$Bonus[27] = -175; 
$Bonus[28] = -166; 	$Bonus[29] = -158; 	$Bonus[30] = -149; 	$Bonus[31] = -141; 	
$Bonus[32] = -133; 	$Bonus[33] = -125; 	$Bonus[34] = -117; 	$Bonus[35] = -110; 
$Bonus[36] = -102; 	$Bonus[37] = -95; 	$Bonus[38] = -87; 	$Bonus[39] = -80; 
$Bonus[40] = -72; 	$Bonus[41] = -65;		$Bonus[42] = -57; 	$Bonus[43] = -50; 	
$Bonus[44] = -43; 	$Bonus[45] = -36; 	$Bonus[46] = -29; 	$Bonus[47] = -21;
$Bonus[48] = -14; 	$Bonus[49] = -7; 		$Bonus[50] = 0;

/*
$elo_actuel 							= $_GET['elo_actuel'];
$moyenne_elo_adversaire 	= $_GET['moyenne_elo_adversaire'];
$score										= $_GET['score'];
$nb_adversaires						= $_GET['nb_adversaires'];
$jeune										= $_GET['jeune'];
*/	
	
function calculer_elo($elo_actuel, $moyenne_elo_adversaire, $score, $nb_adversaires, $Jeune)
{
	global $Bonus;

	$EloDep 					= 0;
	$DtElo 						= 0;
	$Pourcent 				= 0;
	$Perf 						= 0;
	$Signe 						= 0;
	$NewElo 					= 0;
	$NbrParties 			= 0;

	$new_elo					= '---';

	if ($nb_adversaires >= 1) {

		$DtElo 					= $moyenne_elo_adversaire;

		$Pourcent 			= $score / $nb_adversaires;

		$Pourcent 			= round (100 * $Pourcent);

		$Signe 					= 0;

		if ($Pourcent > 50) {

			$Pourcent 		= 100 - $Pourcent;

			$Perf 				= $Bonus[$Pourcent];

			$Perf 				= (-1) * $Perf;

		} else	{

			$Perf 				= $Bonus[$Pourcent];
		}

		$Perf 					= $Perf + $moyenne_elo_adversaire;
		
		$EloDep 				= $elo_actuel;

		$NbrParties 		= $nb_adversaires;

		if ($EloDep > 0) {

			if ($Perf == $EloDep) {

				$NewElo 		= $EloDep;
			}

			if ($Perf < $EloDep) {

				$NewElo 		= ((16*$EloDep) + ($NbrParties * $Perf)) / ((16*1) + ($NbrParties*1));
			}

			if ($Perf > $EloDep) {

				if (($NbrParties >= 8 && $Jeune == 1) || ($NbrParties >= 16 && $Jeune == 0)) {
					
					$NewElo 	= $Perf;
				
				} else	{

					$NewElo 	= (($Perf * $NbrParties) + ($EloDep * ((16*1)- ($NbrParties*1)))) / 16;
				}
			}
	
			$new_elo			= round($NewElo);
		}
	}
	
	return $new_elo;
}

// echo "ELO = " . calculer_elo($elo_actuel, $moyenne_elo_adversaire, $score, $nb_adversaires, $jeune);

?>