<?php
/*======================================================================*\
|| #################################################################### ||
|| # EoT-Clan BC2 Server Stats                                        # ||
|| # ---------------------------------------------------------------- # ||
|| # Dieses Copyright darf nicht entfernt werden                      # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # -----------------BC2 Server Stats is a free Software-------------# ||
|| #                      http://eot-clan.net                         # ||
|| #################################################################### ||
\*======================================================================*/
require_once ('../inc/config.php');
include_once ('../lang/language.php');
include_once ('../lang/'.$lang_file.'');

$_GET = security::escape($_GET);
$_POST = security::escape($_POST); 

if(isset($_GET['cPage']) && !empty($_GET['cPage']) && $_GET['cPage'] != ''){
   $seite = $_GET['cPage'];
   }else{
   	$seite = 1;
  }
  
 $entrys = 30; 
 $start = $seite * $entrys - $entrys; 

$result = db_query("SELECT
                      a.ClanTag,
                      SUM(b.playerScore)
                    FROM tbl_playerdata a
                     INNER JOIN tbl_playerstats b ON b.StatsID = a.PlayerID
                    WHERE 
                     a.ClanTag != ''
                    AND 
                     b.playerScore >= 100
                    GROUP BY 
                     a.ClanTag");                  
$num_rows = mysql_num_rows($result);
$Sites = ceil($num_rows / $entrys);


?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">
	      <head>
					<title><?php echo lang_clanstats_title; ?></title>
	         <link rel="stylesheet" type="text/css" href="../css/fancybox.css" media="screen">
	        <link rel="stylesheet" type="text/css" href="../css/style.css" /> 
	      </head>
	       
		<div class="body_ministats">
	<table class="default_m" cellspacing="0" cellpadding="0" style="margin-top:12px;">
     <td class="head" colspan="10" cellspacing="0"><a href="<?php echo $installdir; ?>/index.php" target="_self"><span style="float:left;margin-left:5px;"><?php echo lang_index_startseite; ?></a></span><?php echo lang_clanstats_title; ?></td>
			<tr> 
				 <th style="text-align:left;"><?php echo lang_clanstats_place; ?></th>
				  <th style="text-align:center;"><?php echo lang_clanstats_players; ?></th>
			    <th style="text-align:center;"><?php echo lang_clanstats_clan; ?></th>
			     <th style="text-align:center;"><?php echo lang_clanstats_time; ?></th>
			      <th style="text-align:center;"><?php echo lang_clanstats_round; ?></th>
			       <th style="text-align:center;"><?php echo lang_clanstats_kills; ?></th>
				    <th style="text-align:center;"><?php echo lang_clanstats_deaths; ?></th> 
				   <th style="text-align:center;"><?php echo lang_clanstats_kd; ?></th>
				  <th style="text-align:center;"><?php echo lang_clanstats_headshots; ?></th>
				  <th style="text-align:center;"><?php echo lang_clanstats_points; ?></th>
			</tr>
			<?php
		 $query = "SELECT 
		             d.ClanTag,
		             SUM(s.playerScore) AS playerScore,
		             SUM(s.playerKills) AS playerKills,
		             SUM(s.playerDeaths) AS playerDeaths,
		             SUM(s.playerHeadshots) AS playerHeadshots,
		             SUM(s.playerRounds) AS playerRounds,
		             SUM(s.playerPlaytime) AS playerPlaytime
		           FROM tbl_playerstats s
                 INNER JOIN tbl_playerdata d ON d.PlayerID = s.StatsID
               WHERE 
                 d.ClanTag != '' 
               AND 
                 s.playerScore >= 100
               GROUP BY 
                 d.ClanTag 
               ORDER BY 
                 playerScore DESC LIMIT {$start}, {$entrys}";

	$result = db_query($query);
	$num_rows = mysql_num_rows($result);
 
 if(!isset($seite)){
   $place = 1;
 }
elseif ($seite == 1){
 $place = 1;
}else{
 	$place = $seite*$entrys-($entrys-1);
}

 	while ($row = mysql_fetch_assoc($result)){
		$pClantag = mysql_escape_string($row['ClanTag']);
		$pSql = "
		 SELECT a.PlayerID, a.ClanTag, count(a.PlayerID) AS pPlayers FROM tbl_playerdata a
        WHERE a.ClanTag = '{$pClantag}'
        GROUP BY a.ClanTag";
        $pResult = db_query($pSql);
        $players = mysql_fetch_assoc($pResult);
        
        $time = sprintf("%02dh",$row['playerPlaytime']/60/60);
        
        if ($row['playerDeaths'] == 0){
		        $KD = '0';
	        }else{ 
	       	  $KD = ($row['playerKills']/$row['playerDeaths']);   	
	      }
	      
	       if ($KD > 1){
             $KD = "<span class='positiv'>" . number_format($KD, 2, '.', '.') . "</span>";	
            }else{
             $KD = "<span class='negativ'>" . number_format($KD, 2, '.', '.') . "</span>";	
         }
         
   
?>	
	<tr class="hov">
		<td class="result" style="text-align:left;">&nbsp;<?php echo $place++; ?></td>
		 <td class="result" style="text-align:center;"><?php echo  number_format($players['pPlayers'], 0, ',', ' '); ?></td>
		  <td class="result" style="text-align:center;"><?php echo htmlspecialchars ($row['ClanTag']); ?></td>
		   <td class="result" style="text-align:center;"><?php echo $time; ?></td>
		    <td class="result" style="text-align:center;"><?php echo  number_format($row['playerRounds'], 0, ',', ' '); ?></td>
		     <td class="result" style="text-align:center;"><?php echo  number_format($row['playerKills'], 0, ',', ' '); ?></td>
		    <td class="result" style="text-align:center;"><?php echo  number_format($row['playerDeaths'], 0, ',', ' '); ?></td>
		   <td class="result" style="text-align:center;"><?php echo $KD; ?></td>
		  <td class="result" style="text-align:center;"><?php echo  number_format($row['playerHeadshots'], 0, ',', ' '); ?></td>
		 <td class="result" style="text-align:center;"><?php echo  number_format($row['playerScore'], 0, ',', ' '); ?></td>
	</tr>	
<?php		
	}
	
	function blaetterfunktion_c($seite,$maxseite,$url="",$anzahl=6,$get_name="cPage") {
   global $installdir;
   $anhang = "?";
   if($anzahl%2 != 0) $anzahl++;

   $a = $seite-($anzahl/2);
   $b = 0;
   $blaetter = array();
   while($b <= $anzahl) {
      if($a > 0 AND $a <= $maxseite) {
         $blaetter[] = $a;
         $b++;
         }
      else if($a > $maxseite AND ($a-$anzahl-2)>=0) {
         $blaetter = array();
         $a -= ($anzahl+2);
         $b = 0;
       }
      else if($a > $maxseite AND ($a-$anzahl-2)<0) {
         break;
         }

      $a++;
      }
   $return = "";
   if(!in_array(1,$blaetter) AND count($blaetter) > 1) {
      if(!in_array(2,$blaetter)) $return .= "&nbsp;<a id=clanstats href=\"{$installdir}/stats/clanstats.php?{$get_name}=1\">1</a>&nbsp;...";
      else $return .= "&nbsp;<a id=clanstats href=\"{$installdir}/stats/clanstats.php?{$get_name}=1\">1</a>&nbsp;";
      }

   foreach($blaetter AS $blatt) {
      if($blatt == $seite) $return .= "&nbsp;<b>$blatt</b>&nbsp;";
      else $return .= "&nbsp;<a id=clanstats href=\"{$installdir}/stats/clanstats.php?{$get_name}=$blatt\">$blatt</a>&nbsp;";
      }

   if(!in_array($maxseite,$blaetter) AND count($blaetter) > 1) {
      if(!in_array(($maxseite-1),$blaetter)) $return .= "...&nbsp;<a id=clanstats href=\"{$installdir}/stats/clanstats.php?{$get_name}=$maxseite\">".server_gerneral_server_last."($maxseite)</a>&nbsp;";
      else $return .= "&nbsp;<a id=clanstats href=\"{$installdir}/stats/clanstats.php?{$get_name}=$maxseite\">$maxseite</a>&nbsp;";
      }

   if(empty($return))
      return  "&nbsp;<b>1</b>&nbsp;";
   else
      return $return;
   }  
   
			echo "</tbody><tr>
        <td class=result colspan=14>
      <b>".server_gerneral_serverb_page.":<font color=orange></b>";
echo blaetterfunktion_c($seite,  $Sites)."</td></font>";

echo "</table>";

 include_once ("../inc/footerm.php");
 ?>
 <script type="text/javascript">  	
		     $(document).ready(function() {
				 $("a#clanstats").fancybox({
			    'autoDimensions'	: true,
			    'transitionIn'		: 'none',
			    'transitionOut'		: 'none',
			    'padding' : 0
			  });
      });
	   </script> 
	  <br /> 
</div>	