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

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

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

	////////////////////////////////////////////////////////////////
	// OPTIONS
	if(isset($_COOKIE['ZITROSTATS']['CLANS_LIMIT_WEAPONS']))
		$options['CLANS_LIMIT_WEAPONS'] =
			min(max(1,intval($_COOKIE['ZITROSTATS']['CLANS_LIMIT_WEAPONS'])),$options['CLANS_LIMIT_WEAPONS_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]" : "");

	if($name = @$_GET['name']) {

		////////////////////////////////////////////////////////////////
		// OUTPUT DETAILS
		if(!$clan = $sql->fetcharray($sql->query("
			SELECT
				`clans`.`name` as `name`,
				`clans`.`homepage` as `homepage`,
				`clans`.`channel` as `channel`,
				COUNT(`players`.`player`) as `players`,
				ROUND(AVG(`players`.`points`)) as `points`,
				SUM(`players`.`kills`) as `kills`,
				SUM(`players`.`deaths`) as `deaths`,
				IFNULL(SUM(`players`.`kills`)/SUM(`players`.`deaths`),0) 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`)
			WHERE `players`.`clan`='".$sql->escape($name)."' AND (`players`.`kills`>0 OR `players`.`deaths`>0)
			GROUP BY `players`.`clan`
			LIMIT 1
			"))) {
			$clan = array(
				'name'		=> '',
				'homepage'	=> '',
				'channel'	=> '',
				'players'	=> 0,
				'points'	=> 1000,
				'kills'		=> 0,
				'deaths'	=> 0,
				'ratio'		=> NULL,
				'selfkills'	=> 0,
				'last'		=> '0000-00-00 00:00:00'
				);
		}
		$stats = array(
			'minpoints'		=> 1000,
			'maxpoints'		=> 1000,
			'maxplayers'	=> 0,
			'maxkills'		=> 0,
			'maxdeaths'		=> 0,
			'maxselfkills'	=> 0,
			'sumkills'		=> 0,
			'sumdeaths'		=> 0,
			'sumselfkills'	=> 0,
			'clans'			=> 0
			);
		$result = $sql->query("
			SELECT
				COUNT(`player`) 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`".($filter['rank'] ? " HAVING $filter[rank]" : "")."
			");
		$clan['rank'] = 0;
		if(list($players,$points,$kills,$deaths,$selfkills) = $sql->fetchrow($result)) {
			$stats['minpoints']		= $points;
			$stats['maxpoints']		= $points;
			$stats['maxplayers']	= $players;
			$stats['maxkills']		= $kills;
			$stats['maxdeaths']		= $deaths;
			$stats['maxselfkills']	= $selfkills;
			$stats['sumkills']		= $kills;
			$stats['sumdeaths']		= $deaths;
			$stats['sumselfkills']	= $selfkills;
			$stats['clans'] ++;
			if($clan['kills']>=$options['CLANS_MIN_KILLS'] && $clan['players']>=$options['CLANS_MIN_PLAYERS']) $clan['rank'] = $points>$clan['points'] ? 2 : 1;

			while(list($players,$points,$kills,$deaths,$selfkills) = $sql->fetchrow($result)) {
				$stats['minpoints']		= min($stats['minpoints'],$points);
				$stats['maxpoints']		= max($stats['maxpoints'],$points);
				$stats['maxplayers']	= max($stats['maxplayers'],$players);
				$stats['maxkills']		= max($stats['maxkills'],$kills);
				$stats['maxdeaths']		= max($stats['maxdeaths'],$deaths);
				$stats['maxselfkills']	= max($stats['maxselfkills'],$selfkills);
				$stats['sumkills']		+= $kills;
				$stats['sumdeaths']		+= $deaths;
				$stats['sumselfkills']	+= $selfkills;
				$stats['clans'] ++;
				if($clan['kills']>=$options['CLANS_MIN_KILLS'] && $clan['players']>=$options['CLANS_MIN_PLAYERS'] && $points>$clan['points']) $clan['rank'] ++;
			}
		}

		////////////////////////////////////////////////////////////////
		// OUTPUT DETAILS
		$i = 0;
		$out['details'] =
			out_table(
				out_table_caption("Infobox").
				out_table_row(
					out_table_cell(0,1,$clan['name']?2:1,"Clan",H).
					out_table_cell(0,1,1,"<img src=\"images/clan.gif\" alt=\"Clan: ".htmlentities($name)."\" />",R).
					out_table_cell(0,1,1,"<span class=\"larger\">".str_replace(" ","&nbsp;",htmlentities($name))."</span>",L),
					++$i&1 ? "light" : "dark").
					($clan['name'] ? out_table_row(
						out_table_cell(0,2,1,out_clanname($clan['name']),C),
						$i&1 ? "light" : "dark"):
						"").
				out_table_row(
					out_table_cell(0,1,1,"Rank",H).
					out_table_cell(0,2,1,$clan['rank'] ?
						"<span class=\"larger\">$clan[rank]</span> / <span class=\"smaller\">$stats[clans]</span>" :
						"---<br />".
						"<span class=\"smaller\">".($options['CLANS_MIN_KILLS']>$clan['kills'] ? ($options['CLANS_MIN_KILLS']-$clan['kills'])." more Kill".($options['CLANS_MIN_KILLS']-$clan['kills']==1?"":"s").($options['CLANS_MIN_PLAYERS']>$clan['players'] ? " and<br />" : ""):"").($options['CLANS_MIN_PLAYERS']>$clan['players'] ? ($options['CLANS_MIN_PLAYERS']-$clan['players'])." more Player".($options['CLANS_MIN_PLAYERS']-$clan['players']==1?"":"s"):"")." needed</span>",
						C),
					++$i&1 ? "light" : "dark").
				out_table_row(
					out_table_cell(0,1,$clan['rank']>1?2:1,"Points",H).
					out_table_cells_bar(0,1,$clan['points'],0,$stats['maxpoints'],"points"),
					++$i&1 ? "light" : "dark").
				($clan['rank']>1 ? out_table_row(
					out_table_cell(0,2,1,($stats['maxpoints']-$clan['points'])." below max",C),
					$i&1 ? "light" : "dark"):
					"").
				out_table_row(
					out_table_cell(0,1,1,"Members",H).
					out_table_cells_bar(0,1,$clan['players'],0,$stats['maxplayers']),
					++$i&1 ? "light" : "dark").
				out_table_row(
					out_table_cell(0,1,$stats['maxkills']?2:1,"Kills",H).
					out_table_cells_bar(0,1,$clan['kills'],0,$stats['maxkills'],"kills"),
					++$i&1 ? "light" : "dark").
				($stats['maxkills'] ? out_table_row(
					out_table_cell(0,2,1,str_replace(".","<span class=\"smaller\">.",sprintf("%.3f",$clan['kills']/$stats['maxkills']*100))."</span> % of max<br />".str_replace(".","<span class=\"smaller\">.",sprintf("%.3f",$clan['kills']/$stats['sumkills']*100))."</span> % of all",C),
					$i&1 ? "light" : "dark"):
					"").
				out_table_row(
					out_table_cell(0,1,$stats['maxdeaths']?2:1,"Deaths",H).
					out_table_cells_bar(0,1,$clan['deaths'],0,$stats['maxdeaths'],"deaths"),
					++$i&1 ? "light" : "dark").
				($stats['maxdeaths'] ? out_table_row(
					out_table_cell(0,2,1,str_replace(".","<span class=\"smaller\">.",sprintf("%.3f",$clan['deaths']/$stats['maxdeaths']*100))."</span> % of max<br />".str_replace(".","<span class=\"smaller\">.",sprintf("%.3f",$clan['deaths']/$stats['sumdeaths']*100))."</span> % of all",C),
					$i&1 ? "light" : "dark"):
					"").
				out_table_row(
					out_table_cell(0,1,1,"K/D Ratio",H).
					out_table_cell(0,2,1,$clan['ratio'] ? $clan['ratio'] : "---",C),
					++$i&1 ? "light" : "dark").
				out_table_row(
					out_table_cell(0,1,$stats['maxselfkills']?2:1,"Selfkills",H).
					out_table_cells_bar(0,1,$clan['selfkills'],0,$stats['maxselfkills'],"selfkills"),
					++$i&1 ? "light" : "dark").
				($stats['maxselfkills'] ? out_table_row(
					out_table_cell(0,2,1,str_replace(".","<span class=\"smaller\">.",sprintf("%.3f",$clan['selfkills']/$stats['maxselfkills']*100))."</span> % of max<br />".str_replace(".","<span class=\"smaller\">.",sprintf("%.3f",$clan['selfkills']/$stats['sumselfkills']*100))."</span> % of all",C),
					$i&1 ? "light" : "dark"):
					"").
				out_table_row(
					out_table_cell(0,1,1,"Last Action",H).
					out_table_cell(0,2,1,$clan['last']=='0000-00-00 00:00:00' ? "---" : "<span class=\"smaller\">".str_replace(" ","</span><br />",$clan['last']),C),
					++$i&1 ? "light" : "dark")
				).
			($clan['homepage'] || $clan['channel'] ? "<span class=\"smaller\">".
				($clan['homepage'] ? "<a href=\"http://".htmlentities($clan['homepage'])."\">".htmlentities($clan['homepage'])."</a><br />\n" : "").
				($clan['channel'] ? "<a href=\"irc://irc.quakenet.org/".htmlentities($clan['channel'])."\">#".htmlentities($clan['channel'])."</a><br />\n" : "").
				"</span><br />\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,"Clan's Weapons",H).
					out_table_cell(0,1,1,"<input type=\"text\" name=\"weapons\" size=\"2\" maxlength=\"2\" value=\"$options[CLANS_LIMIT_WEAPONS]\"/>",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 MEMBERS
		$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`
			FROM `$options[SQL_TABPREFIX]players`
			WHERE `clan`='".$sql->escape($name)."' AND (`kills`>0 OR `deaths`>0)
			"));
		$result = $sql->query("
			SELECT
				`player`,
				`points`,
				`kills`,
				`deaths`,
				`kills`/`deaths` as `ratio`,
				`selfkills`,
				`last`
			FROM `$options[SQL_TABPREFIX]players`
			WHERE `clan`='".$sql->escape($name)."' AND (`kills`>0 OR `deaths`>0)
			ORDER BY
				`points` DESC,
				`kills` DESC,
				`deaths` ASC,
				`selfkills` ASC,
				`player` ASC
			");
		if($sql->numrows($result)) {
			$out['members'] = '';
			$i = $max = $last = 0;
			$rank = 1;
			while($player = $sql->fetcharray($result)) {
				if($i==0) $max = $last = $player['points'];
				if($player['points']<$last) {
					$rank = $i+1;
					$last = $player['points'];
				}
				$out['members'] .= out_table_row(
					out_table_cell(0,1,1,$rank ? $rank : '---',C).
					out_table_cells_player(0,1,$player['player']).
					out_table_cells_bar(1,1,$player['points'],$stats['minpoints'],$stats['maxpoints'],"points").
					out_table_cells_bar(0,1,$player['kills'],0,$stats['maxkills'],"kills").
					out_table_cells_bar(0,1,$player['deaths'],0,$stats['maxdeaths'],"deaths").
					out_table_cell(0,1,1,$player['ratio'] ? $player['ratio'] : "---",C).
					out_table_cells_bar(0,1,$player['selfkills'],0,$stats['maxselfkills'],"selfkills").
					out_table_cell(0,1,1,out_history(array('player'=>$player['player']),$player['last']=='0000-00-00 00:00:00' ? "---" : $player['last']),C),
					++$i&1 ? "light" : "dark");
			}
			$out['members'] = out_table_body($out['members']);
		} else {
			$out['members'] = out_table_row(
				out_table_cell(0,13,1,"<span class=\"smaller\">no Players listed here</span>",C),
				"light");
		}
		$out['members'] = out_table(
			out_table_caption("Members").
			out_table_head(
				out_table_row(
					out_table_cell(0,1,1,"Rank").
					out_table_cell(0,2,1,"Player").
					out_table_cell(1,2,1,"Points").
					out_table_cell(0,2,1,"Kills").
					out_table_cell(0,2,1,"Deaths").
					out_table_cell(0,1,1,"K/D").
					out_table_cell(0,2,1,"Selfkills").
					out_table_cell(0,1,1,"Last Action")
					)
				).
				$out['members']
			);

		////////////////////////////////////////////////////////////////
		// OUTPUT KILLWEAPONS
		$result = $sql->query("
			SELECT
				`playerkillweapons`.`weapon` as `weapon`,
				SUM(`playerkillweapons`.`kills`) as `kills`,
				MAX(`playerkillweapons`.`last`) as `last`
			FROM `$options[SQL_TABPREFIX]players` as `players`
			LEFT JOIN `$options[SQL_TABPREFIX]playerkillweapons` as `playerkillweapons` USING(`player`)
			WHERE `players`.`clan`='".$sql->escape($name)."' AND (`players`.`kills`>0 OR `players`.`deaths`>0)
			GROUP BY `weapon` HAVING `kills`
			ORDER BY `kills` DESC
			");
		if($sql->numrows($result)) {
			$out['killweapons'] = '';
			$i = $max = $last = 0;
			$rank = 1;
			$rest = $clan['kills'];
			while((!$options['CLANS_LIMIT_WEAPONS'] || $i<$options['CLANS_LIMIT_WEAPONS']) && ($weapon = $sql->fetcharray($result))) {
				if($i==0) $max = $last = $weapon['kills'];
				if($weapon['kills']<$last) {
					$rank = $i+1;
					$last = $weapon['kills'];
				}
				$rest -= $weapon['kills'];
				$out['killweapons'] .= out_table_row(
					out_table_cell(0,1,1,$rank,C).
					out_table_cells_weapon(0,1,$weapon['weapon']).
					out_table_cells_bar(1,1,$weapon['kills'],0,$max,"kills").
					out_table_cell(0,1,1,out_history(array('type'=>'kills','player'=>$name,'weapon'=>$weapon['weapon']),$weapon['last']=='0000-00-00 00:00:00' ? "---" : "<span class=\"smaller\">$weapon[last]</span>"),C),
					++$i&1 ? "light" : "dark");
			}
			if(($i=$sql->numrows($result)-$i)>0) {
				$out['killweapons'] .= out_table_row(
					out_table_cell(0,6,1,"<span class=\"smaller\">and $rest Kill".($rest==1?"":"s")." with $i other Weapon".($i==1?"":"s")."</span>",C),
					$options['CLANS_LIMIT_WEAPONS']&1 ? "dark" : "light");
			}
		} else {
			$out['killweapons'] = out_table_row(
				out_table_cell(0,6,1,"<span class=\"smaller\">no Weapons listed here</span>",C),
				"light");
		}
		$out['killweapons'] = out_table(
			out_table_caption("Kill Weapons").
			out_table_head(
				out_table_row(
					out_table_cell(0,1,1,"#").
					out_table_cell(0,2,1,"Weapon").
					out_table_cell(1,2,1,"Kills").
					out_table_cell(0,1,1,"Last Kill")
					)
				).
				out_table_body($out['killweapons'])
			);

		////////////////////////////////////////////////////////////////
		// OUTPUT DEATHWEAPONS
		$result = $sql->query("
			SELECT
				`playerdeathweapons`.`weapon` as `weapon`,
				SUM(`playerdeathweapons`.`deaths`) as `deaths`,
				MAX(`playerdeathweapons`.`last`) as `last`
			FROM `$options[SQL_TABPREFIX]players` as `players`
			LEFT JOIN `$options[SQL_TABPREFIX]playerdeathweapons` as `playerdeathweapons` USING(`player`)
			WHERE `players`.`clan`='".$sql->escape($name)."' AND (`players`.`kills`>0 OR `players`.`deaths`>0)
			GROUP BY `weapon` HAVING `deaths`
			ORDER BY `deaths` DESC
			");
		if($sql->numrows($result)) {
			$out['deathweapons'] = '';
			$i = $max = $last = 0;
			$rank = 1;
			$rest = $clan['deaths'];
			while((!$options['CLANS_LIMIT_WEAPONS'] || $i<$options['CLANS_LIMIT_WEAPONS']) && ($weapon = $sql->fetcharray($result))) {
				if($i==0) $max = $last = $weapon['deaths'];
				if($weapon['deaths']<$last) {
					$rank = $i+1;
					$last = $weapon['deaths'];
				}
				$rest -= $weapon['deaths'];
				$out['deathweapons'] .= out_table_row(
					out_table_cell(0,1,1,$rank,C).
					out_table_cells_weapon(0,1,$weapon['weapon']).
					out_table_cells_bar(1,1,$weapon['deaths'],0,$max,"deaths").
					out_table_cell(0,1,1,out_history(array('type'=>'kills','opponent'=>$name,'weapon'=>$weapon['weapon']),$weapon['last']=='0000-00-00 00:00:00' ? "---" : "<span class=\"smaller\">$weapon[last]</span>"),C),
					++$i&1 ? "light" : "dark");
			}
			if(($i=$sql->numrows($result)-$i)>0) {
				$out['deathweapons'] .= out_table_row(
					out_table_cell(0,6,1,"<span class=\"smaller\">and $rest Death".($rest==1?"":"s")." from $i other Weapon".($i==1?"":"s")."</span>",C),
					$options['CLANS_LIMIT_WEAPONS']&1 ? "dark" : "light");
			}
		} else {
			$out['deathweapons'] = out_table_row(
				out_table_cell(0,6,1,"<span class=\"smaller\">no Weapons listed here</span>",C),
				"light");
		}
		$out['deathweapons'] = out_table(
			out_table_caption("Death Weapons").
			out_table_head(
				out_table_row(
					out_table_cell(0,1,1,"#").
					out_table_cell(0,2,1,"Weapon").
					out_table_cell(1,2,1,"Deaths").
					out_table_cell(0,1,1,"Last Death")
					)
				).
				out_table_body($out['deathweapons'])
			);

		////////////////////////////////////////////////////////////////
		// OUTPUT
		$navi[] = "<a href=\"?inc=clans\">Clans</a>";
		$navi[] = "<a href=\"?inc=clan&amp;name=".urlencode($name)."\">".htmlentities($name)."</a>";
		$links[] = "<link rel=\"up\" href=\"?inc=clans\" />";
		$out =
			"<h1>Clan Profile: ".str_replace(" ","&nbsp;",htmlentities($name))."</h1>\n".
			"<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n".
			"	<tr>\n".
			"		<td align=\"center\" valign=\"top\" style=\"width:190px\">\n".
			out_indent($out['details'],3).
			"			<br />\n".
			"			<br />\n".
			"			<br />\n".
			out_indent($out['display'],3).
			"		</td>\n".
			"		<td class=\"line\">\n".
			"		</td>\n".
			"		<td align=\"center\" valign=\"top\">\n".
			"			<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n".
			"				<tr>\n".
			"					<td align=\"center\" valign=\"top\" colspan=\"3\">\n".
			out_indent($out['members'],6).
			"					</td>\n".
			"				</tr>\n".
			"				<tr>\n".
			"					<td align=\"center\" valign=\"top\" style=\"width:340px\">\n".
			out_indent($out['killweapons'],6).
			"					</td>\n".
			"					<td class=\"space\">\n".
			"					</td>\n".
			"					<td align=\"center\" valign=\"top\" style=\"width:340px\">\n".
			out_indent($out['deathweapons'],6).
			"					</td>\n".
			"				</tr>\n".
			"			</table>\n".
			"		</td>\n".
			"	</tr>\n".
			"</table>\n";

	} else {
		$out =
			"Error:<br />\n".
			"No Clan Name given!\n";
	}

?>
Return current item: Zitro-Stats