<?php
	session_start();
	 
	include('correlation.php');
	
	
	
/******** eviter des soumission intempestives lors d'un reload de la page F5  ************************************/	
	if(!empty($_POST) )
	{
		$_SESSION['sauvegarde_formulaire'] = $_POST ;
     
		$page_actuelle = $_SERVER['PHP_SELF'] ;
		if(!empty($_SERVER['QUERY_STRING']))
		{
			$page_actuelle .= '?' . $_SERVER['QUERY_STRING'] ;
		}
     
		header('Location: ' . $page_actuelle);
		exit;
	}
	
	if(isset($_SESSION['sauvegarde_formulaire']))
	{
		$_POST = $_SESSION['sauvegarde_formulaire'] ;
		unset($_SESSION['sauvegarde_formulaire']);		
	}	
/************************************************************************/	
function traitement_on_chess_master($tab)
{	
	
	//$results2 = mysql_query("SELECT gagnant FROM chessmaster");
	
	for ($i=0; $i < count($tab); $i++) 
	{
		$tab_gagnant[] = $tab[$i][2];
	}
	
	//while($row = mysql_fetch_assoc($results2))
	//{
	//	$tab_gagnant[] = $row['gagnant'];
	//}
	
	//print_r($tab_gagnant);
	
	$sans_doublon = array_unique($tab_gagnant);
	$reindex = array_values($sans_doublon);	
	//print_r($reindex);
	
	for ($i=0; $i < count($reindex); $i++) 
	{
			$cpt = 0;
			$up = 0;
			$tab_temp = array();
		
			//echo $reindex[$i];
			
			for ($j=0; $j < count($tab_gagnant); $j++) 
			{
				$test = strcmp($reindex[$i], $tab_gagnant[$j]);
				//echo $test;
				if($test == 0)
				{
					$cpt = $cpt + 1;
					$flag = "master";
					$up = $up + 1;

				}	
				if($test != 0 && $flag == "master")
				{
					$tab_temp[] = $up;
					$up = 0;
				}
				if($j == count($tab_gagnant) - 1)
				{
					if( $test == 0)
					{
							$tab_temp[] = $up;
					}
				}
				
			}
		
			//$tab_temp = array_unique($tab_temp);
			// supprime les 0 
			foreach($tab_temp as $array_key=>$array_item)
			{
				if($tab_temp[$array_key] == 0)
				{
					unset($tab_temp[$array_key]);
				}
			}
			
			$tab_temp[] = $cpt;
			$tab_temp[] = $reindex[$i];
			$tab_glob[] = array_values($tab_temp);
		
			//echo "+++++ " .$cpt ."<br>";
			
	}
	
	return($tab_glob);
}	
	  
?>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Description" content="Site perso,cours,ouvrages" />
<meta name="Keywords" content="WEBSITE perso " />
<meta name="Distribution" content="Global" />
<meta name="Robots" content="" />
<meta http-equiv="Content-Language" content="fr" />
<meta name="rating" content="general" />
<meta name="author" content="Patrice DELPY" />
<meta name="copyright" content="Copyright 2013" />
<meta name="generator" content="Patoche" />
<title>Patoche WEBSITE - 2013 - Template du site - en cours de développemment ...</title>



<!--
en php
header("Cache-Control: no-cache, must-revalidate"); 
-->

<!---------------------------------------------------------->	
<?php
		include('include_css_template_commun.php');
		include('include_css.php');
		include('css_tools_skill.php');
		include('include_javascript.php');

?> 

<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<style type="text/css">
#bloc_table_forum { float: left;
					width: 831px; 
					padding: 50px 50px 25px 50px;
					}	
					
#navigation_tournoi{
					float: left;
					width: 910px; // 870
					padding: 10px 0px 0px 10px;
					text-align: center;
		}
					
.marge {
    margin: auto;
    width: 60%;
    padding: 10px ;
	}
	
label {
		display:block;
		float:left;
		width:100px;
	  }

}					
					
</style>

<script type="text/javascript">
function call_update()
{
			//ajax_routine(); 
			scruter();
			setTimeout("call_update()", 1000); //appelle la fonction 1 fois par  seconde
}

