Permalink
Browse files

Merge pull request #28 from agoragames/members-from-rank-range

Members from rank range
  • Loading branch information...
2 parents 852d8fd + f0e3ae8 commit a3ccf1bd5d1192290c9eac3233cb4755b6a1e95e @czarneckid czarneckid committed Oct 12, 2012
Showing with 86 additions and 0 deletions.
  1. +46 −0 lib/leaderboard.rb
  2. +20 −0 spec/leaderboard_spec.rb
  3. +20 −0 spec/reverse_leaderboard_spec.rb
View
@@ -646,6 +646,52 @@ def members_from_score_range_in(leaderboard_name, minimum_score, maximum_score,
end
end
+ # Retrieve members from the leaderboard within a given rank range.
+ #
+ # @param starting_rank [int] Starting rank (inclusive).
+ # @param ending_rank [int] Ending rank (inclusive).
+ # @param options [Hash] Options to be used when retrieving the data from the leaderboard.
+ #
+ # @return members from the leaderboard that fall within the given rank range.
+ def members_from_rank_range(starting_rank, ending_rank, options = {})
+ members_from_rank_range_in(@leaderboard_name, starting_rank, ending_rank, options)
+ end
+
+ # Retrieve members from the named leaderboard within a given rank range.
+ #
+ # @param leaderboard_name [String] Name of the leaderboard.
+ # @param starting_rank [int] Starting rank (inclusive).
+ # @param ending_rank [int] Ending rank (inclusive).
+ # @param options [Hash] Options to be used when retrieving the data from the leaderboard.
+ #
+ # @return members from the leaderboard that fall within the given rank range.
+ def members_from_rank_range_in(leaderboard_name, starting_rank, ending_rank, options = {})
+ leaderboard_options = DEFAULT_LEADERBOARD_REQUEST_OPTIONS.dup
+ leaderboard_options.merge!(options)
+
+ starting_rank -= 1
+ if starting_rank < 0
+ starting_rank = 0
+ end
+
+ ending_rank -= 1
+ if ending_rank > total_members_in(leaderboard_name)
+ ending_rank = total_members_in(leaderboard_name) - 1
+ end
+
+ if @reverse
+ raw_leader_data = @redis_connection.zrange(leaderboard_name, starting_rank, ending_rank, :with_scores => false)
+ else
+ raw_leader_data = @redis_connection.zrevrange(leaderboard_name, starting_rank, ending_rank, :with_scores => false)
+ end
+
+ if raw_leader_data
+ return ranked_in_list_in(leaderboard_name, raw_leader_data, leaderboard_options)
+ else
+ return []
+ end
+ end
+
# Retrieve a member at the specified index from the leaderboard.
#
# @param position [int] Position in leaderboard.
View
@@ -618,4 +618,24 @@
leaders[-1][:member].should eql('member_25')
leaders[-1][:score].to_i.should be(25)
end
+
+ it 'should allow you to retrieve a given set of members from the leaderboard in a rank range' do
+ rank_members_in_leaderboard(25)
+
+ members = @leaderboard.members_from_rank_range(5, 9)
+ members.size.should be(5)
+ members[0][:member].should eql('member_21')
+ members[0][:score].to_i.should be(21)
+ members[4][:member].should eql('member_17')
+
+ members = @leaderboard.members_from_rank_range(1, 1)
+ members.size.should be(1)
+ members[0][:member].should eql('member_25')
+
+ members = @leaderboard.members_from_rank_range(-1, 26)
+ members.size.should be(25)
+ members[0][:member].should eql('member_25')
+ members[0][:score].to_i.should be(25)
+ members[24][:member].should eql('member_1')
+ end
end
@@ -334,4 +334,24 @@
@leaderboard.total_members.should be(2)
@leaderboard.leaders(1).first[:member].should eql('member_1')
end
+
+ it 'should allow you to retrieve a given set of members from the leaderboard in a rank range' do
+ rank_members_in_leaderboard(25)
+
+ members = @leaderboard.members_from_rank_range(5, 9)
+ members.size.should be(5)
+ members[0][:member].should eql('member_5')
+ members[0][:score].to_i.should be(5)
+ members[4][:member].should eql('member_9')
+
+ members = @leaderboard.members_from_rank_range(1, 1)
+ members.size.should be(1)
+ members[0][:member].should eql('member_1')
+
+ members = @leaderboard.members_from_rank_range(-1, 26)
+ members.size.should be(25)
+ members[0][:member].should eql('member_1')
+ members[0][:score].to_i.should be(1)
+ members[24][:member].should eql('member_25')
+ end
end

0 comments on commit a3ccf1b

Please sign in to comment.