<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
        function show_gamelist(){
		global $bcoosConfig;
		$bcoosConfig['title'] .= " - "._MI_ARCADE_NAME;
                include(XOOPS_ROOT_PATH."/header.php");
                OpenTable();
                // Category Links
                _show_cat_links();
                // Latest Games
                _show_latest_games();
                CloseTable();
                include(XOOPS_ROOT_PATH."/footer.php");
        }
        function show_category($cat_id){
		global $bcoosConfig;
		$bcoosConfig['title'] .= " - "._MI_ARCADE_NAME;
                include(XOOPS_ROOT_PATH."/header.php");
                OpenTable();
                // Category Links
                _show_cat_links();
                // Games for this category
                _show_cat_games($cat_id);
                CloseTable();
                include(XOOPS_ROOT_PATH."/footer.php");
        }
        function play_game($gid)
        {
                global $db;
                include(XOOPS_ROOT_PATH."/header.php");
                OpenTable();
                $sql = "SELECT * from ".$db->prefix('arcade_games')." WHERE gid=$gid";
                $res = $db->query($sql);
                if($res && $db->getRowsNum($res) > 0)
                {
                        $game = $db->fetchArray($res);
                }
                // Get High Score
                $highscore = _get_game_highscore($gid);
                // Get PB
                $pbscore = _get_game_pbscore($gid);
?>
<table width='100%' border='0' cellspacing='1' cellpadding='4' align='center'>
  <tr>
  <td width='75%' align='center' valign='top'>
  <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0' width='<?php echo $game[gwidth] ?>' height='<?php echo $game[gheight]; ?>'>
  <param name='movie' value='cache/<?php echo $game[gname]; ?>.swf?tgame=0'>
  <param name='bgcolor' value='#<?php echo $game[bgcolor]; ?>'>
  <param name='quality' value='high'>
  <param name='menu' value='false'>
  <embed src='cache/<?php echo $game[gname]; ?>.swf?tgame=0' width='<?php echo $game[gwidth]; ?>' height='<?php echo $game[gheight]; ?>' bgcolor='#<?php echo $game[bgcolor]; ?>' quality='high' menu='false' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer/'></embed>
  <noembed>Sorry, you will need the <a href='http://www.macromedia.com/go/getflashplayer/' target='_blank'>Flash Player</a> to play <?php echo $game[gtitle]; ?>.</noembed>
</object></td>
  <td valign='top' align='left' width='25%'><div class='bg2'>
          <table width='100%' border='0' cellspacing='1' cellpadding='4'>
          <tr>
        <th width='100%' align='center' nowrap='nowrap' class='titlemedium'><?php echo $game[gtitle]; ?></th>
        </tr>
        <tr><td align='center' class='bg3'><img src='cache/images/<?php echo $game[gname]; ?>1.gif'></td></tr>
        <tr><td align='center' class='bg3'><?php echo $highscore[uname]; ?></td></tr>
        <tr><td align='center' class='bg3'>Score to Beat:<br><?php echo $highscore[score]; ?></td></tr>
        <tr><td align='center' class='bg1'><a href='index.php?act=Arcade'><?php echo _MD_BACK_TO_ARCADE; ?></a><br><a href='index.php?act=Arcade&do=stats&gameid=<?php echo $game[gid]; ?>'>View High Scores</a></td></tr>
        <tr><th width='100%' align='center' nowrap='nowrap' class='bg4'>The Object of <?php echo $game[gtitle]; ?></th></tr>
        <tr><td align='center' class='bg3'><?php echo $game[object]; ?></td></tr>
        <tr><th align='center' class='bg4'>The Keys</th></tr>
        <tr><td align='left' class='bg3'><?php echo $game[gkeys]; ?></td></tr>
        </table></div></td>
  </tr>
</table>
<?php
                CloseTable();
                include(XOOPS_ROOT_PATH."/footer.php");
        }
        function submit_newscore()
        {
                global $db, $_POST, $xoopsUser;
                include(XOOPS_ROOT_PATH."/header.php");
                OpenTable();
                                echo "<center><h3>"._MD_YOUR_SCORE.": ".$_POST[gscore]."</h3></center>";
                // find the game based on gname to get the gid
                $sql = "SELECT * from ".$db->prefix('arcade_games')." WHERE gname='".$_POST['gname']."'";
                $res = $db->query($sql);
                if($res && $db->getRowsNum($res) > 0)
                {
                        $game = $db->fetch_object($res);
                                                if($xoopsUser)
                                                {
                                                        // Look for a score by this user for this game
                                                        $s = "SELECT score from ".$db->prefix('arcade_scores')." WHERE gid=".$game->gid." AND uid=".$xoopsUser->uid();
                                                        $r = $db->query($s);
                                                        if($r && $db->getRowsNum($r) == 1)
                                                        {
                                                                        $score_row = $db->fetchArray($r);
                                                                        if($_POST[gscore] > $score_row[0])
                                                                        {
                                                                                        // Update score to new highscore
                                                                                        $s = "UPDATE ".$db->prefix('arcade_scores')." SET score=".$_POST[gscore]." , score_date=".time()." WHERE gid=".$game->gid." AND uid=".$xoopsUser->uid();
                                                                                        $r = $db->query($s);
                                                                                        echo "<div align=center><img src='cache/images/trophy.gif'>&nbsp;<font size='+1' color=red>"._MD_PB_SCORE."</font><br /></div>";
                                                                        }
                                                        }
                                                        else
                                                        {
                                                                        // insert a new score
                                                                        $s = "INSERT INTO ".$db->prefix('arcade_scores')." SET score=".$_POST[gscore]." , gid=".$game->gid." , uid=".$xoopsUser->uid().", score_date=".time();
                                                                        $r = $db->query($s);
                                                        }
                                                }
                                                else
                                                {
                                                        echo _MD_REG_TO_SAVE_SCORE."<br/>";
                                                }
                        // Increment play count
                        $s = "UPDATE ".$db->prefix('arcade_games')." SET gcount=gcount+1 WHERE gid=".$game->gid;
                        $r = $db->query($s);
                        $highscore = _get_game_highscore($game->gid);
                        if($_POST[gscore] == $highscore['score'])
                        {
                                echo "<div align=center><img src='cache/images/trophy.gif'>&nbsp;<font size='+1' color=red>"._MD_HIGH_SCORE."</font><br /></div>";
                        }
                                        echo '<br /><br />';
                                        _display_gamestats($game->gid);
                }
                CloseTable();
                include(XOOPS_ROOT_PATH."/footer.php");
        }
        function show_gamestats($gid)
        {
                global $db, $_POST, $xoopsUser,$bcoosConfig;
				
				$result = $db->query("SELECT gname FROM ".$db->prefix("arcade_games")." WHERE gid=".intval($gid));
				list($gname) = $db->fetchRow($result);
				$bcoosConfig['title'] = _MI_ARCADE_NAME." - ".$gname;
                include(XOOPS_ROOT_PATH."/header.php");
                OpenTable();
                                _display_gamestats($gid);
                CloseTable();
                include(XOOPS_ROOT_PATH."/footer.php");
        }
        function _show_cat_links()
        {
                global $db;
                $sql1 = "SELECT * FROM ".$db->prefix('arcade_cats')." WHERE is_active=1";
                $res1 = $db->query($sql1);
                echo '<table width="100%" cellpadding=0 cellspacing=0><tr><td class="bg2">';
                echo '<table width="100%" cellpadding=1 cellspacing=1><tr><td class="bg4"><b>'._MD_GAME_CATEGORIES.'</b></td></tr>';
                echo '<tr><td  class="bg3">';
                while ($row = $db->fetch_object($res1))
                {
                        $sql2 = "SELECT count(*) from ".$db->prefix('arcade_games')." WHERE active=1 AND cat_id=".$row->cat_id;
                        $res2 = $db->query($sql2);
                        list($count) = $db->fetchArray($res2);
                        echo "<a href='index.php?act=show_cat&cat_id=".$row->cat_id."'><b>".$row->cat_name."&nbsp($count)</b></a>&nbsp;&nbsp;";
                }
                echo '</td></tr>';
                echo '</table>';
                echo '</td></tr></table><br />';
        }
        function _show_cat_games($cat_id)
        {
                global $db, $_GET, $options;
                $sql = "SELECT * FROM ".$db->prefix('arcade_cats')." WHERE cat_id=$cat_id";
                $res = $db->query($sql);
                $catrow = $db->fetch_object($res);
                echo '<p><b>'.$catrow->cat_name.':</b></p>';
                $sql1 = "SELECT count(*) from ".$db->prefix('arcade_games')." WHERE cat_id=$cat_id AND active=1 order by gtitle";
                $res1 = $db->query($sql1);
                                list($total_games) = $db->fetchArray($res1);
                                $pager = new PageNav($total_games, $options['games_per_page'], $_GET['start'], "start", "act=show_cat&cat_id=$cat_id");
                                echo "<div align=right>".$pager->renderNav()."</div><br />";
                $sql2 = "SELECT * from ".$db->prefix('arcade_games')." WHERE cat_id=$cat_id AND active=1 order by gtitle";
                $res2 = $db->query($sql2, $options['games_per_page'], $_GET['start']);
                while ($row = $db->fetch_object($res2))
                {
                        _display_game_info($row);
                }
                                echo "<div align=right>".$pager->renderNav()."</div>";
        }
        function _show_latest_games()
        {
                global $db, $options;
                $sql = "SELECT * from ".$db->prefix('arcade_games')." WHERE active=1 order by date_added DESC";
                $res = $db->query($sql, $options['games_latest']);
                echo '<p><b>Latest '.$options['games_latest'].' Games:</b></p>';
                while ($row = $db->fetch_object($res))
                {
                        _display_game_info($row);
                }
        }
        function _display_game_info($game_row)
        {
                // Get High Score
                $highscore = _get_game_highscore($game_row->gid);
                // Get PB
                $pbscore = _get_game_pbscore($game_row->gid);
?>
        <table width="100%" cellpadding=0 cellspacing=0><tr><td class="bg2">
        <table cellpadding='1' cellspacing='1' border='0' align='center' width='100%'>
                <tr>
                        <td class="bg3"><b><?php echo $game_row->gtitle; ?></b></td>
                </tr>
                <tr>
                <td width='100%' td class="bg1">
                        <table cellpadding='2' cellspacing='1' border='0' width='100%'>
                                <tr>
                                <td width='10%'>
                                <img src='cache/images/<?php echo $game_row->gname; ?>1.gif' border='0' align='top'>
                                </td>
                                <td width='35%' align='center'>
                                        <?php echo $game_row->gwords; ?>
                                </td>
                                <td width='30%' valign='middle'>
                                        <ul>
                                                <li><a href='index.php?act=play_game&gid=<?php echo $game_row->gid; ?>'><?php echo _MD_PLAY; ?></a></li>
                                                <li><a href='index.php?act=show_stats&gid=<?php echo $game_row->gid; ?>'><?php echo _MD_VIEW_HIGHSCORES; ?></a></li>
                                                <li><?php echo _MD_YOUR_HIGHSCORE; ?> <b><?php echo $pbscore; ?></b></li>
                                                <li><?php echo _MD_TIMES_PLAYED; ?> <?php echo $game_row->gcount; ?></li>
                                        </ul>
                                </td>
                                <td width='25%' align='center'>
                                        <img src='cache/images/crown.gif' border='0'><br>
                                        <b><?php echo $highscore['uname']; ?></b>
                                        <br><?php echo _MD_KING_OF; ?> <?php echo $game_row->gtitle; ?><br>
                                        <?php echo _MD_WITH_SCORE; ?>
                                        <br><b><?php echo $highscore['score']; ?></b>
                                </td>
                                </tr>
                        </table>
                </td>
                </tr>
        </table>
        </td></tr></table><br />
<?php
        }
        function _get_game_highscore($gid)
        {
                global $db;
                $highscore['score'] = 0;
                $highscore['uname'] = '???';
                $sql = "SELECT uid, score from ".$db->prefix('arcade_scores')." where gid=$gid ORDER BY score DESC";
                $res = $db->query($sql);
                if($res && $db->getRowsNum($res) > 0)
                {
                        $row = $db->fetchArray($res);
                        $highscore['uname'] = _uname_link($row[0]);
                        $highscore['score'] = $row[1];
                }
                return $highscore;
        }
        function _get_game_pbscore($gid)
        {
                global $db, $xoopsUser;
                $pbscore = 0;
                if($xoopsUser)
                {
                        $sql = "SELECT max(score) from ".$db->prefix('arcade_scores')." where gid=$gid AND uid=".$xoopsUser->uid();
                        $res = $db->query($sql);
                        if($res && $db->getRowsNum($res) > 0)
                        {
                                $row = $db->fetchArray($res);
                                $pbscore = $row[0];
                        }
                }
                return $pbscore;
        }
        function _uname_link($uid)
        {
                global $bcoosConfig;
                $link = '';
                if ($uid == 0)
                {
                        $link .= $bcoosConfig['anonymous'];
                }
                else
                {
                        $uname = XoopsUser::getUnameFromId($uid);
                        $link .= "<a href='".XOOPS_URL."/userinfo.php?uid=$uid'>$uname</a>";
                }
                return $link;
        }
                function _display_gamestats($gid)
        {
                global $db, $_POST, $xoopsUser;
                                $sql = "SELECT gtitle FROM ".$db->prefix('arcade_games')." WHERE gid=$gid";
                                $res = $db->query($sql);
                                list($gtitle) = $db->fetchArray($res);
                                $sql = "SELECT * FROM ".$db->prefix('arcade_scores')." WHERE gid=$gid ORDER BY score DESC";
                                $res = $db->query($sql, 10);
                                $iRow = 1;
                            echo "<table width='100%' cellpadding=0 cellspacing=0><tr><td class='bg2'>";
                        echo "<table cellpadding='1' cellspacing='1' border='0' align='center' width='100%'>";
                                echo "<tr class=bg4><td colspan=4 align=center><font size='+1'><b>".$gtitle." "._MD_LEADERBOARD."</b></font></td></tr>";
                                echo "<tr class=bg2><td></td><td><b>"._MD_SCORE."</b></td><td><b>"._USER."</b></td><td><b>"._DATE."</b></td></tr>";
                                while($row = $db->fetch_object($res))
                                {
                                        echo "<tr><td class=bg3>$iRow</td><td class=bg1>".$row->score."</td><td class=bg3>"._uname_link($row->uid)."</td><td class=bg1>".formatTimestamp($row->score_date, 'm')."</td></tr>";
                                        $iRow ++;
                                }
                                echo "</table>";
                                echo "</td></tr></table><br />";
                echo "<div align=center><b><a href='index.php'>"._MD_BACK_TO_ARCADE." </a> | <a href='index.php?act=play_game&gid=$gid' >"._MD_PLAY_AGAIN."</a><b></div>";
        }
?>