<?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);
			// $result = mysql_query("SELECT * FROM classement WHERE joueur ='".$J1."'");
			// $row = mysql_fetch_assoc($results)
			// $elo_J1 = $row["elo"];
			// $parties_J1 = $row["parties"];
			
			// $result = mysql_query("SELECT * FROM classement WHERE joueur ='".$J2."'");
			// $row = mysql_fetch_assoc($results)
			// $elo_J2 = $row["elo"];
			// $parties_J2 = $row["parties"];
			
			
			//if($elo_J1 > 900 && $parties_J1 >= 20 $elo_J2 > 900 && $parties_J2 >= 20)

			// si J1 onchess 0.5  contre noire 0.5     alors noir gagne 
				
			if($J1 != "marek" && $J2 != "marek")
			{
			
					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;  // avant J1	
										//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)
									{   // noir qui gagne
										$_SESSION['onchessmaster'] = $J2;	
										//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 = " INSERT INTO " .$_SESSION['chessmaster'];
			$sql = $sql . " (blanc, noir, gagnant, date) VALUES ";
			$sql = $sql . " ('$J1', '$J2','$gagnant','$timestamp') ";
			//echo $sql."<br>";
			
			$result = mysql_query($sql);
}
?>