<?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                         # ||
|| #################################################################### ||
\*======================================================================*/
if(isset($_GET['page']) && !empty($_GET['page']) && $_GET['page'] != ''){
   $seite = $_GET['page'];
   }else{
   	$seite = 1;
  }
  	    
  
 $entrys = $PlayerLimit; 
 $start = $seite * $entrys - $entrys; 
 

  
  if(isset($_GET['search']) && !empty($_GET['search']) && $_GET['search'] != '') {
     $search = '&search='.urlencode($_GET['search']);
     $SQLsearch = urlencode($_GET['search']);
          if (strlen($_GET['search']) >= 3 && strlen($_GET['search']) <= 20){
   	        $SQLsearch = "a.SoldierName Like '%{$SQLsearch}%'";
            $Limit = "{$start}, {$entrys}";
            $AND = ""; 
          }else{
	          $SQLsearch = "1";
            $Limit = "{$start}, {$entrys}";
            $AND = "AND TO_DAYS(c.LastSeenOnServer)+{$pDaysOnline} > TO_DAYS(NOW()) AND c.playerScore > {$pScore}";
         }
  }else{
   	$search = '';
   	$SQLsearch = "1";
    $Limit = "{$start}, {$entrys}";
    $AND = "AND TO_DAYS(c.LastSeenOnServer)+{$pDaysOnline} > TO_DAYS(NOW()) AND c.playerScore > {$pScore}";
  }

