<?php
/*********************************************
  CPG Dragonfly - DF_Arcade Module
  ********************************************
  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

  UNDER NO CIRCUMSTANCES MAY THIS GPL CODE BE USED FOR ANY COMMERCIAL
  PURPOSES UNDER THE GUISE OF V.I.P. OR ANY OTHER DESCRIPTION
********************************************************/
// FUNCTION ORDER LIST CHECK
// arcadehome
// navigator
// bannerstats
// yourarcade
// leaderboard
// displaycategories
// displaycat
// playgame
// gamesearchbox
// alphagames
// gheadstrip
/***** All functions below provided by GPL ref documentation for ALL CMS releases ****/
// pnFlashGames_getChecksum
// GPL - provided by http://www.pnflashgames.com/docs/ch05s02.html
// storeScore
// GPL - provided by http://www.pnflashgames.com/docs/ch05s02.html
// loadGame
// GPL - provided by http://www.pnflashgames.com/docs/ch05s02.html
// saveGame
// GPL - provided by http://www.pnflashgames.com/docs/ch05s02.html
// loadGameScores
// GPL - provided by http://www.pnflashgames.com/docs/ch05s02.html
// ScoreCatcher component constructed by Gerry Shields following correct
// utilisation of POST statement as specified by pnFlashGames

