<?php

		include('database.php');
	
		DB_connexion(); 
		
		date_default_timezone_set('Europe/Paris');
		
		$tab_global = transfert_DB();
		
		mysql_query('TRUNCATE TABLE chessmaster;');
		
		$indice =  0;	
		$cpt_nul = 0;
		for ($i=0; $i < count($tab_global); $i++)
		{
				$datas = $tab_global[$i];
				list($jour, $mois, $annee) = explode('/', $datas[0]);
				$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
				
				$donnees[0] = $timestamp; 
				$J1 = $datas[1]; 
				$J2 = $datas[2]; 
				$score_blanc = $datas[3];  
				$score_noir = $datas[4]; 
				echo "+++++++++++  " .$i ."  +++++++++++";
				echo "<br>";
				//print("<pre>");
				//print_r($donnees);
				
			if($J1 != "marek" && $J2 != "marek")
			{
			
					if($timestamp >= 1409522400)
					{
							$verrou = false;
						
							if($J1 == "damien" && $J2 == "remy" && $timestamp == "1409522400")
							{
								$_SESSION['onchessmaster'] = $J2;
								//echo $indice ." --------init------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster'] ."  " .$date; 
								//echo "<br>";
								insert_chessmaster_DB($J1, $score_blanc , $J2, $score_noir, $_SESSION['onchessmaster'], $timestamp);
							}
						
							if($timestamp > 1409522400)
							{
									
								if($J1 != $_SESSION['onchessmaster'] &&  $J2 == $_SESSION['onchessmaster'] )
								{			
									
									if($score_noir == 1)
									{
										$_SESSION['onchessmaster'] = $J2;	
										//echo $indice ."--------------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster'] ."   " .$$timestamp ."  " .$date; 
										//echo "<br>";
										insert_chessmaster_DB($J1, $score_blanc , $J2, $score_noir, $_SESSION['onchessmaster'], $timestamp);
									}		
									if($score_noir == 0)
									{
										$_SESSION['onchessmaster'] = $J1;
										//echo $indice ."--------------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster']."   " .$$timestamp ."  " .$date; 
										//echo "<br>";
										insert_chessmaster_DB($J1, $score_blanc , $J2, $score_noir, $_SESSION['onchessmaster'], $timestamp);
									}
									if($score_blanc == 0.5 && $score_noir == 0.5)
									{	
										$_SESSION['onchessmaster'] = $J2;	
										//echo $indice ."--------------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster'] ."   " .$$timestamp ."  " .$date; 
										//echo "<br>";
										insert_chessmaster_DB($J1, $score_blanc , $J2, $score_noir, $_SESSION['onchessmaster'], $timestamp);
										$cpt_nul = $cpt_nul + 1;
									}		
									$verrou = true;	
								}
							
								if($J1 == $_SESSION['onchessmaster'] &&  $J2 != $_SESSION['onchessmaster'] && $verrou == false)
								{
									
									if($score_blanc == 1)
									{
										$_SESSION['onchessmaster'] = $J1;	
										//echo $indice ."--------------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster']."   " .$$timestamp ."  " .$date; 
										//echo "<br>";
										insert_chessmaster_DB($J1, $score_blanc , $J2, $score_noir,  $_SESSION['onchessmaster'], $timestamp);
									}		
									if($score_blanc == 0)
									{
										$_SESSION['onchessmaster'] = $J2;
										//echo $indice ."--------------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster']."   " .$$timestamp ."  " .$date; 
										//echo "<br>";
										insert_chessmaster_DB($J1, $score_blanc , $J2, $score_noir, $_SESSION['onchessmaster'], $timestamp);
									}
									if($score_blanc == 0.5 && $score_noir == 0.5)
									{   // noir qui gagne
										$_SESSION['onchessmaster'] = $J2;	
										//echo $indice ."--------------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster']."   " .$timestamp ."  " .$date; 
										//echo "<br>";
										insert_chessmaster_DB($J1, $score_blanc , $J2, $score_noir, $_SESSION['onchessmaster'], $timestamp);
										$cpt_nul = $cpt_nul + 1;
									}
									

								}	
									
							}
						
					}
			}		
				
				
				
		}
		
		echo $cpt_nul;
		
		
function insert_chessmaster_DB($J1, $score_blanc,  $J2, $score_noir , $gagnant, $timestamp)
{
			$sql = " INSERT INTO chessmaster ";
			$sql = $sql . " (blanc , score_blanc , noir , score_noir , gagnant, date) VALUES ";
			$sql = $sql . " ('$J1', '$score_blanc' , '$J2', '$score_noir' , '$gagnant','$timestamp') ";
			echo $sql."<br>";
			$result = mysql_query($sql);
}		

function transfert_DB()
{	
		$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;
		}
		
		//$retour[] = $nb_parties;
		//$retour[] = $table_fichier;
		return($table_fichier);
}

?>