<?php
	session_start();
	include('database.php');
	DB_connexion(); 
	error_reporting(0);
	
	/*if(!isset($_SESSION['logged']) )
	{		header ('Location: interdiction.php');
	}
	
	if($_SESSION['logged']== "no")
	{
		header ('Location: interdiction.php');
	}*/

	
	$playerID = $_GET['playerID'];
	
	$sql = "SELECT * FROM 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 ";
	$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 ";
	$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";
	$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']);		
	}	
	
	
	if( isset($_POST['valide_form']) && $_POST['valide_form'] == "ok" )
	  {
			//header ('Location: salut.php');
			$_SESSION['message'] = "ok";
			
			echo $_POST['selection_score'];
			
			switch ($_POST['selection_score'])
			{
				case "J1 gagne":
					$elo_J1 = $_SESSION['$J1_gagne_score'] ;
					$elo_J2 = $_SESSION['$J2_perd_score'] ;
				break;
				
				case "J1 perd":
					$elo_J1 = $_SESSION['$J1_perd_score'] ;
					$elo_J2 = $_SESSION['$J2_gagne_score'] ;
				break;
				
				case "J2 gagne":
					$elo_J1 = $_SESSION['$J1_perd_score'] ;
					$elo_J2 = $_SESSION['$J2_gagne_score'] ;
				break;
				
				case "J2 perd":
					$elo_J1 = $_SESSION['$J1_gagne_score'] ;
					$elo_J2 = $_SESSION['$J2_perd_score'] ;
				break;
				
				case "Match nul":
					$elo_J1 = $_SESSION['$J1_match_nul'] ;
					$elo_J2 = $_SESSION['$J2_match_nul'] ;
				break;

				default:
					echo "erreur dans le traitement";
			}
		
			echo '<br/>';
			echo $elo_J1;
			echo '<br/>';
			echo $elo_J2;
			
		
			$sql = "SELECT * FROM joueurs WHERE nom = '".$_SESSION['$J1_nom']."' ";
			echo $sql;
			$result = mysql_query($sql);		
			$data = mysql_fetch_assoc($result);
			$id = $data['id'];
			echo $id;			
			$sql = "UPDATE joueurs SET score = '".$elo_J1."'	WHERE id = '$id' " ;
			echo $sql;
			//$requete = mysql_query($sql);
			
			$sql = "SELECT * FROM joueurs WHERE nom = '".$_SESSION['$J2_nom']."' ";
			echo $sql;
			$result = mysql_query($sql);		
			$data = mysql_fetch_assoc($result);
			$id = $data['id'];
			echo $id;			
			$sql = "UPDATE joueurs SET score = '".$elo_J2."'	WHERE id = '$id' " ;
			echo $sql;
			//$requete = mysql_query($sql);
			
			$heure = date("H:i");
			
			$J1_nom = $_SESSION['$J1_nom'];
			$J2_nom = $_SESSION['$J2_nom'];
			$resultat_partie = $_POST['selection_score'];
			
			$sql = "INSERT INTO partie( id, joueur1, joueur2, resultat_partie, score_joueur1, score_joueur2 ,date_partie, heure_partie) 
					VALUES('', '$J1_nom', '$J2_nom', '$resultat_partie' , '$elo_J1' , '$elo_J2'  , NOW(), '$heure' )"; 
			echo $sql;		
			$requete = mysql_query($sql);
			
			//
			/*CREATE TABLE IF NOT EXISTS partie (
		id tinyint(4) unsigned NOT NULL AUTO_INCREMENT,
		joueur1 varchar(30) NOT NULL DEFAULT '',
		joueur2 varchar(30) NOT NULL DEFAULT '',
		resultat_partie varchar(30) NOT NULL DEFAULT '',
		score_joueur1 varchar(30) NOT NULL DEFAULT '',
		score_joueur2 varchar(30) NOT NULL DEFAULT '',
		date_partie date NOT NULL DEFAULT '0000-00-00',
		heure_partie time NOT NULL DEFAULT '00:00:00',
		PRIMARY KEY (id)
		)*/
	  }
	
?>
  
<!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;
					
		}

		