function arcadehome() {
	global $game_id, $cat_id;
    	OpenTable();
    	navigator();
	bannerstats();
	CloseTable();
}
function navigator(){
	global $db, $prefix, $module_name, $func, $game_id;
	$func = Fix_Quotes($func);
	$game_id = intval($game_id);
	echo "<div align=\"center\"><table cellpadding=\"5\"><tr>";
	if ($func != ""){
		echo "<td align=\"center\"><a href=\"".getlink(''.$module_name.'')."\">"._RGAHOME."</a></td>";
	}
	if ($func != "yourarcade"){
		echo "<td align=\"center\"><a href=\"".getlink(''.$module_name.'&amp;func=yourarcade')."\">"._YOURARCADE."</a></td>";
	}
	if ($func != "displaycategories"){
		echo "<td align=\"center\"><a href=\"".getlink(''.$module_name.'&amp;func=displaycategories')."\">"._RGACATS."</a></td>";
	}
	if ($func == "playgame"){
		$result = $db->sql_query("SELECT g.gcid, c.cname FROM ".$prefix."_rgarcade_games g, ".$prefix."_rgarcade_cat c WHERE g.game_id='$game_id' and g.gcid=c.cat_id");
		list($cat_id, $cname) = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);
		echo "<td align=\"center\"><a href=\"".getlink(''.$module_name.'&amp;func=displaycat&amp;cat_id='.$cat_id.'')."\">$cname</a></td>";
	}
	if ($func != "leaderboard"){
		echo "<td align=\"center\"><a href=\"".getlink(''.$module_name.'&amp;func=leaderboard')."\">"._ARCLDRBOARD."</a></td>";
	}
	$serverdate = date("D jS F y, g\:ia");
	echo "<td align=\"center\"><font class=\"small\">Server Time: <i>$serverdate</i></font></td>"
	."</tr></table></div><hr />";
}
function bannerstats(){
	global $MAIN_CFG, $admin, $db, $prefix, $module_name, $userinfo;
	$nukeurl = $MAIN_CFG['global']['nukeurl'];
	echo "<div align=\"center\"><table cellpadding=\"4\"><tr><td align=\"center\" valign=\"top\">";
	alphagames();
	echo "</td><td align=\"center\" valign=\"top\">";
		$cuid = 1;
	$toteplayers = 0;
	$result = $db->sql_query("SELECT uid FROM ".$prefix."_rgarcade_hiscores ORDER BY uid ASC");
	while (list($uid) = $db->sql_fetchrow($result)) {
		if ($cuid != $uid){
			$cuid = $uid;
			$toteplayers++;
		}
	};
	if (is_user()) {
    	$userid = $userinfo['user_id'];
    	$username = $userinfo['username'];
	$result = $db->sql_query("SELECT COUNT(*) FROM ".$prefix."_rgarcade_hiscores where uid='$userid'");
	list($played) = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	$a = 0;
	$b = 0;
	$c = 0;
	$opt = "";
	$result = $db->sql_query("SELECT game_id, gtype FROM ".$prefix."_rgarcade_games WHERE gtype<'3' ORDER BY game_id ASC");
	while (list($game_id, $gtype) = $db->sql_fetchrow($result)) {
		if ($gtype == 1){
			$opt = "DESC";
		} elseif ($gtype == 2){
			$opt == "ASC";
		}
		$d = 0;
		$result2 = $db->sql_query("SELECT uid FROM ".$prefix."_rgarcade_hiscores WHERE game_id='$game_id' ORDER BY playerscore $opt LIMIT 0,3");
		while (list($uid) = $db->sql_fetchrow($result2)) {
			$d++;
			if ($uid == $userid){
				if ($d == 1){
					$a++;
				} elseif ($d == 2){
					$b++;
				} elseif ($d == 3){
					$c++;
				}
			}
		}
	}
	}
	echo "<table width=\"100%\" cellpadding=\"2\"><tr>"
	."<td align=\"center\"><font class=\"title\">Active Gamers</font><td>"
	."</tr><tr>"
	."<td align=\"center\"><font class=\"content\"><b>$toteplayers</b></font><td>"
	."</tr><tr>";
	if (is_user()) {
		echo "<td align=\"center\"><font class=\"title\">"._RGAGYP."</font><td>"
		."</tr><tr>"
		."<td align=\"center\"><font class=\"content\"><b>$played</b></font><td>"
		."</tr><tr>"
		."<td align=\"center\"><font class=\"title\">"._RGARANH."</font><td>"
		."</tr><tr>"
		."<td align=\"center\"><font class=\"content\">1st <b>$a</b>&nbsp;|&nbsp;2nd <b>$b</b>&nbsp;|&nbsp;3rd <b>$c</b></font><td>";
	} else {
		echo "<td align=\"center\"><font class=\"small\">"._RGAANONMSG." <a href=\"".getlink('Your_Account&amp;file=register')."\">"._RGAREGISTER."</a></font></td>";
	}
	echo "</tr></table>"
	."</td></tr></table></div>";
}
function yourarcade(){
	global $prefix, $db, $module_name, $userinfo;
	if (isset($_GET['player_id'])) { $player_id = intval($_GET['player_id']); }
	if ($player_id < 2){
		$player_id = $userinfo['user_id'];
		$playername = $userinfo['username'];
	}
	OpenTable();
    	navigator();
	bannerstats();
	CloseTable();

}
function leaderboard(){
	global $prefix, $db, $module_name, $userinfo;
	$thisplayer_id = $userinfo['user_id'];
	$thisplayername = $userinfo['username'];
	OpenTable();
    	navigator();
	bannerstats();
	CloseTable();
}
function displaycategories(){
	global $prefix, $db, $module_name, $admin;
	OpenTable();
	navigator();
	echo "<table width=\"100%\"><tr>";
	$result = $db->sql_query("SELECT COUNT(*) FROM ".$prefix."_rgarcade_cat");
	list($nocats) = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	if ($nocats < 1){
		echo "<td align=\"center\">"
		.""._RGANOCATS."";
		if (can_admin(strtolower($module_name))) {
			echo "<hr/><br /><font class=\"title\">"._RGAAPRCAT."</font>"
			."<br /><br /><a href=\"".adminlink('DF_Arcade')."\">"._RGAACP."</a><br /><hr/>";
		}
		echo "<br /></td></tr>";
	} else {
		$a = 0;
		$result = $db->sql_query("SELECT cat_id FROM ".$prefix."_rgarcade_cat ORDER BY cat_id ASC");
		while (list($cat_id) = $db->sql_fetchrow($result)) {
			$result2 = $db->sql_query("SELECT COUNT(*) FROM ".$prefix."_rgarcade_games WHERE gcid='$cat_id'");
			list($nogames) = $db->sql_fetchrow($result2);
			$db->sql_freeresult($result2);
			if ($nogames > 0){
				$result2 = $db->sql_query("SELECT cname, cdescription, cimage, hits FROM ".$prefix."_rgarcade_cat WHERE cat_id='$cat_id'");
				list($cname, $cdescription, $cimage, $chits) = $db->sql_fetchrow($result2);
				$db->sql_freeresult($result2);
				$a++;
				echo "<td width=\"50%\" align=\"center\">"
					."<table width=\"100%\" cellpadding=\"0\" border=\"1\"><tr><td>"
					."<table width=\"100%\" cellpadding=\"2\"><tr>"
					."<td width=\"150\">"
					."<a href=\"".getlink('DF_Arcade&amp;func=displaycat&amp;cat_id='.$cat_id.'')."\"><img src=\"images/DF_Arcade/cat/$cimage\" border=\"0\" alt=\"$cname "._RGACATAV." $nogames "._RGAGAV."\" width=\"140\"></a></td>"
					."<td align=\"center\"><a href=\"".getlink('DF_Arcade&amp;func=displaycat&amp;cat_id='.$cat_id.'')."\"><font class=\"title\">$cname</font></a>"
					."<br /><br />"
					."<font class=\"small\"><b>$nogames</b> "._RGAGAMES.""
					."&nbsp;|&nbsp;"
					."<i>$chits</i> "._RGAHITS."<br /><br />"
					."$cdescription</font></td>"
					."</tr></table>"
					."</td></tr></table>"
				."</td>";
			}
			if ($a == 2){
				echo "</tr><tr>";
				$a = 0;
			}
		}
		if ($a == 1){
			echo "<td>&nbsp;</td></tr>";
		}
	}
	echo "</table>";
	bannerstats();
	CloseTable();
}
function displaycat() {
	global $prefix, $db, $module_name, $admin, $bgcolor1, $bgcolor2;
	if (isset($_GET['cat_id'])) { $cat_id = intval($_GET['cat_id']); }
	$result = $db->sql_query("SELECT cimage, cname, hits FROM ".$prefix."_rgarcade_cat WHERE cat_id='$cat_id'");
	list($cimage, $cname, $chits) = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	$result = $db->sql_query("UPDATE ".$prefix."_rgarcade_cat set hits=hits+1 WHERE cat_id='$cat_id'");
	OpenTable();
	navigator();
	echo "<table width=\"100%\"><tr><td align=\"center\">"
	."<img src=\"images/DF_Arcade/cat/$cimage\" alt=\"$cname\"><br /><br />"
	."<i>$chits "._RGAHITS."</i></td></tr></table>";
	$result = $db->sql_query("SELECT COUNT(*) FROM ".$prefix."_rgarcade_games WHERE gcid='$cat_id'");
	list($nogames) = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	echo "<table width=\"100%\"><tr>";
	if ($nogames < 1){
		echo "<td align=\"center\">"
		.""._RGANOGAV."";
		if (can_admin(strtolower($module_name))) {
			echo "<hr/><br /><font class=\"title\">"._RGAACP."</font>"
			."<br /><br /><a href=\"".adminlink('&amp;mode=DF_ArcadeGameMenu')."\">"._RGAAGOPT."</a><br /><hr/>";
		}
		echo "<br /></td></tr>";
	} else {
// no pageanation - will be added from already built RG_Sports GPL functions.
		$x = 0;
		$result = $db->sql_query("SELECT game_id, gtype, gname, gdescription, gimage, gauthorname, gauthorurl, gwidth, gheight, hits FROM ".$prefix."_rgarcade_games WHERE gcid='$cat_id' ORDER BY gname");
		while (list($game_id, $gtype, $gname, $gdescription, $gimage, $gauthorname, $gauthorurl, $gwidth, $gheight, $hits) = $db->sql_fetchrow($result)) {
			$x++;
			echo "<td width=\"50%\" align=\"center\">"
				."<table width=\"100%\" cellpadding=\"0\" border=\"1\"><tr><td>"
					."<table width=\"100%\" cellpadding=\"2\"><tr>"
					."<td valign=\"top\" align=\"center\">";
					$pic = "modules/$module_name/screenshot/$gimage";
					if (file_exists($pic)){
						$piccy = "<img src=\"modules/$module_name/screenshot/$gimage\" border=\"0\" alt=\""._RGAPLAY." '$gname'\" width=\"140\" height=\"100\">";
					} else {
						$piccy = _RGANOIMG;
					}
					echo "<a href=\"".getlink(''.$module_name.'&amp;func=playgame&amp;game_id='.$game_id.'')."\">$piccy</a>";

					if (is_admin($admin)){
						echo "<br /><br />"
						."<form action=\"".adminlink('&amp;mode=DF_ArcadeUpGame')."\" method=\"post\" name=\"rgarcade\">"
						."<input type=\"hidden\" name=\"game_id\" value=\"$game_id\">"
						."<input type=\"submit\" value=\""._RGAGMOD."\">"
						."</form></div>";
					}
					echo "<br /><br />"
					."<a href=\"".getlink(''.$module_name.'&amp;func=playgame&amp;game_id='.$game_id.'')."\"><font class=\"title\">"._RGAPLAYGAME."</font></a>"
					."</td><td align=\"center\">"
					."<a href=\"".getlink(''.$module_name.'&amp;func=playgame&amp;game_id='.$game_id.'')."\"><font class=\"title\">$gname</font></a>"
   					."<br /><br /><font class=\"content\">"._RGAHITS.": <b>$hits</b><br /><br />";
   					if ($gauthorurl == ""){
   						echo ""._RGAAAN." $gauthorname";
   					} else {
   						echo ""._RGAAAN." <a href=\"$gauthorurl\" target=\"blank\">$gauthorname</a>";
   					}
					echo "<br /><br /><font class=\"small\">$gdescription</font>"
					."</td><td align=\"center\" valign=\"top\">";
					if ($gtype == "2"){
						$order = "ASC";
					} else {
						$order = "DESC";
					}
   						echo "<table width=\"100%\" border=\"0\" align=\"center\"><tr>"
   						."<td valign=\"top\" align=\"center\" bgcolor=\"$bgcolor1\" nowrap=\"NOWRAP\">"
   						."<font class=\"small\"><b>"._RGABSCO."</b></font></td>"
   						."</tr>";
   						$result2 = $db->sql_query("SELECT COUNT(*) FROM ".$prefix."_rgarcade_hiscores WHERE game_id='$game_id'");
						list($anynormhighs) = $db->sql_fetchrow($result2);
						$db->sql_freeresult($result2);
						if ($anynormhighs == 0){
							echo "<tr><td align=\"center\"><font class=\"content\"><i>"._RGANOSCORES."</i></font>";
						} else {
						$pos = 0;
						echo "<tr><td align=\"center\">"
							."<table width=\"100%\">";
							$result2 = $db->sql_query("SELECT g.playerscore, DATE_FORMAT(g.date, '%l:%i%p %W %D %M %Y') AS 'nicedate', u.username FROM ".$prefix."_rgarcade_hiscores g, ".$prefix."_users u where game_id='$game_id' and g.uid=u.user_id ORDER BY g.playerscore $order LIMIT 0,3");
							while (list($playerscore, $nicedate, $playername) = $db->sql_fetchrow($result2)) {
								$pos++;
								if ($pos == 1){
   									$award = "<img src=\"images/DF_Arcade/awards/icon_award_gold.gif\">";
   								} elseif ($pos == 2){
   									$award = "<img src=\"images/DF_Arcade/awards/icon_award_silver.gif\">";
   								} else {
   									$award = "<img src=\"images/DF_Arcade/awards/icon_award_bronze.gif\">";
   								}
								echo "<tr>"
								."<td align=\"center\">$award</td>"
   								."<td align=\"center\"><a href=\"".getlink('Your_Account&amp;profile='.$playername.'')."\">$playername</a><br /><font class=\"small\"><b>$playerscore</b></font></td>"
   								."</tr><tr>"
   								."<td colspan=\"2\" align=\"center\" nowrap=\"nowrap\"><font class=\"small\">$nicedate</font><hr/></td>"
   								."</tr>";
   							}
   							echo "</table>";
   					}
					echo "</td></tr></table>"
				."</td></tr></table>"
   			."</td></tr></table>";
   			if ($x == 2){
				echo "</tr><tr>";
				$x = 0;
			}
		}
		if ($a == 1){
			echo "<td>&nbsp;</td></tr>";
		}
   	}
   	echo "</table>";
   	bannerstats();
   	CloseTable();
}
function playgame(){
	global $MAIN_CFG, $admin, $prefix, $db, $userinfo, $module_name, $bgcolor1, $bgcolor2, $game_id;
	if (isset($_GET['game_id'])) { $game_id = intval($_GET['game_id']); }
	$ThemeSel = $MAIN_CFG['global']['Default_Theme'];
	$domain = $MAIN_CFG['server']['domain'];
	$result = $db->sql_query("SELECT COUNT(*) FROM ".$prefix."_rgarcade_games WHERE game_id='$game_id'");
	list($isagame) = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	OpenTable();
	navigator();
	if ($isagame < 1){
		echo "<br /><br /><div align=\"center\"><font class=\"title\">"._RGAGNOTT."</font><br /><br />";
	} else {
		echo "<table width=\"100%\"><tr>";
		$result = $db->sql_query("UPDATE ".$prefix."_rgarcade_games SET hits=hits+1 WHERE game_id='$game_id'");
		$result = $db->sql_query("SELECT COUNT(*) FROM ".$prefix."_rgarcade_hiscores WHERE game_id='$game_id'");
		list($ldrpts) = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);
		if ($ldrpts == 0){
			$ldrpts = 1;
		}
		$username = $userinfo['username'];
		$result = $db->sql_query("SELECT gtype, gname, gdescription, gimage, gfilename, gauthorname, gauthorurl, gwidth, gheight, gcolor, hits, glicensekey FROM ".$prefix."_rgarcade_games WHERE game_id='$game_id'");
		list($gtype, $gname, $gdescription, $gimage, $gfilename, $gauthorname, $gauthorurl, $gwidth, $gheight, $gcolor, $ghits, $glicensekey) = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);
		if ($gwidth == "") {
			$gwidth = "400";
		}
		if ($gheight == "") {
			$gheight = "510";
		}
		$tdw = $gwidth+20;
		$tdh = $gheight+20;
		echo "<td bgcolor=\"$bgcolor1\" align=\"center\" height=\"$tdh\" width=\"$tdw\">";
		$gamepath = "modules/$module_name/games/$gfilename";
		if (file_exists($gamepath)) {
			$checksum = "".pnFlashGames_getChecksum("modules/DF_Arcade/games/$gfilename");
			echo "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"
  codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\"
  id=\"$gname\" width=\"$gwidth\" height=\"$gheight\"><param name=\"movie\" value=\"modules/$module_name/games/$gfilename?gid=$game_id&amp;uname=$username&amp;pn_gid=$game_id&amp;pn_uname=$username&amp;pn_domain=$domain&amp;pn_license=$glicensekey&amp;pn_checksum=$checksum&amp;pn_script=index.php?&amp;pn_modvar=name&amp;pn_modvalue=DF_Arcade\">
  			<param name=\"quality\" value=\"high\">
  			<param name=\"bgcolor\" value=\"$gcolor\">
  			<PARAM NAME=\"MENU\" VALUE=\"false\">
    			<embed name=\"$gname\" src=\"modules/$module_name/games/$gfilename?gid=$game_id&amp;uname=$username&amp;pn_gid=$game_id&amp;pn_uname=$username&amp;pn_domain=$domain&amp;pn_license=$glicensekey&amp;pn_checksum=$checksum&amp;pn_script=index.php?&pn_modvar=name&amp;pn_modvalue=DF_Arcade\" quality=\"high\" bgcolor=\"$gcolor\" swLiveConnect=\"true\" width=\"$gwidth\" height=\"$gheight\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"></embed>
			</object>";
		} else {
			echo "<font class=\"title\">"._RGAGERR."</font><br /><br /><br />";
		}
		echo "</td><td align=\"center\" valign=\"top\">"
		."<br /><br /><font class=\"title\">$gname</font><br /><br />"
		."<font class=\"small\">$gdescription</font><br /><br />";
		if ($gauthorurl == ""){
   			echo ""._RGAAAN." $gauthorname";
   		} else {
   			echo ""._RGAAAN." <a href=\"$gauthorurl\" target=\"blank\">$gauthorname</a>";
   		}
		echo "&nbsp;|&nbsp;<b>$ghits</b> "._RGAHITS.""
		."&nbsp;|&nbsp;<b>$ldrpts</b> "._RGALDRPTS."</font><hr />"
			."<table cellpadding=\"4\"><tr>"
			."<td align=\"center\" colspan=\"2\">"
			."<font class=\"title\">"._RGABSCO."</font>"
			."</td></tr>"
			."<tr>";
			$result = $db->sql_query("SELECT COUNT(*) FROM ".$prefix."_rgarcade_hiscores WHERE game_id='$game_id'");
			list($anynormhighs) = $db->sql_fetchrow($result);
			$db->sql_freeresult($result);
			if ($anynormhighs > 0){
				if ($gtype == "2"){
					$typeopt = "ASC";
				} else {
					$typeopt = "DESC";
				}
				$pos = 0;
				$result = $db->sql_query("SELECT uid, playerscore, DATE_FORMAT(date, '%k:%i %D %b %y') AS 'nicedate' FROM ".$prefix."_rgarcade_hiscores where game_id='$game_id' ORDER BY playerscore $typeopt LIMIT 0,3");
				while (list($uid, $playerscore, $nicedate) = $db->sql_fetchrow($result)) {
					$pos++;
					$result2 = $db->sql_query("SELECT username FROM ".$prefix."_users WHERE user_id='$uid'");
					list($playername) = $db->sql_fetchrow($result2);
					$db->sql_freeresult($result2);
					if ($pos == 1){
   						$award = "<img src=\"images/DF_Arcade/awards/icon_award_gold.gif\">";
   					} elseif ($pos == 2){
   						$award = "<img src=\"images/DF_Arcade/awards/icon_award_silver.gif\">";
   					} else {
   						$award = "<img src=\"images/DF_Arcade/awards/icon_award_bronze.gif\">";
   					}
   				echo "<tr><td align=\"center\">$award</td>"
   				."<td align=\"center\" valign=\"top\"><br /><br /><a href=\"".getlink('DF_Arcade&amp;func=yourarcade&amp;player_id='.$uid.'')."\">$playername</a>&nbsp;:&nbsp;<font class=\"small\"><b>$playerscore</b><br />$nicedate</font></td></tr>";
   			}
   		} else {
   			echo "<tr><td align=\"center\" colspan=\"2\">"
   			."<font class=\"content\">"._RGANOSCORES."</font>"
   			."</td></tr>";
   		}
   		echo "</table>";
		if (is_admin($admin)){
			echo "<br /><br /><div align=\"center\">"
			."<form action=\"".adminlink('&amp;mode=DF_ArcadeUpGame')."\" method=\"post\" name=\"rgarcade\">"
			."<input type=\"hidden\" name=\"game_id\" value=\"$game_id\">"
			."<input type=\"submit\" value=\""._RGAGMOD."\">"
			."</form></div>";
		}
		echo "</td></tr></table>";
   	}
	bannerstats();
	CloseTable();
}
// functions provided by http://www.pnflashgames.com/docs/ch05s02.html
function pnFlashGames_getChecksum($file){
	if($fp = fopen($file, 'r')){
		$filecontent = fread($fp, filesize($file));
		fclose($fp);
		return md5($filecontent);
	} else {
		return false;
	}
}
// function structure provided by http://www.pnflashgames.com/docs/ch05s02.html
function storeScore($player_score, $game_id) {
	global $MAIN_CFG, $prefix, $db, $userinfo, $module_name;

	if (is_user()) {
    		$userid = $userinfo['user_id'];
    		$username = $userinfo['username'];
    		$player_name = $username;
	} else {
		$errormsg = _RGANOANON;
		$notscored = 1;
	}
	$html = $MAIN_CFG['email']['allow_html_email'];
	$adminmail = $MAIN_CFG['global']['adminmail'];
	$sitename = $MAIN_CFG['global']['sitename'];
	$nukeurl = $MAIN_CFG['global']['nukeurl'];
	$CPG_SESS['arcade_score'] = true;
	$player_score = isset($player_score) ? intval($player_score) : '0';
	$game_id = isset($game_id) ? intval($game_id) : '0';
// Get game info ONLY if not already set
	if ($player_score < 1){
		$player_score = isset($_POST['score']) ? intval($_POST['score']) : '';
		$game_id = isset($_POST['gid']) ? intval($_POST['gid']) : '';
	}

// Work out if this is a better score
	$date = date("Y-m-d H:i");
	$count = 0;
	$repscore = 0;
	$result = $db->sql_query("UPDATE ".$prefix."_rgarcade_games set hits=hits+1 WHERE game_id='$game_id'");
	$result = $db->sql_query("SELECT gtype, gname, gimage FROM ".$prefix."_rgarcade_games WHERE game_id='$game_id'");
	list($gtype, $gname, $gimage) = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	if ($gtype < 3){
// Check if user has already got scored in this game
	$result = $db->sql_query("SELECT COUNT(*) FROM ".$prefix."_rgarcade_hiscores WHERE game_id='$game_id' and uid='$userid'");
	list($count) = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
// If score already, test to see if better
	if($count > 0) {
		$result = $db->sql_query("SELECT playerscore FROM ".$prefix."_rgarcade_hiscores WHERE game_id='$game_id' and uid='$userid'");
		list($cur_score) = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);
		$opt = "";
// Higher score is better
	if ($gtype == 1){
		if ( $player_score > $cur_score){
			$repscore = 1;
		} else {
			$notscored = 1;
			$errormsg = _RGANHE;
		}
// Lower score is better
	} elseif ($gtype == 2){
		if ( $player_score < $cur_score){
			$repscore = 1;
		} else {
			$notscored = 1;
			$errormsg = _RGANLE;
		}
	}
	if ($repscore == 1){
		$gurl = "$nukeurl/index.php?name=$module_name&amp;func=playgame&amp;game_id=$game_id";
		$gimage2 = "$nukeurl/modules/$module_name/screenshot/$gimage";
		$p = 0;
		$result = $db->sql_query("SELECT uid, playerscore, DATE_FORMAT(date, '%l:%i%p %W %D %M %Y') AS 'nicedate' FROM ".$prefix."_rgarcade_hiscores WHERE game_id='$game_id' ORDER BY playerscore $opt LIMIT 0,3");
		while (list($uid, $playerscore, $nicedate) = $db->sql_fetchrow($result)) {
// If score beats any/all current top 3 email them to let them know.
			$p++;
// unless it is THIS user
			if ($userid != $uid){
				if ( ( ( $gtype == 1 ) && ( $player_score > $playerscore ) ) || ( ( $gtype == 2) && ( $player_score < $playerscore ) ) ) {
					$result2 = $db->sql_query("SELECT username, user_email FROM ".$prefix."_users WHERE user_id='$uid'");
					list($recipient_name, $recipient_email) = $db->sql_fetchrow($result2);
					$db->sql_freeresult($result2);
					$sender_name = "GamesMaster";
					$sender_email = $adminmail;
					$subject = "$module_name - $sitename";
					$personal_message = "Current position ($p) for <a href=\"$gurl\">$gname</a> has been lost to $username with a score of $player_score beating your best score $playerscore achieved $nicedate<br /><br />Click the following image to regain your position:<br /><br /><a href=\"$gurl\"><img src=\"$gimage2\" border=\"0\" alt=\"click to play $gname now!\"></a><br /><br />";
// Mailer function from Tell a Friend - GPL
					send_mail($mailer_message, $personal_message, $html, $subject, $recipient_email, $recipient_name, $sender_email, $sender_name);
				}
			}
		}
// update the existing score
		$result = $db->sql_query("UPDATE ".$prefix."_rgarcade_hiscores set playerscore='$player_score' WHERE game_id='$game_id' and uid='$userinfo[user_id]'");
		}
	} else {
		$gurl = "$nukeurl/index.php?name=$module_name&amp;func=playgame&amp;game_id=$game_id";
		$gimage2 = "$nukeurl/modules/$module_name/screenshot/$gimage";
		$p = 0;
		$result = $db->sql_query("SELECT uid, playerscore, DATE_FORMAT(date, '%l:%i%p %W %D %M %Y') AS 'nicedate' FROM ".$prefix."_rgarcade_hiscores WHERE game_id='$game_id' ORDER BY playerscore $opt LIMIT 0,3");
		while (list($uid, $playerscore, $nicedate) = $db->sql_fetchrow($result)) {
			$p++;
			if ($userid != $uid){
				if ( ( ( 1 == $gtype ) && ( $player_score > $playerscore ) ) || ( ( 2 == $gtype ) && ( $player_score < $playerscore ) ) ) {
					$result2 = $db->sql_query("SELECT username, user_email FROM ".$prefix."_users WHERE user_id='$uid'");
					list($recipient_name, $recipient_email) = $db->sql_fetchrow($result2);
					$db->sql_freeresult($result2);
					$sender_name = "GamesMaster";
					$sender_email = $adminmail;
					$subject = "$module_name - $sitename";
					$personal_message = "Current position ($p) for <a href=\"$gurl\">$gname</a> has been lost to $username with a score of $player_score beating your best score $playerscore achieved $nicedate<br /><br />Click the following image to regain your position:<br /><br /><a href=\"$gurl\"><img src=\"$gimage2\" border=\"0\" alt=\"click to play $gname now!\"></a><br /><br />";
// Mailer function from Tell a Friend - GPL
					send_mail($mailer_message, $personal_message, $html, $subject, $recipient_email, $recipient_name, $sender_email, $sender_name);
				}
			}
		}
		$result = $db->sql_query("INSERT INTO ".$prefix."_rgarcade_hiscores (high_id, game_id, uid, playerscore, date) VALUES (NULL,'$game_id','$userinfo[user_id]','$player_score','$date')");
	}
	} else {
		$errormsg = _RGANOSCORES;
		$notscored = 1;
	}
