<?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);
			
	  }
	
?>
  
<!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 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';
			document.getElementById('affiche_resultat').innerHTML = "";
			
			document.getElementById('bloc_listing').style.display = "none";
			//document.getElementById("div_filtre").display: none ='hidden';
			//document.getElementById("div_filtre").style.display = "none";

//			document.getElementById("div_filtre").style.display = "block";
	//		document.getElementById("div_filtre").innerHTML = "nada";
/*			
			var div = document.getElementById('div_filtre');
while(div.firstChild){
    div.removeChild(div.firstChild);
}
*/

/*var element = document.getElementById("div_filtre");
element.parentNode.removeChild(element);*/
			
		/*	var e = document.getElementById("div_filtre");
			if(e.style.display == 'block')
				e.style.display = 'none';
			else
				e.style.display = 'block';*/
		
			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 J1 = document.getElementById("J1").value;
			var J2 = document.getElementById("J2").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)
				{
					retour = xhr.responseText;
					document.getElementById('affiche_resultat').innerHTML = retour;
				}
			}

			// Ici on va voir comment faire du post
			xhr.open("POST","new_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
						
			//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 id="bloc_filtre">

			<input type="hidden" name="valide_form" id="valide_form" value="" />
			<input type="hidden" name="filtrage" id="filtrage" value="" />
			
			<fieldset style="width: 600px">
			<legend>Paramétre du filtre </legend>
				
			<p>
			<label for="nom">Joueur1 :</label>
			<select name='J1' id='J1' onchange='mise_jour_joueur2()'>
<?php
			$sql = "SELECT * FROM classement";
			$results = mysql_query($sql);
			while($val = mysql_fetch_array($results)) 
			{
				echo "<option>".$val["joueur"]."</option>\n";
			}
?>
			</select>
			</p>
				
			<div id="div_joueur2" style="visibility: hidden;"  >  
			</div> 
				
			
			<div id="affiche_resultat"></div>		
			
			</fieldset>
			</div>	
	
<!------------------------------------------------------------------------------>	
			<div id="bloc_listing">
			<div align='center'>Parties disputées:</div>
			<br>

			<table width="750" align="center">
			<tr>
			<div id = "alignement"> 
			<td width="15" align='center' bgcolor="#CCCCCC">ID</td>
			<td width="15" align='center' bgcolor="#CCCCCC">Resultat</td>
			<td width="120" align='center' bgcolor="#CCCCCC">ELO</td>
			<td width="40" align='center' nowrap="nowrap" bgcolor="#CCCCCC">Couleur</td>
			<td width="200" align='center' bgcolor="#CCCCCC">Adversaire</td>
			<td width="120" align='center' bgcolor="#CCCCCC">Date</td>
			</div>	
			</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);
	
				$id = 1;
		
				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);
					
					$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='15' align='center'>".$id."</td>
									<td width='15' align='center'><img src='images/win.png'></td>
									<td width='120' align='center' >".$w_rating. " (" .$w_brating ." " .$diff_w ." )<img src='images/up.png'></td>
									<td width='40' align='center'>White</td>
									<td width='200' align='center'>".$b_name." ".$b_rating ." (" .$b_brating ." " .$diff_b .") Black  <img src='images/down.png'></td>
									<td width='120' align='center'>".$date."</td>
									</tr>";
						}
						if($result==2)
						{
							echo "  
									<tr>
									<td width='15' align='center'>".$id."</td>
									<td width='15' align='center'><img src='images/loss.png'></td>
									<td width='120' align='center' >".$w_rating. " (" .$w_brating ." " .$diff_w ." )<img src='images/down.png'></td>
									<td width='40' align='center'>White</td>
									<td width='200' align='center'>".$b_name." ".$b_rating ." (" .$b_brating ." " .$diff_b .") Black  <img src='images/up.png'></td>
									<td width='120' align='center'>".$date."</td>
									</tr>";
						}
						if ($result==3)
						{
							if($diff_w < 0)
							{
								$s1 = "<td width='120' align='center'>".$w_rating.  " (" .$w_brating ." " .$diff_w .")<img src='images/down.png'></td>";
								$s2 = "<td width='200' align='center'>".$b_name." ".$b_rating ." (" .$b_brating ." " .$diff_b .") Black <img src='images/up.png'></td>";
							}
							else
							{
								$s1 = "<td width='120' align='center'>".$w_rating.  " (" .$w_brating ." " .$diff_w .")<img src='images/up.png'></td>";
								$s2 = "<td width='200' align='center'>".$b_name." ".$b_rating ." (" .$b_brating ." " .$diff_b .") Black <img src='images/down.png'></td>";
							}
					
							echo "  
									<tr>
									<td width='15' align='center'>".$id."</td>
									<td width='15' align='center'><img src='images/draw.png'></td>";
									echo $s1;
							echo "	<td width='40' align='center'>White</td>";
									echo $s2;
							echo "	<td width='120' align='center'>".$date."</td>
									</tr>";
						}
					}
			
					if ($black==$playerID)
					{
						if($result==1)
						{
							echo "  
									<tr>
									<td width='15' align='center'>".$id."</td>
									<td width='15' align='center'><img src='images/loss.png'></td>
									<td width='120' align='center' >".$b_rating. " (" .$b_brating ." " .$diff_b .")<img src='images/down.png'></td>
									<td width='40' align='center'>Black</td>
									<td width='200' align='center'>".$w_name." ".$w_rating ." (" .$w_brating ." " .$diff_w .") White <img src='images/up.png'></td>
									<td width='120' align='center'>".$date."</td>
									</tr>";
						}
						if ($result==2)
						{
							echo "  
									<tr>
									<td width='15' align='center'>".$id."</td>
									<td width='15' align='center'><img src='images/win.png'></td>
									<td width='120' align='center'>".$b_rating. " (" .$b_brating ." " .$diff_b.")<img src='images/up.png'></td>
									<td width='40' align='center'>Black</td>
									<td width='200' align='center'>".$w_name." ".$w_rating ." (" .$w_brating ." " .$diff_w  .") White <img src='images/down.png'></td>
									<td width='120' align='center'>".$date."</td>
									</tr>";
						}
						if ($result==3)
						{
							if($diff_w < 0)
							{
								$s1 = "<td width='120' align='center' >".$b_rating.  " (" .$b_brating ." " .$diff_b .")<img src='images/down.png'></td>";
								$s2 = "<td width='200' align='center'>".$w_name." ".$w_rating ." (" .$w_brating ." " .$diff_w .") Black <img src='images/up.png'></td>";
							}
							else
							{
								$s1 = "<td width='120' align='center'>".$b_rating.  " (" .$b_brating ." " .$diff_b .")<img src='images/up.png'></td>";
								$s2 = "<td width='200' align='center'>".$w_name." ".$w_rating ." (" .$w_brating ." " .$diff_w .") Black <img src='images/down.png'></td>";
							}
					
							echo "  
									<tr>
									<td width='15' align='center'>".$id."</td>
									<td width='15' align='center'><img src='images/draw.png'></td>";
							echo 	$s1;
							echo "	<td width='40' align='center'>Black</td>";
							echo 	$s2;			
							echo "	<td width='120' align='center'>".$date."</td>
									</tr>";
						}
					} 
			
			
					$id = $id + 1;
				} 
			}
	
			else
			{
				echo "<td colspan='5' align='center'>Aucunes parties disputées!</td>";
			}
?>
	  
			</table>
			<br/>
			<br/>
			</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>


