<?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 stockage_joueurs_DB($donnees)
{
			$nb_ligne =  count($donnees);
			
			for ($i=0; $i < $nb_ligne; $i++)
			{

				$prenom = $donnees[$i];
				$partie = 0;
				$prior = 0;
			
				if($prenom == "henrique")
				{
					$elo = 1300;
				}
				if($prenom == "fabien")
				{
					$elo = 1300;
				}
				if($prenom == "dominique")
				{
					$elo = 1100;
				}
				if($prenom == "remy")
				{
					$elo = 1100;
				}
				if($prenom == "stephaner")
				{
					$elo = 1000;
				}
				if($prenom == "laurent")
				{
					$elo = 1200;
				}
				if($prenom == "damien")
				{
					$elo = 1000;
				}
				if($prenom == "franckdc")
				{
					$elo = 1000;
				}
				if($prenom == "franckv")
				{
					$elo = 1000;
				}
				if($prenom == "bordi")
				{
					$elo = 1000;
				}
				if($prenom == "amokrane")
				{
					$elo = 1050;
				}
				if($prenom == "olivier")
				{
					$elo = 1250;
				}
				if($prenom == "stephaneb")
				{
					$elo = 1000;
				}
				if($prenom == "patrick")
				{
					$elo = 1000;
				}
				if($prenom == "bertrand")
				{
					$elo = 950;
				}
				if($prenom == "emile")
				{
					$elo = 850;
				}
				if($prenom =="joel")
				{
					$elo = 950;
				}
				if($prenom == "alexandre")
				{
					$elo = 750;
				}
				
				$sql = " INSERT INTO classement ";
				$sql = $sql . " (joueur, elo, parties, prior, initial) VALUES ";
				$sql = $sql . " ('$prenom', '$elo','$partie','$prior', '$elo') ";
				echo $sql;
				echo "<br>";
				$result = mysql_query($sql);
				if (!$result)
				{ 
					echo("ERROR: " . mysql_error() . "\n$SQL\n");	
				}
					
			}	
}		

/***********************************************/
function regenerer_classement($id, $new_datas)				
{
	
	// sauvegarde du classement dans un fichier
	
	$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'];
					$elo = $row['elo'];
					$date = $row['date'];
					$resultat = $row['result'];
					$date_partie = date('d/m/Y', $date);
					
					$w_apres_elo1 = $row['w_arating'];
					$w_avant_elo1 = $row['w_brating'];
					$b_apres_elo1 = $row['b_arating'];
					$b_avant_elo2 = $row['b_brating'];
					
					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] = $date_partie;
				$table_csv[1] = $blanc;
				$table_csv[2] = $noir;
				$table_csv[3] = $score_blanc;
				$table_csv[4] = $score_noir;

				$table_fichier[] = $table_csv;
	}
	
	$nom_fichier = "pipo.txt";
				
	$fp = fopen($nom_fichier, 'w'); 
	for ($i=0; $i < $nb_parties; $i++)
	{
		$str_comma_separated = implode(";", $table_fichier[$i]);
		fputs ($fp, $str_comma_separated);
		if($i != $nb_parties - 1)
		fputs($fp, "\n");
	}
				
	fclose($fp);
	
	// creation du fichier modifie
	
	$nom_fichier = "new.txt";
				
	$fp = fopen($nom_fichier, 'w'); 
	for ($i=0; $i < $nb_parties; $i++)
	{

		if($i == $id - 1)
		{
			$table_fichier[$i][1] = $new_datas[0];
			$table_fichier[$i][2] = $new_datas[1];
			$table_fichier[$i][3] = $new_datas[2]; 
			$table_fichier[$i][4] = $new_datas[3];
		}
		
		$str_comma_separated = implode(";", $table_fichier[$i]);
		fputs ($fp, $str_comma_separated);
		if($i != $nb_parties - 1)
		fputs($fp, "\n");
	}
				
	fclose($fp);

		
   // joueur
    $joueur = array();
   
	$sql = "SELECT * FROM classement ORDER by joueur asc"; 
	$results = mysql_query($sql);
					
	while($row = mysql_fetch_assoc($results))
	{
		$joueur[] = $row['joueur'];
		
	}
	
	echo "<pre>";
	print_r($joueur);
	
/*	
	// regenerer le classement
	mysql_query('TRUNCATE TABLE classement;');
	mysql_query('TRUNCATE TABLE evolution;');
	mysql_query('TRUNCATE TABLE parties;');
	
	stockage_joueurs_DB($joueur);
	
	$file = fopen ($nom_fichier,"r");
		
	$indice =  1;	
	while(! feof($file))
	{
			$tmp = fgets($file);
			$tmp = strtolower($tmp);
			//echo $tmp ."<BR>";
			$tmp = str_to_noaccent($tmp);
			$pieces = explode(";", $tmp);
			$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];
					
			echo "<pre>";
			print_r($donnees);

			algo_elo($donnees, "statistique", "sql", $indice );
			
			$indice = $indice + 1;
	}
	
	
*/	
				
	echo "done";
	
}
/**********************************************/
	
		
?>

