= leaderboard Leaderboards backed by Redis, http://redis.io. Builds off ideas proposed in http://blog.agoragames.com/2011/01/01/creating-high-score-tables-leaderboards-using-redis/. == Installation Install the gem: gem install leaderboard Make sure your redis server is running! Redis configuration is outside the scope of this README, but check out the Redis documentation, http://redis.io/documentation. == Usage Create a new leaderboard or attach to an existing leaderboard named 'highscores': ruby-1.8.7-p302 > highscore_lb = Leaderboard.new('highscores') => #, @leaderboard_name="highscores"> Add members to your leaderboard: ruby-1.8.7-p302 > 1.upto(10) do |index| ruby-1.8.7-p302 > highscore_lb.add_member("member_#{index}", index) ruby-1.8.7-p302 ?> end => 1 Get some information about your leaderboard: ruby-1.8.7-p302 > highscore_lb.total_members => 10 ruby-1.8.7-p302 > highscore_lb.total_pages => 1 Get some information about a specific member(s) in the leaderboard: ruby-1.8.7-p302 > highscore_lb.score_for('member_4') => 4.0 ruby-1.8.7-p302 > highscore_lb.rank_for('member_4') => 7 ruby-1.8.7-p302 > highscore_lb.rank_for('member_10') => 1 Get page 1 in the leaderboard: ruby-1.8.7-p302 > highscore_lb.leaders(1) => [{:member=>"member_10", :rank=>1, :score=>"10"}, {:member=>"member_9", :rank=>2, :score=>"9"}, {:member=>"member_8", :rank=>3, :score=>"8"}, {:member=>"member_7", :rank=>4, :score=>"7"}, {:member=>"member_6", :rank=>5, :score=>"6"}, {:member=>"member_5", :rank=>6, :score=>"5"}, {:member=>"member_4", :rank=>7, :score=>"4"}, {:member=>"member_3", :rank=>8, :score=>"3"}, {:member=>"member_2", :rank=>9, :score=>"2"}, {:member=>"member_1", :rank=>10, :score=>"1"}] Add more members to your leaderboard: ruby-1.8.7-p302 > 50.upto(95) do |index| ruby-1.8.7-p302 > highscore_lb.add_member("member_#{index}", index) ruby-1.8.7-p302 ?> end => 50 ruby-1.8.7-p302 > highscore_lb.total_pages => 3 Get an "Around Me" leaderboard for a member: ruby-1.8.7-p302 > highscore_lb.around_me('member_53') => [{:member=>"member_65", :rank=>31, :score=>"65"}, {:member=>"member_64", :rank=>32, :score=>"64"}, {:member=>"member_63", :rank=>33, :score=>"63"}, {:member=>"member_62", :rank=>34, :score=>"62"}, {:member=>"member_61", :rank=>35, :score=>"61"}, {:member=>"member_60", :rank=>36, :score=>"60"}, {:member=>"member_59", :rank=>37, :score=>"59"}, {:member=>"member_58", :rank=>38, :score=>"58"}, {:member=>"member_57", :rank=>39, :score=>"57"}, {:member=>"member_56", :rank=>40, :score=>"56"}, {:member=>"member_55", :rank=>41, :score=>"55"}, {:member=>"member_54", :rank=>42, :score=>"54"}, {:member=>"member_53", :rank=>43, :score=>"53"}, {:member=>"member_52", :rank=>44, :score=>"52"}, {:member=>"member_51", :rank=>45, :score=>"51"}, {:member=>"member_50", :rank=>46, :score=>"50"}, {:member=>"member_10", :rank=>47, :score=>"10"}, {:member=>"member_9", :rank=>48, :score=>"9"}, {:member=>"member_8", :rank=>49, :score=>"8"}, {:member=>"member_7", :rank=>50, :score=>"7"}, {:member=>"member_6", :rank=>51, :score=>"6"}, {:member=>"member_5", :rank=>52, :score=>"5"}, {:member=>"member_4", :rank=>53, :score=>"4"}, {:member=>"member_3", :rank=>54, :score=>"3"}, {:member=>"member_2", :rank=>55, :score=>"2"}] Get rank and score for an arbitrary list of members (e.g. friends): ruby-1.8.7-p302 > highscore_lb.ranked_in_list(['member_1', 'member_62', 'member_67'], true) => [{:rank=>55, :member=>"member_1", :score=>1.0}, {:rank=>33, :member=>"member_62", :score=>62.0}, {:rank=>28, :member=>"member_67", :score=>67.0}] == Performance Metrics 10 million sequential scores insert: ruby-1.8.7-p302 > insert_time = Benchmark.measure do ruby-1.8.7-p302 > 1.upto(10000000) do |index| ruby-1.8.7-p302 > highscore_lb.add_member("member_#{index}", index) ruby-1.8.7-p302 ?> end ruby-1.8.7-p302 ?> end => # Average time to request an arbitrary page from the leaderboard: ruby-1.8.7-p302 > requests_to_make = 50000 => 50000 ruby-1.8.7-p302 > lb_request_time = 0 => 0 ruby-1.8.7-p302 > 1.upto(requests_to_make) do ruby-1.8.7-p302 > lb_request_time += Benchmark.measure do ruby-1.8.7-p302 > highscore_lb.leaders(rand(highscore_lb.total_pages)) ruby-1.8.7-p302 ?> end.total ruby-1.8.7-p302 ?> end => 1 ruby-1.8.7-p302 > ruby-1.8.7-p302 > p lb_request_time / requests_to_make 0.001808 => nil 10 million random scores insert: ruby-1.8.7-p302 > insert_time = Benchmark.measure do ruby-1.8.7-p302 > 1.upto(10000000) do |index| ruby-1.8.7-p302 > highscore_lb.add_member("member_#{index}", rand(50000000)) ruby-1.8.7-p302 ?> end ruby-1.8.7-p302 ?> end => # Average time to request an arbitrary page from the leaderboard: ruby-1.8.7-p302 > requests_to_make = 50000 => 50000 ruby-1.8.7-p302 > lb_request_time = 0 => 0 ruby-1.8.7-p302 > 1.upto(requests_to_make) do ruby-1.8.7-p302 > lb_request_time += Benchmark.measure do ruby-1.8.7-p302 > highscore_lb.leaders(rand(highscore_lb.total_pages)) ruby-1.8.7-p302 ?> end.total ruby-1.8.7-p302 ?> end => 1 ruby-1.8.7-p302 > ruby-1.8.7-p302 > p lb_request_time / requests_to_make 0.00179680000000001 => nil == Future Ideas * Bulk insert * Is nil? OK to return if Redis returns no data or should it be []? == Contributing to 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 Copyright (c) 2011 David Czarnecki. See LICENSE.txt for further details.