Location: PHPKode > projects > Zitro-Stats > include/out_players.inc.php
<?php

	// ANTIHACK
	defined('INSTATS') AND defined('OUT') OR die();

	////////////////////////////////////////////////////////////////
	// COOKIES
	if(@$_POST['mode']=='display') {
		setcookie("ZITROSTATS[PLAYERS_LIMIT]",@$_POST['players'],time()+intval($options['COOKIES_EXPIRE_TIME']));
		header("Location: $_SERVER[REQUEST_URI]");
		die();
	}

	////////////////////////////////////////////////////////////////
	// OPTIONS
	if(isset($_COOKIE['ZITROSTATS']['PLAYERS_LIMIT']))
		$options['PLAYERS_LIMIT'] =
			min(max(1,intval($_COOKIE['ZITROSTATS']['PLAYERS_LIMIT'])),$options['PLAYERS_LIMIT_MAX']);
	$filter['rank']		= $options['PLAYERS_MIN_KILLS'] ? "`kills`>=$options[PLAYERS_MIN_KILLS]" : "";
	$filter['page']		= intval(@$_GET['page']);
	$filter['offset']	= $filter['page'] * $options['PLAYERS_LIMIT'];
	$filter['name']		= @$_GET['name'];
	$filter['kills']	= @$_GET['kills']=='' ? $options['PLAYERS_MIN_KILLS'] : intval(@$_GET['kills']);
	$filter['sort']		= @$_GET['sort'] && in_array($_GET['sort'],array('player','kills','deaths','ratio','selfkills','last')) ? $_GET['sort'] : 'points';
	$filter['user'] =
		($filter['name'] ? "LCASE(`player`) LIKE '%".str_replace(array('%','_'),array('\%','\_'),strtolower($sql->escape($filter['name'])))."%'" : "").
		($filter['kills'] ? ($filter['name'] ? " AND " : "")."`kills`>=$filter[kills]" : "");

	////////////////////////////////////////////////////////////////
	// OUTPUT FILTER
	$i = 0;
	$out['filter'] =
		"<form method=\"get\" action=\"\">\n".
		out_table(
			out_table_caption("Filter Options").
			out_table_row(
				out_table_cell(0,1,1,"Name",H).
				out_table_cell(0,1,1,"<input type=\"text\" name=\"name\" size=\"15\" maxlength=\"24\" value=\"".htmlentities($filter['name'])."\"/>",L),
				++$i&1 ? "light" : "dark").
			out_table_row(
				out_table_cell(0,1,1,"Kills",H).
				out_table_cell(0,1,1,"<input type=\"text\" name=\"kills\" size=\"5\" maxlength=\"5\" value=\"$filter[kills]\"/> or more",L),
				++$i&1 ? "light" : "dark").
			out_table_row(
				out_table_cell(0,1,1,"Order",H).
				out_table_cell(0,1,1,"<select name=\"sort\" size=\"1\">".
					"<option value=\"player\"".($filter['sort']=='player'?" selected=\"selected\"":"").">Player</option>".
					"<option value=\"points\"".($filter['sort']=='points'?" selected=\"selected\"":"").">Points</option>".
					"<option value=\"kills\"".($filter['sort']=='kills'?" selected=\"selected\"":"").">Kills</option>".
					"<option value=\"deaths\"".($filter['sort']=='deaths'?" selected=\"selected\"":"").">Deaths</option>".
					"<option value=\"ratio\"".($filter['sort']=='ratio'?" selected=\"selected\"":"").">K/D Ratio</option>".
					"<option value=\"selfkills\"".($filter['sort']=='selfkills'?" selected=\"selected\"":"").">Selfkills</option>".
					"<option value=\"last\"".($filter['sort']=='last'?" selected=\"selected\"":"").">Last Action</option>".
					"</select>",L),
				++$i&1 ? "light" : "dark")
			).
		"<div>\n".
		"	<input type=\"hidden\" name=\"inc\" value=\"$include\"/>\n".
		"	<input class=\"button\" type=\"submit\" value=\"Filter\"/>\n".
		"</div>\n".
		"</form>\n";

	////////////////////////////////////////////////////////////////
	// OUTPUT DISPLAY
	$i = 0;
	$out['display'] =
		"<form method=\"post\" action=\"".htmlentities($_SERVER['REQUEST_URI'])."\">\n".
		out_table(
			out_table_caption("Display Options").
			out_table_row(
				out_table_cell(0,1,1,"Players per Page",H).
				out_table_cell(0,1,1,"<input type=\"text\" name=\"players\" size=\"2\" maxlength=\"2\" value=\"$options[PLAYERS_LIMIT]\"/>",L),
				++$i&1 ? "light" : "dark")
			).
		"<div>\n".
		"	<input type=\"hidden\" name=\"mode\" value=\"display\" /><input class=\"button\" type=\"submit\" value=\"Save\"/>\n".
		"</div>\n".
		"</form>\n";

	////////////////////////////////////////////////////////////////
	// OUTPUT INFO
	$out['info'] =
		"Players' Ranks depend on their Points. Read more about Points in <a href=\"?inc=history\">History</a>.<br />\n".
		($options['PLAYERS_MIN_KILLS'] ? "<br />\nA Player needs at least $options[PLAYERS_MIN_KILLS] Kill".($options['PLAYERS_MIN_KILLS']==1?"":"s")." to be ranked. Players with less Kills can be seen when changing the Filter Options above.<br />\n" : "");

	////////////////////////////////////////////////////////////////
	// OUTPUT LIST
	$stats = $sql->fetcharray($sql->query("
		SELECT
			MIN(`points`) as `minpoints`,
			MAX(`points`) as `maxpoints`,
			MAX(`kills`) as `maxkills`,
			MAX(`deaths`) as `maxdeaths`,
			MAX(`selfkills`) as `maxselfkills`,
			COUNT(*) as `players`
		FROM `$options[SQL_TABPREFIX]players`
		".($filter['user'] ? "WHERE $filter[user]" : "")."
		"));
	$result = $sql->query("
		SELECT
			`player`,
			`clan`,
			`points`,
			`kills`,
			`deaths`,
			`kills`/`deaths` as `ratio`,
			`selfkills`,
			`last`
		FROM `$options[SQL_TABPREFIX]players`
		".($filter['user'] ? "WHERE $filter[user]" : "")."
		ORDER BY
			`$filter[sort]` ".($filter['sort']=='player'?"ASC":"DESC").",
			`points` DESC,
			`kills` DESC,
			`deaths` ASC,
			`selfkills` ASC,
			`player` ASC
		".($options['PLAYERS_LIMIT']?"LIMIT $filter[offset],$options[PLAYERS_LIMIT]":"")."
		");
	if($sql->numrows($result)) {
		$out['list'] = '';
		$i = $filter['offset'];
		$last = $rank = $ranked = 0;
		while($player = $sql->fetcharray($result)) {
			// CALCULATE RANK:
			// IF NOT RANKED
			if($player['kills']<$options['PLAYERS_MIN_KILLS']) {
				$rank = 0;
			// IF RANKED AND LIST SORTED BY POINTS AND EVERY RANKED IS IN LIST
			} elseif($filter['sort']=='points' && $filter['kills']<=$options['PLAYERS_MIN_KILLS'] && !$filter['name']) {
				// IF NO PLAYER RANKED BEFORE ON THIS PAGE
				if(!$ranked) {
					$last = $player['points'];
					// NUMBER OF RANKED PLAYERS WITH MORE POINTS TO GET RANK OF CURRENT PLAYER
					$ranked = $rank = pop($sql->fetchrow($sql->query("
						SELECT COUNT(*)+1
						FROM `$options[SQL_TABPREFIX]players`
						WHERE `points`>$player[points]
						".($filter['rank'] ? "AND $filter[rank]" : "")."
						")));
					// IF FIRST PLAYER OF PAGE IS RANKED: NUMBER OF RANKED PLAYERS WITH SAME POINTS BUT LISTED ON PREVIOUS PAGE
					if($filter['offset'] && $i==$filter['offset'])
						$ranked += pop($sql->fetchrow($sql->query("
							SELECT COUNT(*)
							FROM `$options[SQL_TABPREFIX]players`
							WHERE `points`=$player[points]
							AND (`kills`>$player[kills] OR (`kills`=$player[kills] AND (`deaths`<$player[deaths] OR (`deaths`=$player[deaths] AND (`selfkills`<$player[selfkills] OR (`selfkills`=$player[selfkills] AND `player`<'".$sql->escape($player['player'])."'))))))
							".($filter['rank'] ? "AND $filter[rank]" : "")."
							")));
				// IF RANK CAN BE CALCULATED FROM PREVIOUS RANKS
				} else {
					$rank = $ranked ++;
					if($player['points']<$last) {
						$rank = $ranked;
						$last = $player['points'];
					}
				}
			// IF RANKED BUT NOT SORTED BY POINTS: ASK DATABASE FOR RANK
			} else {
				$rank = pop($sql->fetchrow($sql->query("
					SELECT COUNT(*)+1
					FROM `$options[SQL_TABPREFIX]players`
					WHERE `points`>$player[points]
					".($filter['rank'] ? "AND $filter[rank]" : "")."
					")));
			}
			$out['list'] .= out_table_row(
				out_table_cell(0,1,1,$rank ? $rank : '---',C).
				out_table_cells_player($filter['sort']=='player'?1:0,1,$player['player']).
				out_table_cells_clan($filter['sort']=='clan'?1:0,1,$player['clan']).
				out_table_cells_bar($filter['sort']=='points'?1:0,1,$player['points'],$stats['minpoints'],$stats['maxpoints'],"points").
				out_table_cells_bar($filter['sort']=='kills'?1:0,1,$player['kills'],0,$stats['maxkills'],"kills").
				out_table_cells_bar($filter['sort']=='deaths'?1:0,1,$player['deaths'],0,$stats['maxdeaths'],"deaths").
				out_table_cell($filter['sort']=='ratio'?1:0,1,1,$player['ratio'] ? $player['ratio'] : "---",C).
				out_table_cells_bar($filter['sort']=='selfkills'?1:0,1,$player['selfkills'],0,$stats['maxselfkills'],"selfkills").
				out_table_cell($filter['sort']=='last'?1:0,1,1,out_history(array('player'=>$player['player']),$player['last']=='0000-00-00 00:00:00' ? "---" : $player['last']),C),
				++$i&1 ? "light" : "dark");
		}
		// GENERATE LINK FOR OTHER PLAYER PAGES
		$p3 = ceil($stats['players']/$options['PLAYERS_LIMIT'])-1;
		$p1 = max(min($filter['page']-floor($options['PLAYERS_PAGES']/2),$p3-$options['PLAYERS_PAGES']),0);
		$p2 = min($p1+$options['PLAYERS_PAGES'],$p3);
		if($filter['page']>0) {
			$links[] = "<link rel=\"first\" href=\"?inc=$include&amp;page=0".($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['kills']!=$options['PLAYERS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['sort']!='points'?"&amp;sort=$filter[sort]":"")."\" />";
			$links[] = "<link rel=\"previous\" href=\"?inc=$include&amp;page=".($filter['page']-1).($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['kills']!=$options['PLAYERS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['sort']!='points'?"&amp;sort=$filter[sort]":"")."\" />";
		}
		if($filter['page']<$p3) {
			$links[] = "<link rel=\"last\" href=\"?inc=$include&amp;page=$p3".($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['kills']!=$options['PLAYERS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['sort']!='points'?"&amp;sort=$filter[sort]":"")."\" />";
			$links[] = "<link rel=\"next\" href=\"?inc=$include&amp;page=".($filter['page']+1).($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['kills']!=$options['PLAYERS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['sort']!='points'?"&amp;sort=$filter[sort]":"")."\" />";
		}
		$pagelinks = array();
		for($i=$p1; $i<=$p2; $i++) {
			$pagelinks[] = ($i==$filter['page'] ? "<span class=\"larger\">" : "<a href=\"?inc=$include&amp;page=$i".($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['kills']!=$options['PLAYERS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['sort']!='points'?"&amp;sort=$filter[sort]":"")."\">").
				" ".($i+1)." ".
				($i==$filter['page'] ? "</span>" : "</a>");
		}
		$out['list'] = out_table_foot(
			out_table_row(
				out_table_cell(0,3,1,$sql->numrows($result)." of $stats[players] Player".($stats['players']==1?"":"s"),L).
				out_table_cell(0,14,1,
					"Pages: ".
					($p1>0 ? "<a href=\"?inc=$include&amp;page=0".($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['kills']!=$options['PLAYERS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['sort']!='points'?"&amp;sort=$filter[sort]":"")."\"> 1 </a>".($p1>1?"...":",") : "").
					implode(",",$pagelinks).
					($p2<$p3 ? ($p2<$p3-1?"...":",")."<a href=\"?inc=$include&amp;page=$p3".($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['kills']!=$options['PLAYERS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['sort']!='points'?"&amp;sort=$filter[sort]":"")."\"> ".($p3+1)." </a>" : ""),
					R)
				)
			).
			out_table_body($out['list']);
	} else {
		$out['list'] = out_table_body(
			out_table_row(
				out_table_cell(0,17,1,"<span class=\"smaller\">no Players listed here</span>",C),
				"light")
			);
	}
	$out['list'] = out_table(
		out_table_head(
			out_table_row(
				out_table_cell(0,1,1,"Rank").
				out_table_cell($filter['sort']=='player'?1:0,2,1,"Player").
				out_table_cell($filter['sort']=='clan'?1:0,2,1,"Clan").
				out_table_cell($filter['sort']=='points'?1:0,2,1,"Points").
				out_table_cell($filter['sort']=='kills'?1:0,2,1,"Kills").
				out_table_cell($filter['sort']=='deaths'?1:0,2,1,"Deaths").
				out_table_cell($filter['sort']=='ratio'?1:0,1,1,"K/D").
				out_table_cell($filter['sort']=='selfkills'?1:0,2,1,"Selfkills").
				out_table_cell($filter['sort']=='last'?1:0,1,1,"Last Action")
				)
			).
			$out['list']
		);

	////////////////////////////////////////////////////////////////
	// OUTPUT
	$navi[] = "<a href=\"?inc=players\">Players</a>";
	$links[] = "<link rel=\"up\" href=\"?inc=index\" />";
	$out =
		"<h1>Player Rankings</h1>\n".
		"<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n".
		"	<tr>\n".
		"		<td align=\"center\" valign=\"top\" style=\"width:170px\">\n".
		out_indent($out['filter'],3).
		"			<br />\n".
		"			<br />\n".
		"			<br />\n".
		out_indent($out['display'],3).
		"			<br />\n".
		"			<br />\n".
		"			<br />\n".
		out_indent($out['info'],3).
		"		</td>\n".
		"		<td class=\"line\">\n".
		"		</td>\n".
		"		<td align=\"center\" valign=\"top\" style=\"width:750px\">\n".
		out_indent($out['list'],3).
		"		</td>\n".
		"	</tr>\n".
		"</table>\n";

?>
Return current item: Zitro-Stats