File "build_chart_data.php"

Full Path: /home/analogde/www/download/build_chart_data.php
File size: 13.11 KB
MIME-type: text/x-php
Charset: utf-8

<?php

	include('database.php');
	DB_connexion(); 
	header('Content-Type: text/html; charset=utf-8');
	
	
function createDateRangeArray($strDateFrom,$strDateTo)
{
    // takes two dates formatted as YYYY-MM-DD and creates an
    // inclusive array of the dates between the from and to dates.

    // could test validity of dates here but I'm already doing
    // that in the main script

    $aryRange = [];

    $iDateFrom = mktime(1, 0, 0, substr($strDateFrom, 5, 2), substr($strDateFrom, 8, 2), substr($strDateFrom, 0, 4));
    $iDateTo = mktime(1, 0, 0, substr($strDateTo, 5, 2), substr($strDateTo, 8, 2), substr($strDateTo, 0, 4));

    if ($iDateTo >= $iDateFrom) {
        array_push($aryRange, date('Y-m-d', $iDateFrom)); // first entry
        while ($iDateFrom<$iDateTo) {
            $iDateFrom += 86400; // add 24 hours
            array_push($aryRange, date('Y-m-d', $iDateFrom));
        }
    }
    return $aryRange;
}	
	
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;
}	

	?>
	<table width="750" align="center">
			<tr>
			<div id = "alignement"> 
			<td width="50" 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	

	echo " Extraction des parties ....";
	echo "<br><br>";

	$playerID = "12";

	$sql = "SELECT * FROM parties_new 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) 
						{
							echo "  
									<tr>
									<td width='50' align='center'>".$id."  " ."W1" ."</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_convert."</td>
									</tr>";
									
									//$pipo =array( $timestamp, $date_convert , $w_rating);
									
									$pipo = array("aaa"=>$timestamp,"bbb"=>$date_convert,"ccc"=>$w_rating);
									
						}
						if($result==2)
						{
							echo "  
									<tr>
									<td width='50' align='center'>".$id."  " ."W2"  ."</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_convert."</td>
									</tr>";
									
									//$pipo =array($timestamp, $date_convert , $w_rating);
									$pipo = array("aaa"=>$timestamp,"bbb"=>$date_convert,"ccc"=>$w_rating);
									
						}
						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>";
							}
					
							//$pipo =array( $timestamp, $date_convert , $w_rating);
							$pipo = array("aaa"=>$timestamp,"bbb"=>$date_convert,"ccc"=>$w_rating);
									
					
							echo "  
									<tr>
									<td width='50' 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='50' align='center'>".$id  ."  " ."B1" ."</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_convert."</td>
									</tr>";
									
									//$pipo =array( $timestamp, $date_convert , $b_rating);
									$pipo = array("aaa"=>$timestamp,"bbb"=>$date_convert,"ccc"=>$b_rating);
									

						}
						if ($result==2)
						{
							echo "  
									<tr>
									<td width='50' align='center'>".$id ."  " ."B2"  ."</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_convert."</td>
									</tr>";
									
									//$pipo =array( $timestamp, $date_convert , $b_rating);
									$pipo = array("aaa"=>$timestamp,"bbb"=>$date_convert,"ccc"=>$b_rating);


						}
						if ($result==3)
						{
							if($diff_b < 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>";
							}
					
							//$pipo =array( $timestamp, $date_convert , $b_rating);
							$pipo = array("aaa"=>$timestamp,"bbb"=>$date_convert,"ccc"=>$b_rating);

					
							echo "  
									<tr>
									<td width='50' 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>";
						}
					} 
			
				$graph_data[] = $pipo;
				
					$id = $id + 1;
				} 
			
	
			
?>
	  
			</table>
	
