<?php
/*******************************************************************************************************************
 * MRServerlink v0.0.2 Beta for TM2
 *
 * Plugin by vni
 *
 * Visit www.maniarecords.org
 *
 ********************************************************************************************************************/
 
 

/***********************************
 * Xaseco2-Events
 ***********************************/
	Aseco::registerEvent('onSync',							'mrsl_event_Sync');
	Aseco::registerEvent('onPlayerConnect', 				'mrsl_event_Connect');
	Aseco::registerEvent('onBeginRound', 					'mrsl_event_StartRound');
	Aseco::registerEvent('onBeginRound', 					'mrsl_event_StartRound');
	Aseco::registerEvent('onRestartMap', 					'mrsl_event_StartRound');
	Aseco::registerEvent('onBeginMap2',						'mrsl_event_StartRound');
	
	Aseco::registerEvent('onEndRound', 						'mrsl_event_EndRound');
	Aseco::registerEvent('onEndMap1', 						'mrsl_event_EndRound');

	Aseco::registerEvent('onEverySecond',					'mrsl_event_onEverySecond');

/***********************************
 * Initialization
 ***********************************/
	global $mrsl, $wr_config;
	$mrsl['version_string'] = '0.0.2';
	$mrsl['panel_id']['main'] = 9002123;
	
	
	$mrsl['lastdata']		= '';
	$mrsl['lastrotation']	= time();
	$mrsl['lastrotation_offset']	= 0;
	$mrsl['current_data']	= '';
	$mrsl['data']			= false;
	$mrsl['cachedpanel']	= false;
	
//Loading the XML-Config
	$mrslXML = simplexml_load_file('mrserverlink.xml');

//General Styling
	//$mrsl['config']['fontsize'] = (float)$settingsXML->fontSize ? (float)$settingsXML->fontSize : 1 ; //1.5;
	$mrsl['XML']['rotation_time'] = (string)$mrslXML->rotation_time ? (string)$mrslXML->rotation_time : 0;

	$mrsl['XML']['color_playercount'] = (string)$mrslXML->color_playercount ? (string)$mrslXML->color_playercount : 'F90';
	$mrsl['XML']['color_track'] = (string)$mrslXML->color_track ? (string)$mrslXML->color_track : 'FFF';

	$mrsl['XML']['mode_on_race'] = (string)$mrslXML->mode_on_race ? (string)$mrslXML->mode_on_race : 'vertical';
	$mrsl['XML']['mode_on_score'] = (string)$mrslXML->mode_on_score ? (string)$mrslXML->mode_on_score : 'horizontal';

	$mrsl['XML']['max_on_race'] = (int)$mrslXML->max_on_race ? (int)$mrslXML->max_on_race : 2;
	$mrsl['XML']['max_on_score'] = (int)$mrslXML->max_on_score ? (int)$mrslXML->max_on_score : 3;

	$mrsl['XML']['posX_on_race'] = (float)$mrslXML->posX_on_race ? (float)$mrslXML->posX_on_race : 0;
	$mrsl['XML']['posY_on_race'] = (float)$mrslXML->posY_on_race ? (float)$mrslXML->posY_on_race : 0;

	$mrsl['XML']['posX_on_score'] = (float)$mrslXML->posX_on_score ? (float)$mrslXML->posX_on_score : 10;
	$mrsl['XML']['posY_on_score'] = (float)$mrslXML->posY_on_score ? (float)$mrslXML->posY_on_score : 10;

	$mrsl['XML']['width_on_race'] = (float)$mrslXML->width_on_race ? (float)$mrslXML->width_on_race : 15;
	$mrsl['XML']['width_on_score'] = (float)$mrslXML->width_on_score ? (float)$mrslXML->width_on_score : 15;

	$mrsl['XML']['height_on_race'] = (float)$mrslXML->height_on_race ? (float)$mrslXML->height_on_race : 3.5;
	$mrsl['XML']['height_on_score'] = (float)$mrslXML->height_on_score ? (float)$mrslXML->height_on_score : 3.5;

	$mrsl['XML']['active_on_race'] = (string)$mrslXML->active_on_race ? (string)$mrslXML->active_on_race : 'true';
	$mrsl['XML']['active_on_score'] = (string)$mrslXML->active_on_score ? (string)$mrslXML->active_on_score : 'true';


