<?php

	session_start();

	include('database.php');
	include('elo_glissant02.php');
	DB_connexion(); 
	error_reporting( 0 );

	include("FusionCharts.php");
	include("FC_Colors.php");

	/*if(!isset($_SESSION['logged']) )
	{		
		header ('Location: interdiction.php');
	}

	if($_SESSION['logged']== "no")
	{
		header ('Location: interdiction.php');
	}*/
	
?>

<!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 ...Classement ELO</title>

<!---------------------------------------------------------->	
<?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>
<script language="javascript" src="FusionCharts.js"></script>

<style type="text/css">

#bloc_classement_joueur { 	float: left;
							width: 831px; 
							padding: 50px 50px 25px 50px;
						}	

#navigation_tournoi		{
							float: left;
							width: 910px; // 870
							padding: 10px 0px 0px 10px;
							text-align: center;
						}
</style>

<script type="text/javascript">

function call_update()
{
			scruter(); 
			setTimeout("call_update()", 1000); 
}

function scruter()
{
			var xhr = getXhr()
			// On défini ce qu'on va faire quand on aura la réponse
			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);
}

</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_selected"><a href="classement.php" title="aller à la section">ELO</a></li>
				<li class="tournoi_master_tag"><a href="on_chess_master.php" title="aller à la section">Master</a></li>
				</ul>

				<br>
				
				<ul id="menu_horiz">
				<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>
				<li class="tournoi_securite_tag"><a href="admin.php" title="aller à la section">Admin</a></li>
				</ul>
			</div>

			<div id="bloc_classement_joueur">

<?php			

			//$sql = "select * from classement where active=1 ORDER by elo desc ";
			
			/***************** Filtre 1 **************************/
			
			$sql = "select * from classement ORDER by elo desc ";
			$results = mysql_query($sql);
			$number = mysql_num_rows($results);
			$indice = 0;

			while($row = mysql_fetch_assoc($results))
			{
							$joueur = $row["joueur"]; 
							$elo = $row["elo"];
							$parties = $row["parties"];
							$wins = $row["wins"];
							$losses = $row["losses"];
							$draws = $row["draws"];
							$playerID=$row["id"];
							$oldrating=$row["oldrating"];

							if($elo > 900 && $parties >= 20)
							{
									$arrData[$indice][1] = $joueur;
									$arrData[$indice][2] = $elo;
									$arrData[$indice][3] = $parties;
									$arrData[$indice][4] = $wins;
									$arrData[$indice][5] = $losses;
									$arrData[$indice][6] = $draws;
									$arrData[$indice][7] = $playerID;
									$arrData[$indice][8] = $oldrating;
									
									// pour le podium
									/*if($indice < 3)
									{
										$arrData[$indice][7] = $joueur;
									}
									else
									{
										$arrData[$indice][7] = "no_podium";
									}*/

									$indice = $indice + 1;
							}
			}	
			
			//echo "***** " . sizeof($arrData);
			//echo "<br>";
			
			//echo "<pre>";
			//print_r($arrData);
			
			//echo "<br>";

			/***************** Filtre 2 **************************/
			
			$date = date("d-m-Y");
			list($jour, $mois, $annee) = explode('-', $date);
			$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
			//echo "Timestamp du jour: " .$timestamp;
			//echo "<br>";
			
			$nb_joueur = sizeof($arrData);
						
			for ($i=0; $i < $nb_joueur ; $i++) 
			{
				//$joueur = $liste_joueur[$i];
				$joueur = $arrData[$i][1];
			
				$sql = "SELECT * FROM parties WHERE w_name = '$joueur' OR b_name = '$joueur' ORDER by date"; //  desc LIMIT 20";
				//echo $sql;
				//echo "<br>";

				$results = mysql_query($sql);
				//$nb_parties = mysql_num_rows($results);
				//echo $nb_parties;
				//echo "<br>";
					
				$tmp = array();
			
				while($row = mysql_fetch_assoc($results))
				{
					$tmp[] = $row['date'];
				}
			
				$store = array();
				
				$derniere_partie = max($tmp);
				$temps_ecouler = ceil( ( $timestamp - max($tmp) ) / 86400);
				
				// figure dans le classement
				if($temps_ecouler < 50) // 3 mois
				{
					$store[0] = $joueur;
					$store[1] = $arrData[$i][2];;
					$store[2] = max($tmp);
					$store[3]  = date('d/m/Y',  max($tmp));
					$store[4] =  ceil( ( $timestamp - max($tmp) ) / 86400);
					
					$store[5] = $arrData[$i][3]; 
					$store[6] = $arrData[$i][4]; 
					$store[7] = $arrData[$i][5]; 
					$store[8] = $arrData[$i][6];
					$store[9] = $arrData[$i][7];
					$store[10] = $arrData[$i][8];
					
					$tab_classement[] = $store;
				}
			}
			
			//echo "<br>";
			//echo "<pre>";
			//print_r($tab_classement);
			
			
			//Init  <graph> element

			//$strXML = "<graph caption='Classement score ELO' numberPrefix='' formatNumberScale='0' decimalPrecision='0'>";

			$strXML = "<graph caption='Classement score ELO' yAxisMinValue='900' formatNumber='0' formatNumberScale='0' decimalPrecision='0'>";

			//Convertir les datas en XML puis append
	
