<?php
//Class for managing general ranking

include_once('PlayerTotal.php');

class GeneralRanking
{
	/**
	 * @name GetPlayerPoints
	 * @param i_pid int
	 * @return int
	 * @desc Return the points of the player in the general ranking
	 */
	function GetPlayerPoints($i_pid)
	{
		$r_res=mysql_query('SELECT pts FROM general_rank WHERE pid = '.$i_pid);
		if(mysql_num_rows($r_res)==0) return 0;
		else return (mysql_result($r_res,0,0));
	}

	/**
	 * @name GetPlayerGeneralRank
	 * @param i_pid int
	 * @return int
	 * @desc Return the positon in the general ranking
	 */
	function GetPlayerGeneralRank($i_pid)
	{
		$i_points = $this->GetPlayerPoints($i_pid);
		$r_res=mysql_query('SELECT pid FROM general_rank WHERE pts > '.$i_points);
		return (mysql_num_rows($r_res)+1);
	}
	
	/**
	 * @name GetGeneralRanking
	 * @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 GetGeneralRanking($i_start=1,$i_end=NULL)
	{
		$s_sql='';
		$i_pos=0;
		
		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 general_rank ORDER BY pts 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 SetPlayerGeneralRank
	 * @param i_pid int
	 * @param i_pts int
	 * @desc Update the general rank of the player
	 */
	function SetPlayerGeneralRank($i_pid, $i_pts)
	{
		mysql_query('UPDATE general_rank SET pts = '.$i_pts.' WHERE pid = '.$i_pid);
		if(mysql_affected_rows()==0) mysql_query('INSERT INTO general_rank (pid,pts) VALUES ('.$i_pid.','.$i_pts.')');
	}
}
?>