<?php
/*****************************************************************************************
******************************************************************************************
**  Description : Displays the checkpoint-records of the current track in a widget      **
**                                                                                      **
**  Version: 0.94                                                                       **  
**  Author: junglehero                                                                  **
******************************************************************************************
*****************************************************************************************/                                                          

Aseco::registerEvent('onPlayerConnect', 'playerConnection');
Aseco::registerEvent("onCheckpoint", "get_cp_data");
Aseco::registerEvent("onEndMap", "update_display");
Aseco::registerEvent("onBeginMap", "init_cprecords");

$widget_data = array();
$cpdata = array();
$settings = array();

class tab{
    var $time;
	var $nickname;
	
	function tab($time, $nickname){
		$this->time = $time;
		$this->nickname = $nickname;
	}
}

function update_display($aseco, $end) {
    global $widget_data, $settings;
    if(strtoupper($settings->podium) =='FALSE') {
	   $xml='<manialink id="987123654"></manialink>';
	   $aseco->client->addCall("SendDisplayManialinkPage", array($xml, 0, false));
	}
	else {
	   $xml = get_manialink($widget_data, true);
	   $aseco->client->query("SendDisplayManialinkPage", $xml, 0, false);
	}
}

function init_cprecords($aseco, $challenge) {
    global $numcps, $widget_data, $settings;
    $settings = simplexml_load_file('cprecords.xml');
    $numcps = $challenge->nbchecks;
    $widget_data = array();
    $xml='<manialink id="987123654"></manialink>';
    $aseco->client->addCall("SendDisplayManialinkPage", array($xml, 1, false));
}

function playerConnection($aseco, $player) {
    global $widget_data;
    $xml = '';
    if($widget_data) {
        $xml = get_manialink($widget_data);
        $aseco->client->addCall("SendDisplayManialinkPageToLogin", array($player->login, $xml, 0, false));
    }
}

function get_cp_data($aseco, $param){
	global  $widget_data, $nickname, $time, $cp, $numcps;
	$xml = '';
	$cp_update = false;
	$nickname = $aseco->server->players->player_list[$param[1]]->nickname;
	$time = $param[2];
	$cp = $param[4];
	
	
	if(isset($widget_data[$cp])) {
        if($time < $widget_data[$cp]->time && $cp < ($numcps-1)) {
    	   $widget_data[$cp] = new tab($time, $nickname);
    	   $cp_update = true;
    	}
	}
	else if($cp < ($numcps-1)) {
        $widget_data[$cp] = new tab($time, $nickname);
        $cp_update = true;
	}
	if($cp_update) {
    	$xml = get_manialink($widget_data);
    	$aseco->client->query("SendDisplayManialinkPage", $xml, 0, false);
	}
 }
	
function get_manialink($cpdata, $end=false) {
    global $numcps, $settings,$aseco;
    $startX = $settings->pos->posX;
    $startY = $settings->pos->posY;
    $width = 14.5;
    $height = 3;
    $heightcount = 0;
	$count = 0;
	$cpnr = 1;
	$cpdisp = '';
	$max_disp_cps = $settings->numtabs;
	$orientation = strtoupper($settings->orientation);
	$line = 0;
	$newLine = 1;
	$number_of_lines = 1;
	$player = '';
	$zeit = 0;
	$disp_cpnum = 0;
	$has_value = false;
    
	if($end == true && strtoupper($settings->switchPos) == 'TRUE') {
	   $startX = $settings->end->pos->posX;
	   $startY = $settings->end->pos->posY;
	   $max_disp_cps = $settings->end->numtabs;
	   $orientation = 'H';
	   if(strtoupper($settings->end->center) == 'TRUE') {
	       $leftX = $settings->end->range->posXleft;
	       $rightX = $settings->end->range->posXright;
	       $number_of_lines = ceil(count($cpdata) / $max_disp_cps);
	       		
	       if(count($cpdata) < $max_disp_cps) {
	           $number_of_tabs = count($cpdata);
	       }
	       else {
	           $number_of_tabs = $max_disp_cps;
	       }
	       $startX = round((($leftX + $rightX) /2 - $number_of_tabs * 14.5 / 2), 1);
	   }
	}
	
	$xml='<?xml version="1.0" encoding="UTF-8"? >';
	$xml.='<manialink id="987123654">';
	$xml.='<frame posn="'.$startX.' '.$startY.' 0">';
	$xml.='<format textsize="1"/>';
	
	
	for($i=0; $i<($numcps-1); $i++) {
	   if(isset($cpdata[$i])) {
            $zeit = $cpdata[$i]->time;
            $player = $cpdata[$i]->nickname;
            $disp_cpnum = $i+1;
            $has_value = true;
        }
        if($has_value) {
        $min = (int) (($zeit) / 60000);
		$sec = (int) ((($zeit) - $min * 60000) / 1000);
		$mil = (($zeit - $min*60000 - $sec*1000));
		if($sec < 10) {
		  $sec = '0'.$sec;
		}
		if($mil < 100 && $mil >= 10) {
		  $mil = '0'.$mil;
		}
		if($mil < 10) {
		  $mil = '00'.$mil;
		}
        
        if(($cpnr-1) % $max_disp_cps == 0 && ($cpnr-1) != 0) {
            $newLine++;
            if($orientation == 'H') {
                $Y_Pos = -3;
                $X_Pos = 0;
            }
            if($orientation == 'V') {
                $Y_Pos = 0;
                $X_Pos = 14.5;
            }
            
            if($orientation == 'H' && $number_of_lines == $newLine && (strtoupper($settings->end->center)) == 'TRUE' && $end == true) {
                $tabs_in_last_line = count($cpdata) - ($newLine-1)  * $max_disp_cps;
                if($tabs_in_last_line < $max_disp_cps) {
                    $X_Pos = round(($max_disp_cps * 14.5 /2 - $tabs_in_last_line * 14.5 / 2), 1);
                }
                else {
                    $X_Pos = 0;
                }
            }
            
            $count = 0;
            $heightcount = 0; 
            $line++;
            $xml.='<frame posn="'.$X_Pos.' '.$Y_Pos.' 0">';
        }
        
        
        if($orientation == 'V') {
            $pos_y = -3*$heightcount;
            $pos_y1 = -3*$heightcount-0.9;
            $pos_x = 0;
            $pos_x1 = 2;
            $pos_x2 = 9;
            $heightcount++; 
        }
        if($orientation == 'H') {
            $pos_y = '0';
            $pos_y1 = '-0.9';
            $pos_x = $width * $count;
            $pos_x1 = $width * $count + 2;
            $pos_x2 = $width * $count + 9;
            $count++; 
        }
        $xml.='<quad sizen="14 3" posn="'.$pos_x.' '.$pos_y.' 0" style="BgsPlayerCard" substyle="BgCard"/>';
        $xml.='<label sizen="2 1" halign="left" posn="'.$pos_x.' '.$pos_y1.' 0.1" textcolor="0FFA" text="$nCP'.$disp_cpnum.'"/>';
        $xml.='<label sizen="4 1" halign="left" posn="'.$pos_x1.' '.$pos_y1.' 0.1" text="$n'.$min.':'.$sec.'.'.$mil.'"/>';
        $xml.='<label sizen="8 1" halign="center" posn="'.$pos_x2.' '.$pos_y1.' 0.1" text="'.$player.'"/>';
             
        $cpnr++;
        $has_value = false;
	}
	}
	
	for($i=0; $i<$line; $i++) {
        $xml.='</frame>';
    }
    
	$xml.='</frame></manialink>'; 
	return $xml;
	

}


?>