//			foreach ($arrData as $arSubData)
//			$strXML .= "<set name='" . $arSubData[1] . "' value='" . $arSubData[2] ."' color='". getFCColor() ."' />";
		
			$nb_joueur = sizeof($tab_classement);
			
			for ($i=0; $i < $nb_joueur ; $i++) 
			{
				$strXML .= "<set name='" . $tab_classement[$i][0] . "' value='" . $tab_classement[$i][1] ."' color='". getFCColor() ."' />";
			}
		
			$strXML .= "</graph>";

			//Creation du chart - Column 3D Chart avec les datas contenues dans strXML

			echo renderChart("FCF_Column3D.swf", "", $strXML, "elo", 600, 300);	

			echo "<br><br>";
?>

<?php

	$J1 = $tab_classement[0][0]; 
	$J2 = $tab_classement[1][0]; 
	$J3 = $tab_classement[2][0]; 

	$J1_image = "Chess joueurs/".$J1 .".jpg";
	$J2_image = "Chess joueurs/".$J2 .".jpg";
	$J3_image = "Chess joueurs/".$J3 .".jpg";

?>

<!-- Podium --->

<center>
<table width="500" border="0">

  <tr>
    <th scope="col"><img src="<?php echo $J2_image; ?>" width="128" height="128" title = "Le Challenger Italien !"/></th>
    <th scope="col"><img src="<?php echo $J1_image; ?>" alt="" width="128" height="128"  title="L'indétronable  le retour!" /></th>
    <th scope="col"><img src="<?php echo $J3_image; ?>" alt="" width="128" height="128" title = "Le troublion en embuscade !" /></th>
  </tr>
  
  <tr>
    <td>&nbsp;</td>
    <td rowspan="3" align="center" bgcolor="#F5BF0C"><?php echo $tab_classement[0][0]; ?> </td>
    <td>&nbsp;</td>

  </tr>

  <tr>
    <td rowspan="2" align="center" bgcolor="#B0D7F8"><?php echo $tab_classement[1][0]; ?> </td>
    <td>&nbsp;</td>
  </tr>

  <tr>
    <td align="center" bgcolor="#8CBA02"><?php echo $tab_classement[2][0]; ?></td>
  </tr>

</table>	
</center>

<br><br>

