<?php
//Class for managing ranking

include_once('PlayerTotal.php');

class Ranking
{
	/**
	 * @name GetPlayerRank
	 * @param i_pid int
	 * @return int
	 * @desc Return the positon in the current ranking
	 */
	function GetPlayerRank($i_pid)
	{
		$o_player = new PlayerTotal();
		$o_player->SetPlayerId($i_pid);
		$i_seed=$o_player->GetPlayerSeed();
		$r_res=mysql_query('SELECT pid FROM player_totals WHERE seed > '.$i_seed);
		return (mysql_num_rows($r_res)+1);
	}
	
	/**
	 * @name GetRanking
	 * @param i_start int[optional]
	 * @param i_end int[optional]
	 * @return int
	 * @desc Return the pid of player between position i_start and i_end
	 */
	function GetRanking($i_start=1,$i_end=NULL)
	{
		$s_sql='';
		$i_pos=0;
		$a_return=array();
		
		if($i_end!==NULL) $s_sql=' LIMIT 0,'.$i_end;
		if($i_end!==NULL && $i_end<$i_start) return false;
		
		$r_res=mysql_query('SELECT pid FROM player_totals ORDER BY seed DESC'.$s_sql);
		while($a_infos=mysql_fetch_assoc($r_res))
		{
			$i_pos++;
			if($i_pos<$i_start) continue;
			$a_return[$i_pos]=$a_infos['pid'];
		}
		return $a_return;
	}
	
	/**
	 * @name ResetRanking
	 * @param i_pid int[optionnal]
	 * @return int
	 * @desc Delete the current ranking or only for a Player
	 */
	function ResetRanking($i_pid=NULL)
	{
		if($i_pid==NULL) mysql_query('DELETE FROM player_totals');
		else mysql_query('DELETE FROM player_totals WHERE pid='.$_ipid);
	}
}
?>