$result = db_query("SELECT 
                     c.LastSeenOnServer, 
                     c.playerScore
                    FROM 
                     tbl_playerdata a
                    Left JOIN 
                     tbl_playerstats c ON a.PlayerID = c.StatsID
                    WHERE
                      {$SQLsearch} {$AND}");
                     
$num_rows = mysql_num_rows($result);
$Sites = ceil($num_rows / $entrys);


if(!isset($_GET['order']) or empty($_GET['order']) or $_GET['order'] == '' or !isset($_GET['sortby']) or empty($_GET['sortby']) or $_GET['sortby'] == '') {
     $order = "desc";
     $sortby = "playerScore";
   }else{
     $order = $_GET['order'];
     $sortby = $_GET['sortby'];
  }

   if ($order == "desc"){
       $Sorder = "asc";
     }elseif ($order == "asc"){
       $Sorder = "desc";
     }else{
       $Sorder = "desc";
   }



 if ($order == "asc" && $sortby == 'playerKills'){
       $orderimg_k = '<img src="img/desc.png" border="0" title="'.lang_stats_sort_desc.'" />';
     }elseif ($order == "desc" && $sortby == 'playerKills'){
       $orderimg_k = '<img src="img/asc.png" border="0" title="'.lang_stats_sort_asc.'" />';
     }else{
       $orderimg_k = '<img src="img/small.png" border="0" title="'.lang_stats_sort_default.'" />';
   }
   
 if ($order == "asc" && $sortby == 'SoldierName'){
       $orderimg_n = '<img src="img/desc.png" border="0" title="'.lang_stats_sort_desc.'" />';
     }elseif ($order == "asc" && $sortby == 'SoldierName'){
       $orderimg_n = '<img src="img/desc.png" border="0" title="'.lang_stats_sort_asc.'" />';
     }else{
       $orderimg_n = '<img src="img/small.png" border="0" title="'.lang_stats_sort_default.'" />';
   }
   
 if ($order == "asc" && $sortby == 'playerDeaths'){
       $orderimg_d = '<img src="img/desc.png" border="0" title="'.lang_stats_sort_desc.'" />';
     }elseif ($order == "desc" && $sortby == 'playerDeaths'){
       $orderimg_d = '<img src="img/asc.png" border="0" title="'.lang_stats_sort_asc.'" />';
     }else{
       $orderimg_d = '<img src="img/small.png" border="0" title="'.lang_stats_sort_default.'" />';
   }
   
    if ($order == "asc" && $sortby == 'playerHeadshots'){
       $orderimg_hs = '<img src="img/desc.png" border="0" title="'.lang_stats_sort_desc.'" />';
     }elseif ($order == "desc" && $sortby == 'playerHeadshots'){
       $orderimg_hs = '<img src="img/asc.png" border="0" title="'.lang_stats_sort_asc.'" />';
     }else{
       $orderimg_hs = '<img src="img/small.png" border="0" title="'.lang_stats_sort_default.'" />';
   }
   
    if ($order == "asc" && $sortby == 'KD'){
       $orderimg_kd = '<img src="img/desc.png" border="0" title="'.lang_stats_sort_desc.'" />';
     }elseif ($order == "desc" && $sortby == 'KD'){
       $orderimg_kd = '<img src="img/asc.png" border="0" title="'.lang_stats_sort_asc.'" />';
     }else{
       $orderimg_kd = '<img src="img/small.png" border="0" title="'.lang_stats_sort_default.'" />';
   }
   
    if ($order == "asc" && $sortby == 'playerPlaytime'){
       $orderimg_t = '<img src="img/desc.png" border="0" title="'.lang_stats_sort_desc.'" />';
     }elseif ($order == "desc" && $sortby == 'playerPlaytime'){
       $orderimg_t = '<img src="img/asc.png" border="0" title="'.lang_stats_sort_asc.'" />';
     }else{
       $orderimg_t = '<img src="img/small.png" border="0" title="'.lang_stats_sort_default.'" />';
   }
   
    if ($order == "asc" && $sortby == 'playerRounds'){
       $orderimg_r = '<img src="img/desc.png" border="0" title="'.lang_stats_sort_desc.'" />';
     }elseif ($order == "desc" && $sortby == 'playerRounds'){
       $orderimg_r = '<img src="img/asc.png" border="0" title="'.lang_stats_sort_asc.'" />';
     }else{
       $orderimg_r = '<img src="img/small.png" border="0" title="'.lang_stats_sort_default.'" />';
   }
   
    if ($order == "asc" && $sortby == 'playerScore'){
       $orderimg_p = '<img src="img/desc.png" border="0" title="'.lang_stats_sort_desc.'" />';
     }elseif ($order == "desc" && $sortby == 'playerScore'){
       $orderimg_p = '<img src="img/asc.png" border="0" title="'.lang_stats_sort_asc.'" />';
     }else{
       $orderimg_p = '<img src="img/small.png" border="0" title="'.lang_stats_sort_default.'" />';
   }
   
   if ($order == "asc" && $sortby == 'Killstreak'){
       $orderimg_ks = '<img src="img/desc.png" border="0" title="'.lang_stats_sort_desc.'" />';
     }elseif ($order == "desc" && $sortby == 'Killstreak'){
       $orderimg_ks = '<img src="img/asc.png" border="0" title="'.lang_stats_sort_asc.'" />';
     }else{
       $orderimg_ks = '<img src="img/small.png" border="0" title="'.lang_stats_sort_default.'" />';
   }
   
   if ($order == "asc" && $sortby == 'Deathstreak'){
       $orderimg_ds = '<img src="img/desc.png" border="0" title="'.lang_stats_sort_desc.'" />';
     }elseif ($order == "desc" && $sortby == 'Deathstreak'){
       $orderimg_ds = '<img src="img/asc.png" border="0" title="'.lang_stats_sort_asc.'" />';
     }else{
       $orderimg_ds = '<img src="img/small.png" border="0" title="'.lang_stats_sort_default.'" />';
   }
   
   


?>

<script type="text/javascript">
function check_length()
{
   var eingabe = document.getElementById('searchbox').value.length;
   
   if (eingabe <=2 )
   {
      alert ("<?php echo lang_stats_search_length ?>");
      return false;
   }
}
</script>


<table class="default_h" cellspacing="0" cellpadding="0">
     <thead>
      <td class="head" colspan="14"><form action="statistics.php" method="GET" onSubmit ='return check_length();'>
        <input style="float:left;margin-left:10px;height:14px;font-size:11px;background-color:#1A1A1A;color:#ffffff;border:1px solid #303030;" type="text" id="searchbox" value="<?php echo lang_stats_search_default; ?>" name="search" id="search" maxlength="20" onblur="if(this.value=='')this.value='<?php echo lang_stats_search_default; ?>';" onfocus="if(this.value=='<?php echo lang_stats_search_default; ?>')this.value='';" />
        	<input style="float:left;margin-left:5px;height:18px;font-size:11px;background-color:#1A1A1A;color:#ffffff;border:1px solid #303030;" type="submit" value="<?php echo lang_stats_search_button; ?>"/>
		</form>
		<span style="margin-right:10px;font-size:10px;"><?php echo lang_stats_players_at.$num_rows.lang_stats_logged; ?></span>
    	</td>
   <tr> 
   	<td><?php echo lang_stats_place; ?></td> 
   	<td></td>
   	<th width="20px"></th>
	    <td width="170px"><a href="statistics.php?sortby=SoldierName&order=<?php echo $Sorder; ?>"><b><?php echo $orderimg_n.lang_stats_name; ?></b></a></td> 
         <td width="80px"><a href="statistics.php?sortby=playerKills&order=<?php echo $Sorder; ?>"><b><?php echo $orderimg_k.lang_kills; ?></b></a></td> 
           <td width="80px"><a href="statistics.php?sortby=playerDeaths&order=<?php echo $Sorder; ?>"><b><?php echo $orderimg_d.lang_deaths; ?></b></a></td> 
            <td width="90px"><a href="statistics.php?sortby=playerHeadshots&order=<?php echo $Sorder; ?>"><b><?php echo $orderimg_hs.lang_stats_head; ?></b></a></td>
             <td width="60px"><a href="statistics.php?sortby=KD&order=<?php echo $Sorder; ?>"><b><?php echo $orderimg_kd.lang_stats_kd; ?></b></a></td> 
         <td width="125px"><a href="statistics.php?sortby=playerPlaytime&order=<?php echo $Sorder; ?>"><b><?php echo $orderimg_t.lang_stats_time; ?></b></a></td> 
       <td width="80px"><a href="statistics.php?sortby=playerRounds&order=<?php echo $Sorder; ?>"><b><?php echo $orderimg_r.lang_stats_rounds; ?></b></a></td> 
      <td width="100px"><a href="statistics.php?sortby=playerScore&order=<?php echo $Sorder; ?>"><b><?php echo $orderimg_p.lang_stats_points; ?></b></a></td> 
      <td width="60px"><a href="statistics.php?sortby=Killstreak&order=<?php echo $Sorder; ?>"><b><?php echo $orderimg_ks.lang_stats_kstreak; ?></b></a></td>
      <td width="60px"><a href="statistics.php?sortby=Deathstreak&order=<?php echo $Sorder; ?>"><b><?php echo $orderimg_ds.lang_stats_dstreak; ?></b></a></td>
    </tr>
  </thead>
 <tbody>
 	 <?php
  	

    
   	
 $sSql = "SELECT 
           a.PlayerID,
           a.ClanTag,
           a.SoldierName,
           a.CountryCode,
           a.BonusPoints,
           c.playerScore, 
           b.bfbcsID,
           b.Rank, 
           c.*,
           c.playerKills / CASE WHEN c.playerDeaths = 0 THEN ( 1 ) ELSE c.playerDeaths END AS KD
          FROM 
           tbl_playerdata a
          Left JOIN 
           tbl_bfbcs b ON a.PlayerID = b.bfbcsID
          Left JOIN 
           tbl_playerstats c ON a.PlayerID = c.StatsID
          WHERE 
           {$SQLsearch}
          {$AND}
         ORDER BY 
				   {$sortby} {$order} LIMIT {$Limit}"; 
 $result = db_query($sSql);
 

if(!isset($seite)){
   $place = 1;
 }
elseif ($seite == 1){
 $place = 1;
}else{
 	$place = $seite*$entrys-($PlayerLimit-1);
}


if (!$result){
	echo '<td class="result" colspan="15">'.lang_error_player_error_order.'</td>';
}else{
$num_rows = mysql_num_rows($result);

$error = '';
if ($num_rows === 0) {
$error = '<td class="result" colspan="15">'.lang_error_player_search.'</td>';
}
echo $error;
while($row = mysql_fetch_assoc($result)) {

 
             if($row['KD']  >= 1 ){
	             $KD = "<span class='positiv'>" . number_format($row['KD'], 2, '.', '.') . "</span>";
	           }else{
             	 $KD = "<span class='negativ'>" . number_format($row['KD'], 2, '.', '.') . "</span>";
	         }
	               
	               $time_a	 		= $row['playerPlaytime'];
                  $s = $time_a;
                    $time = sprintf("%02dd %02dh %02dmin",$s/24/60/60,$s/60/60%24,$s/60%60);
                    
if(strlen($row['SoldierName']) <= 12) {
           $PlayerN = $row['SoldierName'];
        } else {
          $PlayerN = substr($row['SoldierName'],0,10) . '..'; 
      }


 
if(isset($row['Rank'])){
 $Rank = $row['Rank'];
}else{
  	$Rank = '0'; 
}


if(in_array($row['SoldierName'],$ServerPlayers)){
	$Soldier = '<img src="img/online.png" border="0" title="'.lang_online_player.'"/>'.htmlspecialchars($PlayerN).'';
}else{
	$Soldier = htmlspecialchars($PlayerN);
}

if (in_array($row['ClanTag'], $ClanTag) && $ClanTag != ''){
	$tr = '<tr class="hov_v">';
}else{
	$tr = '<tr class="hov">';
}


	?>
	<?php echo $tr; ?>
	  <td class="result" height="20px"><?php echo $place++; ?></td>
	  <td class="result"><img src="img/flags/<?php echo $row['CountryCode']?>.png" border="0" alt="country" title="Land"/></td>
		<td class="result"><img src="img/rank/thumb/<?php echo $Rank; ?>.png" border="0" width="20px" title="<?php echo htmlspecialchars($row['SoldierName']).lang_stats_haverank.$Rank.'('.$rankname[$Rank].')'; ?>" /></td>
	    <td class="result"><a href="ministats.php?name=<?php echo urlencode($row['SoldierName']); ?>" target="_self"><sup><span class="sonstige"><?php echo htmlspecialchars($row['ClanTag']); ?></span></sup>&nbsp;<?php echo $Soldier; ?></a></td>
				<td class="result"><?php echo number_format($row['playerKills'], 0, ',', ' '); ?></td>
				 <td class="result"><?php echo number_format($row['playerDeaths'], 0, ',', ' '); ?></td>
				  <td class="result"><?php echo number_format($row['playerHeadshots'], 0, ',', ' '); ?></td>
				  <td class="result"><?php echo $KD ?></td>
				<td class="result"><?php echo $time ?></td>
			<td class="result"><?php echo number_format($row['playerRounds'], 0, ',', ' '); ?></td>
		<td class="result"><?php echo number_format($row['playerScore'], 0, ',', ' '); ?></td>
   <td class="result"><?php echo number_format($row['Killstreak'], 0, ',', ' '); ?></td>
	<td class="result"><?php echo number_format($row['Deathstreak'], 0, ',', ' '); ?></td>
	</tr>	
<?php				
	}  
}
echo "</tbody><tr>
        <td class=result colspan=14>
      <b>".server_gerneral_serverb_page.":<font color=orange></b>";

echo blaetterfunktion($seite,  $Sites)."</td></font>";

echo "</table>";

?>

	      