<?php
	session_start();
	include('database.php');
	DB_connexion(); 
	error_reporting(0);
	
	//$_SESSION['chessmaster'] = "chessmaster_new";	
	//$_SESSION['classement'] = "classement_new";
	//$_SESSION['parties'] = "parties_new";
	//$_SESSION['evolution'] = "evolution_new";


	$sql = "select * from " .$_SESSION['classement'] ." ORDER by elo desc ";
	$results = mysql_query($sql);
	$indice = 0;

	while($row = mysql_fetch_assoc($results))
	{
		$joueur = $row["joueur"]; 
		$elo = $row["elo"];
		$parties = $row["parties"];

		//if($elo > 900 && $parties >= 20)
		//{
			$arrData[$indice]["label"] = $joueur;
			$arrData[$indice]["value"] = $elo;
			$indice = $indice + 1;
		//}
	}	
	
	//echo "<pre>";
	//print_r($arrData);
	//echo "</pre>";
	
	/*if(!isset($_SESSION['logged']) )
	{		header ('Location: interdiction.php');
	}
	
	if($_SESSION['logged']== "no")
	{
		header ('Location: interdiction.php');
	}*/

	
	$playerID = 12; //$_GET['playerID'];
	
	//$sql = "SELECT * FROM classement WHERE id = '$playerID' ";
	$sql = "SELECT * FROM " .$_SESSION['classement'] ." WHERE id = '$playerID' ";	
	$retid = mysql_query($sql);
	
	while ($row = mysql_fetch_array($retid)) 
	{ 
		$joueur = $row["joueur"]; 
		$rating = $row["elo"];
		$games = $row["parties"];
		$gcount=$row["gcount"];
		$wins=$row["wins"];
		$losses=$row["losses"];
		$draws=$row["draws"];
		$oldrating = $row["oldrating"];
		$incr = $rating - $oldrating;
	}
	
	if ($gcount !=0 )
	{
		$winp=round(($wins/$gcount)*100,2);
		$lossp=round(($losses/$gcount)*100,2);
		$drawp=round(($draws/$gcount)*100,2);
	}
	else 
	{
		$winp=0;
		$lossp=0;
		$drawp=0;
	}

	//$sql = "SELECT * FROM evolution WHERE playerid = '$playerID' ORDER by rating desc ";	
	$sql = "SELECT * FROM " .$_SESSION['evolution'] ." WHERE playerid = '$playerID' ORDER by rating desc ";		
	$retid = mysql_query($sql);
	$row = mysql_fetch_array($retid);
	$max_rating = $row["rating"]; 
	$max_date = $row["date"];
	//$max_date = date("d j, Y",$max_date); // date("F j, Y",$max_date
	$max_date=date("d F Y",$max_date);
	$max_date = strftime('%d-%m-%Y',strtotime($max_date));
	
	$graph_max=$max_rating+50;

	//$sql = "SELECT * FROM evolution WHERE playerid = '$playerID' ORDER by rating asc ";	
	$sql = "SELECT * FROM " .$_SESSION['evolution'] ." WHERE playerid = '$playerID' ORDER by rating asc ";	
	$retid = mysql_query($sql);
	$row = mysql_fetch_array($retid);
	$min_rating = $row["rating"];
	$graph_min=$min_rating-50;
	
	//$query = "SELECT * FROM evolution WHERE playerID = '$playerID' ORDER by date asc";	
	$query = "SELECT * FROM " .$_SESSION['evolution'] ." WHERE playerID = '$playerID' ORDER by date asc";	
	$result = mysql_query($query);
	$num = mysql_num_rows($result);

	if ($num != 0) 
	{

		$file= fopen("include/data.xml", "w");
		$_xml="<chart caption='Evolution du classement' xAxisName='Parties' yAxisName='Niveau ELO' decimals='0' 
		formatNumberScale='0' formatNumber='0' showValues='0' yAxisMinValue='".$graph_min."' 
		yAxisMaxValue='".$graph_max."'>";
	
		$label = "";
	
		while ($row = mysql_fetch_array($result))
		{
			if ($row["playerID"])
			{
				$infos = $row["rating"] ."" .$row["date"];
				$_xml .="\t<set label=\"" . $label . "\"";
				$_xml .=" value='" . $row["rating"] ."'/>\r\n";
				//$_xml .=" value='" . $row["rating"] ."'/>\r\n";
			} 
			else 
			{
				$_xml .="\t<page title=\"Nothing Returned\">\r\n";
				$_xml .="\t\t<file>none</file>\r\n";
				$_xml .="\t</page>\r\n";
			}
		}

		$_xml .="</chart>";

		fwrite($file, $_xml);
		fclose($file);
	}
	
	
	/*********************************************************************************/
	
	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']);		
	}	
