<?php
// vous n etes pas sense modifier ce fichier sauf si vous avez besoin d' y ajouter des fonctions
// allez dans tm_gery_config.php pour configurer ce programme

require 'includes/tm_gery_config.php';		// charge tm_gery_config (necessaire pour configurer tm_gery)

$RankServer = array ('Login' =>  0);
$RankMap = array ('filename' =>  0);
$RankMap2 = array ('filename' =>  0);
$RankMappeur = array ('Login' =>  0);
$RankMappeur2 = array ('Login' =>  0);
$RecordLogin = array ('Login' =>  0);
$Moyenne= 0;

function server_ranking ($ChallengeList) {
global $RankServer;
global $reprecord;
global $maxtop;
global $RecordLogin;


	echo "Calcul rang serveur: ";

	$points=0;
	$RankServer = array();
	$RecordLogin = array();
	foreach ($ChallengeList as $Challenge) {
		$filename=$Challenge;
		if(LireFichier($reprecord.$filename,$result)) {
			$tmp=strstr($result,'<Login>');
			$tmp=str_replace('<Login>','',$tmp);
			$stop=strpos($tmp,'</');
			if ($stop > 0) $Login = substr($tmp,0,$stop); else $Login = ' ';
			if ($Login != ' '){
				//if (array_key_exists($Login, $RankServer)) {
				if (isset($RankServer[$Login] )) {
					$RankServer[$Login] += $maxtop;
				}else{   
					$RankServer["$Login"] = $maxtop;
				}
				//if (array_key_exists($Login, $RankServer)) {
				if (isset($RecordLogin[$Login] )) {
					$RecordLogin[$Login] += 1;
				}else{
					$RecordLogin["$Login"] = 1;
				}
			}
			//echo "$Login\n\r";
			$nb=preg_match_all("|<Top(\d+)Login>(.*)</Top\\1Login>|", $result, $matches);
			// indice de depart = 0 pour le second
			//for ($i=2;$i<=$maxtop;$i++) {
			$nbpoints=$maxtop-1;
			for ($i=0; ($i<=$maxtop -2)&&($i<count($matches[2])); $i++, $nbpoints--) {
//			for ($i=0; $i<=$maxtop -2; $i++, $nbpoints--) {
				//echo "nbpoints: $nbpoints\n\r";
				//$tmp=strstr($result,'<Top'.$i.'Login>');
				//$tmp=str_replace('<Top'.$i.'Login>','',$tmp);
				//$stop=strpos($tmp,'</');
				//if ($stop > 0) $Login = substr($tmp,0,$stop); else $Login = ' ';
				$Login = $matches[2][$i];
				if ($Login != ' '){
					//if (array_key_exists($Login, $RankServer)) {
					//    $points = $RankServer[$Login];
					//    //$RankServer[$Login] = $points + ($maxtop + 1 - $i - 2) ;
					//    $RankServer[$Login] = $points + $nbpoints;
					//}else{   
					//    $RankServer["$Login"] = 0;
					//    $points = $RankServer[$Login];
					//    //$RankServer[$Login] = $points + ($maxtop + 1 - $i - 2);
					//    $RankServer[$Login] = $points + $nbpoints;
					//}
					//if ( !array_key_exists($Login, $RankServer)) {
					//:    $RankServer[$Login] = 0;
					//}
					if( !isset($RankServer[$Login]) ) {
						$RankServer["$Login"] = 0;
					}
					$RankServer[$Login] += $nbpoints;
				}
				//usleep(1);
			} 
			//echo 'nb: '.$nb."\n\r";
			//print_r($matches);
			//echo "classement:\n\r";
			//print_r($RankServer);
		}
		usleep(1);
	}
   
	arsort($RankServer);

	SaveSRank();
	echo "OK \n\r";
}

