Leaderboards backed by Redis in Ruby.
Builds off ideas proposed in http://www.agoragames.com/blog/2011/01/01/creating-high-score-tables-leaderboards-using-redis/.
gem install leaderboard
or in your Gemfile
gem 'leaderboard'
Make sure your redis server is running! Redis configuration is outside the scope of this README, but check out the Redis documentation.
The gem has been built and tested under 2.2.1.
NOTE: The gem should work fine in earlier versions of Ruby such as 1.8.7 or 1.9.3. There are no specific aspects of Ruby 2.x that we utilize at this time.
Be sure to require the leaderboard library:
require 'leaderboard'
Create a new leaderboard or attach to an existing leaderboard named 'highscores':
highscore_lb = Leaderboard.new('highscores')
=> #<Leaderboard:0x0000010307b530 @leaderboard_name="highscores", @page_size=25, @redis_connection=#<Redis client v2.2.2 connected to redis://localhost:6379/0 (Redis v2.2.5)>>
The Leaderboard::DEFAULT_OPTIONS
are as follows:
DEFAULT_OPTIONS = {
:page_size => DEFAULT_PAGE_SIZE,
:reverse => false,
:member_key => :member,
:rank_key => :rank,
:score_key => :score,
:member_data_key => :member_data,
:member_data_namespace => 'member_data',
:global_member_data => false
}
The DEFAULT_PAGE_SIZE
is 25.
You would use the option, :reverse => true
, if you wanted a leaderboard sorted from lowest-to-highest score. You may also set the reverse
option on a leaderboard after you have created a new instance of a leaderboard. The various ..._key
options above control what data is returned in the hash of leaderboard data from calls such as leaders
or around_me
. Finally, the global_member_data
option allows you to control whether optional member data is per-leaderboard (false
) or global (true
).
If you need to pass in options for Redis, you can do this in the initializer:
redis_options = {:host => 'localhost', :port => 6379, :db => 1}
=> {:host=>"localhost", :port=>6379, :db=>1}
highscore_lb = Leaderboard.new('highscores', Leaderboard::DEFAULT_OPTIONS, redis_options)
=> #<Leaderboard:0x00000103095200 @leaderboard_name="highscores", @page_size=25, @redis_connection=#<Redis client v2.2.2 connected to redis://localhost:6379/1 (Redis v2.2.5)>>
You can pass in an existing connection to Redis using :redis_connection
in the redis_options
hash:
redis = Redis.new
=> #<Redis client v2.2.2 connected to redis://127.0.0.1:6379/0 (Redis v2.2.5)>
redis_options = {:redis_connection => redis}
=> {:redis_connection=>#<Redis client v2.2.2 connected to redis://127.0.0.1:6379/0 (Redis v2.2.5)>}
highscore_lb = Leaderboard.new('highscores', Leaderboard::DEFAULT_OPTIONS, redis_options)
=> #<Leaderboard:0x000001028791e8 @leaderboard_name="highscores", @page_size=25, @redis_connection=#<Redis client v2.2.2 connected to redis://127.0.0.1:6379/0 (Redis v2.2.5)>>
To use the same connection for multiple leaderboards, reset the options hash before instantiating more leaderboards:
redis = Redis.new
=> #<Redis client v2.2.2 connected to redis://127.0.0.1:6379/0 (Redis v2.2.5)>
redis_options = {:redis_connection => redis}
=> {:redis_connection=>#<Redis client v2.2.2 connected to redis://127.0.0.1:6379/0 (Redis v2.2.5)>}
highscore_lb = Leaderboard.new('highscores', Leaderboard::DEFAULT_OPTIONS, redis_options)
redis_options = {:redis_connection => redis}
other_highscore_lb = Leaderboard.new('other_highscores', Leaderboard::DEFAULT_OPTIONS, redis_options)
You can set the page size to something other than the default page size (25):
highscore_lb.page_size = 5
=> 5
highscore_lb
=> #<Leaderboard:0x000001028791e8 @leaderboard_name="highscores", @page_size=5, @redis_connection=#<Redis client v2.2.2 connected to redis://127.0.0.1:6379/0 (Redis v2.2.5)>>
Add members to your leaderboard using rank_member
:
1.upto(10) do |index|
highscore_lb.rank_member("member_#{index}", index)
end
=> 1
You can call rank_member
with the same member and the leaderboard will be updated automatically.
Get some information about your leaderboard:
highscore_lb.total_members
=> 10
highscore_lb.total_pages
=> 1
The rank_member
call will also accept an optional parameter, member_data
that could
be used to store other information about a given member in the leaderboard. This
may be useful in situations where you are storing member IDs in the leaderboard and
you want to be able to store a member name for display. You could use JSON to
encode a Hash of member data. Example:
require 'json'
highscore_lb.rank_member('84849292', 1, {'username' => 'member_name'}.to_json)
You can retrieve, update and remove the optional member data using the
member_data_for
, update_member_data
and remove_member_data
calls. Example:
JSON.parse(highscore_lb.member_data_for('84849292'))
=> {"username"=>"member_name"}
highscore_lb.update_member_data('84849292', {'last_updated' => Time.now, 'username' => 'updated_member_name'}.to_json)
=> "OK"
JSON.parse(highscore_lb.member_data_for('84849292'))
=> {"username"=>"updated_member_name", "last_updated"=>"2012-06-09 09:11:06 -0400"}
highscore_lb.remove_member_data('84849292')
If you delete the leaderboard, ALL of the member data is deleted as well.
If you use optional member data, the use of the remove_members_in_score_range
or remove_members_outside_rank
methods
will leave data around in the member data hash. This is because the internal Redis method, zremrangebyscore
,
only returns the number of items removed. It does not return the members that it removed.
Get some information about a specific member(s) in the leaderboard:
highscore_lb.score_for('member_4')
=> 4.0
highscore_lb.rank_for('member_4')
=> 7
highscore_lb.rank_for('member_10')
=> 1
Get page 1 in the leaderboard:
highscore_lb.leaders(1)
=> [{:member=>"member_10", :rank=>1, :score=>10.0}, {:member=>"member_9", :rank=>2, :score=>9.0}, {:member=>"member_8", :rank=>3, :score=>8.0}, {:member=>"member_7", :rank=>4, :score=>7.0}, {:member=>"member_6", :rank=>5, :score=>6.0}, {:member=>"member_5", :rank=>6, :score=>5.0}, {:member=>"member_4", :rank=>7, :score=>4.0}, {:member=>"member_3", :rank=>8, :score=>3.0}, {:member=>"member_2", :rank=>9, :score=>2.0}, {:member=>"member_1", :rank=>10, :score=>1.0}]
You can pass various options to the calls leaders
, all_leaders
, around_me
, members_from_score_range
, members_from_rank_range
and ranked_in_list
. Valid options are:
:with_member_data
-true
orfalse
(default) to return the optional member data.:page_size
- An integer value to change the page size for that call.:members_only
-true
orfalse
(default) to return only the members without their score and rank.:sort_by
- Valid values for:sort_by
are:none
(default),:score
and:rank
.:include_missing
-true
(default) orfalse
to return members that are not ranked.
You can also use the members
and members_in
methods as aliases for the leaders
and leaders_in
methods.
There are also a few convenience methods to be able to retrieve all leaders from a given leaderboard. They are all_leaders
and all_leaders_from
. You may also use the aliases all_members
or all_members_from
. Use any of these methods sparingly as all the information in the leaderboard will be returned.
Add more members to your leaderboard:
50.upto(95) do |index|
highscore_lb.rank_member("member_#{index}", index)
end
=> 50
highscore_lb.total_pages
=> 3
Get an "Around Me" leaderboard page for a given member, which pulls members above and below the given member:
highscore_lb.around_me('member_53')
=> [{:member=>"member_65", :rank=>31, :score=>65.0}, {:member=>"member_64", :rank=>32, :score=>64.0}, {:member=>"member_63", :rank=>33, :score=>63.0}, {:member=>"member_62", :rank=>34, :score=>62.0}, {:member=>"member_61", :rank=>35, :score=>61.0}, {:member=>"member_60", :rank=>36, :score=>60.0}, {:member=>"member_59", :rank=>37, :score=>59.0}, {:member=>"member_58", :rank=>38, :score=>58.0}, {:member=>"member_57", :rank=>39, :score=>57.0}, {:member=>"member_56", :rank=>40, :score=>56.0}, {:member=>"member_55", :rank=>41, :score=>55.0}, {:member=>"member_54", :rank=>42, :score=>54.0}, {:member=>"member_53", :rank=>43, :score=>53.0}, {:member=>"member_52", :rank=>44, :score=>52.0}, {:member=>"member_51", :rank=>45, :score=>51.0}, {:member=>"member_50", :rank=>46, :score=>50.0}, {:member=>"member_10", :rank=>47, :score=>10.0}, {:member=>"member_9", :rank=>48, :score=>9.0}, {:member=>"member_8", :rank=>49, :score=>8.0}, {:member=>"member_7", :rank=>50, :score=>7.0}, {:member=>"member_6", :rank=>51, :score=>6.0}, {:member=>"member_5", :rank=>52, :score=>5.0}, {:member=>"member_4", :rank=>53, :score=>4.0}, {:member=>"member_3", :rank=>54, :score=>3.0}, {:member=>"member_2", :rank=>55, :score=>2.0}]
Get rank and score for an arbitrary list of members (e.g. friends) from the leaderboard:
highscore_lb.ranked_in_list(['member_1', 'member_62', 'member_67'])
=> [{:member=>"member_1", :rank=>56, :score=>1.0}, {:member=>"member_62", :rank=>34, :score=>62.0}, {:member=>"member_67", :rank=>29, :score=>67.0}]
Retrieve members from the leaderboard in a given score range:
members = highscore_lb.members_from_score_range(4, 19)
=> [{:member=>"member_10", :rank=>47, :score=>10.0}, {:member=>"member_9", :rank=>48, :score=>9.0}, {:member=>"member_8", :rank=>49, :score=>8.0}, {:member=>"member_7", :rank=>50, :score=>7.0}, {:member=>"member_6", :rank=>51, :score=>6.0}, {:member=>"member_5", :rank=>52, :score=>5.0}, {:member=>"member_4", :rank=>53, :score=>4.0}]
Retrieve a single member from the leaderboard at a given position:
members = highscore_lb.member_at(4)
=> {:member=>"member_92", :rank=>4, :score=>92.0}
Retrieve a range of members from the leaderboard within a given rank range:
members = highscore_lb.members_from_rank_range(1, 5)
=> [{:member=>"member_95", :rank=>1, :score=>95.0}, {:member=>"member_94", :rank=>2, :score=>94.0}, {:member=>"member_93", :rank=>3, :score=>93.0}, {:member=>"member_92", :rank=>4, :score=>92.0}, {:member=>"member_91", :rank=>5, :score=>91.0}]
The option :sort_by
is useful for retrieving an arbitrary list of
members from a given leaderboard where you would like the data sorted
when returned. The follow examples demonstrate its use:
friends = highscore_lb.ranked_in_list(['member_6', 'member_1', 'member_10'], :sort_by => :rank)
=> [{:member=>"member_10", :rank=>47, :score=>10.0}, {:member=>"member_6", :rank=>51, :score=>6.0}, {:member=>"member_1", :rank=>56, :score=>1.0}]
friends = highscore_lb.ranked_in_list(['member_6', 'member_1', 'member_10'], :sort_by => :score)
=> [{:member=>"member_1", :rank=>56, :score=>1.0}, {:member=>"member_6", :rank=>51, :score=>6.0}, {:member=>"member_10", :rank=>47, :score=>10.0}]
You can pass a lambda to the rank_member_if
method to conditionally rank a member in the leaderboard. The lambda is passed the following 5 parameters:
member
: Member name.current_score
: Current score for the member in the leaderboard. May benil
if the member is not currently ranked in the leaderboard.score
: Member score.member_data
: Optional member data.leaderboard_options
: Leaderboard options, e.g. :reverse => Value of reverse option
highscore_check = lambda do |member, current_score, score, member_data, leaderboard_options|
return true if current_score.nil?
return true if score > current_score
false
end
highscore_lb.rank_member_if(highscore_check, 'david', 1337)
highscore_lb.score_for('david')
=> 1337.0
highscore_lb.rank_member_if(highscore_check, 'david', 1336)
highscore_lb.score_for('david')
=> 1337.0
highscore_lb.rank_member_if(highscore_check, 'david', 1338)
highscore_lb.score_for('david')
=> 1338.0
NOTE: Use a lambda and not a proc, otherwise you will get a LocalJumpError
as a return statement in the proc will return from the method enclosing the proc.
Insert multiple data items for members and their associated scores:
As a splat:
highscore_lb.rank_members('member_1', 1, 'member_5', 5, 'member_10', 10)
Or as an array:
highscore_lb.rank_members(['member_1', 1, 'member_5', 5, 'member_10', 10])
Use this method to do bulk insert of data, but be mindful of the amount of data you are inserting since a single transaction can get quite large.
highscore_lb.rank_member_across(['highscores', 'more_highscores'], 'david', 50000, { :member_name => "david" })
The leaderboard library offers 3 styles of ranking. This is only an issue for members with the same score in a leaderboard.
Default: The Leaderboard
class uses the default Redis sorted set ordering, whereby different members having the same score are ordered lexicographically. As per the Redis documentation on Redis sorted sets, "The lexicographic ordering used is binary, it compares strings as array of bytes."
Tie ranking: The TieRankingLeaderboard
subclass of Leaderboard
allows you to define a leaderboard where members with the same score are given the same rank. For example, members in a leaderboard with the associated scores would have the ranks of:
| member | score | rank |
-----------------------------
| member_1 | 50 | 1 |
| member_2 | 50 | 1 |
| member_3 | 30 | 2 |
| member_4 | 30 | 2 |
| member_5 | 10 | 3 |
The TieRankingLeaderboard
accepts one additional option, :ties_namespace
(default: ties), when initializing a new instance of this class. Please note that in its current implementation, the TieRankingLeaderboard
class uses an additional sorted set to rank the scores, so please keep this in mind when you are doing any capacity planning for Redis with respect to memory usage.
Competition ranking: The CompetitionRankingLeaderboard
subclass of Leaderboard
allows you to define a leaderboard where members with the same score will have the same rank, and then a gap is left in the ranking numbers. For example, members in a leaderboard with the associated scores would have the ranks of:
| member | score | rank |
-----------------------------
| member_1 | 50 | 1 |
| member_2 | 50 | 1 |
| member_3 | 30 | 3 |
| member_4 | 30 | 3 |
| member_5 | 10 | 5 |
delete_leaderboard: Delete the current leaderboard
member_data_for(member): Retrieve the optional member data for a given member in the leaderboard
update_member_data(member, member_data): Update the optional member data for a given member in the leaderboard
remove_member_data(member): Remove the optional member data for a given member in the leaderboard
remove_member(member): Remove a member from the leaderboard
total_members: Total # of members in the leaderboard
total_pages: Total # of pages in the leaderboard given the leaderboard's page_size
total_members_in_score_range(min_score, max_score): Count the number of members within a score range in the leaderboard
change_score_for(member, delta): Change the score for a member by some amount delta (delta could be positive or negative)
rank_for(member): Retrieve the rank for a given member in the leaderboard
score_for(member): Retrieve the score for a given member in the leaderboard
check_member?(member): Check to see whether member is in the leaderboard
score_and_rank_for(member): Retrieve the score and rank for a member in a single call
remove_members_in_score_range(min_score, max_score): Remove members from the leaderboard within a score range
remove_members_outside_rank(rank): Remove members from the leaderboard outside a given rank
percentile_for(member): Calculate the percentile for a given member
score_for_percentile(percentile): Calculate the score for a given percentile value in the leaderboard
page_for(member, page_size): Determine the page where a member falls in the leaderboard
expire_leaderboard(seconds): Expire the leaderboard in a set number of seconds.
expire_leaderboard_at(timestamp): Expire the leaderboard at a specific UNIX timestamp.
rank_members(members_and_scores): Rank an array of members in the leaderboard where you can call via (member_name, score) or pass in an array of [member_name, score]
merge_leaderboards(destination, keys, options = {:aggregate => :min}): Merge leaderboards given by keys with this leaderboard into destination
intersect_leaderboards(destination, keys, options = {:aggregate => :min}): Intersect leaderboards given by keys with this leaderboard into destination
top(number, options): Retrieve members from the leaderboard within a range from 1 to the number given.
Check the online documentation for more detail on each method.
10 million sequential scores insert:
highscore_lb = Leaderboard.new('highscores')
=> #<Leaderboard:0x0000010205fc50 @leaderboard_name="highscores", @page_size=25, @redis_connection=#<Redis client v2.2.2 connected to redis://localhost:6379/0 (Redis v2.2.5)>>
insert_time = Benchmark.measure do
1.upto(10000000) do |index|
highscore_lb.rank_member("member_#{index}", index)
end
end
=> 323.070000 148.560000 471.630000 (942.068307)
Average time to request an arbitrary page from the leaderboard:
requests_to_make = 50000
=> 50000
lb_request_time = 0
=> 0
1.upto(requests_to_make) do
lb_request_time += Benchmark.measure do
highscore_lb.leaders(rand(highscore_lb.total_pages))
end.total
end
=> 1
p lb_request_time / requests_to_make
0.001513999999999998
=> 0.001513999999999998
10 million random scores insert:
insert_time = Benchmark.measure do
1.upto(10000000) do |index|
highscore_lb.rank_member("member_#{index}", rand(50000000))
end
end
=> 338.480000 155.200000 493.680000 (2188.702475)
Average time to request an arbitrary page from the leaderboard:
1.upto(requests_to_make) do
lb_request_time += Benchmark.measure do
highscore_lb.leaders(rand(highscore_lb.total_pages))
end.total
end
=> 1
p lb_request_time / requests_to_make
0.0014615999999999531
=> 0.0014615999999999531
Ranking individual members:
insert_time = Benchmark.measure do
1.upto(1000000) do |index|
highscore_lb.rank_member("member_#{index}", index)
end
end
=> 29.340000 15.050000 44.390000 ( 81.673507)
Ranking multiple members at once:
member_data = []
=> []
1.upto(1000000) do |index|
member_data << "member_#{index}"
member_data << index
end
=> 1
insert_time = Benchmark.measure do
highscore_lb.rank_members(member_data)
end
=> 22.390000 6.380000 28.770000 ( 31.144027)
The following ports have been made of the leaderboard gem.
Officially supported:
- CoffeeScript: https://github.com/agoragames/leaderboard-coffeescript
- Python: https://github.com/agoragames/leaderboard-python
- Ruby: https://github.com/agoragames/leaderboard
Unofficially supported (they need some feature parity love):
- Java: https://github.com/agoragames/java-leaderboard
- PHP: https://github.com/agoragames/php-leaderboard
- Scala: https://github.com/agoragames/scala-leaderboard
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
- Fork the project
- Start a feature/bugfix branch
- Commit and push until you are happy with your contribution
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
- Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright (c) 2011-2015 David Czarnecki. See LICENSE.txt for further details.