<?php
		session_start();
		include('database.php');
		DB_connexion(); 

/*************************************************************************************/
		
function insert_record_chessmaster_DB($J1, $J2, $gagnant, $timestamp)
{
			//$sql = " INSERT INTO chessmaster ";
			$sql = " INSERT INTO " .$_SESSION['chessmaster'];
			$sql = $sql . " (blanc, noir, gagnant, date) VALUES ";
			$sql = $sql . " ('$J1', '$J2','$gagnant','$timestamp') ";
			//echo $sql."<br>";
			$result = mysql_query($sql);
}

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


/// copy pour la migration vers la nouvelle version 
/*
			$old_parties = "parties";
			$old_classement = "classement";
			$old_evolution = "evolution";

			$new_table = "xxx";
			$sql = "CREATE TABLE " .$new_table ." LIKE " .$_SESSION['chessmaster'];
			mysql_query($sql);
			$sql = "INSERT INTO " .$new_table ." SELECT * FROM " .$_SESSION['chessmaster'];
			mysql_query($sql);
*/			
/// fin copy

// traitement pour regenerer la table chessmaster

			
			echo '<a href="admin.php">retour ...</a>  
			<br>
			<br>';

			
			$sql = "SELECT * FROM " .$_SESSION['chessmaster'];

			// la table n'existe pas ???
			$query = mysql_query($sql);
			if(!$query)
			{
				echo "**** " .$_SESSION['chessmaster'] ." does not exists : created";
				echo "<br><br>";
				$sql = "CREATE TABLE " .$_SESSION['chessmaster'] ." (
							id int(11) NOT NULL auto_increment,
							blanc varchar(30) NOT NULL default '',
							noir varchar(30) NOT NULL default '',
							gagnant varchar(30) NOT NULL default '',
							date int(11) NOT NULL default '0',
							PRIMARY KEY  (id)
						)"; 
				echo $sql;
				echo "<br><br>";
				mysql_query($sql);
			}
			
			// elle existe , on l'efface avant de la re-créer
			else
			{
				echo "**** " .$_SESSION['chessmaster'] ." exists : regenerated ";
				echo "<br><br>";
				
				$sql = "DROP TABLE " .$_SESSION['chessmaster'];
				mysql_query($sql);

				$sql = "CREATE TABLE " .$_SESSION['chessmaster'] ." (
							id int(11) NOT NULL auto_increment,
							blanc varchar(30) NOT NULL default '',
							noir varchar(30) NOT NULL default '',
							gagnant varchar(30) NOT NULL default '',
							date int(11) NOT NULL default '0',
							PRIMARY KEY  (id)
						)"; 

				echo $sql;
				echo "<br><br>";					
				mysql_query($sql);
			}
		
		
			$sql = "CREATE TABLE MyGuests (
					id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
					firstname VARCHAR(30) NOT NULL,
					lastname VARCHAR(30) NOT NULL,
					email VARCHAR(50),
					reg_date TIMESTAMP
			)";
			//mysql_query($sql);
		
		
			//$sql = "SELECT * FROM parties"; //  ORDER by date desc";  // du plus ancien au plus recent  //asc  du plus recent au plus ancien
			$sql = "SELECT * FROM " .$_SESSION['parties']; //  ORDER by date desc";  // du plus ancien au plus recent  //asc  du plus recent au plus ancien
			
			echo $sql;
			echo "<br>";
			$results = mysql_query($sql);
			$number = mysql_num_rows($results);
			
			echo "****** " .$number;
			echo "<br>";
			
			$detection = false;

			while($row = mysql_fetch_assoc($results))
			{
						$blanc = $row['w_name'];
						$noir = $row['b_name'];
						$resultat = $row['result'];
						$timestamp = $row['date'];
						$date_partie = date('d/m/Y', $timestamp);
						
						$store = array();
						
						if( $detection == true )
						{
							$store[0] = $blanc;
							$store[1] = $noir;
							$store[2] = $resultat;
							$store[3] = $date_partie;
							$donnee[] = $store;
						}
						// date a partir de laquelle le classement chessmaster a été mis en place
						// // debut 01/09/2014	   position 68  1409522400
						if($blanc == "damien" && $noir == "remy" && $timestamp == "1409522400")
						{
							$detection = true;
							$store[0] = $blanc;
							$store[1] = $noir;
							$store[2] = $resultat;
							$store[3] = $date_partie;
							$donnee[] = $store;
						}
			}

			echo "------------ start -------------------";
			echo "<br>";
			
			echo ("<pre>");
			print_r($donnee);	

			echo "------------ stop -------------------";
			echo "<br>";
			
			/*************** Traitement **********************/

			$nb = sizeof($donnee);
			
			echo "******** " . $nb;
			echo "<br>";
			echo "<br>";
			$cpt = 1;
			
			for ($i=0; $i < $nb ; $i++)
			{
					$J1 = $donnee[$i][0];
					$J2 = $donnee[$i][1];
					$score = $donnee[$i][2];
					$date = $donnee[$i][3];
					
					$flag = "none";
			
					if($i == 0)
					{
						$store = array();
					
						if($score == 1)
						{
							$master = $J1;
						}
						if($score == 2)
						{
							$master = $J2;
						}
						$str = $cpt ."  " .$J1  ."   " .$J2  ." *****  "  .$master ."   " .$date; 
						//insert_record_chessmaster_DB($J1, $J2, $J1, $date, $dbname);
						$cpt = $cpt + 1;
						$store[0] = $J1;
						$store[1] = $J2;
						$store[2] = $master;
						$store[3] = $date;
						$store_chessmaster[] = $store;
						//echo $str;
						//echo "<br>";
					}
					
					else
					{
						if($J1 == $master &&  $J2 != $master)
						{
							$flag = "step1";
							$store = array();
						
							if($score == "1")
							{
								$str = $cpt  ."  " .$J1  ."   " .$J2  ." *****  "  .$J1 ."   " .$date; 
								$master = $J1;
								//insert_record_chessmaster_DB($J1, $J2, $J1, $date, $dbname);
								$cpt = $cpt + 1;
								$store[0] = $J1;
								$store[1] = $J2;
								$store[2] = $master;
								$store[3] = $date;
								$store_chessmaster[] = $store;
							}
							if($score == "2" )
							{
								$str = $cpt  ."  " .$J1  ."   " .$J2  ." *****  "  .$J2 ."   " .$date; 
								$master = $J2;
								//insert_record_chessmaster_DB($J1, $J2, $J2, $date, $dbname);
								$cpt = $cpt + 1;
								$store[0] = $J1;
								$store[1] = $J2;
								$store[2] = $master;
								$store[3] = $date;
								$store_chessmaster[] = $store;

							}
							if($score == "3")
							{
								$str = $cpt  ."  " .$J1  ."   " .$J2  ." *****  "  .$J2 ."   " .$date; 
								$master = $J2;
								//insert_record_chessmaster_DB($J1, $J2, $J2, $date, $dbname);  // le noir gagne
								$cpt = $cpt + 1;
								$store[0] = $J1;
								$store[1] = $J2;
								$store[2] = $master;
								$store[3] = $date;
								$store_chessmaster[] = $store;
							}
							
							//echo $str;
							//echo "<br>";
						}	
				
						if($J1 != $master &&  $J2 == $master && $flag != "step1")
						{
							$store = array();
							
							if($score == "1")
							{
								$str = $cpt  ."  " .$J1  ."   " .$J2  ." *****  "  .$J1 ."   " .$date; 
								$master = $J1;
								//insert_chessmaster_DB($J1, $J2, $J2, $date, $dbname);
								$cpt = $cpt + 1;
								$store[0] = $J1;
								$store[1] = $J2;
								$store[2] = $master;
								$store[3] = $date;
								$store_chessmaster[] = $store;
							}
							if($score == "2")
							{
								$str = $cpt  ."  " .$J1  ."   " .$J2  ." *****  "  .$J2 ."   " .$date; 
								$master = $J2;
								//insert_record_chessmaster_DB($J1, $J2, $J1, $date, $dbname);
								$cpt = $cpt + 1;
								$store[0] = $J1;
								$store[1] = $J2;
								$store[2] = $master;
								$store[3] = $date;
								$store_chessmaster[] = $store;
							}
							if($score == "3")
							{
								$str = $cpt  ."  " .$J1  ."   " .$J2  ." *****  "  .$J2 ."   " .$date; 
								$master = $J2;
								//insert_record_chessmaster_DB($J1, $J2, $J2, $date, $dbname );
								$cpt = $cpt + 1;
								$store[0] = $J1;
								$store[1] = $J2;
								$store[2] = $master;
								$store[3] = $date;
								$store_chessmaster[] = $store;
							}
							
							//echo $str;
							//echo "<br>";
							
						}	
					}
			}
			
			echo "-------------------------------";
			echo "<br>";
			
			echo ("<pre>");
			print_r($store_chessmaster);	
			
			$nb = sizeof($store_chessmaster);
			
			echo "******** " . $nb;
			echo "<br>";
			echo "<br>";
			
			
			
			for ($i=0; $i < $nb ; $i++)
			{
				$J1 = $store_chessmaster[$i][0];
				$J2 = $store_chessmaster[$i][1];
				$master = $store_chessmaster[$i][2];
				$timestamp = $store_chessmaster[$i][3];
				
				list($jour, $mois, $annee) = explode('/', $store_chessmaster[$i][3]);
				$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
				
				$str = $i  ."  " .$J1  ."   " .$J2  ." *****  "  .$master ."   " .$timestamp; 
				echo $str;
				echo "<br>";
							
				insert_record_chessmaster_DB($J1, $J2, $master, $timestamp);
			}	
			
	
			mysql_close();
			
			echo "<br>";
			echo "Finished";
			
			//DROP TABLE chessmaster_new;
			// 		mysql_query('DROP TABLE chessmaster_new;');

?>

			

