<?php

			include('correlation.php');
			include('echec_fonctions_communes.php');
			include('chessmaster.php');

			$J1 = $_POST['J1'];
			$J2 = $_POST['J2'];
			$selection_score = $_POST['selection_score'];
			$id_partie = $_POST['id_partie'];
			$flag_validation = false;
			
			// $J1 = "joel";
			// $J2 = "olivier";
			// $selection_score = "Match nul";
			// $id_partie = 5;
			// $flag_validation = false;
		
			switch($selection_score)
			{
				case "Blanc gagne":
					$score_blanc = "1";
					$score_noir = "0";
					$flag_validation = true;
					break;
				
				case "Blanc perd":
					$score_blanc = "0";
					$score_noir = "1";
					$flag_validation = true;
					break;
				case "Match nul":
					$score_blanc = "0.5";
					$score_noir = "0.5";
					$flag_validation = true;
					break;
			}
			
			if($flag_validation == true)
			{
				$id_partie = $id_partie - 1;
				
				$tmp = transfert_DB();
				$nb_parties = $tmp[0];
				$table_parties = $tmp[1];				

				for ($i=0; $i < $nb_parties; $i++)
				{
					if($i == $id_partie )
					{
						$table_parties[$i][1] = $J1;
						$table_parties[$i][2] = $J2;
						$table_parties[$i][3] = $score_blanc;
						$table_parties[$i][4] = $score_noir;
					}
				}
				
				//echo "<pre>";
				//print_r($table_parties);

			   // joueur
				$joueur = array();
				$sql = "SELECT * FROM classement ORDER by joueur asc"; 
				$results = mysql_query($sql);
								
				while($row = mysql_fetch_assoc($results))
				{
					$joueur[] = $row['joueur'];
				}
				
				// effacer les tables
				mysql_query('TRUNCATE TABLE classement;');
				mysql_query('TRUNCATE TABLE evolution;');
				mysql_query('TRUNCATE TABLE parties;');
				mysql_query('TRUNCATE TABLE chessmaster;');
	
				stockage_joueurs_DB($joueur);
	
				for ($item=0; $item < $nb_parties ; $item++)
				{
					$donnees[0] = $table_parties[$item][0];
					$donnees[1] = $table_parties[$item][1];
					$donnees[2] = $table_parties[$item][2];
					$donnees[3] = $table_parties[$item][3];
					$donnees[4] = $table_parties[$item][4];
			
					$indice = $item + 1;
					algo_elo($donnees, "nostatistique", "sql", $indice );
					chess_master($donnees,$indice);
				}
				
				echo 1;
				
			}

function transfert_DB()
{	
		$sql = "SELECT * FROM parties ORDER by date asc"; 
		$results = mysql_query($sql);
		$nb_parties = mysql_num_rows($results);

		while($row = mysql_fetch_assoc($results))
		{
			$blanc = $row['w_name'];
			$noir = $row['b_name'];
			$timestamp = $row['date'];
			$resultat = $row['result'];
			
			if($resultat == 1)
			{
				$_score_blanc = 1;
				$_score_noir = 0;
			}
			if($resultat == 2)
			{
				$_score_blanc = 0;
				$_score_noir = 1;
			}
			if($resultat == 3)
			{
				$_score_blanc = 0.5;
				$_score_noir = 0.5;
			}

			$table_csv[0] = $timestamp;
			$table_csv[1] = $blanc;
			$table_csv[2] = $noir;
			$table_csv[3] = $_score_blanc;
			$table_csv[4] = $_score_noir;
			$table_fichier[] = $table_csv;
		}
		
		$retour[] = $nb_parties;
		$retour[] = $table_fichier;
		return($retour);
}
			
?>