<?php
/*********************************************
  CPG Dragonfly - DF_Arcade Block
  ********************************************
  CPG Dragonfly is copyright © 2004 - 2005 by CPG Dev Team
  http://www.dragonflycms.com

  Dragonfly and DF_Arcade are released under the terms
  and conditions of the GNU GPL version 2 or any later version

  port of pnFlashGames - http://pnflashgames.com/
  Written by gerry@redgerry.com
  redgerry.com - Home of RG_Sports
  Revision Date: 16/10/2005 11:46
  Version Number 1.00
  Credit for this fabulously effecient piece of code belongs to Biggles 

  UNDER NO CIRCUMSTANCES MAY THIS CODE BE USED FOR ANY COMMERCIAL
  PURPOSES UNDER THE GUISE OF V.I.P. OR ANY OTHER DESCRIPTION
********************************************************/
if (!defined('CPG_NUKE')) { exit; }
global $prefix, $db, $bgcolor1, $bgcolor2, $cpgtpl;


$rga_topscrolls_module_name = "DF_Arcade";

// Create a temporary table of the high scores
$tmp_table = 'tmp_' . rand();
$result_hiscores = $db->sql_query( 'CREATE TABLE ' . $tmp_table . ' SELECT a.game_id, IF ( b.gtype = 2, min(a.playerscore), max(a.playerscore) ) AS playerscore FROM ' . $prefix . '_rgarcade_hiscores a, ' . $prefix . '_rgarcade_games b WHERE a.game_id = b.game_id AND b.gtype < 3 GROUP BY a.game_id', false, __FILE__, __LINE__ );

// Select the list of top scores for each game
$result_games = $db->sql_query( "SELECT a.game_id, a.playerscore, DATE_FORMAT( a.date, '%D %b' ) AS 'nicedate', c.gname, d.username, d.user_avatar FROM " . $prefix . '_rgarcade_hiscores a, ' . $tmp_table . ' b, ' . $prefix . '_rgarcade_games c, ' . $prefix . '_users d WHERE a.game_id = b.game_id AND a.playerscore = b.playerscore AND a.game_id = c.game_id AND a.uid = d.user_id ORDER BY rand()', false, __FILE__, __LINE__ );
while( $row_game = $db->sql_fetchrow( $result_games, SQL_ASSOC ) ) {
  if ( '' == $row_game['user_avatar'] || NULL == $row_game['user_avatar'] ) {
    $avatar = 'images/avatars/gallery/blank.gif';
  }
  elseif ( eregi( "http://", $row_game['user_avatar'] ) ) {
    $avatar = $row_game['user_avatar'];
  }
  elseif ( eregi( "gallery", $row_game['user_avatar'] ) || eregi( "/", $row_game['user_avatar'] ) ) {
    $avatar = 'images/avatars/' . $row_game['user_avatar'];
  }
  else {
    // Avatar is uploaded
    $avatar = 'uploads/avatars/' . $row_game['user_avatar'];
  }

  $cpgtpl->assign_block_vars( 'rgarcadetopscores', array(
    'S_PLAYER_NAME'     => $row_game['username'],
    'S_IMAGE_AVATAR'    => $avatar,
    'S_PLAYER_SCORE'    => $row_game['playerscore'],
    'S_GAME_DATE'       => $row_game['nicedate'],
    'S_GAME_NAME'       => $row_game['gname'],

    'U_PLAYER_PROFILE'  => getlink( 'Forums&amp;file=profile&amp;mode=viewprofile&amp;u=' . $row_game['username'] ),
    'U_GAME'            => getlink( $rga_topscrolls_module_name . '&amp;func=play&amp;game_id=' . $row_game['game_id'] )
    )
  );
}
$db->sql_freeresult( $result_games );

// Delete the temporary table
$result_drop = $db->sql_query( 'DROP TABLE ' . $tmp_table, false, __FILE__, __LINE__ );

$cpgtpl->assign_vars( array(
  'S_BGCOLOR_1' => $bgcolor1,
  'S_BGCOLOR_2' => $bgcolor2,

  'U_ARCADE'    => getlink( $rga_topscrolls_module_name )
  )
);


// Display the high scores
ob_start();
$cpgtpl->set_template();
$cpgtpl->set_filenames( array( 'block_rga_topscores' => 'blocks/block-RGA_Scroll-TopScores.html' ) );
$cpgtpl->display( 'block_rga_topscores' );
$content = ob_get_clean();
