Random Games Block

Started by Chen Zhen, September 10, 2024, 12:59:49 AM

Previous topic - Next topic

Chen Zhen

Random games block (icons only)

Arcade version: SMF Arcade 2.7.0.2 or newer



This one is set up only to appear when the user is not playing a game.
There are 2 limit settings... one for users and one for guests. You can set both as the same if you wish.

Create a new php block...

Enter this for custom display options:
(For viewing on portal - leave this line blank)
~action|arcade

$evenRows = [BOOLEAN] option to trim uneven rows
$guestMax = [INT] to equal the amount preferred
$userMax = [INT] to equal the amount preferred
$icons_per_row = [INT] to equal the amount preferred

Main code:
/**************************
    set $gameTypes
    0 = Permission based
    1 = Flash/HTML
    2 = ROM only
    3 = All game types
**************************/

// adjustable variables
list($evenRows, $guestMax, $userMax, $icons_per_row, $arcade, $gameTypes) = array(true, 60, 28, 4, '', 0);

global $scripturl, $arcadeModSettings, $smcFunc, $user_info, $settings;
$check_game = isset($_REQUEST['game']) ? floatval($_REQUEST['game']) : 0;
$defaultIcon = file_exists($settings['default_theme_dir'] . '/images/arc_icons/game.gif') ? $settings['default_images_url'] . '/arc_icons/game.gif' : '';
list($arcade, $arcadePermissionMode, $count1) = array('', (!isset($arcadeModSettings['arcadePermissionMode']) ? 1 : $arcadeModSettings['arcadePermissionMode']), 0);

switch($gameTypes) {
    case 1:
        $whereRom = ' AND game.rom_flag = {int:romflag}';
        $romflag = 0;
        break;
    case 2:
        $whereRom = ' AND game.rom_flag = {int:romflag}';
        $romflag = 1;
        break;
    case 3:
        $whereRom = '';
        $romflag = 0;
        break;
    default:
        $romflag = 0;
        if (allowedTo('arcade_view_retro_arch')) {
            $whereRom = '';
        }
        else {
            $whereRom = ' AND game.rom_flag = {int:romflag}';
        }
}

// check permissions
if (allowedTo('arcade_admin'))
{
    $see_game = '1=1';
    $see_category = '1=1';
}
else
{
    if ($arcadePermissionMode >= 2)
    {
        if ($user_info['is_guest'])
            $see_game = '(game.id_cat = 0 AND ' . (allowedTo('arcade_view') ? 1 : 0) . ' = 1) OR (game.local_permissions = 0 OR FIND_IN_SET(-1, game.member_groups))';
        else
            $see_game = '(game.local_permissions = 0 OR (FIND_IN_SET(' . implode(', game.member_groups) OR FIND_IN_SET(', $user_info['groups']) . ', game.member_groups)))';
    }

    if ($arcadePermissionMode == 1 || $arcadePermissionMode >= 3)
    {
        if ($user_info['is_guest'])
            $see_category = '(game.id_cat = 0 AND ' . (allowedTo('arcade_view') ? 1 : 0) . ' = 1) OR (FIND_IN_SET(-1, category.member_groups))';
        else
            $see_category = '(FIND_IN_SET(' . implode(', category.member_groups) OR FIND_IN_SET(', $user_info['groups']) . ', category.member_groups) OR ISNULL(category.member_groups))';
    }
}

if (empty($arcadePermissionMode))
    $where = 'enabled = 1';
elseif ($arcadePermissionMode == 1)
    $where = '(enabled = 1 AND ' . $see_category . ')';
elseif ($arcadePermissionMode == 2)
    $where = '(enabled = 1 AND ' . $see_game . ')';
elseif ($arcadePermissionMode == 3)
    $where = '(enabled = 1 AND (' . $see_category . ' AND ' . $see_game . '))';
elseif ($arcadePermissionMode == 4)
    $where = '(enabled = 1 AND (' . $see_category . ' OR ' . $see_game. '))';

$where = !empty($where) ? $where : '1=1';


