<?php

				include('correlation.php');
				include('echec_fonctions_communes.php');
				include('chessmaster.php');
			
				$id_partie =  $_POST['id_partie'] - 1;
		
				$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>";
					}
					
					
				}
				
				$str1 = "TRUNCATE TABLE " .$_SESSION['classement'];
				$str2 = "TRUNCATE TABLE " .$_SESSION['evolution'];
				$str3 = "TRUNCATE TABLE " .$_SESSION['parties'];
				
				mysql_query($str1);
				mysql_query($str2);
				mysql_query($str3);
				
	
				//stockage_joueurs_DB($joueur);
/**/
				$joueur_chess = array();

				$sql = "SELECT * FROM BLITZ_affectation ORDER BY joueur ASC";
	
				$results = mysql_query($sql);
				while($data = mysql_fetch_assoc($results))
				{
					$joueur_chess[] = $data['joueur'];
				}
	
			
				for ($i = 0; $i < count($joueur_chess); $i++)
				{
					$sql = "INSERT INTO BLITZ_classement (joueur, elo, initial, parties, prior, bonus, oldrating, gcount, wins, losses, draws, active) VALUES
							( '$joueur_chess[$i]', 1100, 1100, 0, 0, 0, 1100, 0, 0, 0, 0, 1)";
					mysql_query($sql);
				}
				
				
				
/**/
				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;
					
					algo_elo($donnees, "nostatistique", "sql", $indice );
					
				}

				echo 1;
				
				
function transfert_DB()
{	
			//$sql = "SELECT * FROM parties ORDER by date asc"; 
			$sql = "SELECT * FROM " .$_SESSION['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);
}
				
?>