<?php

session_start();

function chess_master($donnees,$indice)
{
			$timestamp_ajout = $donnees[0];
			$J1 = $donnees[1];
			$J2 = $donnees[2];
			$score_blanc = $donnees[3];
			$score_noir = $donnees[4];
			
			//////$date = date('d/m/Y', $timestamp_ajout);
			
			if($timestamp_ajout >= 1409522400)
			{
					$verrou = false;
				
					if($J1 == "damien" && $J2 == "remy" && $timestamp_ajout == "1409522400")
					{
						$_SESSION['onchessmaster'] = $J2;
						//echo $indice ." --------init------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster'] ."  " .$date; 
						//echo "<br>";
						insert_chessmaster_DB($J1, $J2, $_SESSION['onchessmaster'], $timestamp_ajout);
					}
				
					if($timestamp_ajout > 1409522400)
					{
							
						if($J1 != $_SESSION['onchessmaster'] &&  $J2 == $_SESSION['onchessmaster'] )
						{			
							
							if($score_noir == 1)
							{
								$_SESSION['onchessmaster'] = $J2;	
								//echo $indice ."--------------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster'] ."   " .$timestamp_ajout ."  " .$date; 
								//echo "<br>";
								insert_chessmaster_DB($J1, $J2, $_SESSION['onchessmaster'], $timestamp_ajout);
							}		
							if($score_noir == 0)
							{
								$_SESSION['onchessmaster'] = $J1;
								//echo $indice ."--------------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster']."   " .$timestamp_ajout ."  " .$date; 
								//echo "<br>";
								insert_chessmaster_DB($J1, $J2, $_SESSION['onchessmaster'], $timestamp_ajout);
							}
							if($score_blanc == 0.5 && $score_noir == 0.5)
							{	
								$_SESSION['onchessmaster'] = $J2;	
								//echo $indice ."--------------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster'] ."   " .$timestamp_ajout ."  " .$date; 
								//echo "<br>";
								insert_chessmaster_DB($J1, $J2, $_SESSION['onchessmaster'], $timestamp_ajout);
							}		
							$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_ajout ."  " .$date; 
								//echo "<br>";
								insert_chessmaster_DB($J1, $J2, $_SESSION['onchessmaster'], $timestamp_ajout);
							}		
							if($score_blanc == 0)
							{
								$_SESSION['onchessmaster'] = $J2;
								//echo $indice ."--------------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster']."   " .$timestamp_ajout ."  " .$date; 
								//echo "<br>";
								insert_chessmaster_DB($J1, $J2, $_SESSION['onchessmaster'], $timestamp_ajout);
							}
							if($score_blanc == 0.5 && $score_noir == 0.5)
							{
								$_SESSION['onchessmaster'] = $J1;	
								//echo $indice ."--------------------> " .$J1 ."  " .$J2 ." ---- " .$_SESSION['onchessmaster']."   " .$timestamp_ajout ."  " .$date; 
								//echo "<br>";
								insert_chessmaster_DB($J1, $J2, $_SESSION['onchessmaster'], $timestamp_ajout);
							}
							
						}	
							
					}
				
			}
			
}

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