Skip to content

Commit

Permalink
Added changeScoreFor function and test
Browse files Browse the repository at this point in the history
  • Loading branch information
David Czarnecki committed Jun 19, 2011
1 parent 4645b65 commit b461e53
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/Leaderboard.php
Expand Up @@ -43,6 +43,10 @@ public function totalMembersInScoreRange($minScore, $maxScore) {
return $this->_redis_connection->zCount($this->_leaderboard_name, $minScore, $maxScore);
}

public function changeScoreFor($member, $delta) {
return $this->_redis_connection->zIncrBy($this->_leaderboard_name, $delta, $member);
}

public function rankFor($member, $useZeroIndexForRank = false) {
$rank = $this->_redis_connection->zRevRank($this->_leaderboard_name, $member);
if ($useZeroIndexForRank == false) {
Expand Down
13 changes: 13 additions & 0 deletions test/LeaderboardTest.php
Expand Up @@ -66,6 +66,19 @@ function testTotalMembersInScoreRange() {
$this->assertEquals(3, $leaderboard->totalMembersInScoreRange(2, 4));
}

function testChangeScoreFor() {
$leaderboard = new Leaderboard('leaderboard');

$leaderboard->changeScoreFor('member_1', 5);
$this->assertEquals(5, $leaderboard->scoreFor('member_1'));

$leaderboard->changeScoreFor('member_1', 5);
$this->assertEquals(10, $leaderboard->scoreFor('member_1'));

$leaderboard->changeScoreFor('member_1', -5);
$this->assertEquals(5, $leaderboard->scoreFor('member_1'));
}

function testRankFor() {
$leaderboard = new Leaderboard('leaderboard');
for ($i = 1; $i <= Leaderboard::DEFAULT_PAGE_SIZE + 1; $i++) {
Expand Down

0 comments on commit b461e53

Please sign in to comment.