label
{   
	display: block;
	width: 150px;
	float: left;
	//padding: 28px;
}

select
{
width: 100px;
}


		
</style>

<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 mise_jour_joueur2()
{
				var xhr = getXhr();
				document.getElementById("div_joueur2").style.visibility='visible';
				
				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){
						leselect = xhr.responseText;
						// On se sert de innerHTML pour rajouter les options a la liste
						document.getElementById('div_joueur2').innerHTML = leselect;
					}
				}

				// Ici on va voir comment faire du post
				xhr.open("POST","ajax_mise_jour_joueur2.php",true);
				// ne pas oublier ça pour le post
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				// ne pas oublier de poster les arguments
				
				sel = document.getElementById('J1');
				joueur = sel.options[sel.selectedIndex].value;
				xhr.send("joueur="+joueur);
}

function filtre()
{
	var choix1 = document.getElementById("choix1").checked;
	var choix2 = document.getElementById("choix2").checked;
	var choix3 = document.getElementById("choix3").checked;
	
	//alert(choix1);
	
	var J1 = document.getElementById("J1").value;
	var J2 = document.getElementById("J2").value;
	var resultat = document.getElementById("resultat").value;
	
	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){
						leselect = xhr.responseText;
						// On se sert de innerHTML pour rajouter les options a la liste
						document.getElementById('affiche_resultat').innerHTML = leselect;
					}
				}

				// Ici on va voir comment faire du post
				xhr.open("POST","filtre.php",true);
				// ne pas oublier ça pour le post
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				// ne pas oublier de poster les arguments
				
				// étude des cas
				if(choix1 == true)
				{  var choice1 = "ok";
				}
			    else { var choice1 = "no";
					}
				
				if(choix2 == true)
				{  var choice2 = "ok";
				}
			    else { var choice2 = "no";
					}

				if(choix3 == true)
				{  var choice3 = "ok";
				}
			    else { var choice3 = "no";
					}			
				
				xhr.send("J1="+J1+"&J2="+J2+"&choix1="+choice1+"&choix2="+choice2+"&choix3="+choice3+"&resultat="+resultat);
				
				//xhr.send("J1="+J1+"&J2="+J2);
			
}

</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_back_tag_selected"><a href="classement.php" title="Retour au menu principal">Retour</a></li>
				<!--
				<li class="tournoi_partie_tag"><a href="partie.php" title="aller à la section">Partie</a></li>
				<li class="tournoi_classement_tag"><a href="classement.php" title="aller à la section">classement</a></li>
				<li class="tournoi_liste_joueurs_tag"><a href="liste_joueurs.php" title="aller à la section">Joueurs</a></li>
				<li class="tournoi_stats_tag_selected"><a href="selection_filtre.php" title="aller à la section">stats</a></li>
				<li class="tournoi_securite_tag"><a href="admin.php" title="aller à la section">Admin</a></li>
				!-->
				</ul>
			</div>



			
			<div id="bloc_stats">


			<table> <!--align="center" -->
			
			<tr>
			<td bgcolor="#CCCCCC">Joueur:</td>
    		<td><?php echo $joueur; ?></td>
			</tr>
			
			<tr>
			<td height="35.7px" bgcolor="#CCCCCC">ELO:</td>
			<td>
	
<?php 
	echo $rating." "; 
	if ($oldrating !=0 || $incr!=0)
	{
		if ($incr>0 && $oldrating !=0)
		{ 
			echo "(+".$incr.")&nbsp;<img src='images/up.png'";
		} 
		elseif($incr<0 && $oldrating !=0) 
		{
			echo "(".$incr.")&nbsp;<img src='images/down.png'";
		}
	}
	
