High Scores module
This is a Zend AMF endpoint I created to receive postback from a client's existing Flash game and convert the received info into an account-associated Drupal node. The nodes were then used to provide all-time and personal score info to users of the Drupal-based website.
highscore.php:
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'on');
require 'Zend/Amf/Server.php';
require 'highscore.class.php';
$server = new Zend_Amf_Server();
$server->setClass('highscore');
echo $server->handle();highscore.class.php:
<?php
class highscore
{
// Constructor stubs so AMF Server doesn't get angry
public function __construct() {}
public function highscore() {}
// Individual values method
public function submit(
$user_id = null,
$song_id = null,
$difficulty = null,
$score = null,
$start_time = null,
$end_time = null
) {
// Make sure we got everything we needed from the postback
if (
!is_null($user_id) &&
!is_null($song_id) &&
!is_null($difficulty) &&
!is_null($score) &&
!is_null($start_time) &&
!is_null($end_time)
) {
// Pretend to be Drupal & pull in utility functions
define('DRUPAL_ROOT', '/home/[USERNAME REMOVED]/public_html/drupal');
require(DRUPAL_ROOT . '/sites/all/themes/[CLIENT NAME REMOVED]/teacher-codes.php');
// Obfuscate some stuff using utility
$user_id = teacher_code($user_id);
$song_id = teacher_code($song_id);
// Bootstrap Drupal
require(DRUPAL_ROOT . '/includes/bootstrap.inc');
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// Set up some more config info for node creation
define('DRUPAL_ADMIN_UID', 1);
define('DEFAULT_TIMEZONE', 'America/Chicago');
// Get user object
$user = user_load($user_id);
// Check that user account is valid
if (!empty($user)) {
// Instantiate node
$node = new stdClass();
// Set normal field values
$node->title = 'Score for ' . $user->name . ' @ ' . $start_time;
$node->type = 'score';
$node->language = LANGUAGE_NONE;
$node->uid = DRUPAL_ADMIN_UID;
$node->status = DRUPAL_BOOL_FALSE;
$node->promote = DRUPAL_BOOL_FALSE;
$node->comments = DRUPAL_COMMENTS_OFF;
// Set custom field values
$node->field_user[$node->language][] = array(
'uid' => $user_id,
'access' => 1,
'user' => $user
);
$node->field_song[$node->language][] = array(
'nid' => $song_id,
'access' => 1,
'node' => node_load($song_id)
);
$node->field_score[$node->language][]['value'] = $score;
$node->field_difficulty[$node->language][]['value'] = $difficulty;
// Convert timestamp info from ugly Flash format
$start_time = strtotime($start_time);
$end_time = strtotime($end_time);
// Add timezone-adjusted timestamps to node
$node->field_time[$node->language][] = array(
'value' => $start_time,
'value2' => $end_time,
'timezone' => DEFAULT_TIMEZONE,
'data_type' => 'date',
'db' => array(
'value' => new DateObject($start_time, DEFAULT_TIMEZONE),
'value2' => new DateObject($end_time, DEFAULT_TIMEZONE)
)
);
// Save the node
node_save($node);
// Determine if we were successful & send an appropriate message
if (!empty($node)) {
return 'Successfully created node.';
} else {
return 'Successfully built node, but encountered a Drupal error when attempting to save. (Variable method)';
}
} else {
return 'Unable to identify user. Score was not stored.';
}
} else {
return 'All parameters are required. Score was not stored.';
}
}
}