diff --git a/classes/APIv1.php b/classes/APIv1.php index e9be530..e2e94ae 100644 --- a/classes/APIv1.php +++ b/classes/APIv1.php @@ -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.'; diff --git a/classes/CustomModule.php b/classes/CustomModule.php index 663bdab..d77e89d 100644 --- a/classes/CustomModule.php +++ b/classes/CustomModule.php @@ -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'])) { diff --git a/modules/api/v1/leaderboard.php b/modules/api/v1/leaderboard.php index 023b673..fda44c8 100644 --- a/modules/api/v1/leaderboard.php +++ b/modules/api/v1/leaderboard.php @@ -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()); + } } } diff --git a/templates/api.phtml b/templates/api.phtml index 3b3c97a..c94b09b 100644 --- a/templates/api.phtml +++ b/templates/api.phtml @@ -48,7 +48,7 @@ if ($this->module['api_key'])
Returns the members, scores and ranks sorted in descending order by score. {uid} corresponds with the UID of the leaderboards which can be found on the My Leaderboards page.
+Returns the members with scores sorted in descending order by score. {uid} corresponds with the UID of the leaderboards which can be found on the My Leaderboards page.
Optionally the results can be filtered by member where member is a unique value that maps back to your system. We highly recommend using something that won’t change in the future like an auto incremented field from your database.