<?php

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

	date_default_timezone_set('Europe/Paris');
	
	// recupére toutes les dates
	$sql = "SELECT * FROM parties";
	$results = mysql_query($sql);
	//$nb_results = mysql_num_rows($results);
	//echo $nb_results;
	
	while($row = mysql_fetch_assoc($results))
	{
		$game[] = intval($row["date"]);
	}

	echo "<pre>";
	print_r($game);	
	
	$date_ajout = $timestamp_ajout = mktime (0, 0, 0, 3, 29, 2015); //31 -> 4  30 -> 1  28 -> 0 29 -> 0
	
	// recherche si la date existe ?

	$test =  array_search( $date_ajout , $game); // retourne la clé si elle existe
	
	if($test != "")
	{
		echo "Timestamp existe...";
			
		echo "Ce que je recherche -> " .$date_ajout;
		echo "<br>";
		echo "Position " .$test;
		echo "<br>";
		echo $game[$test];
		$array = array_count_values($game);
		$nb_partie = $array[$date_ajout];
		echo "<br>";
		echo $nb_partie;
		echo "<br>";
		$index_ajout =  $test + $nb_partie;
		echo $index_ajout;
	}
	
	else
	{
		echo "Timestamp au plus proche...";
		
		// on fait un recherche pour trouver le timestamp le plus proche
		$num= $date_ajout; 
		$diff=$num;
		$min=$num;

		foreach($game as $a)
		{
          if( abs($a-$num)< $diff )
		  {
              $diff=abs($a-$num);
              $min=$a;
          }
		}
	
		echo "Ce que je recherche -> " .$date_ajout;
		echo "<br>";
		echo "Ce que je trouve ->" .$min;
		echo "<br>";
		echo "La date :" .date('d/m/Y', $min);
	
		$test =  array_search( $min , $game);
		echo "<br>";
		echo "Position " .$test;
		$array = array_count_values($game);
		$nb_partie = $array[$min];
		echo "<br>";
		echo $nb_partie;
		echo "<br>";
		if( $min - $date_ajout > 0) 
		{
			echo "apres";
			$index_ajout =  $test - $nb_partie;
			echo $index_ajout;
		}
		else
		{
			echo "avant";
			$index_ajout =  $test + $nb_partie;
			echo $index_ajout;
		}
	
		
	}
/*	
	CREATE TABLE IF NOT EXISTS `parties` (
  `w_playerID` int(11) default NULL,
  `w_name` varchar(30) NOT NULL default '',
  `b_playerID` int(11) default NULL,
  `b_name` varchar(30) NOT NULL default '',
  `w_brating` int(11) default NULL,
  `w_arating` int(11) default NULL,
  `b_brating` int(11) default NULL,
  `b_arating` int(11) default NULL,
  `result` int(11) default NULL,
  `date` int(11) default NULL
)
*/
	
	/*$sql = "SELECT * FROM parties";
	$results = mysql_query($sql);
	$nb_results = mysql_num_rows($results);
	echo $nb_results;
	
	while($row = mysql_fetch_assoc($results))
	{
		$game[] = $row["date"];
		$game[] = $row["date"];
		$game[] = $row["date"];
		$game[] = $row["date"];
		$game[] = $row["date"];
		$game[] = $row["date"];
		$game[] = $row["date"];
		$game[] = $row["date"];
		$game[] = $row["date"];
		$game[] = $row["date"];
		$game[] = $row["date"];
		$game[] = $row["date"];
	}*/
	
							$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'];
									$date = $row['date'];
									$resultat = $row['result'];
									$date_partie = date('d/m/Y', $date);

									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 = "sauvegarde_insertion.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_insertion.txt";

							if($option_date == "avant")
							{
								$id_position = $id_partie -1;
							}	

							if($option_date == "apres")
							{
								$id_position = $id_partie ;
							}	


							$tab1 = array();
							$tab2 = array();

							$fp = fopen($nom_fichier, 'w'); 
							for ($i=0; $i <= $id_position - 1; $i++)
							{
								$tab1[] = $table_fichier[$i];
							}

							$table[] = $donnees;

							for ($i=$id_position ; $i < $nb_parties; $i++)
							{
								$tab2[] = $table_fichier[$i];
							}

							$tab_global = array_merge($tab1,$table,$tab2);
							
							$fp = fopen($nom_fichier, 'w'); 
							
							for ($i=0; $i < count($tab_global); $i++)
							{
								$str_comma_separated = implode(";", $tab_global[$i]);
								fputs ($fp, $str_comma_separated);

								if($i != count($tab_global) - 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'];
							}
					
							// regenerer le classement
							mysql_query('TRUNCATE TABLE classement;');
							mysql_query('TRUNCATE TABLE evolution;');
							mysql_query('TRUNCATE TABLE parties;');
	
							stockage_joueurs_DB($joueur);
	
							$file = fopen ("new_insertion.txt","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];
								algo_elo($donnees, "nostatistique", "sql", $indice );
								$indice = $indice + 1;
							}
				
	
							// traitement inserer partie.php
	
	
	
	/*$num= $date_ajout; 
	$diff=$num;
	$min=$num;

	foreach($game as $a)
	{
          if( abs($a-$num)< $diff )
		  {
              $diff=abs($a-$num);
              $min=$a;
          }
	}

	
	echo "-----> " .$date_ajout;
	echo "<br>";
	
	echo $min;
	echo "<br>";
	echo date('d/m/Y', $min);
	
	$array = array_count_values($game);
	
	echo $array[$date_ajout];
	
	print_r(array_count_values($game));
	
	//php find closest number in array*/
	
	//http://stackoverflow.com/questions/6147356/find-number-which-is-greater-than-or-equal-to-n-in-an-array
?>
