<?php
	session_start();
	include('database.php');
	DB_connexion(); 
		
/****************************************************************************/ 
// Calcul de la probabilité de gagner du joueur J1
function estimation($elo_1, $elo_2)
{
	$exp = ($elo_2 - $elo_1) / 400;
	return (1/ (1 + pow(10,$exp)));
}
/****************************************************************************/ 
// Calcul de la nouvelle cote du joueur J1
function calcul_elo_J1($elo_1, $elo_2, $score)
{
	$k = facteur_k($elo_1);
	$estimation = estimation($elo_1, $elo_2);
	$nouveau_rang = $elo_1 + $k * ($score - $estimation);
 
	// si la cote devient trop petite < 300, on la clamp à cette valeur 
	if ($nouveau_rang < 300)
	{
		$nouveau_rang = 300;
	}
 
	return array($nouveau_rang, $estimation);
}
/****************************************************************************/ 
// valeur de K en fonction de la cote du joueur
function facteur_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;
}
/****************************************************************************/ 
 /*
// Calcul des nouvelles cotes de P1 et P2
// score = 1 si P1 gagne
// score = 0 si P1 perd
// score = 0.5 s'il y a match nul
*/
 
function nouveau_rangs($elo_1, $elo_2, $score)
{
	// Score pour P2 VS P1
	$score_2 = 1 - $score;
 
	$calcul_J1 = calcul_elo_J1($elo_1, $elo_2, $score);
	$estimation_J1 = $calcul_J1[1];
	$elo_J1 = round($calcul_J1[0]);
 
	$calcul_J2 = calcul_elo_J1($elo_2, $elo_1, $score_2);
	$estimation_J2 = $calcul_J2[1];
	$elo_J2 = round($calcul_J2[0]);
 
	return array($elo_J1, $elo_J2, $estimation_J1, $estimation_J2);
}
/****************************************************************************/ 
function afficher_difference($nb)
{
	if ($nb >= 0)
	{
		$nb = '+'.$nb;
	}
 
	return $nb;
}
/****************************************************************************/ 
function convertir_pourcentage($nb)
{
	return round($nb * 100, 2).' %';
}
/****************************************************************************/ 
function resultat_partie($score)
{
		if($score == 1)
		{
			return 'J1 gagne';
		}
 
		if($score == 0)
		{
			return 'J2 gagne';
		}
 
		if ($score == 0.5)
		{
			return 'Match nul';
		}
}
 /****************************************************************************/ 
	
			if(isset($_POST["J1"]) && isset($_POST["J2"]) )
			{
				$J1_nom =  $_POST["J1"]; 
				$J2_nom =  $_POST["J2"];	
			}
			
			//ddd
			//mysql_connect("localhost","root","");
			//mysql_select_db("website");
				
			$sql = "SELECT * FROM joueurs WHERE nom = '".$J1_nom."' ";
			$results = mysql_query($sql);
			$val = mysql_fetch_array($results);
			$J1_score_elo =  $val['score'];
			
			$sql = "SELECT * FROM joueurs WHERE nom = '".$J2_nom."' ";
			$results = mysql_query($sql);
			$val = mysql_fetch_array($results);
			$J2_score_elo =  $val['score'];
	
	
			// score = 1 si P1 gagne
			// score = 0 si P1 perd
			// score = 0.5 s'il y a match nul

			$score = 0;
			

	
/****************************************************************************/ 
	
			// Traitement des résultats

			/*$retour = nouveau_rangs($J1_score_elo, $J2_score_elo, $score);
			$elo_J1 = $retour[0];
			$elo_J2 = $retour[1];
			$estimation_J1 = $retour[2];
			$estimation_J2 = $retour[3];
 
			$difference_J1 = afficher_difference($elo_J1 - $J1_score_elo);
			$difference_J2 = afficher_difference($elo_J2 - $J1_score_elo);*/
  
/****************************************************************************/ 
/****************************************************************************/ 
/****************************************************************************/ 

	echo '
			<h3>côte initiale</h3>
			Joueur1 : '.$J1_score_elo.'<br>
			Joueur2 : '.$J2_score_elo.'<br>
		
			<br/>
			<hr />
			<br />';
			
			$retour = nouveau_rangs($J1_score_elo, $J2_score_elo, 1);
			$elo_J1 = $retour[0];
			$elo_J2 = $retour[1];
			$estimation_J1 = $retour[2];
			$estimation_J2 = $retour[3];
			//$difference_J1 = afficher_difference($elo_J1 - $J1_score_elo);
			//$difference_J2 = afficher_difference($elo_J2 - $J1_score_elo);
			
			$s1 = "J1 Probabilité de gagner : "; 
			$s2 = convertir_pourcentage($estimation_J1) .'<br>';
			echo $s1 .'   '  .$s2;
						
			$s1 = "J2 Probabilité de gagner : "; 
			$s2 = convertir_pourcentage($estimation_J2) .'<br>';
			echo $s1 .'   '  .$s2;
			echo '<br>';		
