<?php

				include('correlation.php');
				include('echec_fonctions_communes.php');
				include('chessmaster.php');
			
				$id_partie =  $_POST['id_partie'] - 1;
		
				//19/05/2014
				//$id_partie = 2;
		
				$tmp = transfert_DB();
				$nb_parties = $tmp[0];
				$table_parties = $tmp[1];	

				//echo "N parties : " .$nb_parties ."<br>"; 		

				for ($i=0; $i < $nb_parties; $i++)
				{
					if($i != $id_partie )
					{
						$new_table_parties[] = $table_parties[$i];
						//echo $i ."<br>";
					}
					
					
				}
				
				//echo "<pre>";
				//print_r($new_table_parties);
				//echo "<br>";
		
			   // extraire les joueurs
				$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 < count($new_table_parties) ; $item++)
				{
					$donnees[0] = $new_table_parties[$item][0];
					$donnees[1] = $new_table_parties[$item][1];
					$donnees[2] = $new_table_parties[$item][2];
					$donnees[3] = $new_table_parties[$item][3];
					$donnees[4] = $new_table_parties[$item][4];
			
					$indice = $item + 1;
					
					//echo "<pre>";
					//print_r($donnees);
					//echo "<br>";
					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);
}
				
?>