<?php

	session_start();
	//include('ELO_code.php');
	include('correlation.php');
	include('chessmaster.php');

	//include('database.php');
	//DB_connexion(); 

	//$datas = $_POST['datas'];
	
	//$_SESSION['transfert_data'] = $_POST['datas'];
	
	
	echo "<a href = 'blitz_admin.php'>Retour</a>";
	
	
	
	$table_parties = array_filter(explode("\n", $_SESSION['transfert_data'] ));

	echo "<pre>";
	print_r($table_parties);
	echo "<pre>";

	$nom_fichier = "tmp_db.csv"; //tmp_db.csv";

	//$file_open = fopen("tmp_db.csv","w+"); 
	//fwrite($file_open, $_POST['datas']);
	//fclose($file_open);
	
/******************************************/
	
function lire_csv($nom_fichier )
{
    $donnee = array();   
	$joueur = array();
	$table_csv = array();
	
	$file = fopen ($nom_fichier,"r");
		
	while(! feof($file))
	{
			$tmp = fgets($file);
			$tmp = strtolower($tmp);
			//echo $tmp ."<BR>";
			$tmp = str_to_noaccent($tmp);
			$pieces = explode(";", $tmp);
			if($pieces[1] != "")
			{
				$table_csv[] = $pieces;
				$joueur[] = $pieces[1];
				$joueur[] = $pieces[2];
			}
	}
	
	fclose($file);
	
	$nb_ligne = count($table_csv);
	
	for ($i=0; $i < $nb_ligne; $i++)
	{
			$val = $table_csv[$i][4];
			$position = strpos($val, "5");
			if($position !== false)
			{ 
				//echo " Match nul";
				//echo "<br>";
				$table_csv[$i][3] = "0.5";
				$table_csv[$i][4] = "0.5";
			}
	
			$date_partie = $table_csv[$i][0];
			if($date_partie != "")
			{	$date = mise_en_forme_date($date_partie);
				$sauve = $date;
				$table_csv[$i][0] = $date;
			}
			else
			{	$table_csv[$i][0] = $sauve; 
			}
			
	}	
	
	
	$sans_doublon = array_unique($joueur);
	$sans_doublon = array_values($sans_doublon);
	// ordonner par ordre alphabetique
	usort($sans_doublon, "strcasecmp");
	$merge = array($table_csv, $sans_doublon );
	//echo "<pre>";
	//print_r($merge);
	
	return $merge;
	
}
/******************************************/
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 modification_db($nom_fichier)
{
	
	$donnees = lire_csv($nom_fichier);
	
	// fragmentation
	$liste_parties = array();
	$liste_joueurs = array();
	$liste_parties = $donnees[0];
	$liste_joueurs = $donnees[1]; 
	// affichage
	echo " ------ Extraction des parties ------------------------" ."<br>";
	echo "<pre>";
	print_r($liste_parties);
	echo "<pre>";
	//echo " ------------------------------" ."<br>";
	//echo "<pre>";
	//print_r($liste_joueurs);

	
	$str1 = "TRUNCATE TABLE " .$_SESSION['classement'];
	$str2 = "TRUNCATE TABLE " .$_SESSION['evolution'];
	$str3 = "TRUNCATE TABLE " .$_SESSION['parties'];
				
	mysql_query($str1);
	mysql_query($str2);
	mysql_query($str3);

	$joueur_chess = array();

	$sql = "SELECT * FROM BLITZ_affectation ORDER BY joueur ASC";
	//echo $sql;
	
	$results = mysql_query($sql);
	while($data = mysql_fetch_assoc($results))
    {
		$joueur_chess[] = $data['joueur'];
    }

	echo " ------ Extraction des joueurs ------------------------" ."<br>";
	var_dump($joueur_chess);
	
	echo " ------ Initialisation du classement ------------------------" ."<br>";
			
	for ($i = 0; $i < count($joueur_chess); $i++)
	{
				$prenom = $joueur_chess[$i];
				
				//$sql = " INSERT INTO " .$_SESSION['classement'];
				//$sql = $sql . " (joueur, elo, initial, parties, prior, bonus, oldrating, gcount, wins, losses, draws, active) VALUES ";
				//$sql = $sql . " ('$prenom', '1100','1100','0', '0', '0', '1100' , '0' , '0', '0', '0', '1' ) ";
				//echo $sql;
				
				$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)";
				
				echo $sql ."\n";
				
				mysql_query($sql);
	}
	
	echo " ------ ELO generation  ------------------------" ."<br>";
	

	$nb_parties = count($liste_parties);
	
	for ($item=0; $item < $nb_parties ; $item++)
	{
			$donnees[0] = $liste_parties[$item][0];
			$donnees[1] = $liste_parties[$item][1];
			$donnees[2] = $liste_parties[$item][2];
			$donnees[3] = $liste_parties[$item][3];
			$donnees[4] = $liste_parties[$item][4];
			
			$indice = $item + 1;
			algo_elo($donnees, "statistique", "sql", $indice );
			//chess_master($donnees,$item);
			
	}

	echo "<br>";
	echo " ------ Insertion de la nouvelle partie : OK  ------------------------" ."<br>";

	echo "Script executé avec succés";	
	echo "<br>";


	echo "Done";
	
}		

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

	
	modification_db($nom_fichier);



?>