if (empty($check_game))
{
    $arcade = '
<div style="overflow: hidden;">
    <div style="display: table;width: 100%;margin: 0 auto;padding: 2px;overflow: hidden;">
        <div style="display: table-row;">';
    $id_game = false;
    $result =  $smcFunc['db_query']('', '
        SELECT COUNT(*) as total
        FROM {db_prefix}arcade_games as game
        LEFT JOIN {db_prefix}arcade_categories AS category ON (category.id_cat = game.id_cat)
        WHERE {raw:query_see_game}',
        array(
            'query_see_game' => $where . $whereRom, 'romflag' => $romflag,
        )
    );
    $totalView = $smcFunc['db_fetch_assoc']($result);
    $smcFunc['db_free_result']($result);

    $totalPermitted = !empty($totalView['total']) ? $totalView['total'] : 0;
    $guestMax = $totalPermitted >= $guestMax ? (int)$guestMax : (int)$totalPermitted;
    $userMax = $totalPermitted >= $userMax ? (int)$userMax : (int)$totalPermitted;
    $guestMax = $evenRows ? $guestMax - ($guestMax % $icons_per_row) : $guestMax;
    $userMax = $evenRows ? $userMax - ($userMax % $icons_per_row) : $userMax;

    if (empty($user_info['is_guest']) && !empty($totalPermitted))
    {
        $request = $smcFunc['db_query']('', '
            SELECT game.id_game, game.game_name, game.game_directory, game.thumbnail, game.id_cat, game.rom_flag, category.member_groups
            FROM {db_prefix}arcade_games AS game
            LEFT JOIN {db_prefix}arcade_categories AS category ON (category.id_cat = game.id_cat)
            WHERE {raw:query_see_game}
            ORDER BY RAND()
            LIMIT ' . $userMax,
            array(
                'query_see_game' => $where . $whereRom, 'romflag' => $romflag,
            )
        );

        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            $path = str_replace('\\', '/', !empty($row['rom_flag']) ? $arcadeModSettings['romGamesDirectory'] : $arcadeModSettings['gamesDirectory']);
            $filepath = rtrim($path, '/');
            $gamesUrl = !empty($row['rom_flag']) ? $arcadeModSettings['romGamesUrl'] : $arcadeModSettings['gamesUrl'];
            $action = !empty($row['rom_flag']) ? 'retro_arch' : 'arcade';
            if ($count1 % $icons_per_row == 0)
                $arcade .= '
    </div>
    <div style="display: table-row;">';


            $thumbnailFile = '';

            if (!empty($row['thumbnail']))
            {
                $thumbnailFile = $filepath . '/' . (!empty($row['game_directory']) ? $row['game_directory'] . '/' : '') . $row['thumbnail'];
                $thumbnailPath = file_exists($thumbnailFile) ? $gamesUrl . '/' . (!empty($row['game_directory']) ? $row['game_directory'] . '/' : '') . $row['thumbnail'] : '';
            }
            elseif (!empty($defaultIcon))
                $thumbnailPath = $defaultIcon;

            $count1++;

            if (empty($thumbnailFile))
                continue;

            $arcade .= '
            <div class="centertext" style="display: table-cell;borser-spacing: 1px;">
                <a href="' . $scripturl . '?action=' . $action . ';sa=play;game=' . $row['id_game'] . '">
                    <img style="width:42px; height:42px;" src="' . $thumbnailPath . '" alt="'.$row['game_name'].'" title="'.$row['game_name'].'" />
                </a>
            </div>';

        }
        $smcFunc['db_free_result']($request);

        $arcade .= '
        </div>
    </div>
</div>';
    }
    elseif (!empty($totalPermitted))
    {
        $request = $smcFunc['db_query']('', '
            SELECT game.id_game, game.game_name, game.game_directory, game.thumbnail, game.id_cat, game.rom_flag, category.member_groups
            FROM {db_prefix}arcade_games AS game
            LEFT JOIN {db_prefix}arcade_categories AS category ON (category.id_cat = game.id_cat)
            WHERE {raw:query_see_game}
            ORDER BY RAND()
            LIMIT ' . $guestMax,
            array(
                'query_see_game' => $where . $whereRom, 'romflag' => $romflag,
            )
        );

        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            $path = str_replace('\\', '/', !empty($row['rom_flag']) ? $arcadeModSettings['romGamesDirectory'] : $arcadeModSettings['gamesDirectory']);
            $filepath = rtrim($path, '/');
            $gamesUrl = !empty($row['rom_flag']) ? $arcadeModSettings['romGamesUrl'] : $arcadeModSettings['gamesUrl'];
            $action = !empty($row['rom_flag']) ? 'retro_arch' : 'arcade';
            if ($count1 % $icons_per_row == 0)
                $arcade .= '
        </div>
        <div style="display: table-row;">';


            $thumbnailFile = '';

            if (!empty($row['thumbnail']))
            {
                $thumbnailFile = $filepath . '/' . (!empty($row['game_directory']) ? $row['game_directory'] . '/' : '') . $row['thumbnail'];
                $thumbnailPath = file_exists($thumbnailFile) ? $gamesUrl . '/' . (!empty($row['game_directory']) ? $row['game_directory'] . '/' : '') . $row['thumbnail'] : '';
            }
            elseif (!empty($defaultIcon))
                $thumbnailPath = $defaultIcon;

            $count1++;

            if (empty($thumbnailFile))
                continue;

            $arcade .= '
                <div class="centertext" style="display: table-cell;border-spacing: 1px;">
                    <a href="' . $scripturl . '?action=' . $action . ';sa=play;game=' . $row['id_game'] . '">
                        <img style="width:42px; height:42px;" src="' . $thumbnailPath . '" alt="'.$row['game_name'].'" title="'.$row['game_name'].'" />
                    </a>
                </div>';
        }

        $arcade .= '
        </div>
    </div>
</div>';

        $smcFunc['db_free_result']($request);
    }

    if (!allowedTo('arcade_view') && $gameTypes == 0) {
        $arcade = '';
    }
   
    echo $arcade;
}