<?php
	
			echo "<p align=center>" .$nb_joueur . " joueurs en lice.</p><br>";
			//echo "<p align=center>Joueurs en lice.</p><br>";

			echo "<table border=1 align=center width='80%'><tr>
			<th>ID</th>
			<th>Joueur</th>
			<th>ELO</th>
			<th>Parties</th>	
			<th>Victoires</th>	
			<th>Défaites</th>	
			<th>Match nul</th>
			<th>Efficacité (20 parties)</th>";
			
			for ($i=0; $i < $nb_joueur ; $i++) 
			{
				$indice = $i + 1;
				
				$joueur = $tab_classement[$i][0]; 
				$elo = $tab_classement[$i][1]; ;
				$parties = $tab_classement[$i][5]; 
				$wins = $tab_classement[$i][6]; 
				$losses = $tab_classement[$i][7]; 
				$draws = $tab_classement[$i][8]; 
				$playerID = $tab_classement[$i][9]; 
				$oldrating = $tab_classement[$i][10]; 
				
				$difference_elo = $elo - $oldrating;
				//$plage = $parties - 20;
				$plage = 20;
				
				$sql = "SELECT * FROM parties WHERE w_playerid = '$playerID' OR b_playerid = '$playerID' ORDER by date desc LIMIT " .$plage;
				//echo $sql;
				//echo "<br>";

				$partie_win = 0;
				$partie_losses = 0;
				$partie_nulle = 0;
				
				$results = mysql_query($sql);
				$nb_parties = mysql_num_rows($results);
				//echo $nb_parties;
				//echo "<br>";
				
				while ($row = mysql_fetch_array($results)) 
				{	 
						$white = $row["w_playerID"]; 
						$black = $row["b_playerID"];
						$result=$row["result"];

						if($white==$playerID)
						{
							if ($result==1) 
							{
								$partie_win = $partie_win + 1;		
							}

							if($result==2)
							{
								$partie_losses = $partie_losses + 1;		
							}

							if ($result==3)
							{
								$partie_nulle = $partie_nulle + 1;		
							}
						}

						if ($black==$playerID)
						{
							if($result==1)
							{
								$partie_losses = $partie_losses + 1;		
							}

							if ($result==2)
							{
								$partie_win = $partie_win + 1;		
							}

							if ($result==3)
							{
								$partie_nulle = $partie_nulle + 1;		
							}
						} 
				} 
	
				//echo $partie_win ."  " .$partie_losses ."  " . $partie_nulle;
				//echo "<br>";	
				
				$efficacite = ($partie_win + 0.5 * $partie_nulle) / $plage;
				$efficacite = $efficacite * 100;
				$efficacite = round($efficacite, 2);

				//echo $efficacite;
				//echo "<br>";	
				
				/*
				[0] => stephaneb
				[1] => 941
				[2] => 1506549600
				[3] => 28/09/2017
				[4] => 31
				[5] => 45
				[6] => 21
				[7] => 23
				[8] => 1
				[9] => playerID
				*/
				
				
				echo "<tr>
					  <td align=center>".$indice."</td>";

				echo "<td align=center><a href=\"statistiques_joueurs.php?playerID=$playerID\" title='Statistiques'>".$joueur."</a></td>";
				
				if($elo > $oldrating)
				{
					echo "<td align=center>".$elo." (+".$difference_elo.")&nbsp;<img src='images/up.png' title='Derniére partie'></td>";
				}
				if($difference_elo < 0)
				{
					echo "<td align=center>".$elo." (".$difference_elo.")&nbsp;<img src='images/down.png' title='Derniére partie'></td>";
				}
				//echo "<td align=center>".$elo."</td>";  // temporaire
				
				
				echo "<td align=center>".$parties."</td>
					  <td align=center>".$wins."</td>
					  <td align=center>".$losses."</td>
					  <td align=center>".$draws."</td>
					  <td align=center>".$efficacite."</td>
					  </tr>";
			}			
			
			/*
			for ($i=0; $i < $nb_joueur ; $i++) 
			{
				$playerID = $tab_classement[$i][9]; 
				$parties = $tab_classement[$i][5]; 
				$plage = $parties - 20;
				
				$sql = "SELECT * FROM parties WHERE w_playerid = '$playerID' OR b_playerid = '$playerID' ORDER by date desc LIMIT " .$plage;
				echo $sql;
				echo "<br>";
					
				$retid = mysql_query($sql);

				$partie_win = 0;
				$partie_losses = 0;
				$partie_nulle = 0;
				
				$results = mysql_query($sql);
				$nb_parties = mysql_num_rows($results);
				echo $nb_parties;
				echo "<br>";
				
				while ($row = mysql_fetch_array($retid)) 
				{	 
						$white = $row["w_playerID"]; 
						$black = $row["b_playerID"];
						$result=$row["result"];

						if($white==$playerID)
						{
							if ($result==1) 
							{
								$partie_win = $partie_win + 1;		
							}

							if($result==2)
							{
								$partie_losses = $partie_losses + 1;		
							}

							if ($result==3)
							{
								$partie_nulle = $partie_nulle + 1;		
							}
						}

						if ($black==$playerID)
						{
							if($result==1)
							{
								$partie_losses = $partie_losses + 1;		
							}

							if ($result==2)
							{
								$partie_win = $partie_win + 1;		
							}

							if ($result==3)
							{
								$partie_nulle = $partie_nulle + 1;		
							}
						} 
				} 
	
				echo $partie_win ."  " .$partie_losses ."  " . $partie_nulle;
				echo "<br>";	
				
				$efficacite = ($partie_win + 0.5 * $partie_nulle) / $plage;
				$efficacite = $efficacite * 100;
				$efficacite = round($efficacite, 2);

				echo $efficacite;
				echo "<br>";	
				
			}
			*/
			
			
	/*****/