// Game Data requirements specified by pnFlashGames
// GPL - http://www.pnflashgames.com/docs/ch05s03.html
	if($notscored == 1){
		echo "&amp;opSuccess=false&amp;error=$errormsg&amp;endvar=1";
		$gameData = "&amp;opSuccess=false&amp;error=$errormsg&amp;endvar=1";
	} else {
		echo "&amp;opSuccess=true&amp;endvar=1";
		$gameData = "&amp;opSuccess=true&amp;endvar=1";
	}
	RETURN urlencode($gameData);
}
// instructions/syntax available GPL - http://www.pnflashgames.com/docs/ch05s03.html
function loadGame(){
	$gameData = "DF_Arcade";
	echo "&amp;Success=true&amp;gameData=$gameData&amp;endvar=1";
	RETURN urlencode($gameData);
}
// instructions/syntax available GPL - http://www.pnflashgames.com/docs/ch05s03.html
function saveGame(){
	echo "&amp;opSuccess=true&amp;gameData=$gameData&amp;endvar=1";
	$gameData = "&amp;opSuccess=true&amp;gameData=$gameData&amp;endvar=1";
	RETURN urlencode($gameData);
}
// instructions/syntax available GPL - http://www.pnflashgames.com/docs/ch05s03.html
function loadGameScores(){
	global $prefix, $db;
	if (isset($_POST['gid'])) { $game_id = intval($_GET['gid']); }
	$result = $db->sql_query("SELECT gtype FROM ".$prefix."_rgarcade_games WHERE game_id='$game_id'");
	list($gtype_id) = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	if ($gtype_id == 1){
		$opt = "DESC";
	} elseif ($gtype_id == 2){
		$opt = "ASC";
	} else {
		$opt = "";
	}
	$a = 0;
	$missing = 0;
	$score = "";
	$result = $db->sql_query("SELECT playerscore, DATE_FORMAT(h.date' '%e-%c-%Y'), u.username FROM ".$prefix."_rgarcade_hiscorers h, ".$prefix."_users WHERE h.game_id='$game_id' and h.uid=u.user_id ORDER BY playerscore $opt LIMIT 0,3");
	while (list($playerscore, $nicedate, $username) = $db->sql_fetchrow($result)) {
		$a++;
		if ($playerscore){
			$score .= "<score rank='.$a.' score='.$playerscore.' player='.$username.' date='.$nicedate.' /><br />";
		}
	}

	$scorelist = "<scorelist>$score</scorelist>";
	$scorelist = urlencode($scorelist);
	echo "&amp;opSuccess=true&amp;gameScores=$scorelist&amp;endvar=1";
	$gameData = "&amp;opSuccess=true&amp;gameScores=$scorelist&amp;endvar=1";
	RETURN urlencode($gameData);
}
function gamesearchbox() {
    echo "<div align=\"center\"><form action=\"".getlink('DF_Arcade&amp;func=listgames')."\" method=\"post\" name=\"gamesearch\">"
	."<input type=\"text\" size=\"20\" name=\"gsearch\">&nbsp;&nbsp;"
	."<input type=\"submit\" value=\""._GAMESEARCH."\">"
	."</form>"
	."</div>";
}
function alphagames() {
    global $prefix, $db;
    	$result = $db->sql_query("SELECT count(*) FROM ".$prefix."_rgarcade_games");
    	list($totgames) = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
    	$result = $db->sql_query("SELECT count(*) FROM ".$prefix."_rgarcade_games WHERE gtype<'3'");
	list($scoregames) = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
    	$result = $db->sql_query("SELECT count(*) FROM ".$prefix."_rgarcade_cat");
    	list($cats) = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	echo "<div align=\"center\"><table cellpadding=\"4\">"
	."<tr><td align=\"center\"><font class=\"content\">"._SYSGAMES."</font></td><td align=\"center\"><table border=\"1\" cellpadding=\"3\"><tr><td align=\"center\"><font class=\"content\">$totgames&nbsp;(<b>$scoregames</b>)<br />$cats Categories</font></td></tr></table></td></tr></table></div><br />";
    $alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M",
                       "N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
    $num = count($alphabet) - 1;
    echo "<div align=\"center\">[ ";
    $counter = 0;
    while (list(, $ltr) = each($alphabet)) {
    		$result = $db->sql_query("SELECT count(*) FROM ".$prefix."_rgarcade_games WHERE UPPER(gname) LIKE '$ltr%'");
    		list($numrows) = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);
		if ($numrows > 0) {
	    		echo "<a href=\"".getlink('DF_Arcade&amp;func=listgames&ltr='.$ltr.'')."\"><font class=\"title\">$ltr</font></a>";
		} else {
	    		echo "$ltr";
		}
       		if ( $counter == round($num/2) ) {
            			echo " ]\n<br />\n[ ";
        		} elseif ( $counter != $num ) {
            			echo "&nbsp;|&nbsp;\n";
        		}
        		$counter++;
    }
    echo " ]</div><br />";
    gamesearchbox();
}
function listgames() {
    	global $prefix, $db;
    	$module_name = "DF_Arcade";
    	OpenTable();
    	navigator();
    	if (isset($_GET['ltr'])) { $ltr = Fix_Quotes($_GET['ltr']); }
    	if (isset($_POST['gsearch'])) { $gsearch = Fix_Quotes($_POST['gsearch']); }
	if ($ltr){
		$boxtitle = ""._FARCADELTR." - $ltr";
	} elseif ($gsearch) {
		$boxtitle = ""._FARCADESRCH." - $gsearch";
	} else {
		$boxtitle = _GALPHALISTING;
	}
	echo "<br /><div align=\"center\"><font class=\"title\">$boxtitle</font><br />"
	."<table border=\"1\" align=\"center\" cellpadding=\"5\" width=\"100%\">";
	if ($ltr){		
	    	$result = $db->sql_query("SELECT game_id, gcid, gtype, gname, gimage, hits FROM ".$prefix."_rgarcade_games WHERE UPPER(gname) LIKE '$ltr%'");
		$numrows = $db->sql_numrows($result);
		if ($numrows == 0) {
	    		echo "<tr><td align=\"center\"><font class=\"content\"><i>"._NGTHISLTR.": $ltr.</i></font></td></tr>";
		} else {
			$numrows++;
			echo "<tr><td valign=\"top\" width=\"50%\">";
			gheadstrip();
				$counter = 0;
				while (list($game_id, $gcid, $gtype, $gname, $gimage, $ghits) = $db->sql_fetchrow($result)) {
					if ($counter == round($numrows/2) ) {
            					echo "</table><br /></td><td valign=\"top\" width=\"50%\">";
						gheadstrip();
        				}
        				$counter++;
					$result2 = $db->sql_query("SELECT cname FROM ".$prefix."_rgarcade_cat WHERE cat_id='$gcid'");
					list($cat_name) = $db->sql_fetchrow($result2);
					$db->sql_freeresult($result2);
					if ($gtype == 1){
						$opt = "DESC";
						$type = _RGAHW;
					} elseif ($gtype == 2){
						$opt = "ASC";
						$type = _RGALW;
					} else {
						$opt = "";
						$type = _RGASLESS;
						}
					if ($gtype < 3){
						$result2 = $db->sql_query("SELECT playerscore FROM ".$prefix."_rgarcade_hiscores WHERE game_id='$game_id' ORDER BY playerscore $opt LIMIT 0,1");
						$numrows = $db->sql_numrows($result);
						if ($numrows == 0){
							$topscore = _RGANOSCO;
						} else {
						list($topscore) = $db->sql_fetchrow($result2);
						$db->sql_freeresult($result2);
						}
					} else {
						$topscore = _RGANOSCOING;
					}
					if ($gimage){
						$gshow = "<a href=\"".getlink(''.$module_name.'&amp;func=playgame&amp;game_id='.$game_id.'')."\"><img src=\"modules/DF_Arcade/screenshot/$gimage\" border=\"0\" alt=\"Play $gname\" width=\"32\" height=\"32\"><br />$gname</a>";
					} else {
						$gshow = "<a href=\"".getlink(''.$module_name.'&amp;func=playgame&amp;game_id='.$game_id.'')."\">"._RGANOIMG."<br /><br />$gname</a>";
						}
				echo "<tr>"
	    			."<td align=\"center\">$gshow</td>"
	    			."<td align=\"center\"><a href=\"".getlink(''.$module_name.'&amp;func=displaycat&amp;cat_id='.$gcid.'')."\">$cat_name</a></td>"
	    			."<td align=\"center\"><font class=\"content\">$type</font></td>"
	    			."<td align=\"center\"><font class=\"content\">$ghits</font></td>"
	    			."<td align=\"center\"><font class=\"content\">$topscore</font></td>"
	    			."</tr>";
			}
			echo "</table><br /></td></tr>";
		}
	} elseif ($gsearch){
		$result = $db->sql_query("SELECT game_id, gcid, gtype, gname, gimage, hits FROM ".$prefix."_rgarcade_games WHERE gname LIKE '%$gsearch%'");
	    	$numrows = $db->sql_numrows($result);
		if ($numrows == 0) {
        			echo "<tr><td align=\"center\"><font class=\"content\"><i>"._NGTHISSRCH."</i></font></td></tr>";
    		} else {
    			$num = $numrows + 1;
			echo "<tr><td valign=\"top\" width=\"50%\">";
			gheadstrip();
			$counter = 0;
    			while (list($game_id, $gcid, $gtype, $gname, $gimage, $ghits) = $db->sql_fetchrow($result)) {
        			$counter++;
				if ($counter == round($num/2) ) {
            				echo "</table><br /></td><td valign=\"top\" width=\"50%\">";
					gheadstrip();
        			}
				$result2 = $db->sql_query("SELECT cname FROM ".$prefix."_rgarcade_cat WHERE cat_id='$gcid'");
				list($cat_name) = $db->sql_fetchrow($result2);
				$db->sql_freeresult($result2);
				if ($gtype == 1){
					$opt = "DESC";
					$type = "Highest Wins";
				} elseif ($gtype == 2){
					$opt = "ASC";
					$type = "Lowest Wins";
				} else {
					$opt = "";
					$type = "Scoreless";
					}
				if ($gtype < 3){
					$result2 = $db->sql_query("SELECT playerscore FROM ".$prefix."_rgarcade_hiscores WHERE game_id='$game_id' ORDER BY playerscore $opt LIMIT 0,1");
					$numrows = $db->sql_numrows($result);
					if ($numrows == 0){
						$topscore = "<b>No Score</b>";
					} else {
						list($topscore) = $db->sql_fetchrow($result2);
						$db->sql_freeresult($result2);
					}
				} else {
					$topscore = "<i>No Scoring</i>";
				}
				if ($gimage){
					$gshow = "<a href=\"".getlink(''.$module_name.'&amp;func=playgame&amp;game_id='.$game_id.'')."\"><img src=\"modules/DF_Arcade/screenshot/$gimage\" border=\"0\" alt=\"Play $gname\" width=\"32\" height=\"32\"><br />$gname</a>";
				} else {
					$gshow = "<a href=\"".getlink(''.$module_name.'&amp;func=playgame&amp;game_id='.$game_id.'')."\">"._RGANOIMG."<br /><br />$gname</a>";
				}
				echo "<tr>"
	    			."<td align=\"center\">$gshow</td>"
	    			."<td align=\"center\"><a href=\"".getlink(''.$module_name.'&amp;func=displaycat&amp;cat_id='.$gcid.'')."\">$cat_name</a></td>"
	    			."<td align=\"center\"><font class=\"content\">$type</font></td>"
	    			."<td align=\"center\"><font class=\"content\">$ghits</font></td>"
	    			."<td align=\"center\"><font class=\"content\">$topscore</font></td>"
	    			."</tr>";
			}
			echo "</table><br /></td></tr>";
		}
	}
	echo "</table></div><br />";
	alphagames();
	CloseTable();
}
function gheadstrip(){
	global $bgcolor1;
	echo "<table width=\"100%\" cellpadding=\"3\">"
	."<tr bgcolor=\"$bgcolor1\">"
	."<td align=\"center\" nowrap=\"NOWRAP\"><font class=\"title\">"._RGAAGN."</font></td>"
	."<td align=\"center\" nowrap=\"NOWRAP\"><font class=\"content\">"._RGAAGC."</font></td>"
	."<td align=\"center\"><font class=\"content\">"._RGAAGTYPE."</font></td>"
	."<td align=\"center\"><font class=\"content\">"._RGAHITS."</font></td>"
	."<td align=\"center\"><font class=\"content\">"._TOPSCORE."</font></td></tr>";
}
?>