?>
  
<!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>
<script language="JavaScript" src="include/chartembed.js"></script>

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

?> 

<style type="text/css">
#bloc_filtre { float: left;
					width: 831px; 
					padding: 50px 50px 25px 50px;

					}	
					
#bloc_stats { float: left;
			  width: 850px; 
			  padding: 50px 10px 25px 10px;

					}	
					
					
#navigation_tournoi{
					float: left;
					width: 885px;
					padding: 10px 0px 0px 10px;
					text-align: center;
				}
		
label
{   
	display: block;
	width: 150px;
	float: left;
	//padding: 28px;
}

select
{
width: 100px;
}
		

#alignement
{
	
	text-align: center;
	
}
		
dddtd {
    text-align: left;
}
		
</style>

<script src="Charts_2022/js/fusioncharts.js"></script>
<script src="Charts_2022/js/themes/fusioncharts.theme.fusion.js"></script>

<script type="text/javascript">
function call_update()
{
			ajax_routine(); 
			setTimeout("call_update()", 1000); //appelle la fonction 1 fois par  seconde
}
		
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;
}
			
function plot_chart()
{
			var xhr = getXhr();
			
			xhr.onreadystatechange = function()
			{
				if(xhr.readyState == 4 && xhr.status == 200)
				{
					
					//alert(xhr.responseText);
					
					var import_data = <?php echo json_encode($arrData); ?>;
					
					 const chartData = [{
											"label": "Venezuela",
											"value": "290"
										}, {
											"label": "Saudi",
											"value": "260"
										}, {
											"label": "Canada",
											"value": "180"
										}, {
											"label": "Iran",
											"value": "140"
										}, {
											"label": "Russia",
											"value": "115"
										}, {
											"label": "UAE",
											"value": "100"
										}, {
											"label": "US",
											"value": "30"
										}, {
											"label": "China",
											"value": "30"
										}];

					//STEP 3 - Chart Configurations
					const chartConfig = {
					type: 'column3d',
					renderAt: 'chart-container',
					width: '100%',
					height: '400',
					dataFormat: 'json',
					dataSource: {
						// Chart Configuration
						"chart": {
							"caption": "Classement ELO",
							"subCaption": "Onsemi Toulouse",
							"xAxisName": "Joueurs",
							"yAxisName": "ELO",
							"formatNumberScale": "0",
							"yAxisMaxValue": "1800",
							"yAxisMinValue": "900",
							//"numberSuffix": "K",
							"theme": "fusion",
							},
						// Chart Data
						"data": import_data //chartData
						}
					};
					FusionCharts.ready(function(){
					var fusioncharts = new FusionCharts(chartConfig);
					fusioncharts.render();
					});
				
				}
			}
			
			xhr.open("POST","rien.php",true);
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr.send();
}			

</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="classement02.php" title="aller à la section">ELO</a></li>
				<li class="tournoi_master_tag"><a href="on_chess_master02.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_reglement_tag"><a href="reglement.php" title="aller à la section">Rules</a></li>
				<li class="tournoi_securite_tag"><a href="admin.php" title="aller à la section">Admin</a></li>
				</ul>
			</div>
			
			<div id="bloc_stats">

<?php

			$sql = "select * from " .$_SESSION['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)
							//{
								$tab_classement[$indice][0] = $indice;
								$tab_classement[$indice][1] = $joueur;
								$tab_classement[$indice][2] = $elo;
								$tab_classement[$indice][3] = $parties;
								$tab_classement[$indice][4] = $wins;
								$tab_classement[$indice][5] = $losses;
								$tab_classement[$indice][6] = $draws;
								$tab_classement[$indice][7] = $playerID;
								$tab_classement[$indice][8] = $oldrating;
								
								$indice = $indice + 1;
							//}
			}	
			
			$J1 = $tab_classement[0][1]; 
			$J2 = $tab_classement[1][1]; 
			$J3 = $tab_classement[2][1]; 
	
			//echo " ---> " .$J1 ."  " .$J2  ."  " .$J3;

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

			if(!file_exists($J1_image)) 
			{
				$J1 = "nophoto";
			} 
	
			if(!file_exists($J2_image)) 
			{
				$J2 = "nophoto";
			} 

			if(!file_exists($J3_image)) 
			{
				$J3 = "nophoto";
			} 

			$sql = "SELECT * FROM " .$_SESSION['parties'];
			$results = mysql_query($sql);
			$number = mysql_num_rows($results);

			echo "Nombre de parties : " .$number; 
			echo "<br><br>"; 

			// affichage du podium