/*	
			$sql = "select * from classement where active=1 ORDER by elo desc ";
			$results = mysql_query($sql);
			$number = mysql_num_rows($results);

			//echo "<p align=center>$number joueurs en lice.</p><br>";
			echo "<p align=center>Joueurs en lice.</p><br>";

			echo "<table border=1 align=center width='80%'><tr>
			<th>ID</th>
			<th>Joueur</th>
			<th>ELO</th>
			<th>Parties</th>	
			<th>Victoires</th>	
			<th>Défaites</th>	
			<th>Match nul</th>
			<th>Efficacité (20 parties)</th>";

			$indice = 1;

			while($row = mysql_fetch_assoc($results))
			{
					$joueur = $row["joueur"]; 
					$elo = $row["elo"];
					$parties = $row["parties"];
					$prior = $row["prior"];
					$bonus = $row["bonus"];
					$wins = $row["wins"];
					$losses = $row["losses"];
					$draws = $row["draws"];

					$toto = ($wins + 0.5 * $draws) / $parties;
					$toto = $toto * 100;
					$toto = round($toto, 2);
					$toto = $toto ." %";

					$oldrating=$row["oldrating"];
					$playerID=$row["id"];
					$rat_increase = $elo - $oldrating;

					// si partie < 20 -> italique <EM>texte en italique</EM>

					if ($oldrating !=0)
					{
						if ($rat_increase>0)
						{
							if($elo > 900 && $parties >= 20)
							{
							    $toto = elo_glissant($playerID);
								echo "	<tr>
									<td align=center>".$indice."</td>";
								/*if($parties >= 20) 		
								{	
									echo "<td align=center><a href=\"statistiques_joueurs.php?playerID=$playerID\" title='Statistiques'>".$joueur."</a></td>";
								}		
								else
								{
									echo "<td align=center><a href=\"statistiques_joueurs.php?playerID=$playerID\" title='Statistiques'><em>".$joueur."</em></a></td>";
								}*/
/*
								echo "<td align=center><a href=\"statistiques_joueurs.php?playerID=$playerID\" title='Statistiques'>".$joueur."</a></td>";		
								echo "
										<td align=center>".$elo." (+".$rat_increase.")&nbsp;<img src='images/up.png' title='Derniére partie'></td>
										<td align=center>".$parties."</td>
										<td align=center>".$wins."</td>
										<td align=center>".$losses."</td>
										<td align=center>".$draws."</td>
										<td align=center>".$toto."</td>
										</tr>";
							}									
						}	 

						else
						{
							if($elo > 900 && $parties >= 20)
							{
							    $toto = elo_glissant($playerID);
								echo "	<tr>
										<td align=center>".$indice."</td>";
								/*if($parties >= 20) 		
								{		
										echo "<td align=center><a href=\"statistiques_joueurs.php?playerID=$playerID\" title='Statistiques'>".$joueur."</a></td>";
								}
								else
								{
										echo "<td align=center><a href=\"statistiques_joueurs.php?playerID=$playerID\" title='Statistiques'><em>".$joueur."</em></a></td>";
								}*/
/*
								echo "<td align=center><a href=\"statistiques_joueurs.php?playerID=$playerID\" title='Statistiques'>".$joueur."</a></td>";
								echo "	<td align=center>".$elo." (".$rat_increase.")&nbsp;<img src='images/down.png' title='Derniére partie'></td>
										<td align=center>".$parties."</td>
										<td align=center>".$wins."</td>
										<td align=center>".$losses."</td>
										<td align=center>".$draws."</td>
										<td align=center>".$toto."</td>
										</tr>";
							}			
						}
					}

					else
					{
						echo "	<tr>
								<td align=center>".$indice."</td>";

						if($parties >= 20) 		
						{		
							echo "<td align=center><a href=\"statistiques_joueurs.php?playerID=$playerID\" title='Statistiques'>".$joueur."</a></td>";
						}
						else
						{
							echo "<td align=center><a href=\"statistiques_joueurs.php?playerID=$playerID\" title='Statistiques'><em>".$joueur."</em></a></td>";
						}

					    $toto = elo_glissant($playerID);

						echo "	<td align=center>".$elo."</td>
								<td align=center>".$parties."</td>
								<td align=center>".$wins."</td>
								<td align=center>".$losses."</td>
								<td align=center>".$draws."</td>
								<td align=center>".$toto."</td>
								</tr>";
					}

					$indice = $indice + 1;

			} 	
*/
			echo "</table>";
			mysql_close();
?>
			</td>
			</tr>
			</table>

			<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>
	
			</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>