<?php

	include('correlation.php');
	
/**********************************************/
function str_to_noaccent($str)
{
    $tmp = $str;
    $tmp = preg_replace('#Ç#', 'C', $tmp);
    $tmp = preg_replace('#ç#', 'c', $tmp);
    $tmp = preg_replace('#è|é|ê|ë#', 'e', $tmp);
    $tmp = preg_replace('#È|É|Ê|Ë#', 'E', $tmp);
    $tmp = preg_replace('#à|á|â|ã|ä|å#', 'a', $tmp);
    $tmp = preg_replace('#@|À|Á|Â|Ã|Ä|Å#', 'A', $tmp);
    $tmp = preg_replace('#ì|í|î|ï#', 'i', $tmp);
    $tmp = preg_replace('#Ì|Í|Î|Ï#', 'I', $tmp);
    $tmp = preg_replace('#ð|ò|ó|ô|õ|ö#', 'o', $tmp);
    $tmp = preg_replace('#Ò|Ó|Ô|Õ|Ö#', 'O', $tmp);
    $tmp = preg_replace('#ù|ú|û|ü#', 'u', $tmp);
    $tmp = preg_replace('#Ù|Ú|Û|Ü#', 'U', $tmp);
    $tmp = preg_replace('#ý|ÿ#', 'y', $tmp);
    $tmp = preg_replace('#Ý#', 'Y', $tmp);
    return ($tmp);
}
/**********************************************/
function mise_en_forme_date($date)
{
		list($jour, $mois, $annee) = explode('/', $date);
		$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
		return($timestamp);
}
/**********************************************/
function parser_fichier($id)				
{				
				$nom_fichier = "ONCHESS_global.csv";
				
				//$str = file_get_contents("ONCHESS_global.csv", true);
				//$str = strtolower($str);
				//$file = fopen ($nom_fichier,"w");
				//fwrite($file, $str);
				//fclose($file);
								
				$file = fopen ($nom_fichier,"r");

				while(! feof($file))
				{
					$tmp = fgets($file);
					$tmp = strtolower($tmp);
					$tmp = str_to_noaccent($tmp);
					$pieces = explode(";", $tmp);

					if($pieces[1] != "")
					{
						$table_csv[] = $pieces;
					}
				}
	
				fclose($file);

				$nb_parties = count($table_csv);
				
				for ($i=0; $i < $nb_parties; $i++)
				{
					$date_partie = $table_csv[$i][0];
					if($date_partie != "")
					{	
						$date = $date_partie;
						$sauve = $date;
						$table_csv[$i][0] = $date;
					}

					else
					{	
						$table_csv[$i][0] = $sauve; 
					}
			
					if($i == $id - 1)
					{
						$table_csv[$i][1] = 2;
						$table_csv[$i][2] = 3;
						$table_csv[$i][3] = 4;
						$table_csv[$i][4] = 5;
						echo "-------------------------" ."<br>";
						echo "<pre>";
						print_r($table_csv[$i]);
						echo "-------------------------" ."<br>";
						
					}
				}	

				//echo "<pre>";
				//print_r($table_csv);
				
				// ouvre le fichier et réduit sa taille à zéro
				$fp = fopen($nom_fichier, 'w'); 
				for ($i=0; $i < $nb_parties; $i++)
				{
					$str_comma_separated = implode(";", $table_csv[$i]);
					fputs ($fp, $str_comma_separated);
					if($i ==  $id - 1)
					{
						fputs($fp, "\n");
					}	
				}
				
				fclose($fp);
				
				/// maintenant on regenere le classement
				mysql_query('TRUNCATE TABLE classement;');
			    mysql_query('TRUNCATE TABLE evolution;');
				mysql_query('TRUNCATE TABLE games;');
				mysql_query('TRUNCATE TABLE parties;');
				mysql_query('TRUNCATE TABLE rating;');

	
				for ($item=0; $item < $nb_parties ; $item++)
				{
					$donnees[0] = $table_csv[$i][0];
					$donnees[1] = $table_csv[$i][1];
					$donnees[2] = $table_csv[$i][2];
					$donnees[3] = $table_csv[$i][3];
					$donnees[4] = $table_csv[$i][4];

					$indice = $item + 1;

					algo_elo($donnees, "nostatistique", "sql", $indice );
				}
	}

}


	parser_fichier(3);	
	
		
?>