//Start in RACE-Mode
	mrsl_make_current_config('RACE');
	
	
	
/***********************************
 * mrsl_make_current_config
 *  Sets the current cfg
 *
 * Last Change: v0.0.1
 ***********************************/
function mrsl_make_current_config($gamestate)
{
	global $mrsl, $wr_config;
	
	if ($gamestate == 'RACE')
	{
		$mrsl['current_config']['active'] = $mrsl['XML']['active_on_race'];
		$mrsl['current_config']['mode'] = $mrsl['XML']['mode_on_race'];
		$mrsl['current_config']['posX'] = $mrsl['XML']['posX_on_race'];
		$mrsl['current_config']['posY'] = $mrsl['XML']['posY_on_race'];
		$mrsl['current_config']['width'] = $mrsl['XML']['width_on_race'];
		$mrsl['current_config']['height'] = $mrsl['XML']['height_on_race'];
		$mrsl['current_config']['max'] = $mrsl['XML']['max_on_race'];
	}
	elseif ($gamestate == 'SCORE')
	{
		$mrsl['lastrotation_offset']=0;
		$mrsl['current_config']['active'] = $mrsl['XML']['active_on_score'];
		$mrsl['current_config']['mode'] = $mrsl['XML']['mode_on_score'];
		$mrsl['current_config']['posX'] = $mrsl['XML']['posX_on_score'];
		$mrsl['current_config']['posY'] = $mrsl['XML']['posY_on_score'];
		$mrsl['current_config']['width'] = $mrsl['XML']['width_on_score'];
		$mrsl['current_config']['height'] = $mrsl['XML']['height_on_score'];
		$mrsl['current_config']['max'] = $mrsl['XML']['max_on_score'];
	}
}
	
/***********************************
 * mrsl_event_Sync
 *  Sets required vars on startup
 *
 * Last Change: v0.0.1
 ***********************************/
function mrsl_event_Sync($aseco)
{
	global $mrsl, $wr_config;
	
	if ($aseco->server->getGame() != 'MP') die('############## Worldrecords-Plugin is for TM2 only! ###################'); 
	$aseco->console('[WR] ##################################');
	$aseco->console('[WR] # Serverlinker v'.$wr_config['version_string']);
	$aseco->console('[WR] # visit www.maniarecords.org');
	$aseco->console('[WR] # by vni');
	$aseco->console('[WR] ##################################');
	
	//Register with Xaseco Version pool
		$aseco->plugin_versions[] = array(
			'plugin'	=> 'plugin.mrserverlink.php',
			'author'	=> 'vni',
			'version'	=> $mrsl['version_string']
		);
}

/***********************************
 * mrsl_event_Connect
 *  Shows a cached panel to new connections
 *
 * Last Change: v0.0.1
 ***********************************/
function mrsl_event_Connect($aseco, $player)
{
	global $mrsl, $wr_config;
	$aseco->client->query('SendDisplayManialinkPageToLogin', $player->login, $mrsl['cachedpanel'], 0, false);
}


/***********************************
 * mrsl_event_StartRound
 *  Race starts here 
 *
 * Last Change: v0.0.1
 ***********************************/
function mrsl_event_StartRound($aseco, $challenge)
{
	global $mrsl, $wr_config;
	
	mrsl_make_current_config('RACE');
	mrsl_update_panel($aseco);
	
}

/***********************************
 * mrsl_event_StartRound
 *  Race ends here 
 *
 * Last Change: v0.0.1
 ***********************************/
function mrsl_event_EndRound($aseco, $challenge)
{
	global $mrsl, $wr_config;
	
	mrsl_make_current_config('SCORE');
	mrsl_update_panel($aseco);
}