?>
	
    </td>
	</tr>		
	
	<tr>
    <td height="35.7px" bgcolor="#CCCCCC">Meilleur ELO:</td>
    <td><?php  echo $max_rating; if($max_rating !=0) { echo (" (le ".$max_date.")");} ?> </td>
	</tr>
	
	<tr>
    <td height="35.7px" bgcolor="#CCCCCC">Parties:</td>
    <td><?php echo $gcount;?></td>
	</tr>
	
	<tr>
    <td height="35.7px" bgcolor="#CCCCCC">Victoires:</td>
    <td><?php echo ($wins." (".$winp." %)"); ?></td>
   	</tr>
	
	<tr>
    <td height="35.7px" bgcolor="#CCCCCC">Défaites:</td>
    <td><?php echo ($losses." (".$lossp." %)"); ?></td>
	</tr>
	
	<tr>
    <td height="35.7px" bgcolor="#CCCCCC">Match nul:</td>
    <td><?php echo ($draws." (".$drawp." %)"); ?></td>
	</tr>
	
	<tr>
    <td colspan="2"><div id="chartdiv" align="center"	></div>
	
<?php 

			if ($gcount !=0)
			{
				echo ("
						<script type=\"text/javascript\">
						var myChart = new FusionCharts(\"include/Line.swf\", \"myChartId\", \"900\", \"350\", \"0\", \"0\");
						myChart.setDataURL(escape(\"include/data.xml?currTime=\" + getTimeForURL()));
						myChart.render(\"chartdiv\");
						function getTimeForURL(){
						var dt = new Date();
						var strOutput = \"\";
						strOutput = dt.getHours() + \"_\" + dt.getMinutes() + \"_\" + dt.getSeconds() + \"_\" + dt.getMilliseconds();
						return strOutput;
						}
						</script>"
				);
			}
			elseif ($gcount == 0) 
			{
				echo "<div align='center'>Pas de données pour ce joueur!</div>";
			}
	
?>	
	
	
	</td>
	</tr>
	
	</table>			
	
	<br>
	<br>
	
	
	<div align='center'>Parties disputées:</div>
	<br>

	<table width="750" align="center">
      <tr>
        <td width="15" bgcolor="#CCCCCC">Resultat</td>
        <td width="120" bgcolor="#CCCCCC">ELO</td>
        <td width="40" nowrap="nowrap" bgcolor="#CCCCCC">Couleur</td>
        <td width="200" bgcolor="#CCCCCC">Adversaire</td>
        <td width="120" bgcolor="#CCCCCC">Date</td>
      </tr>
	  
<?php 

	if($gcount !=0) 
	{
	
		$sql = "SELECT * FROM parties WHERE w_playerid = '$playerID' OR b_playerid = '$playerID' ORDER by date desc";
		$retid = mysql_query($sql);
	
		while ($row = mysql_fetch_array($retid)) 
		{ 
			$white = $row["w_playerID"]; 
			$w_name=$row["w_name"];
			$black = $row["b_playerID"];
			$b_name=$row["b_name"];
			$w_rating = $row["w_arating"];
			$b_rating = $row["b_arating"];
			$w_brating = $row["w_brating"];
			$b_brating = $row["b_brating"];
			$result=$row["result"];
			$date =$row["date"];
			$date=date("d F Y",$date);
			
			// setlocale(LC_TIME, 'fr_FR.UTF8');
			// setlocale(LC_TIME, 'fr_FR');
			// setlocale(LC_TIME, 'fr');
			//setlocale(LC_TIME, 'fra_fra');
			//echo strftime('%d %B %Y');           // 11 octobre 2012
			$date = strftime('%d-%m-%Y',strtotime($date));
			
			$diff_w = $w_rating - $w_brating;
			$diff_b = $b_rating - $b_brating;
			
			if($white==$playerID)
			{
				if ($result==1) 
				{
					echo "  
							<tr>
							<td width='20' height='20px'><div align='center'><img src='images/win.png'></div></td>
							<td width='100' >".$w_rating. " (" .$w_brating ." " .$diff_w .")<img src='images/up.png'></td>
							<td width='30'>White</td>
							<td width='160'>".$b_name." ".$b_rating ." (" .$b_brating ." " .$diff_b .") Black  <img src='images/down.png'></td>
							<td width='120'>".$date."</td>
							</tr>";
				}
				if ($result==3)
				{
					if($diff_w < 0)
					{
						$s1 = "<td width='100' >".$w_rating.  " (" .$w_brating ." " .$diff_w .")<img src='images/down.png'></td>";
						$s2 = "<td width='160'>".$b_name." ".$b_rating ." (" .$b_brating ." " .$diff_b .") Black<img src='images/up.png'></td>";
					}
					else
					{
						$s1 = "<td width='100' >".$w_rating.  " (" .$w_brating ." " .$diff_w .")<img src='images/up.png'></td>";
						$s2 = "<td width='160'>".$b_name." ".$b_rating ." (" .$b_brating ." " .$diff_b .") Black<img src='images/down.png'></td>";
					}
					
					echo "  
							<tr>
							<td width='20' height='20px'><div align='center'><img src='images/draw.png'></div></td>";
							echo $s1;
							/*<td width='100' >".$w_rating.  " (" .$w_brating ." " .$diff_w .")</td>*/
					echo "	<td width='30'>White</td>";
							echo $s2;
						/*	<td width='160'>".$b_name." ".$b_rating ." (" .$b_brating ." " .$diff_b .") Black</td>*/
					echo "	<td width='120'>".$date."</td>
							</tr>";
				}
				if($result==2)
				{
				
					echo "  
							<tr>
							<td width='20' height='20px'><div align='center'><img src='images/loss.png'></div></td>
							<td width='100' >".$w_rating. " (" .$w_brating ." " .$diff_w .")<img src='images/down.png'></td>
							<td width='30'>White</td>
							<td width='160'>".$b_name." ".$b_rating ." (" .$b_brating ." " .$diff_b .") Black <img src='images/up.png'></td>
							<td width='120'>".$date."</td>
							</tr>";
				}
			}
			
			if ($black==$playerID)
			{
				if ($result==2)
				{
					echo "  
							<tr>
							<td width='20' height='20px'><div align='center'><img src='images/win.png'></div></td>
							<td width='100' >".$b_rating. " (" .$b_brating ." " .$diff_b.")<img src='images/up.png'></td>
							<td width='30'>Black</td>
							<td width='160'>".$w_name." ".$w_rating ." (" .$w_brating ." " .$diff_w  .") White <img src='images/down.png'></td>
							<td width='120'>".$date."</td>
							</tr>";
				}
				if ($result==3)
				{
					if($diff_w < 0)
					{
						$s1 = "<td width='100' >".$w_rating.  " (" .$w_brating ." " .$diff_w .")<img src='images/down.png'></td>";
						$s2 = "<td width='160'>".$b_name." ".$b_rating ." (" .$b_brating ." " .$diff_b .") Black<img src='images/up.png'></td>";
					}
					else
					{
						$s1 = "<td width='100' >".$w_rating.  " (" .$w_brating ." " .$diff_w .")<img src='images/up.png'></td>";
						$s2 = "<td width='160'>".$b_name." ".$b_rating ." (" .$b_brating ." " .$diff_b .") Black<img src='images/down.png'></td>";
					}
					
					echo "  
							<tr>
							<td width='20' height='20px'><div align='center'><img src='images/draw.png'></div></td>";
					echo 	$s1;
						/*	<td width='100' >".$b_rating. " (" .$b_brating ." " .$diff_b.")</td>*/
					echo "	<td width='30'>Black</td>";
						/*	<td width='160'>".$w_name." ".$w_rating ." (" .$w_brating ." " .$diff_w .") White</td>*/
					echo 	$s2;			
					echo "	<td width='120'>".$date."</td>
							</tr>";
				}
				if($result==1)
				{
					echo "  
							<tr>
							<td width='20' height='20px'><div align='center'><img src='images/loss.png'></div></td>
							<td width='100' >".$b_rating. " (" .$b_brating ." " .$diff_b .")<img src='images/down.png'></td>
							<td width='30'>Black</td>
							<td width='160'>".$w_name." ".$w_rating ." (" .$w_brating ." " .$diff_w .") White <img src='images/up.png'></td>
							<td width='120'>".$date."</td>
							</tr>";
				}
			} 
		} 
	}
	
	 else
	 {
		echo "<td colspan='5' align='center'>Aucunes parties disputées!</td>";
	 }
?>
	  
	</table>
	<br/>
	<br/>
			
<!-------------------------------------------------------------------------->
 
			</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>


