Code complete on API.

This commit is contained in:
Josh Sherman 2013-12-19 11:25:19 -05:00
parent 1e140d1a09
commit 78c8ed42ac
4 changed files with 117 additions and 6 deletions

View file

@ -11,12 +11,13 @@ class APIv1 extends CustomModule
$response_code = 200;
$error = 'An unexcepted error has occurred.';
echo($_SERVER['REQUEST_METHOD']);
print_r($this->request_methods);
if (!is_array($this->request_methods))
{
$this->request_methods = array($this->request_methods);
}
// Checks the request method
if ((is_array($this->request_methods) && !in_array($_SERVER['REQUEST_METHOD'], $this->request_methods))
|| $this->request_methods != $_SERVER['REQUEST_METHOD'])
if (!in_array($_SERVER['REQUEST_METHOD'], $this->request_methods))
{
$response_code = 400;
$error = 'Invalid request method.';

View file

@ -11,7 +11,9 @@ class CustomModule extends Module
$this->redis = new CustomRedis();
if (substr(get_class($this), 0, 3) != 'api')
$class = get_class($this);
if ($class == 'api' || substr($class, 0, 3) != 'api')
{
if (isset($_COOKIE['__auth']))
{

View file

@ -6,6 +6,114 @@ class api_v1_leaderboard extends APIv1
public function __default()
{
try
{
// Checks if we have a UID
if (!isset($_GET['uid']))
{
throw new Exception('Missing UID.');
}
$leaderboard_key = 'leaderboard:' . $_GET['uid'];
// Checks that the UID is valid and belongs to the key
$leaderboard = $this->redis->hgetall($leaderboard_key);
if (!$leaderboard)
{
throw new Exception('Leaderboard UID does not exist.');
}
if ($this->uid != $leaderboard['uid'])
{
throw new Exception('Leaderboard UID does not belong to you.');
}
// Sets up our key suffixes
list($year, $month, $day, $week) = explode('-', date('Y-m-d-W'));
$week = $year . $week;
$day = $year . $month . $day;
$month = $year . $month;
$suffixes = array(
'day:' . $day,
'week:' . $week,
'month:' . $month,
'year:' . $year,
'alltime',
);
switch ($_SERVER['REQUEST_METHOD'])
{
// Pulls members of the leaderboard
case 'GET':
$members = $this->redis->zrevrange($leaderboard_key . ':alltime', 0, -1, 'WITHSCORES');
return array('leaderboard' => array_merge($leaderboard, array('members' => $members)));
break;
// Adds a score for a member
case 'POST':
if (!isset($_POST['member']))
{
throw new Exception('Missing member.');
}
elseif (!isset($_POST['score']))
{
throw new Exception('Missing score.');
}
elseif (!preg_match('/^-?\d+$/', $_POST['score']))
{
throw new Exception('Score must be an integer.');
}
$this->redis->multi();
foreach ($suffixes as $suffix)
{
$this->redis->zadd($leaderboard_key . ':' . $suffix, $_POST['score'], $_POST['member']);
}
$this->redis->exec();
break;
// Increments a score of a member
case 'PUT':
// Hack because _PUT doesn't exist in PHP land
parse_str(file_get_contents("php://input"), $_PUT);
$increment = 1;
if (!isset($_PUT['member']))
{
throw new Exception('Missing member.');
}
elseif (isset($_PUT['value']))
{
if (!preg_match('/^-?\d+$/', $_PUT['value']))
{
throw new Exception('Value must be an integer.');
}
$increment = $_PUT['value'];
}
$this->redis->multi();
foreach ($suffixes as $suffix)
{
$this->redis->zincrby($leaderboard_key . ':' . $suffix, $increment, $_PUT['member']);
}
$this->redis->exec();
break;
}
}
catch (Exception $e)
{
return array('response_code' => 400, 'error' => $e->getMessage());
}
}
}

View file

@ -48,7 +48,7 @@ if ($this->module['api_key'])
<h4>GET</h4>
<p class="lead">Returns the members, scores and ranks sorted in descending order by score. <span class="label label-primary">{uid}</span> corresponds with the UID of the leaderboards which can be found on the <a href="/leaderboards" target="_blank">My Leaderboards</a> page.</p>
<p class="lead">Returns the members with scores sorted in descending order by score. <span class="label label-primary">{uid}</span> corresponds with the UID of the leaderboards which can be found on the <a href="/leaderboards" target="_blank">My Leaderboards</a> page.</p>
<p class="lead">Optionally the results can be filtered by <span class="label label-primary">member</span> where <span class="label label-primary">member</span> is a unique value that maps back to your system. We highly recommend using something that wont change in the future like an auto incremented field from your database.</p>