<?php

	include('correlation.php');
	//include('echec_fonctions_communes.php');
	//include('chessmaster.php');
	
	//include('database.php');
	//DB_connexion(); 
	
	

	//$datas = $_POST['datas'];
	//$table_parties = array_filter(explode("\n", $_POST['datas']));
			
	$filename = "blitz.csv";		
			
	$fp = fopen($filename, 'r') or die('Could not open file!');
	$data = fread($fp, filesize($filename)) or die("Could not read file!");	
	$table_parties = array_filter(explode("\n", $data));	
			
	echo "<pre>";
	print_r($data);
	echo "</pre>";		
			
	echo "---";	
	echo '<br/>'; 	

			
	echo "<pre>";
	print_r($table_parties);
	echo "</pre>";				
			
	echo "---";	
	echo '<br/>'; 	
			
	$joueurs = array();
	
	
			
/*********************************************************************************************************/
			
	function liste_joueurs()
	{

		$tmp = array();
			
		$sql = "SELECT * FROM BLITZ_affectation ORDER by joueur asc"; 
		$results = mysql_query($sql);
		while($row = mysql_fetch_assoc($results))
		{
					$tmp[] = $row['joueur'];
		}
			
		return $tmp;	

	}

/*********************************************************************************************************/
			
	function vider_table($tables)
	{

		$db_host   = "analogdepat.mysql.db";
   	    $db_user   = "analogdepat";
 	    $db_pass   = "Un92pac007";
	    $db_name   = "analogdepat";
		
		$connexion = mysql_connect($db_host,$db_user,$db_pass); 
		if(!$connexion)
		{
			die('Could not connect: ' . mysql_error());
		}
 
		mysql_select_db($db_name, $connexion); 
		
		$tables_array = explode(',',$tables); 
  
		echo " ---> Vidage des tables:";
		echo '<br/>'; 
		echo '<br/>'; 
		
		$tables= "BLITZ_classement,BLITZ_evolution,BLITZ_parties";
  
		foreach($tables_array as $table)
		{
			//$sql = "DROP TABLE " .$table;
			$sql = "TRUNCATE TABLE " .$table;
			echo $sql;
			mysql_query( $sql );
			echo '<br/>'; 
		}
		
		mysql_close($connexion);
		
       
	}		
			
/*********************************************************************************************************/
		
			
	function generation_classement_table($joueurs)		
	{		
		echo '<br/>'; 	
		echo " ---> Initialisation de la table classement:";
		echo '<br/>'; 
		echo '<br/>'; 	
			
		for ($i = 0; $i < count( $joueurs ); $i++) 
		{
				
				$sql ="INSERT INTO BLITZ_classement (id, joueur, elo, initial, parties, prior, bonus, oldrating, gcount, wins, losses, draws, active) VALUES
					  ($i, '$joueurs[$i]', 1100, 1100, 0, 0, 0, 1100, 0, 0, 0, 0, 1)";
				echo $sql;
				
				echo '<br/>'; 
				
				$results = mysql_query($sql);
				
		}


	}
	
/*********************************************************************************************************/

	function generation_elo($table_parties)
	{
		
			$nb_parties = count($table_parties);
			
			echo "Nb parties : " .$nb_parties;
			echo '<br/>'; 

			for ($item=0; $item < $nb_parties ; $item++)
			{
				
				$pieces = explode(";", $table_parties[$item]);
				
				echo "<pre>";
				print_r($pieces);
				echo "</pre>";
				
			
				$donnees[0] = $pieces[0];
				list($jour, $mois, $annee) = explode('/', $donnees[0]);
				$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
			
				$donnees[0] = $timestamp;
				$donnees[1] = $pieces[1];
				$donnees[2] = $pieces[2];
				$donnees[3] = $pieces[3];
				$donnees[4] = $pieces[4];
			
				$indice = $item + 1;
				//algo_elo($donnees, "nostatistique", "sql", $indice );
				algo_elo($donnees, "nostatistique", "sql", $indice );
				//chess_master($donnees,$indice);
	
			}
	}

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

	
	$joueurs = liste_joueurs();
	
	echo " ---> Listes des joueurs:";
	echo '<br/>'; 
			
	echo "<pre>";
	print_r($joueurs);
	echo "</pre>";
								
			
	$tables= "BLITZ_classement,BLITZ_evolution,BLITZ_parties";
			
	vider_table($tables);
	generation_classement_table($joueurs);
	generation_elo($table_parties);
			
			
//			$sql = "CREATE TABLE BLITZ_copy (
//			joueur varchar(30) DEFAULT NULL
//			elo int(11) NOT NULL DEFAULT '0')";
			
//			mysql_query($sql);
			
/*				
				
			// regenerer le classement
			//mysql_query('TRUNCATE TABLE classement;');
			//mysql_query('TRUNCATE TABLE evolution;');
			//mysql_query('TRUNCATE TABLE parties;');
			//mysql_query('TRUNCATE TABLE chessmaster;');
			
			$str1 = "TRUNCATE TABLE " .$_SESSION['classement'];
			$str2 = "TRUNCATE TABLE " .$_SESSION['evolution'];
			$str3 = "TRUNCATE TABLE " .$_SESSION['parties'];
			//$str4 = "TRUNCATE TABLE " .$_SESSION['chessmaster'];
				
			mysql_query($str1);
			mysql_query($str2);
			mysql_query($str3);
			//mysql_query($str4);
	
			stockage_joueurs_DB($joueur);
	
			$nb_parties = count($table_parties);

			for ($item=0; $item < $nb_parties ; $item++)
			{
				
				$pieces = explode(";", $table_parties[$item]);
			
				$donnees[0] = $pieces[0];
				list($jour, $mois, $annee) = explode('/', $donnees[0]);
				$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
			
				$donnees[0] = $timestamp;
				$donnees[1] = $pieces[1];
				$donnees[2] = $pieces[2];
				$donnees[3] = $pieces[3];
				$donnees[4] = $pieces[4];
			
				$indice = $item + 1;
				algo_elo($donnees, "nostatistique", "sql", $indice );
				//chess_master($donnees,$indice);
	
			}
*/			







			echo 1;

?>