/************************************************************/			
			echo "Si J1 gagne";
			echo '<br>';	
			
			$difference_J1 = abs($J1_score_elo - $elo_J1);
			$difference_J2 = abs($J2_score_elo - $elo_J2);
			
			echo 'J1 : '.$elo_J1.' (variation : '.$difference_J1.').<br>';
			echo 'J2 : '.$elo_J2.' (variation : '.$difference_J2.').<br>';
			echo '<br>';	
			
			$_SESSION['$J1_gagne_score'] = $elo_J1;
			$_SESSION['$J2_perd_score'] = $elo_J2;
			
/************************************************************/			
		
			// J2 gagne
			$retour = nouveau_rangs($J1_score_elo, $J2_score_elo, 0);
			$elo_J1 = $retour[0];
			$elo_J2 = $retour[1];
			$estimation_J1 = $retour[2];
			$estimation_J2 = $retour[3];
			$difference_J1 = afficher_difference($elo_J1 - $J1_score_elo);
			$difference_J2 = afficher_difference($elo_J2 - $J1_score_elo);
						
			echo '<br>';		
			
			echo "Si J2 gagne";
			echo '<br>';	
			
			$difference_J1 = abs($J1_score_elo - $elo_J1);
			$difference_J2 = abs($J2_score_elo - $elo_J2);
			
			echo 'J1 : '.$elo_J1.' (variation : '.$difference_J1.').<br>';
			echo 'J2 : '.$elo_J2.' (variation : '.$difference_J2.').<br>';
			echo '<br>';	
			
			$_SESSION['$J1_perd_score'] = $elo_J1;
			$_SESSION['$J2_gagne_score'] = $elo_J2;

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

			echo "Si match nul";
			echo '<br>';	
			echo 'J1 : '.$J1_score_elo.' (variation : 0).<br>';
			echo 'J2 : '.$J2_score_elo.' (variation : 0).<br>';
			echo '<br>';			
			
//			echo "Si J2 gagne";
//			echo '<br>';	
			// J1 perds
//			$retour = nouveau_rangs($J1_score_elo, $J2_score_elo, 0);
//			$elo_J1 = $retour[0];
//			$elo_J2 = $retour[1];
//			$estimation_J1 = $retour[2];
//			$estimation_J2 = $retour[3];
// 			$difference_J1 = afficher_difference($elo_J1 - $J1_score_elo);
//			$difference_J2 = afficher_difference($elo_J2 - $J1_score_elo);
//			echo 'J1 : '.$elo_J1.' (variation : '.$difference_J1.').<br>';
//			echo 'J2 : '.$elo_J2.' (variation : '.$difference_J2.').<br>';
//			echo '<br>';	
			
//			$_SESSION['$J1_perd_score'] = $elo_J1;
//			$_SESSION['$J2_gagne_score'] = $elo_J2;
						
//			echo "Si match nul";
//			echo '<br>';	
			
//			$retour = nouveau_rangs($J1_score_elo, $J2_score_elo, 0.5);
//			$elo_J1 = $retour[0];
//			$elo_J2 = $retour[1];
//			$estimation_J1 = $retour[2];
//			$estimation_J2 = $retour[3];
// 			$difference_J1 = afficher_difference($elo_J1 - $J1_score_elo);
//			$difference_J2 = afficher_difference($elo_J2 - $J1_score_elo);
//			echo 'J1 : '.$elo_J1.' (variation : '.$difference_J1.').<br>';
//			echo 'J2 : '.$elo_J2.' (variation : '.$difference_J2.').<br>';
//			echo '<br>';				
	
			$_SESSION['$J1_match_nul'] = $J1_score_elo;
			$_SESSION['$J2_match_nul'] = $J2_score_elo;
	
			$_SESSION['$J1_nom'] = $J1_nom;
			$_SESSION['$J2_nom'] = $J2_nom;
			
	
?>	