Daily Lucky Numbers:
9
16
18
41
43
46

Random Games Block

Started by Chen Zhen, July 06, 2013, 02:57:31 PM

Previous topic - Next topic

Chen Zhen

SMF Arcade 2.7.0.1 or older

Random games block (icons only):

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:
// adjustable variables
list($evenRows, $guestMax, $userMax, $icons_per_row, $arcade) = array(true, 60, 28, 4, '');

global $scripturl, $modSettings, $smcFunc, $user_info, $settings;
$check_game = isset($_REQUEST['game']) ? floatval($_REQUEST['game']) : 0;
$path = str_replace('\\', '/', $modSettings['gamesDirectory']);
$filepath = rtrim($path, '/');
$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($modSettings['arcadePermissionMode']) ? 1 : $modSettings['arcadePermissionMode']), 0);


// 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 ($check_game == false)
{
    $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,
        )
    );
    $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, 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,
            )
        );

        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            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) ? $modSettings['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=arcade;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, 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,
            )
        );

        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            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) ? $modSettings['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=arcade;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);
    }

    echo $arcade;
}

Legionaire

When using this code with SMF 2.1 RC1

the following error occurs

Type of error: General
2: mysqli_free_result(): Couldn't fetch mysqli_result https://www.3dogsarcade.com/smf21r/index.php?
/home/legion/public_html/smf21r/Sources/PortalBlocks.php(3683) : eval()'d code (Line 70)

Legionaire

EDIT:

The block works fine, but the error occurs everytime a page is changed from the portal to any other page, and the error log in the ACP begins to fill quickly with this same error.  I am guessing it is an easy fix, but my PHP skills are not that good to fix it

The latest game block code works without error

Hero

I have these mistakes too.

/www/htdocs/xxxxxx/xxxxxxxx/Sources/PortalBlocks.php(3693): eval () 'd code (line 70)

Chen Zhen


This block was made a long time ago.
It has been fixed with the changeable options in the initial commented array.

Let me know if it works as expected.

Hero


Legionaire

Fantastic Chen, works a treat