File "compute_chart_elo_compare.php"

Full Path: /home/analogde/www/Freebox/CHESS_ON/compute_chart_elo_compare.php
File size: 10.61 KB
MIME-type: text/x-php
Charset: utf-8

<?php
	session_start();
	include('database.php');
	DB_connexion(); 
	header('Content-Type: text/html; charset=utf-8');
	
			
	//$J1 = "Henrique"; //$_GET["J1"];
	//$J2 = "Fabien"; //$_GET["J2"];
	
	$J1 = $_POST["J1"];
	$J2 = $_POST["J2"];
	
	$ret = findIdPlayer($J1);
	//echo "J1 : " .$ret;
	
	if(is_null($ret))
	{
		$J1_id = -1;;
	}
	else
	{
		$J1_id = $ret;
	}
	
	$ret = findIdPlayer($J2);
	//echo "J2 : " .$ret;
	
	if(is_null($ret))
	{
		$J2_id = -1;;
	}
	else
	{
		$J2_id = $ret;
	}

	//echo " ---> " .$J1_id ."  ----  " .$J2_id;
	
	if( $J1_id != -1 && $J2_id != -1 )
	{
			generation_data($J1_id, $J2_id);
	}
	
	
function generation_data($J1_id, $J2_id)
{	
	
			//$playerID = $J1_id; //"12";
			$J1_data = extract_data($J1_id);
	
			//echo "<pre>"; 
			//print_r($J1_data);
			//echo "</pre>"; 
	
			//$playerID = $J2_id; //"9";
			$J2_data = extract_data($J2_id);
	
			//echo "<pre>"; 
			//print_r($J2_data);
			//echo "</pre>"; 
	
			/**/
	
			$J1_nb_parties = count($J1_data);
			//echo "NB " .$J1_nb_parties;
			//echo "<br><br>";
	
			$J1_start_timestamp = $J1_data[0]["aaa"]; 
			$J1_start_date = $J1_data[0]["bbb"]; 
			//echo "Start " .$J1_start_timestamp ."  " .$J1_start_date;
			//echo "<br><br>";
		
			$J1_stop_timestamp = $J1_data[$J1_nb_parties-1]["aaa"]; 
			$J1_stop_date = $J1_data[$J1_nb_parties-1]["bbb"]; 
			//echo "Stop " .$J1_stop_timestamp ."  " .$J1_stop_date; 
			//echo "<br><br>";
	
	
			/**/
	
			$J2_nb_parties = count($J2_data);
			//echo "NB " .$J2_nb_parties;
			//echo "<br><br>";
	
			$J2_start_timestamp = $J2_data[0]["aaa"]; 
			$J2_start_date = $J2_data[0]["bbb"]; 
			//echo "Start " .$J2_start_timestamp ."  " .$J2_start_date;
			//echo "<br><br>";
		
			$J2_stop_timestamp = $J2_data[$J2_nb_parties-1]["aaa"]; 
			$J2_stop_date = $J2_data[$J2_nb_parties-1]["bbb"]; 
			//echo "Stop " .$J2_stop_timestamp ."  " .$J2_stop_date; 
			//echo "<br><br>";
	
			if( $J1_start_timestamp < $J2_start_timestamp)
			{
				$date_start = $J1_start_date;
			}
			else
			{
				$date_start = $J2_start_date;
			}
			
			if( $J1_stop_timestamp < $J2_stop_timestamp)
			{
				$date_stop = $J2_stop_date;
			}
			else
			{
				$date_stop = $J1_stop_date;
			}
	
			//echo " +++ " .$date_start ."  " .$date_stop;
	
			$tab_timestamp = interval( date_formatted($date_start), date_formatted($date_stop));
	  
			//echo "<pre>"; 
			//print_r($tab_timestamp);
			//echo "</pre>"; 
		
			$J1_elo_data = array();
	  
			for ($i = 0; $i < count($tab_timestamp)-1; $i++)
			{	
				$timestamp = strtotime($tab_timestamp[$i]);
				$tmp =array( $timestamp, "J1", "val");
				$J1_elo_data[] = $tmp;
			}
	 
			//echo " J1 data";
			//echo "<br><br>";
			//echo "<pre>"; 
			//print_r($J1_elo_data);
			//echo "</pre>"; 
			//echo "<br><br>";
	
			for ($i = 0; $i < count($tab_timestamp)-1; $i++)
			{	
				$timestamp = strtotime($tab_timestamp[$i]);
				$tmp =array( $timestamp, "J2", "val");
				$J2_elo_data[] = $tmp;
			}

			//echo " J2 data";
			//echo "<br><br>";
			//echo "<pre>"; 
			//print_r($J2_elo_data);
			//echo "</pre>"; 
			//echo "<br><br>";
		
			$cpt = 1;
	
			for ($i = 0; $i < count($J1_elo_data); $i++)
			{	
				$find_timestamp = $J1_elo_data[$i][0];
				$find = 0;
			
				for ($j = 0; $j < count($J1_data); $j++)
				{	
						$key = "aaa";
						if( $J1_data[$j][$key] == $find_timestamp)
						{
							//echo " +++++ " .$cpt . "  " .$find_timestamp . "  " .$J1_data[$j]["ccc"];
							//echo "<br>";
							$cpt++;
							$find = 1;
							$J1_elo_data[$i][2] = $J1_data[$j]["ccc"];
							break;
						}
				}
				
				if( $find == 0)
				{
					if($i == 0)
					{	
						$J1_elo_data[$i][2] = "1400";
					}
					else
					{
						$J1_elo_data[$i][2] = $J1_elo_data[$i-1][2];
					}	
				}
			
			}
	
			//echo "------ J1 ---------";	
			//echo "<br><br>";	
			//echo "<pre>"; 
			//print_r($J1_elo_data);
			//echo "</pre>"; 

			/****/
	  
			$cpt = 1;
	
			for ($i = 0; $i < count($J2_elo_data); $i++)
			{	
				$find_timestamp = $J2_elo_data[$i][0];
				$find = 0;
			
				for ($j = 0; $j < count($J2_data); $j++)
				{	
						$key = "aaa";
						if( $J2_data[$j][$key] == $find_timestamp)
						{
							//echo " +++++ " .$cpt . "  " .$find_timestamp . "  " .$J2_data[$j]["ccc"];
							//echo "<br>";
							$cpt++;
							$find = 1;
							$J2_elo_data[$i][2] = $J2_data[$j]["ccc"];
							break;
						}
				}
				
				if( $find == 0)
				{
					if($i == 0)
					{	
						$J2_elo_data[$i][2] = "1400";
					}
					else
					{
						$J2_elo_data[$i][2] = $J2_elo_data[$i-1][2];
					}	
				}
			
			}
	
			//echo "------ J2 ---------";	
			//echo "<br><br>";	
	
			//echo "<pre>"; 
			//print_r($J2_elo_data);
			// echo "</pre>"; 
	  
			///
	  
	  
			for ($i = 0; $i < count($J1_elo_data); $i++)
			{
				$transformation = date('d-M-Y', $J1_elo_data[$i][0]);
				$J1_elo_data[$i][0] =  $transformation;
				$J2_elo_data[$i][0] =  $transformation;
			}
	  
			$elo_combine = array();
	  
			for ($i = 0; $i < count($J1_elo_data); $i++)
			{
				$elo_combine[] = $J1_elo_data[$i];
				$elo_combine[] = $J2_elo_data[$i];
			}
	  
			$json = json_encode($elo_combine);
			
			$file  = "elo_combine.json";
			
			file_put_contents("elo_combine.json", $json);
			//echo $json;
			echo $json;
			
			
			
}
	  