<?php
		echo "<pre>"; 
		print_r($graph_data);
		echo "</pre>"; 

		/// completer 
		
		$nb_parties = count($graph_data);
		echo "NB " .$nb_parties;
		echo "<br><br>";
		
		$start_timestamp = $graph_data[0][0]; 
		$date1 = $graph_data[0][1]; 
		echo "Start " .$date1 ."  " .$start_timestamp;
		echo "<br><br>";
		
		$stop_timestamp = $graph_data[$nb_parties-1][0]; 
		$date2 = $graph_data[$nb_parties-1][1]; 
		echo "Stop " .$date2 ."  " .$stop_timestamp; 
		echo "<br><br>";
		
		//$diff_jours = ($stop_timestamp - $start_timestamp) / 86400;
		//echo $diff_jours; 
		//echo "<br><br>";
		
		// 24 * 60 * 60
		//$interval = date_diff($datetime1, $datetime2);
		
		$diff = strtotime($date2) - strtotime($date1);
		
		//1583877600
		//1583964000
		
  
      // 1 day = 24 hours
      // 24 * 60 * 60 = 86400 seconds
      $nb_jours = abs(round($diff / 86400));

	  $pipo = array();
	  
	  $time = $start_timestamp;
	  
	  for ($i=0; $i<$nb_jours; $i++) 
	  {
			$pipo[] = array($time,22);
			$time = $time + 86400;
	  }
	  
	  //var_dump($pipo);
	  
	  //echo "<pre>"; 
	  //print_r($pipo);
	  //echo "</pre>"; 
	  
	  // 25-Apr-2014
	  // 13-Mar-2020
	  //YYYY-MM-DD
	  
	  $tab = createDateRangeArray("2014-04-25","2020-03-13");
	  
	  //echo "<pre>"; 
	  //print_r($tab);
	  //echo "</pre>"; 
	  
	  /***************************************************************/	  
	 
	  
	  //$tab_timestamp = interval("2014-04-25","2020-03-13");
	  
	  $tab_timestamp = interval( date_formatted($date1), date_formatted($date2));
	  
	  echo "<pre>"; 
	  print_r($tab_timestamp);
	  echo "</pre>"; 
	  
	  $elo_data = array();
	  
	  for ($i = 0; $i < count($tab)-1; $i++)
	  {	
			$timestamp = strtotime($tab[$i]);
			$tmp =array( $timestamp, "val");
			$elo_data[] = $tmp;
	  }
	  
	  echo "<pre>"; 
	  print_r($elo_data);
	  echo "</pre>"; 
	  
	 
	 echo "Find ... " .$elo_data[0][0];
	  
	 // $key = array_search(  $elo_data[0][0] ,$graph_data);
	 
	
	
	//$id = searchForId( $elo_data[0][0], $graph_data);
	
	
	
	
	
	$cpt = 1;
	
	for ($i = 0; $i < count($elo_data); $i++)
	{	
		$find_timestamp = $elo_data[$i][0];
		$find = 0;
	
		for ($j = 0; $j < count($graph_data); $j++)
		{	
				$key = "aaa";
				if( $graph_data[$j][$key] == $find_timestamp)
				{
					echo " +++++ " .$cpt . "  " .$find_timestamp . "  " .$graph_data[$j]["ccc"];
					echo "<br>";
					$cpt++;
					$find = 1;
					$elo_data[$i][1] = $graph_data[$j]["ccc"];
					break;
				}
		}
		
		if( $find == 0)
		{
			$elo_data[$i][1] = $elo_data[$i-1][1];
		}
	
	}
	
	 
	  
	  for ($i = 0; $i < count($elo_data); $i++)
	  {
		 $transformation = date('d-M-Y', $elo_data[$i][0]);
		$elo_data[$i][0] =  $transformation;
	 }
	  
	  echo "<pre>"; 
	  print_r($elo_data);
	  echo "</pre>"; 
	  
	  $json = json_encode($elo_data);
	 file_put_contents("elo_data.json", $json);
	
	//$key = array_search($elo_data[0][0], array_column($graph_data, 'ccc'));
	// echo " +++++ " .$key;	 
	  
	  ///// https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value
	  
	  //   https://www.geeksforgeeks.org/php-multidimensional-array-search-by-value/
	  
	  // https://code.tutsplus.com/tutorials/working-with-php-arrays-in-the-right-way--cms-28606
	  
/*	  
	  for ($i = 0; $i < count($elo_data); $i++)
	  {
	  
			echo $elo_data[$i][0];
			echo "<br>";
			
			$timestamp = $elo_data[$i][0];
	  
$key = array_search(  , $array);
	  
	  }
	*/  
	
function searchForId($elo, $array) 
{
   foreach ($array as $key => $val) 
   {
       if ($val['ccc'] === $elo) 
	   {
           return $key;
       }
   }
   return null;
}	
?>