Game Block

Started by Chen Zhen, September 13, 2024, 11:19:55 PM

Previous topic - Next topic

Chen Zhen

Play a game in a PHP portal block
Version: SMF-Arcade v2.7.0.2+



PHP Block Code -
$gameid: represents the game ID (elect a game id number or 0 for random)
$minimum_height: represents a minimum value if a game height is not found in the db
$container_class: css class background (around the game)
$randomReplay: boolean ~ play button selects random game (true) or same game (false)

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

set $included_game_types ... ROM games are generalized | empty array for all | $gameTypes = 2 automatically adds rom to this list | all game types shown below for reference
array('v1game', 'v2game', 'v3arcade', 'phpbb', 'mochi', 'custom_game', 'ibp', 'ibp2', 'ibp3', 'ibp32', 'html5', 'html52', 'html53', 'rom')
**************************/
$gameTypes = 0;
global $scripturl, $boardurl, $txt, $smcFunc, $arcadeModSettings;

/* Change $gameid to a specific game id (0 for random) ~ other options are available  */
$included_game_types = array('html5', 'html52', 'html53', 'rom');
$minimum_height = '700';
$minimum_width = '400';
$container_class = 'windowbg';
$gameid = 0;
$randomReplay = true;
switch($gameTypes) {
case 1:
$where = ' AND rom_flag = {int:romflag}';
$romflag = 0;
break;
case 2:
$where = ' AND rom_flag = {int:romflag}';
$romflag = 1;
$included_game_types[] = 'rom';
break;
case 3:
$where = '';
$romflag = 0;
break;
default:
$romflag = 0;
if (allowedTo('arcade_view_retro_arch')) {
$where = '';
}
else {
$where = ' AND rom_flag = {int:romflag}';
}
}
list($countz, $gameData, $order) = array(array(), array(), ($gameid == 0 ? 'RAND()' : 'id_game ASC'));
$request = $smcFunc['db_query']('', '
SELECT id_game, extra_data, submit_system, rom_flag, rom_system
FROM {db_prefix}arcade_games
WHERE id_game > {int:idgame} AND enabled = {int:enabled}' . $where . '
ORDER BY ' . $order,
array(
'idgame' => 0,
'enabled' => 1,
'romflag' => $romflag,
)
);

while ($row = $smcFunc['db_fetch_assoc']($request)) {
if (!empty($included_game_types) && $gameTypes != 2 && !in_array($row['submit_system'], $included_game_types))
continue;

if (empty($gameTypes)) {
if ($row['submit_system'] == 'rom' && !allowedTo('arcade_view_retro_arch'))
continue;
elseif (!allowedTo('arcade_play'))
continue;
}
$countz[] = $row['id_game'];
$extraData = unserialize($row['extra_data']);
$extraData['height'] = !empty($extraData['height']) ? preg_replace('/[^0-9]/', '', $extraData['height']) : $minimum_height;
$extraData['width'] = !empty($extraData['width']) ? preg_replace('/[^0-9]/', '', $extraData['width']) : $minimum_width;
$gameData[$row['id_game']] = array('width' => preg_replace('/[^0-9]/', '', $extraData['width']), 'height' => $extraData['height'], 'action' => (!empty($row['rom_flag']) ? 'retro_arch' : 'arcade'), 'rom_system' => (!empty($row['rom_system']) ? $row['rom_system'] : ''));
}
$smcFunc['db_free_result']($request);

$arcadeAllGamesArray = json_encode($countz);
$arcadeAllGamesDimsArray = json_encode($gameData);
$randGameId = array_rand($countz);
$requestGameId = isset($_REQUEST['game']) ? abs((int)$_REQUEST['game']) : abs($gameid);
$ranum = !empty($requestGameId) && in_array($requestGameId, $countz) ? $requestGameId : $countz[$randGameId];
$id = 'x_gameblock' . $gameid . $ranum . '_x';
$romCheck = !empty($gameData[$ranum]['action']) && $gameData[$ranum]['action'] == 'retro_arch' ? 1 : 0;
echo  '
<script type="text/javascript">
var arcadeAllGamesArray = ' . $arcadeAllGamesArray . ';
var arcadeAllGamesDimsArray = ' . $arcadeAllGamesDimsArray . ';
function arcadeResizeIframeBlock(idgamex) {
var arcadeIframBlockId = document.getElementById("' . $id . '");
var arcadeIframBlockContId = document.getElementById("arcCont' . $id . '");
arcadeIframBlockContId.style.overflow = "hidden";
arcadeIframBlockContId.style.height = parseInt(arcadeAllGamesDimsArray[idgamex]["height"]) + "px";
arcadeIframBlockContId.style.minHeight = parseInt(arcadeAllGamesDimsArray[idgamex]["height"]) + "px";
arcadeIframBlockContId.style.maxHeight = parseInt(arcadeAllGamesDimsArray[idgamex]["height"]) + "px";
arcadeIframBlockId.style.overflow = "hidden";
arcadeIframBlockId.style.overflow = "hidden";
arcadeIframBlockId.contentDocument.getElementsByTagName("BODY")[0].style = "overflow: hidden;border: 0px;background: none;";
arcadeIframBlockId.style.visibility = "visible";
}
function arcadeBlockReloadIframeGame() {
var arcadeBlockScrollHeight = document.getElementsByTagName("BODY")[0].scrollHeight;
document.getElementsByTagName("BODY")[0].style.display = "flex";
document.getElementsByTagName("BODY")[0].style.justifyContent = "flex-start";
document.getElementsByTagName("BODY")[0].style.minHeight = arcadeBlockScrollHeight + "px";
document.getElementsByTagName("BODY")[0].style.overflowY = "auto";
document.getElementsByTagName("BODY")[0].style.flex = "1";
document.getElementsByTagName("BODY")[0].style.flexFlow = "column nowrap";
var arcadeIframBlockId = document.getElementById("' . $id . '");
var newRandomArcadeGame = Math.floor(Math.random() * arcadeAllGamesArray.length);
var arcadeBlockRandomReplay = ' . (!empty($randomReplay) ? '1' : '0') . ' == 1 ? arcadeAllGamesArray[newRandomArcadeGame] : ' . $ranum . ';
arcadeIframBlockId.style.visibility = "hidden";
$("#' . $id . '").attr("src", "' . $scripturl . '?action="+arcadeAllGamesDimsArray[arcadeBlockRandomReplay]["action"]+";sa=play;pop=1;full=1;game=" + arcadeBlockRandomReplay);
arcadeResizeIframeBlock(arcadeAllGamesArray[newRandomArcadeGame]);
}
$(document).ready(function() {
arcadeBlockReloadIframeGame();
});
</script>
<div style="clear: both;margin: 0 auto;width: 100%;overflow: hidden;">
<div style="margin: 0 auto;text-align: center;">
<img onclick="arcadeBlockReloadIframeGame()" src="' . $boardurl . '/Themes/default/images/arc_icons/popup_play_btn.gif" alt="" />
</div>
</div>
<div class="' . $container_class . '" id="arcCont' . $id . '" style="clear: both;margin: 0 auto;display: flex;flex-wrap: wrap;flex-direction: column;height: ' . $gameData[$ranum]['height'] . 'px;min-height: ' . $gameData[$ranum]['height'] . 'px;overflow: hidden;">
<iframe allowtransparency="true" id="' . $id . '" name="' . $id . '" src="#" style="clear: both;border: 0px;flex-grow: 1;overflow: hidden;visibility: hidden;"></iframe>
</div>';