/*
			if( $number != 0)
			{	
				echo '
						<center>
						<table width="500" border="0"> ';
				echo '
			
						<tr>
						<th scope="col"><img src="Chess joueurs/' .$J2 .".jpg"  .'" width="128" height="128"  title="Pas de photo disponoble !" /></th>	
						<th scope="col"><img src="Chess joueurs/' .$J1 .".jpg"  .'" width="128" height="128"  title="Pas de photo disponoble !" /></th>	
						<th scope="col"><img src="Chess joueurs/' .$J3 .".jpg"  .'" width="128" height="128"  title="Pas de photo disponoble !" /></th>	
						</tr>';
				echo '  
						<tr>
						<td>&nbsp;</td>
						<td rowspan="3" align="center" bgcolor="#B0D7F8">'  .$tab_classement[0][1] .' </td>
						<td>&nbsp;</td>
						</tr>

						<tr>
						<td rowspan="2" align="center" bgcolor="#F5BF0C">' .$tab_classement[1][1] .' </td>
						<td>&nbsp;</td>
						</tr>

						<tr>
						<td align="center" bgcolor="#8CBA02">' .$tab_classement[2][1] .' </td>
						</tr>

						</table>	
						</center>

						<br><br> ';
			}
*/
			echo '<div id="chart-container">FusionCharts XT will load here!</div>';
	
			echo "<script> plot_chart(); </script>"; 	
?>	
	<br>
	<br>
	
<?php

			$nb_joueur = sizeof($tab_classement);
	
			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][1]; 
				$elo = $tab_classement[$i][2]; ;
				$parties = $tab_classement[$i][3]; 
				$wins = $tab_classement[$i][4]; 
				$losses = $tab_classement[$i][5]; 
				$draws = $tab_classement[$i][6]; 
				$playerID = $tab_classement[$i][7]; 
				$oldrating = $tab_classement[$i][8]; 
				
				$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;
				$sql = "SELECT * FROM " .$_SESSION['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 "<tr>
					  <td align=center>".$indice."</td>";

				echo "<td align=center><a href=\"statistiques_joueurs03.php?playerID=$playerID\" title='Statistiques'>".$joueur."</a></td>";
				
				if( $parties == 0)
				{
					
					//$sql = "SELECT * FROM " .$_SESSION['elo_affectation '] . "where id = " .$playerID ; 
					//$results = mysql_query($sql);

					//while($row = mysql_fetch_assoc($results)) 
					//{
						//echo "<td align=center>ELO initial</td>";
						//echo "<td align=center>" .$row["elo"] ."</td>";
					//}	
						
					echo "<td align=center>ELO initial</td>";	
				}
				
				else
				{
					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>";
					}
					
					if($difference_elo == 0)
					{
						echo "<td align=center>".$elo." (".$difference_elo.")&nbsp;<img src='images/draw.png' title='Derniére partie'></td>";
					}
				
				}
				//echo "<td align=center>".$elo."</td>";  // temporaire
				
				/*if($parties == 0)
				{
					echo "<td align=center>nada</td>";
				}*/
				
				
				if($parties > 20)
				{
					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>";
				}	  
				else
				{
					echo "<td align=center>".$parties."</td>
						  <td align=center>".$wins."</td>
						  <td align=center>".$losses."</td>
						  <td align=center>".$draws."</td>
						  <td align=center> > Calculer sur 20 parties </td>
						  </tr>";
				}
					
			}			

			echo "</table>";
			mysql_close();
			
?>	
 
			</div> 

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

<!---------------------------------------------------------->	
<?php			
			include('include_pied_page.php');
			include('include_publicite.php');
			
			//echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';

?>
<!---------------------------------------------------------->	
			</div>  <!-- contenu -->
<!---------------------------------------------------------->	
			</div>   <!-- main -->
<!---------------------------------------------------------->	

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

</body>
</html>