function SaveSRank() {
global $RankServer;
global $RecordLogin;

	$file = 'srank.xml';

	$buffer  = '<?xml version="1.0" encoding="utf-8" ?>';
	$buffer .= "\n";
	$buffer .= '<srank>';
	$buffer .= "\n";

	$i=0;
	foreach ($RankServer as $key => $Pts) {
		$buffer .= '<player>';
		$buffer .= "\n";
		$buffer .= '<login>';
		$buffer .= $key;
		$buffer .= '</login>';
		$buffer .= "\n";

		$buffer .= '<pts>';
		$buffer .= $Pts;
		$buffer .= '</pts>';
		$buffer .= "\n";

		$buffer .= '<nbrec>';
		if (isset($RecordLogin[$key])) {
//		if (array_key_exists($key, $RecordLogin)) {
			$buffer .= $RecordLogin[$key];
		}
		else {
			$buffer .= '0';
		}
		$buffer .= '</nbrec>';
		$buffer .= "\n";

		$buffer .= '</player>';
		$buffer .= "\n";

		usleep(1);
	}

	$buffer .= '</srank>';

	$nbcar = file_put_contents($file, $buffer);
}

function map_ranking ($ChallengeList) {
global $RankMap;
global $RankMap2;
global $reprecord;
global $Moyenne;

	echo "Calcul rang map : ";

	$RankMap = array();
	$Moyenne = 0;
	$NbMap = 0;
	foreach ($ChallengeList as $Challenge) {
		$filename=$Challenge;
		if(LireFichier($reprecord.$filename,$result)) {
			$tmp=strstr($result,'<Note>');
			$tmp=str_replace('<Note>','',$tmp);
			$stop=strpos($tmp,'</');
			if ($stop > 0) $Note1 = substr($tmp,0,$stop); else $Note1 = '-1';
			$tmp=strstr($result,'<NbVote>');
			$tmp=str_replace('<NbVote>','',$tmp);
			$stop=strpos($tmp,'</');
			if ($stop > 0) $Vote1 = substr($tmp,0,$stop); else $Vote1 = '0';
			if ($Note1 != '-1'){
				$Moyenne = $Moyenne + $Note1;
				$NbMap++;
			}
			if ($Note1 != '-1' && $Vote1 >=5){  
					$RankMap[$filename] = $Note1;
					$RankMap2[$filename] = $Vote1;
			}
		}
		usleep(1);
	}
	
	if ($NbMap) $Moyenne = $Moyenne / $NbMap;
	
	arsort($RankMap);

	SaveMapRank();
	
	echo "OK \n\r";
}

function SaveMapRank() {
global $RankMap;
global $RankMap2;
global $Moyenne;

	$file = 'rankmap.xml';

	$buffer  = '<?xml version="1.0" encoding="utf-8" ?>';
	$buffer .= "\n";
	$buffer .= '<rankmap>';
	$buffer .= "\n";

	$buffer .= '<moyenne>';
	$buffer .= $Moyenne;
	$buffer .= '</moyenne>';
	$buffer .= "\n";

	foreach ($RankMap as $key => $Note) {
		$buffer .= '<map>';
		$buffer .= "\n";
		$buffer .= '<uid>';
		$buffer .= $key;
		$buffer .= '</uid>';
		$buffer .= "\n";

		$buffer .= '<note>';
		$buffer .= $Note;
		$buffer .= '</note>';
		$buffer .= "\n";

		$buffer .= '<nbvote>';
		if (isset($RankMap2[$key])) {
//		if (array_key_exists($key, $RankMap2)) {
			$buffer .= $RankMap2[$key];
		}
		else {
			$buffer .= 0;
		}
		$buffer .= '</nbvote>';
		$buffer .= "\n";

		$buffer .= '</map>';
		$buffer .= "\n";

		usleep(1);
	}

	$buffer .= '</rankmap>';

	$nbcar = file_put_contents($file, $buffer);
}