/***********************************
 * mrsl_event_onEverySecond
 *  Check for new data
 *
 * Last Change: v0.0.2
 ***********************************/
function mrsl_event_onEverySecond($aseco)
{
	global $mrsl, $wr_config;
	if (isset($wr_config['extended_data']['serverinfo']))
	{
		$mrsl['current_data'] = serialize($wr_config['extended_data']['serverinfo']);
		
		//Fetch new data
		if ($mrsl['current_data'] != $mrsl['lastdata'])
		{
			//New Data arrived
			$mrsl['lastdata'] = $mrsl['current_data'];
			$mrsl['data'] = $wr_config['extended_data']['serverinfo'];
		}
		
		//Do the rotation
		if (($mrsl['XML']['rotation_time']>0) && ($mrsl['lastrotation']+$mrsl['XML']['rotation_time'] < time()))
		{
			$mrsl['lastrotation']=time();
			if ($mrsl['lastrotation_offset']+$mrsl['current_config']['max'] > count($mrsl['data'])-1) $mrsl['lastrotation_offset']=0;
			else $mrsl['lastrotation_offset'] = $mrsl['lastrotation_offset']+$mrsl['current_config']['max'];
		}
		
		mrsl_update_panel($aseco);
	}
	else
	{
		$aseco->console('[MRSERVERLINK] No ManiaRecords-Data found. Check your config!');
	}
}

/***********************************
 * mrsl_update_panel
 *  Displays the panel 
 *
 * Last Change: v0.0.1
 ***********************************/
function mrsl_update_panel($aseco)
{
	global $mrsl, $wr_config;
	if ($mrsl['data'])
	{
		$xml = '<manialink id="'.$mrsl['panel_id']['main'].'">';
		if (strtolower($mrsl['current_config']['active']) == 'true')
		{
			$height = $mrsl['current_config']['height'];
			$width = $mrsl['current_config']['width'];
			$y_val = $mrsl['current_config']['posY'];
			$x_val = $mrsl['current_config']['posX'];
			
			
			$count=0; $offset_counter=$mrsl['lastrotation_offset'];
			for($index = 0; $index < $mrsl['current_config']['max']; $index++)
			{
				if (isset($mrsl['data'][$offset_counter]))
				{
					$xml .= '   <frame posn="'.$x_val.' '.$y_val.' 1">
									<quad posn="0 0 0" sizen="'.$mrsl['current_config']['width'].' '.$mrsl['current_config']['height'].'" style="BgsPlayerCard" substyle="BgCardSystem" url="maniaplanet://#join='.$mrsl['data'][$offset_counter]['login'].'"></quad>
									<label posn="1 -1 1" sizen="'.$mrsl['current_config']['width'].' '.$mrsl['current_config']['height'].'" halign="left" valign="center" textsize="1" scale="0.9" textcolor="FFFF" text="'.$mrsl['data'][$offset_counter]['name'].' $z$'.$mrsl['XML']['color_playercount'].'('.$mrsl['data'][$offset_counter]['players_current'].'/'.$mrsl['data'][$offset_counter]['players_max'].')"></label>
									<label posn="1 -2.4 1" sizen="'.$mrsl['current_config']['width'].' '.$mrsl['current_config']['height'].'" halign="left" valign="center" textsize="1" scale="0.7" textcolor="FFFF" text="$'.$mrsl['XML']['color_track'].'Track: $z'.$mrsl['data'][$offset_counter]['currentchallenge']['name'].'"></label>
								</frame>
							';
					if (strtolower($mrsl['current_config']['mode']) == 'vertical') $y_val -= $height;
					if (strtolower($mrsl['current_config']['mode']) == 'horizontal') $x_val += $width;
				}
				$offset_counter++;
				
				if ($offset_counter >= $mrsl['current_config']['max']) $offset_counter=0;
			}
		}
		$xml .= '</manialink>';
		$aseco->client->addCall('SendDisplayManialinkPage', array($xml, 0, false));
		$mrsl['cachedpanel'] = $xml;
	}
}

?>