<?php

/***************************************************************************
 *                                top_players.php
 *                               -----------------
 *   fait le                : Mercredi 31 Janvier, 2007
 *   Par                    : polo http://www.supernova2010.info
 *
 ***************************************************************************/

if ( !defined('IN_PHPBB') )
{
	die("Hacking attempt");
}

$phpbb_root_path = './';
include_once($phpbb_root_path . 'includes/functions_arcade.' . $phpEx);

// read arcade
$arcade_config = array();
$arcade_config = read_arcade_config();

// Dimensions avatars
$width= $arcade_config['avatar_best'];

// Nombre de détenteurs affichés
$limit = $arcade_config['limit_bestplayers'];

//chargement du template
$template->set_filenames(array(
   'top_players' => 'top_players.tpl')
);

	$sql_select = "";
if (( $arcade_config['use_gender_img'] ) && ( !$arcade_config['use_flag_img'] ))
{
	$sql_select = ", u.user_gender";
}
elseif (( $arcade_config['use_flag_img'] ) && ( !$arcade_config['use_gender_img'] ))
{
	$sql_select = ", u.user_from_flag";
}
elseif (( $arcade_config['use_gender_img']) && ($arcade_config['use_flag_img'] ))
{
	$sql_select = ", u.user_gender, u.user_from_flag";
} else {
	$sql_select = "";
}

if (! $arcade_config['admin_best_on'] ){
$sql_admin = ' AND u.user_level<>1';
} else {
$sql_admin = "";
}

if (! $arcade_config['modo_best_on'] ){
$sql_modo = ' AND u.user_level<>2';
} else {
$sql_modo = "";
}

	$sql = "SELECT COUNT(*) AS nbvictoires, g.game_highuser, u.user_id, u.username, u.user_level, u.user_allowavatar, u.user_avatar_type, u.user_avatar $sql_select FROM " . GAMES_TABLE . " g LEFT JOIN
	 " . USERS_TABLE . " u on g.game_highuser = u.user_id WHERE g.game_highuser<>0 $sql_admin $sql_modo GROUP BY g.game_highuser ORDER BY nbvictoires DESC LIMIT 0,$limit";
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(CRITICAL_ERROR, "Could not query games information", "", __LINE__, __FILE__, $sql);
	}
	$place=0;
	$nbvictprec=0;
	while ( $row = $db->sql_fetchrow($result) )
	{
  $place++;
	$nbvictprec=$row['nbvictoires'];

  if ( $row['user_id'] == ANONYMOUS ) {
		$user_online_link = $row['username'];
  } else {
		$user_online_link = '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . "=" . $row['user_id']) . '" alt="Voir le profil de ' . $row['username'] . '" title="Voir le profil de ' . $row['username'] . '">' . $row['username'] . '</a>';
  }
		$user_online_stats = '<a href="' . append_sid("statarcade.$phpEx?uid=" . $row['user_id']) . '">' . "<img src='templates/" . $theme['template_name'] . "/images/scoreboard.gif' border='0' alt='Voir les statistiques de " . $row['username'] . "' title='Voir les statistiques de " . $row['username'] . "'>" . '</a>';

  if ( $arcade_config['use_gender_img'])
  {
    $gender_image = ''; 
   switch ($row['user_gender']) 
   { 
	   case 1 : $gender_image = "<img src=\"" . $images['icon_minigender_male'] . "\" title=\"Sexe: Homme\" border=\"0\" />"; break; 
	   case 2 : $gender_image = "<img src=\"" . $images['icon_minigender_female'] . "\" title=\"Sexe: Femme\" border=\"0\" />"; break; 
	   default : $gender_image=""; 
   }
  }

  if ( $arcade_config['use_flag_img'] )
  {
	$flag = "<img src=\"images/flags/" . $row['user_from_flag'] . "\" alt=\"Le drapeau de " . $row['username'] . " : " . $row['user_from_flag'] . "\" title=\"Le drapeau de " . $row['username'] . " : " . $row['user_from_flag'] ."\" border=\"0\" height=\"15\" >";
	 if ($row['user_from_flag']==NULL)
   { 
	  $flag = "";
	 }
  }

 $avatar= ''; 
   if ( $row['user_avatar_type'] && $row['user_id']  != ANONYMOUS && $row['user_allowavatar'] ) 
   { 
      switch( $row['user_avatar_type'] ) 
      { 
         case USER_AVATAR_UPLOAD: 
            $avatar = ( $board_config['allow_avatar_upload'] ) ? '<img src="' . $board_config['avatar_path'] . '/' . $row['user_avatar'] . '" alt="' . $row['username'] . '" title="' . $row['username'] . '" widht="' . $width . '" height="' . $width . '" border="0"/>' : ''; 
            break; 
         case USER_AVATAR_REMOTE: 
            $avatar = ( $board_config['allow_avatar_remote'] ) ? '<img src="' . $row['user_avatar'] . '" alt="' . $row['username'] . '" title="' . $row['username'] . '" widht="' . $width . '" height="' . $width . '" border="0"/>' : ''; 
            break; 
         case USER_AVATAR_GALLERY: 
            $avatar = ( $board_config['allow_avatar_local'] ) ? '<img src="' . $board_config['avatar_gallery_path'] . '/' . $row['user_avatar'] . '" alt="' . $row['username'] . '" title="' . $row['username'] . '" widht="' . $width . '" height="' . $width . '" border="0"/>' : ''; 
            break;
      } 
   }
   if ( empty($avatar) ) 
   { 
     $avatar = '<img src="images/noavatar.gif" widht="' . $width . '" height="' . $width . '" alt="Pas d\'avatar" title="Pas d\'avatar" border="0" />'; 
   }
    
		$template->assign_vars(array(
      'NB' => $arcade_config['limit_bestplayers'])
		);

		$template->assign_block_vars('top_row', array(
      'PLACE' => $place,
      'AVATAR' =>  $avatar,
			'USERNAME' => $user_online_link,
      'GENDER' =>  $gender_image,
      'FLAG' =>  $flag,
			'STATS' => $user_online_stats,
			'VICTOIRES' => $row['nbvictoires'])
		);

  if ( $arcade_config['use_flag_img'])
  {
	  $template->assign_block_vars('top_row.switch_flag', array());
  }
  if ( $arcade_config['use_gender_img'])
  {
	  $template->assign_block_vars('top_row.switch_gender', array());
  }
  if ( $arcade_config['use_stats_link'])
  {
	  $template->assign_block_vars('top_row.switch_stats', array());
  }
}

$template->assign_var_from_handle('TOP_PLAYERS', 'top_players');		

?>