/***************************************************************/

function findIdPlayer($joueur)
{	
	//$joueur = "Henrique";
	
	//$sql = "SELECT * FROM " .$_SESSION['parties'] ." WHERE w_name = '$joueur' "; //OR b_name = '$joueur' "; //  desc LIMIT 20";
	$sql = "SELECT * FROM parties_new WHERE w_name = '$joueur' "; // OR b_name = '$joueur' "; //  desc LIMIT 20";
		
	$sql = "SELECT * FROM " .$_SESSION['parties'] ." WHERE w_name = '$joueur' "; // OR b_name = '$joueur' "; //  desc LIMIT 20";

		
	//echo $sql;			
				
	$results = mysql_query($sql);
	$nb_parties = mysql_num_rows($results);
	
	//echo " ++++ " .$nb_parties;
	
	if( $nb_parties != 0)
	{
		while ($row = mysql_fetch_array($results)) 
		{ 
			$result = $row["w_playerID"]; 
			//echo " **** " .$result;
			break;
		}
	}
	else
	{
		$sql = "SELECT * FROM parties_new WHERE b_name = '$joueur' "; // OR b_name = '$joueur' "; //  desc LIMIT 20";
		
		$nb_parties = mysql_num_rows($results);
		if( $nb_parties != 0)
		{
			while ($row = mysql_fetch_array($results)) 
			{ 
				$result = $row["w_playerID"]; 
				break;
			}
		}
		else
		{
			//echo "Pas de partie disputées !.";
			$result = NULL;
		}
	}
	
	return $result;
}