function scruter()
{
			var xhr = getXhr()

			xhr.onreadystatechange = function()
			{
				// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
					if(xhr.readyState == 4 && xhr.status == 200)
					{
						//alert(xhr.responseText);
						retour = xhr.responseText;
						
						if(retour == 0)
						{  	
		
							$(function()
							{
									$( "#message_expiration" ).dialog({
									modal: true,
									title: "Infos",
									buttons: {
												Ok: function()
												{
													$( this ).dialog( "close" );
													 document.location.href = "logout_expire.php";
												}
											 }
										});
							});
						}
					}
			}
					
			xhr.open("GET","session_timeout.php",true);
			xhr.send(null);
}

function getXhr()
{
			var xhr = null; 
	
			if(window.XMLHttpRequest) // Firefox et autres
				xhr = new XMLHttpRequest(); 
			else if(window.ActiveXObject)
			{ // Internet Explorer 
				try 
				{
					xhr = new ActiveXObject("Msxml2.XMLHTTP");
				} 
				catch (e) 
				{
					xhr = new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
			
			else 
			{ 	// XMLHttpRequest non supporté par le navigateur 
					alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
					xhr = false; 
			} 

			return xhr;
}

</script>

<!---------------------------------------------------------->	
</head>

<body>
<!---------------------------------------------------------->	
	<div class="main"> <!-- main -->
<!---------------------------------------------------------->	

          <div class="header">
                <div class="balise_titre">Patoche</div>
                <div class="balise_citation">
                &quot;Etre conscient que l'on est ignorant est un grand pas vers le savoir&quot;
                </div>
          </div> 
 
          <div class="banniere"></div>  

<!---------------------------------------------------------->
<?php
/*	include("include_barre_login.php");
	echo '<div id="box_icon">';
	include("debug_connexion_membre.php");
	echo '</div>';
	include("include_menu.php");*/
	
	echo '<div id="box_infos">';
			include("include_menu.php");
			
			echo '<div id="box_icon2">';
			include("connexion_membre.php");
			echo	'</div>';
			
			echo '<div id="box_icon3">';
			include("include_barre_login.php");
			echo	'</div>';

	echo	'</div>';
	$_SESSION['page_active'] = basename (__FILE__ );
?>

<!---------------------------------------------------------->	
            <div class="contenu"> <!-- contenu -->
<!---------------------------------------------------------->	
	
			<div class="barre_vide"></div>
			
			<div id="navigation_tournoi">
				<ul id="menu_horiz">
				<li class="tournoi_partie_tag"><a href="partie.php" title="aller à la section">Duel</a></li>
				<li class="tournoi_simulation_tag"><a href="simulation.php" title="aller à la section">Sim</a></li>
				<li class="tournoi_classement_tag"><a href="classement.php" title="aller à la section">ELO</a></li>
				<li class="tournoi_master_tag_selected"><a href="on_chess_master.php" title="aller à la section">Master</a></li>
				<li class="tournoi_liste_joueurs_tag"><a href="joueurs_echec.php" title="aller à la section">Players</a></li>
				<li class="tournoi_stats_tag"><a href="historique.php" title="aller à la section">Games</a></li>
				<li class="tournoi_securite_tag"><a href="admin.php" title="aller à la section">Admin</a></li>
				</ul>
			</div>
			
			<div id="bloc_historique">
			<br/>
			<br/>
			<br/>
			<br/>
				
<?php			
			/*
							$matrice = array();
							$matrice[0] = array('X','O','X');
							$matrice[1] = array('X','X','O');
							$matrice[2] = array('X','O','O');
			*/	


			$sql = "SELECT * FROM parties"; //  ORDER by date desc";  // du plus ancien au plus recent  //asc  du plus recent au plus ancien
			//echo $sql;
			$results = mysql_query($sql);
			$number = mysql_num_rows($results);
			
			//echo "****** " .$number;
			
			$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 ("<pre>");
			//print_r($donnee);	

			/*************** 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_chessmaster_DB($J1, $J2, $J1, $date);
						$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_chessmaster_DB($J1, $J2, $J1, $date);
								$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_chessmaster_DB($J1, $J2, $J2, $date);
								$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_chessmaster_DB($J1, $J2, $J2, $date);  // 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);
								$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_chessmaster_DB($J1, $J2, $J1, $date);
								$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_chessmaster_DB($J1, $J2, $J2, $date);
								$cpt = $cpt + 1;
								$store[0] = $J1;
								$store[1] = $J2;
								$store[2] = $master;
								$store[3] = $date;
								$store_chessmaster[] = $store;
							}
							
							//echo $str;
							//echo "<br>";
							
						}	
					}
			}
			
			//echo ("<pre>");
			//print_r($store_chessmaster);	

			
			/*************************************************/
			
			//	echo "</table>";
			
			//echo "<br>";
			
			$reversed = array_reverse($store_chessmaster);
			
			$master = $reversed[0][2];
			$date = $reversed[0][3];
			
			$resultats = traitement_on_chess_master($reversed);

			// combien d'enregistrement ?
			//$sql = "SELECT * FROM chessmaster";
			//$results = mysql_query($sql);
			//$n =  mysql_num_rows($results);
			// pointe sur le dernier enregistrement
			//$sql = "SELECT * FROM chessmaster WHERE id = '$n' ";
			//$results = mysql_query($sql);
			//$row = mysql_fetch_assoc($results);
			//$master = $row['gagnant'];
			//$date = date('d/m/Y', $row['date']);
			
			//echo '<div id="mess">';
			//echo $master;
			//echo "<img src='images/medal.png' >";
			//echo "</div>";
			
			$master = ucfirst($master); 
			
			echo "<div style='text-align:center;font-size:36px;'>$master<img src='images/medal.png' ></div>"; 
			echo "<br><br>"; // "font-size:36px;"
			echo "<div style='text-align:center;'>ON Chess master depuis le $date</div>"; 
			echo "<br><br>";
			
			echo "<table border=1 align=center width='80%'>
				<tr>
				<th>ID</th>
				<th>Joueur</th>
				<th>Score</th>
				<th>Longévité</th>
				</tr>";
			
			for ($i=1; $i <= count($resultats); $i++) 
			{
					$tab = $resultats[$i-1];
					$n = count($tab);
					$joueur = $tab[$n-1];
					$efficacite = $tab[$n-2];
					
					$tmp = $tab;
					array_pop($tmp);
					array_pop($tmp);
					$str = implode(" ",$tmp);
					
					echo "	<tr>
							<td align=center>$i</td>
							<td align=center>$joueur</td>
							<td align=center>$efficacite</td>
							<td align=center>$str</td>
							</tr>";	
			}	
			
			echo "</table>";
			
			echo "<br><br>";
			
			
			/***************************************************/
				
			echo "<p align=center style='font-size:36px;'>Confrontations</p><br>";
				
			echo "<table border=1 align=center width='80%'><tr>
	
			<th>ID</th>
			<th>Blanc</th>
			<th>Noir</th>
			<th>Vainqueur</th>	
			<th>Date</th>";
				
			$nb = sizeof($store_chessmaster);	
				
			for ($i=0; $i < $nb ; $i++)
			{

					$indice = $i + 1;
					
					$J1 = $reversed[$i][0];
					$J2 = $reversed[$i][1];
					$gagnant = $reversed[$i][2];
					$date = $reversed[$i][3];
					
					echo "	<tr>
							<td align=center>$indice</td>
							<td align=center>$J1</td>
							<td align=center>$J2</td>
							<td align=center>$gagnant</td>
							<td align=center>$date</td>
							</tr>";

			}		
			
			echo "</table>";

			mysql_close();

?>

			<div id="message_expiration" style="display: none">
			<p>
			<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
			La session a expiré. veuillez vous reconnecter.
			</p>

			</div>
			
			<br/>
			<br/>
			<br/>
			<br/>
			<br/>

<!---------------------------------------------------------->	
<?php			
    include('include_pied_page.php');
	include('include_publicite.php');
?>
<!---------------------------------------------------------->	
			</div>  <!-- contenu -->
<!---------------------------------------------------------->	
	</div>   <!-- main -->
<!---------------------------------------------------------->	

	<script type="text/javascript">
		call_update();
	</script>

</body>
</html>


