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

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

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

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

	////////////////////////////////////////////////////////////////
	// 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,"Members",H).
				out_table_cell(0,1,1,"<input type=\"text\" name=\"players\" size=\"5\" maxlength=\"5\" value=\"$filter[players]\"/> 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=\"clan\"".($filter['sort']=='clan'?" selected=\"selected\"":"").">Clan</option>".
					"<option value=\"players\"".($filter['sort']=='players'?" selected=\"selected\"":"").">Members</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,"Clans per Page",H).
				out_table_cell(0,1,1,"<input type=\"text\" name=\"clans\" size=\"2\" maxlength=\"2\" value=\"$options[CLANS_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'] =
		"Clans' Ranks depend on medial Points of their Members.<br />\n".
		($options['CLANS_MIN_KILLS'] || $options['CLANS_MIN_PLAYERS'] ? "<br />\nA Clan needs at least ".($options['CLANS_MIN_KILLS'] ? "$options[CLANS_MIN_KILLS] Kill".($options['CLANS_MIN_KILLS']==1?"":"s") : "").($options['CLANS_MIN_PLAYERS'] ? ($options['CLANS_MIN_KILLS'] ? " and " : "")."$options[CLANS_MIN_PLAYERS] Member".($options['CLANS_MIN_PLAYERS']==1?"":"s") : "")." to be ranked. Clans with less Kills can be seen when changing the Filter Options above.<br />\n" : "");


	////////////////////////////////////////////////////////////////
	// OUTPUT LIST
	$stats = array(
		'minpoints'		=> 1000,
		'maxpoints'		=> 1000,
		'maxplayers'	=> 0,
		'maxkills'		=> 0,
		'maxdeaths'		=> 0,
		'maxselfkills'	=> 0,
		'sumkills'		=> 0,
		'sumdeaths'		=> 0,
		'sumselfkills'	=> 0,
		);
	$result = $sql->query("
		SELECT
			`clan`,
			COUNT(*) as `players`,
			ROUND(AVG(`points`)) as `points`,
			SUM(`kills`) as `kills`,
			SUM(`deaths`) as `deaths`,
			SUM(`selfkills`) as `selfkills`
		FROM `$options[SQL_TABPREFIX]players`
		WHERE `kills`>0 OR `deaths`>0
		GROUP BY `clan`
		HAVING `clan`!=''
		".($filter['user'] ? "AND $filter[user]" : "")."
		");
	if($stats['clans'] = $sql->numrows($result)) {
		$clan = $sql->fetcharray($result);
		$stats['minpoints']		= $clan['points'];
		$stats['maxpoints']		= $clan['points'];
		$stats['maxplayers']	= $clan['players'];
		$stats['maxkills']		= $clan['kills'];
		$stats['maxdeaths']		= $clan['deaths'];
		$stats['maxselfkills']	= $clan['selfkills'];
		$stats['sumkills']		= $clan['kills'];
		$stats['sumdeaths']		= $clan['deaths'];
		$stats['sumselfkills']	= $clan['selfkills'];

		while($clan = $sql->fetcharray($result)) {
			$stats['minpoints']		= min($stats['minpoints'],$clan['points']);
			$stats['maxpoints']		= max($stats['maxpoints'],$clan['points']);
			$stats['maxplayers']	= max($stats['maxplayers'],$clan['players']);
			$stats['maxkills']		= max($stats['maxkills'],$clan['kills']);
			$stats['maxdeaths']		= max($stats['maxdeaths'],$clan['deaths']);
			$stats['maxselfkills']	= max($stats['maxselfkills'],$clan['selfkills']);
			$stats['sumkills']		+= $clan['kills'];
			$stats['sumdeaths']		+= $clan['deaths'];
			$stats['sumselfkills']	+= $clan['selfkills'];
		}
	}
	$result = $sql->query("
		SELECT
			`players`.`clan` as `clan`,
			`clans`.`name` as `name`,
			`clans`.`channel` as `channel`,
			`clans`.`homepage` as `homepage`,
			COUNT(*) as `players`,
			ROUND(AVG(`players`.`points`)) as `points`,
			SUM(`players`.`kills`) as `kills`,
			SUM(`players`.`deaths`) as `deaths`,
			SUM(`players`.`kills`)/SUM(`players`.`deaths`) as `ratio`,
			SUM(`players`.`selfkills`) as `selfkills`,
			MAX(`players`.`last`) as `last`
		FROM `$options[SQL_TABPREFIX]players` as `players`
		LEFT JOIN `$options[SQL_TABPREFIX]clans` as `clans` USING(`clan`)
		GROUP BY `clan`
		HAVING `clan`!='' AND (`kills`>0 OR `deaths`>0)
		".($filter['user'] ? "AND $filter[user]" : "")."
		ORDER BY
			`$filter[sort]` ".($filter['sort']=='clan'?"ASC":"DESC").",
			`points` DESC,
			`kills` DESC,
			`deaths` ASC,
			`selfkills` ASC,
			`clan` ASC
		".($options['CLANS_LIMIT']?"LIMIT $filter[offset],$options[CLANS_LIMIT]":"")."
		");
	if($sql->numrows($result)) {
		$out['list'] = '';
		$i = $filter['offset'];
		$last = $rank = $ranked = 0;
		while($clan = $sql->fetcharray($result)) {
			// CALCULATE RANK:
			// IF NOT RANKED
			if($clan['kills']<$options['CLANS_MIN_KILLS'] || $clan['players']<$options['CLANS_MIN_PLAYERS']) {
				$rank = 0;
			// IF RANKED AND LIST SORTED BY POINTS AND EVERY RANKED IS IN LIST
			} elseif($filter['sort']=='points' && $filter['kills']<=$options['CLANS_MIN_KILLS'] && $filter['players']<=$options['CLANS_MIN_PLAYERS'] && !$filter['name']) {
				// IF NO CLAN RANKED BEFORE ON THIS PAGE
				if(!$ranked) {
					$last = $clan['points'];
					// NUMBER OF RANKED CLANS WITH MORE POINTS TO GET RANK OF CURRENT CLAN
					$ranked = $rank = 1+$sql->numrows($sql->query("
						SELECT
							SUM(`kills`) as `kills`,
							ROUND(AVG(`points`)) as `points`,
							COUNT(*) as `players`
						FROM `$options[SQL_TABPREFIX]players`
						WHERE `clan`!='' AND (`kills`>0 OR `deaths`>0)
						GROUP BY `clan`
						HAVING `points`>$clan[points]
						".($filter['rank'] ? "AND $filter[rank]" : "")."
						"));
					// IF FIRST CLAN OF PAGE IS RANKED: NUMBER OF RANKED CLANS WITH SAME POINTS BUT LISTED ON PREVIOUS PAGE
					if($filter['offset'] && $i==$filter['offset']) {
						$ranked += $sql->numrows($sql->query("
							SELECT
								`clan`,
								COUNT(*) as `players`,
								ROUND(AVG(`points`)) as `points`,
								SUM(`kills`) as `kills`,
								SUM(`deaths`) as `deaths`,
								SUM(`selfkills`) as `selfkills`
							FROM `$options[SQL_TABPREFIX]players`
							WHERE `clan`!='' AND (`kills`>0 OR `deaths`>0)
							GROUP BY `clan`
							HAVING `points`=$clan[points]
							AND (`kills`>$clan[kills] OR (`kills`=$clan[kills] AND (`deaths`<$clan[deaths] OR (`deaths`=$clan[deaths] AND (`selfkills`<$clan[selfkills] OR (`selfkills`=$clan[selfkills] AND `clan`<'".$sql->escape($clan['clan'])."'))))))
							".($filter['rank'] ? "AND $filter[rank]" : "")."
							"));
					}
				// IF RANK CAN BE CALCULATED FROM PREVIOUS RANKS
				} else {
					$rank = $ranked ++;
					if($clan['points']<$last) {
						$rank = $ranked;
						$last = $clan['points'];
					}
				}
			// IF RANKED BUT NOT SORTED BY POINTS: ASK DATABASE FOR RANK
			} else {
				$rank = 1+$sql->numrows($sql->query("
					SELECT
						ROUND(AVG(`points`)) as `points`,
						SUM(`kills`) as `kills`,
						COUNT(*) as `players`
					FROM `$options[SQL_TABPREFIX]players`
					WHERE `clan`!='' AND (`kills`>0 OR `deaths`>0)
					GROUP BY `clan`
					HAVING `points`>$clan[points]
					".($filter['rank'] ? "AND $filter[rank]" : "")."
					"));
			}
			$out['list'] .= out_table_row(
				out_table_cell(0,1,1,$rank ? $rank : '---',C).
				out_table_cells_clan($filter['sort']=='clan'?1:0,1,$clan['clan'],$clan['name']).
				out_table_cells_bar($filter['sort']=='points'?1:0,1,$clan['points'],$stats['minpoints'],$stats['maxpoints'],"points").
				out_table_cells_bar($filter['sort']=='players'?1:0,1,$clan['players'],0,$stats['maxplayers']).
				out_table_cells_bar($filter['sort']=='kills'?1:0,1,$clan['kills'],0,$stats['maxkills'],"kills").
				out_table_cells_bar($filter['sort']=='deaths'?1:0,1,$clan['deaths'],0,$stats['maxdeaths'],"deaths").
				out_table_cell($filter['sort']=='ratio'?1:0,1,1,$clan['ratio'] ? $clan['ratio'] : "---",C).
				out_table_cells_bar($filter['sort']=='selfkills'?1:0,1,$clan['selfkills'],0,$stats['maxselfkills'],"selfkills").
				out_table_cell($filter['sort']=='last'?1:0,1,1,$clan['last']=='0000-00-00 00:00:00' ? "---" : $clan['last'],C),
				++$i&1 ? "light" : "dark");
		}
		// GENERATE LINK FOR OTHER PLAYER PAGES
		$p3 = ceil($stats['clans']/$options['CLANS_LIMIT'])-1;
		$p1 = max(min($filter['page']-floor($options['CLANS_PAGES']/2),$p3-$options['CLANS_PAGES']),0);
		$p2 = min($p1+$options['CLANS_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['CLANS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['players']!=$options['CLANS_MIN_PLAYERS']?"&amp;players=$filter[players]":"").($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['CLANS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['players']!=$options['CLANS_MIN_PLAYERS']?"&amp;players=$filter[players]":"").($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['CLANS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['players']!=$options['CLANS_MIN_PLAYERS']?"&amp;players=$filter[players]":"").($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['CLANS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['players']!=$options['CLANS_MIN_PLAYERS']?"&amp;players=$filter[players]":"").($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['CLANS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['players']!=$options['CLANS_MIN_PLAYERS']?"&amp;players=$filter[players]":"").($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[clans] Clan".($stats['clans']==1?"":"s"),L).
				out_table_cell(0,12,1,
					"Pages: ".
					($p1>0 ? "<a href=\"?inc=$include&amp;page=$i".($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['kills']!=$options['CLANS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['players']!=$options['CLANS_MIN_PLAYERS']?"&amp;players=$filter[players]":"").($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['CLANS_MIN_KILLS']?"&amp;kills=$filter[kills]":"").($filter['players']!=$options['CLANS_MIN_PLAYERS']?"&amp;players=$filter[players]":"").($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,15,1,"<span class=\"smaller\">no Clans 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']=='clan'?1:0,2,1,"Clan").
				out_table_cell($filter['sort']=='points'?1:0,2,1,"Points").
				out_table_cell($filter['sort']=='player'?1:0,2,1,"Members").
				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=clans\">Clans</a>";
	$links[] = "<link rel=\"up\" href=\"?inc=index\" />";
	$out =
		"<h1>Clan 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:700px\">\n".
		out_indent($out['list'],3).
		"		</td>\n".
		"	</tr>\n".
		"</table>\n";

?>
Return current item: Zitro-Stats