/***************************************************************/
	  
function extract_data($playerID)	
{	

	//$sql = "SELECT * FROM parties_new WHERE w_playerID = '$playerID' OR b_playerID = '$playerID' ORDER by date asc ";		
	
	$sql = "SELECT * FROM " .$_SESSION['parties'] ." WHERE w_playerID = '$playerID' OR b_playerID = '$playerID' ORDER by date asc ";		
	
	//echo $sql;
	//echo "<br><br>";
	
	$graph_data = array();
	
	$result = mysql_query($sql);	
				
				$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_convert = strftime('%d-%m-%Y',strtotime($date));
					
					$timestamp = $row["date"];
					$date= date("d F Y",$timestamp);
					$date_convert = strftime('%d-%h-%Y',strtotime($date));
					
					$diff_w = $w_rating - $w_brating;
					$diff_b = $b_rating - $b_brating;
			
					if($white==$playerID)
					{
						if ($result==1) 
						{
							//$pipo =array( $timestamp, $date_convert , $w_rating);
							$pipo = array("aaa"=>$timestamp,"bbb"=>$date_convert,"ccc"=>$w_rating);
						}
						if($result==2)
						{
							//$pipo =array($timestamp, $date_convert , $w_rating);
							$pipo = array("aaa"=>$timestamp,"bbb"=>$date_convert,"ccc"=>$w_rating);
						}
						if ($result==3)
						{
							//$pipo =array( $timestamp, $date_convert , $w_rating);
							$pipo = array("aaa"=>$timestamp,"bbb"=>$date_convert,"ccc"=>$w_rating);
						}
					}
			
					if ($black==$playerID)
					{
						if($result==1)
						{
							
									
									//$pipo =array( $timestamp, $date_convert , $b_rating);
									$pipo = array("aaa"=>$timestamp,"bbb"=>$date_convert,"ccc"=>$b_rating);
									

						}
						if ($result==2)
						{
							
									
									//$pipo =array( $timestamp, $date_convert , $b_rating);
									$pipo = array("aaa"=>$timestamp,"bbb"=>$date_convert,"ccc"=>$b_rating);


						}
						if ($result==3)
						{
							
					
							//$pipo =array( $timestamp, $date_convert , $b_rating);
							$pipo = array("aaa"=>$timestamp,"bbb"=>$date_convert,"ccc"=>$b_rating);

					
							
						}
					} 
			
				$graph_data[] = $pipo;
				
					$id = $id + 1;
				} 
			
			
			return $graph_data;
}	
function interval($date1, $date2)
{
	//$date1 = '01-10-2010';
	//$date2 = '05-10-2010';
  
	// Declare an empty array
	$array = array();
  
	// Use strtotime function
	$variable1 = strtotime($date1);
	$variable2 = strtotime($date2);
  
	// Use for loop to store dates into array
	// 86400 sec = 24 hrs = 60*60*24 = 1 day
	for ($currentDate = $variable1; $currentDate <= $variable2;  $currentDate += (86400)) 
	{
		$store = date('Y-m-d', $currentDate);
		$array[] = $store;
	}
	
	return $array;
}
	
function date_formatted($date)
{
		$pieces = explode("-", $date);
	  
		switch ($pieces[1]) 
		{
			case 	'Jan':
					$detected_month = "01";
			break;
			case 	'Feb':
					$detected_month = "02";
			break;
			case	'Mar':
					$detected_month = "03";
			break;
			case	'Apr':
					$detected_month = "04";
			break;
			case 	'May':
					$detected_month = "05";
			break;
			case 	'Jun':
					$detected_month = "06";
			break;
			case	'Jul':
					$detected_month = "07";
			break;
			case 	'Aug':
					$detected_month = "08";
			break;
			case 	'Sep':
					$detected_month = "09";
			break;
			case 	'Oct':
					$detected_month = "10";
			break;
			case	'Nov':
					$detected_month = "11";
			break;
			case	'Dec':
					$detected_month = "12";
			break;			
		}
	  
	   $date_str = $pieces[2] ."-" .$detected_month ."-" .$pieces[0];
	   return $date_str;
}	

	
?>