function mappeur_ranking ($ChallengeList) {
global $RankMappeur;
global $RankMappeur2;
global $reprecord;

	echo "Calcul rang mappeur : ";

	$moyen = 0;
	$RankMappeur = array();
	$RankMappeur2 = array();
	foreach ($ChallengeList as $Challenge) {
		$filename=$Challenge;
		if(LireFichier($reprecord.$filename,$result)) {
			$tmp=strstr($result,'<Note>');
			$tmp=str_replace('<Note>','',$tmp);
			$stop=strpos($tmp,'</');
			if ($stop > 0) $Note1 = substr($tmp,0,$stop); else $Note1 = '-1';
			$tmp=strstr($result,'<Author>');
			$tmp=str_replace('<Author>','',$tmp);
			$stop=strpos($tmp,'</');
			if ($stop > 0) $Author = substr($tmp,0,$stop); else $Author = ' ';
			if ($Note1 != '-1'){
				if (isset($RankMappeur[$Author])) {
//				if (array_key_exists($Author, $RankMappeur)) {
					$NbOccu = $RankMappeur2[$Author];
					$moyen = $RankMappeur[$Author];
					$RankMappeur[$Author] = ($moyen*$NbOccu + intval($Note1))/($NbOccu+1);
					$RankMappeur2[$Author]++;
				}else{
					$RankMappeur2["$Author"] = 1;
					$RankMappeur["$Author"] = 0;
					$points = $RankMappeur[$Author];
					$RankMappeur[$Author] = $Note1;
				}
			}
		}
		usleep(1);
	}
	
	arsort($RankMappeur);

	SaveMRank();
	
	echo "OK \n\r";
}

function SaveMRank() {
global $RankMappeur;
global $RankMappeur2;
global $Moyenne;

	$file = 'mrank.xml';

	$buffer  = '<?xml version="1.0" encoding="utf-8" ?>';
	$buffer .= "\n";
	$buffer .= '<mrank>';
	$buffer .= "\n";

	foreach ($RankMappeur as $key => $Note) {
		$buffer .= '<mappeur>';
		$buffer .= "\n";

		$buffer .= '<login>';
		$buffer .= $key;
		$buffer .= '</login>';
		$buffer .= "\n";

		$buffer .= '<note>';
		$buffer .= $Note;
		$buffer .= '</note>';
		$buffer .= "\n";

		$buffer .= '<nbmap>';
		if (isset($RankMappeur2[$key])) {
//		if (array_key_exists($key, $RankMappeur2)) {
			$buffer .= $RankMappeur2[$key];
		}
		else {
			$buffer .= 0;
		}
		$buffer .= '</nbmap>';
		$buffer .= "\n";

		$buffer .= '</mappeur>';
		$buffer .= "\n";

		usleep(1);
	}

	$buffer .= '</mrank>';

	$nbcar = file_put_contents($file, $buffer);
}

function LireFichier($file,&$result){
	$buffer = '';
	if (!file_exists($file)) return False;
	$buffer=file_get_contents($file);
	if( $buffer ){
		$result=$buffer;
		return True; 
	}else{
		return False;
	}
}

function console($text){
	echo "[".date("H:i:s")."]". $text ."\n\r";
	flush();
}

function Error($errstr, $errcode){

	if($errcode != -1000){
		echo("Error: $errstr [$errcode]\n\r");
	}
}

function ReadMapListe(&$ChallengeList) {
global $local_maplist;

	$i = 0;
	$ChallengeList = array();
	
	if ( is_file( $local_maplist ) ) { 
		if ( LireFichier( $local_maplist, $buffer ) ) {
			$a = preg_match_all('#<ident>(.*)</ident>#', $buffer, $matches);
			foreach( $matches[1] as $uid ) {
				$i++;
				$ChallengeList[$i] = $uid;
			}
		}
	}
	return count($ChallengeList);
}

//--------------------------------------------------------------
//--------------------------------------------------------------
while (true) {
	set_time_limit (120);
	$NbChallenge=ReadMapListe($ChallengeList);
	echo "NbChallenge : $NbChallenge\n\r";
	if ($NbChallenge>0) {
		$t1=microtime(true);
		server_ranking($ChallengeList);
		echo "time : ".(microtime(true)-$t1)."\n\r";
		$t1=microtime(true);
		map_ranking($ChallengeList);
		echo "time : ".(microtime(true)-$t1)."\n\r";
		$t1=microtime(true);
		mappeur_ranking($ChallengeList);
		echo "time : ".(microtime(true)-$t1)."\n\r";
	}
	set_time_limit (120);
	sleep(60);
}
?>
