Skip to content

Instantly share code, notes, and snippets.

@miniplay
Last active April 28, 2022 20:02
Show Gist options
  • Save miniplay/5003037 to your computer and use it in GitHub Desktop.
Save miniplay/5003037 to your computer and use it in GitHub Desktop.
Miniplay API - Basic usage of the Javascript API to handle the current logged user (Miniplay connect functionality is automatically used for external games that have permissions).
<?php
/* 1. Game api_key =================================================================================================== */
if (!empty($_GET['mp_game_devel']) && !!$_GET['mp_game_devel']) {
define("API_KEY","## YOUR_SECURE_DEVELOPMENT_API_KEY ##"); /* Set this to the development api_key we've provided you. DO NOT DISPLAY IT!!! */
} else {
define("API_KEY","## YOUR_SECURE_PRODUCTION_API_KEY ##"); /* Set this to the production api_key we've provided you. DO NOT DISPLAY IT!!! */
}
/* 2. Parameters decoding ============================================================================================ */
// Only if your system configuration or language doesn't automatically decodes it
// foreach ($_GET as $key => $value) $_GET[$key] = urldecode($value);
/* 3. Parameters validation (mp_*) =================================================================================== */
/* This section must be removed if your game is external (won't run inside our sites and mini_signature will be empty) */
/* 3.1. Check signature */
if (!isset($_GET['mini_signature']) || $_GET['mini_signature'] != mpSignParameters(API_KEY,$_GET)) {
/* Invalid signature. Application dies now */
die("The 'mini_signature' is invalid.");
}
/* 3.2. Helper function definition, yeah, it will look so good inside a class :) */
/**
* Signs an array of mp_* parameters by concatenating the values to the api key
* @param string $apiKey
* @param array $parameters
* @return string signature
*/
function mpSignParameters($apiKey, array $parameters) {
ksort($parameters); /* Sort array alphabetically by its keys (Although they should be already sorted by key) */
$signatureString = "";
foreach ($parameters as $key=>$value) {
if (substr($key,0,3)==="mp_") $signatureString.=(string)$value;
}
return md5($apiKey.$signatureString);
}
/* 4. Known received parameters from the Miniplay sandbox with their default counterparts ============================ */
/* If your game is external, you'll only receive mp_user_id & mp_user_token GET parameters, the rest won't exist */
$game_id = !empty($_GET['mp_game_id']) ? $_GET['mp_game_id'] : null;
$game_uid = !empty($_GET['mp_game_uid']) ? $_GET['mp_game_uid'] : null;
$game_devel = !empty($_GET['mp_game_devel']) ? !!$_GET['mp_game_devel'] : false;
$game_url = !empty($_GET['mp_game_url']) ? $_GET['mp_game_url'] : null;
$site_url = !empty($_GET['mp_site_url']) ? ($_GET['mp_site_url']) : "http://www.miniplay.com/";
$api_id = !empty($_GET['mp_api_id']) ? $_GET['mp_api_id'] : null;
$api_js_url = !empty($_GET['mp_api_js_url']) ? ($_GET['mp_api_js_url']) : null; // Url of the javascript api to use
$api_js_url_bck = !empty($_GET['mp_api_js_url_bck']) ? ($_GET['mp_api_js_url_bck']) : null; // Url for js api connections
$api_as3_url = !empty($_GET['mp_api_as3_url']) ? ($_GET['mp_api_as3_url']) : null; // Url of the as3 api to use
$api_as3_url_bck = !empty($_GET['mp_api_as3_url_bck']) ? ($_GET['mp_api_as3_url_bck']) : null; // Url for as3 api connections
$api_user_id = !empty($_GET['mp_api_user_id']) ? $_GET['mp_api_user_id'] : null;
$api_user_token = !empty($_GET['mp_api_user_token']) ? $_GET['mp_api_user_token'] : null;
$locale = !empty($_GET['mp_locale']) ? $_GET['mp_locale'] : "en_US";
$timezone = !empty($_GET['mp_timezone']) ? $_GET['mp_timezone'] : "GMT";
?>
<html>
<head>
<!-- ## YOUR OWN HEAD ## -->
<?php if ($api_js_url && $api_id):?>
<!-- Load MINIPLAY JS API Synchronously! -->
<script id='LeChuckAPIjs' src="<?php echo $api_js_url;?>"></script>
<script type='text/javascript'>
lechuck = new LeChuckAPI({
<?php if($game_devel) echo "debug: true,";?>
id: "<?php echo $api_id?>",
site: "<?php echo $site_url?>",
user_id: "<?php echo $api_user_id?>",
user_token: "<?php echo $api_user_token?>",
locale: "<?php echo $locale?>",
url: "<?php echo $api_js_url_bck?>"
});
</script>
<?php endif;?>
</head>
<body style='background-color:#fff'>
<h1>Miniplay JS API: A very basic demo showing the initialization & the guest user handling</h1>
<hr/>
<div id='status-msg'></div>
<div id='game' style='display:none'>
<p>
## Your game content for registered users (or it can be a dynamically attached iframe!, it will have access to the API trough the "parent" object) ##
</p>
<p>
Logged-in user: <span id='game-user'></span>
</p>
</div>
<div id='game-guest' style='display:none'>
<p>
Sorry, only for registered users!. <a href='' onclick='lechuck.user.login();return false;'>Login or register!</a>
</p>
</div>
<!-- GUEST USER HANDLER -->
<script type='text/javascript'>
var status_msg = document.getElementById('status-msg');
var game = document.getElementById('game');
var game_guest = document.getElementById('game-guest');
// Check if the API instance exists
if (lechuck instanceof Object) {
// Attach an onReady event listener to be dispatched once the api is ready!
// if the api is ready it will be automatically dispatched
lechuck.events.onApiReady(function() {
if(lechuck.user.isGuest()) {
game_guest.style.display="block"; // Show guest message
} else {
game.style.display="block"; // Show game! (or load it or insert an iframe to it...)
document.getElementById('game-user').innerHTML = "#" + lechuck.user.getId() + " " +
lechuck.user.getUid() + " <img src='"+lechuck.user.getAvatarMini()+"'>";
}
status_msg.style.display="none"; // Hide status message
});
// Initializing api...
status_msg.innerHTML = "Loading...";
} else {
status_msg.innerHTML = "API not loaded :(";
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment