<?php

include './core.php';

if( !isset( $user ) || !$user->can_access( 'status' ) ) {
} else {
  $year = date( "Y" );
	$month = date( "m" );
	$day = date( "d" ) - 1;	
	$hour = date( "H" );
	$hour_next = $hour - 1;
  
	$starthour = $hour;
	$hours = array();
	
	for( $i=0; $i<24; $i++ ) {
		$player = $MySQL->query_first( "SELECT SUM( value ) / Count( value ) FROM server.serverstatus WHERE server=0 AND statusvariable=0 AND changed > '$year-$month-$day $hour:00:00' AND changed <= '$year-$month-$day $hour_next:00:00'" );
		
		$hours[$i] = "$player[0]";
		
		$hour++;
		if( $hour == 24 ) {
		  $day++;
		  $hour = 0;
		}
		$hour_next = $hour + 1;
	}
	
	$im = @imagecreate( 348, 200 ) or die( "Cannot initialize Image" );
  $background_color = imagecolorallocate( $im, 255, 255, 255 );
	$blue = imagecolorallocate( $im, 0, 0, 255 );
	$black = imagecolorallocate( $im, 0, 0, 0 );
	$blub = imagerectangle( $im, 39, 19, 328, 160, $black );	
	
	$maximum = max( ceil( max( $hours ) ) + 1, 11 );
	$minimum = floor( min( $hours ) ) + 1; 
	
	for( $i=0; $i<24; $i++ ) {
		$blub = imagefilledrectangle( $im, 40 + $i*12, 159 - round( $hours[$i] * 138 / $maximum ), 40 + ($i+1)*12, 159, $blue );   
	}
	
  $tick = floor( $maximum / 4 );
	$tick = ceil( $tick / 10 ) * 10;
	$tickcount = floor( $maximum / $tick );

  $font = "arial.ttf";		
	$fontwidth = imagefontwidth( 0 );
	for( $i=0; $i<$tickcount+1; $i++ ) {
	  $value = $i*$tick;
		$text = "$value";
		imagestring( $im, 0, 37 - ((strlen($text) * $fontwidth)), 159 - round( $value*138 / $maximum + $fontwidth / 2 ), $text, $black );	
  } 
	
	$value = round( $starthour );
	for( $i=0; $i<7; $i++ ) {
		$text = "$value";
		imagestring( $im, 0, 40 - round( $fontwidth * strlen( $text ) / 2 ) + $i*4*12, 170, $text, $black );
	  $value += 4;
		if( $value > 24 ) {
		  $value -= 24;
		}		
  }
	
	Header("Content-type: image/jpeg"); 
  imagejpeg( $im );
  imagedestroy( $im );
}	

?>
