<?
	//####################################################################################################################
	// BLOCK 2
	//####################################################################################################################
	$query4=mysql_query("SELECT player_nick, player_ip, online FROM ".$config['players_table']." WHERE 1=1 ORDER BY online DESC LIMIT 5") or die(mysql_error());
	$data = '';
	
	if(mysql_num_rows($query4) == 0) {
		$menu2 = '';
	} else {
		while($result4=mysql_fetch_assoc($query4)) {
			$player = $result4[player_nick];
			if(strlen($player) > 20) {
				$player = substr($player, 0, 20).'...';
			}
			$res = GeoIP_record_by_addr($gi,$result4['player_ip']); 
			if(!$res->country_code)
				$res->country_code = "err";
			$flag = $res->country_code.".gif";
	
			$data2 .= '
				<tr>
					<td width="80%"><img style="vertical-align:middle;" src="limewizard/_images/flags/'.$flag.'" alt="" /> '.$player.'</td><td width="20%" align="right">'.compacttime($result4[online]).'</td>
				</tr>
			';
		}
		$menu2 = '
			<div class="block">
				<h5 class="blocksubhead smaller">
					<img alt="" src="limewizard/_templates/'.$template.'/images/widget-poll.png" title="Statistic"/>
					Best Online
				</h5>
				<div id="c_blog_comments" class="blockbody">
					<ul id="latestcomments" class="blockrow">
						<li class="smallfont">
									<table width="100%" border="0">
										'.$data2.'
									</table>
						</li>
					</ul>
				</div>
			</div>
			<div class="underblock"></div>
		';
	}
	//####################################################################################################################
	// BLOCK 3
	//####################################################################################################################
	if($config['servers_kz'] != "") {
		$srv_num = substr_count($config['servers_kz'], ",") + 1;
		if($srv_num >= 2) {
			$srv_custom = explode(",",$config['servers_kz']);
			$sqlcond = "";
			foreach($srv_custom as $var) {
				$sqlcond .= " AND ".$config['servers_table'].".id != ".$var;
			}
		}
	}
	$query5 = mysql_query("SELECT MAX(streak_kills), MAX(streak_deaths), SUM(kills), SUM(headshotkills), SUM(deaths), SUM(ffkills), SUM(ffdeaths), SUM(ct_team), SUM(t_team), SUM(connections), last_ip, SUM(".$config['stats_players'].".online) AS online, ".$config['players_table'].".player_nick AS nick, MAX(".$config['stats_players'].".last_time) AS lasttime, SUM(wins) FROM ".$config['stats_players'].", ".$config['players_table'].", ".$config['servers_table']." WHERE ".$config['stats_players'].".dbid = ".$config['players_table'].".id".$sqlcond." GROUP BY nick") or die(mysql_error());
	$total_players=mysql_num_rows($query5);
	if($total_players == 0) {
		$menu3 = '';
	} else {
		while($result5 = mysql_fetch_array($query5)) {
			$auth_name=htmlspecialchars($result5['nick'],ENT_QUOTES);
		
			if($result5['SUM(kills)'] <= 100) { 
				$skill = '?';
				$strike = '?';
				$hs = '?';
				$kd = '?';
			} else {
				$now = date(U);
				$nAct = ($now - $result5['lasttime'])/3600;
				if ($nAct < 24) { $activity = 100; }
				elseif ($nAct > 744) { $activity = 0; }
				else { $activity = 2400/$nAct; }
				$skill=((($result5['SUM(kills)']+$result5['SUM(ffkills)'])*60/$result5['online']) + (2*($result5['SUM(kills)']+$result5['SUM(ffkills)'])/($result5['SUM(deaths)']+$result5['SUM(ffdeaths)'])) + ($result5['MAX(streak_kills)']/$result5['MAX(streak_deaths)']) + (5*$result5['SUM(headshotkills)']/($result5['SUM(kills)']+$result5['SUM(ffkills)'])) + (2*$result5['SUM(wins)']/($result5['SUM(ct_team)'] + $result5['SUM(t_team)'])))*$activity;							
				$skill=round($skill);
				$strike = round($result5['MAX(streak_kills)']);
				$hs = round(100*$result5['SUM(headshotkills)']/($result5['SUM(kills)']+$result5['SUM(ffkills)']),2);
				$kd = round(($result5['SUM(kills)']+$result5['SUM(ffkills)'])/($result5['SUM(deaths)']+$result5['SUM(ffdeaths)']),2);
			}
		
			$res = GeoIP_record_by_addr($gi,$result5['last_ip']); 
			if(!$res->country_code)
				$res->country_code = "err";
			$flag = $res->country_code.".gif";
	
			$array[$result5['nick']] = array($flag,$skill,$strike,$hs,$kd);
		}
		$num = 1;
		uasort($array, "sort_players");
		$top_players = array_insert($array, $num);
	
		$num = 2;
		uasort($array, "sort_players");
		$top_streak = array_insert($array, $num);
	
		$num = 3;
		uasort($array, "sort_players");
		$top_hs = array_insert($array, $num);
	
		$num = 4;
		uasort($array, "sort_players");
		$top_kd = array_insert($array, $num);
	
		$menu3 = '
			<div class="block">
				<h5 class="blocksubhead smaller">
					<img alt="" src="limewizard/_templates/'.$template.'/images/widget-poll.png" title="Statistic"/>
					Best Skill
				</h5>
				<div id="c_blog_comments" class="blockbody">
					<ul id="latestcomments" class="blockrow">
						<li class="smallfont">
							<table width="100%" border="0">
								'.$top_players.'
							</table>
						</li>
					</ul>
				</div>
			</div>
			<div class="underblock"></div>
		';
		$menu4 = '
			<div class="block">
				<h5 class="blocksubhead smaller">
					<img alt="" src="limewizard/_templates/'.$template.'/images/widget-poll.png" title="Statistic"/>
					Best Strike
				</h5>
				<div id="c_blog_comments" class="blockbody">
					<ul id="latestcomments" class="blockrow">
						<li class="smallfont">
							<table width="100%" border="0">
								'.$top_streak.'
							</table>
						</li>
					</ul>
				</div>
			</div>
			<div class="underblock"></div>
		';
		$menu5 = '
			<div class="block">
				<h5 class="blocksubhead smaller">
					<img alt="" src="limewizard/_templates/'.$template.'/images/widget-poll.png" title="Statistic"/>
					Best HS%
				</h5>
				<div id="c_blog_comments" class="blockbody">
					<ul id="latestcomments" class="blockrow">
						<li class="smallfont">
							<table width="100%" border="0">
								'.$top_hs.'
							</table>
						</li>
					</ul>
				</div>
			</div>
			<div class="underblock"></div>
		';
		$menu6 = '
			<div class="block">
				<h5 class="blocksubhead smaller">
					<img alt="" src="limewizard/_templates/'.$template.'/images/widget-poll.png" title="Statistic"/>
					Best K/D
				</h5>
				<div id="c_blog_comments" class="blockbody">
					<ul id="latestcomments" class="blockrow">
						<li class="smallfont">
							<table width="100%" border="0">
								'.$top_kd.'
							</table>
						</li>
					</ul>
				</div>
			</div>
			<div class="underblock"></div>
		';
	}
	$menu_all = '
			<div id="blog_sidebar_generic">
				'.$menu3.$menu4.$menu5.$menu6.$menu2.'	
			</div>
	';
	include("stats.php");		
	echo $center_section;	
?>