This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source 'http://rubygems.org' | |
| gem 'rails', '3.0.3' | |
| gem 'thin', '1.2.7' | |
| gem 'will_paginate' | |
| gem 'nokogiri' | |
| gem 'haml' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Define a constant if not defined: | |
| define_if_not_defined(:A, 1) | |
| assert_equal 1, A | |
| Define a constant and redefine it: | |
| define_if_not_defined(:B, 1) | |
| redefine_without_warning(:B, 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| NOTE: You will need at least Redis 2.1.6 to use the ZREVRANGEBYSCORE method. | |
| Add players to HIGHSCORES table: | |
| fossil:~ dczarnecki$ redis-cli | |
| redis> zadd HIGHSCORES 1 player_1 | |
| (integer) 1 | |
| redis> zadd HIGHSCORES 2 player_2 | |
| (integer) 1 | |
| redis> zadd HIGHSCORES 3 player_3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Create a new leaderboard or attach to an existing leaderboard named 'highscores': | |
| ruby-1.8.7-p302 > highscore_lb = Leaderboard.new('highscores') | |
| => #<Leaderboard:0x1018e4250 @page_size=25, @port=6379, @host="localhost", @redis_connection=#<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.1.10)>, @leaderboard_name="highscores"> | |
| If you need to pass in options for Redis, you can do this with the redis_options parameter: | |
| redis_options = {:host => 'localhost', :port => 6379, :password => 'password', :db => 'some_redis_db'} | |
| highscore_lb = Leaderboard.new('highscores', redis_options[:host], redis_options[:port], Leaderboard::DEFAULT_PAGE_SIZE, redis_options)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| => #<Benchmark::Tms:0x101605660 @label="", @stime=173.61, @total=577.52, @real=911.718175172806, @utime=403.91, @cstime=0.0, @cutime=0.0> | |
| Average time to request an arbitrary page from the leaderboard: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fossil:leaderboard dczarnecki$ irb | |
| ruby-1.8.7-p302 > require 'rubygems' | |
| => true | |
| ruby-1.8.7-p302 > require 'leaderboard' | |
| => true | |
| ruby-1.8.7-p302 > highscore_lb = Leaderboard.new('highscores') | |
| => #<Leaderboard:0x1011b23b0 @leaderboard_name="highscores", @page_size=25, @port=6379, @redis_connection=#<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.0.4)>, @host="localhost", @redis_options={:host=>"localhost", :port=>6379}> | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ruby-1.8.7-p302 > highscore_lb.add_member('DavidCzarnecki', 15) | |
| => false | |
| ruby-1.8.7-p302 > highscore_lb.page_size = 5 | |
| => 5 | |
| ruby-1.8.7-p302 > highscore_lb.around_me('DavidCzarnecki') | |
| => [{:member=>"DavidCzarnecki", :score=>"15", :rank=>1}, {:member=>"member_10", :score=>"10", :rank=>2}, {:member=>"member_9", :score=>"9", :rank=>3}, {:member=>"member_8", :score=>"8", :rank=>4}, {:member=>"member_7", :score=>"7", :rank=>5}] | |
| ruby-1.8.7-p302 > |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ruby-1.8.7-p302 > total_money_lb = Leaderboard.new('total_money') | |
| => #<Leaderboard:0x101180680 @leaderboard_name="total_money", @page_size=25, @port=6379, @redis_connection=#<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.0.4)>, @host="localhost", @redis_options={:host=>"localhost", :port=>6379}> | |
| ruby-1.8.7-p302 > total_money_lb.change_score_for('DavidCzarnecki', 15) | |
| => "15" | |
| ruby-1.8.7-p302 > total_money_lb.change_score_for('ChristianArca', 7) | |
| => "7" | |
| ruby-1.8.7-p302 > total_money_lb.leaders(1) | |
| => [{:member=>"DavidCzarnecki", :score=>"15", :rank=>1}, {:member=>"ChristianArca", :score=>"7", :rank=>2}] | |
| ruby-1.8.7-p302 > |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ruby-1.8.7-p302 > total_money_lb.change_score_for('DavidCzarnecki', -8) | |
| => "7" | |
| ruby-1.8.7-p302 > total_money_lb.change_score_for('ChristianArca', 16) | |
| => "23" | |
| ruby-1.8.7-p302 > total_money_lb.leaders(1) | |
| => [{:member=>"ChristianArca", :score=>"23", :rank=>1}, {:member=>"DavidCzarnecki", :score=>"7", :rank=>2}] | |
| ruby-1.8.7-p302 > |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ruby-1.8.7-p302 > map_1_xp_lb = Leaderboard.new('map_1_xp') | |
| => #<Leaderboard:0x1018d96e8 @leaderboard_name="map_1_xp", @page_size=25, @port=6379, @redis_connection=#<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.0.3)>, @host="localhost", @redis_options={:host=>"localhost", :port=>6379}> | |
| ruby-1.8.7-p302 > map_2_xp_lb = Leaderboard.new('map_2_xp') | |
| => #<Leaderboard:0x1018d3568 @leaderboard_name="map_2_xp", @page_size=25, @port=6379, @redis_connection=#<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.0.3)>, @host="localhost", @redis_options={:host=>"localhost", :port=>6379}> | |
| ruby-1.8.7-p302 > map_3_xp_lb = Leaderboard.new('map_3_xp') | |
| => #<Leaderboard:0x1018cd460 @leaderboard_name="map_3_xp", @page_size=25, @port=6379, @redis_connection=#<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.0.3)>, @host="localhost", @redis_options={:host=>"localhost", :port=>6379}> | |
| ruby-1.8.7-p302 > map_4_xp_lb = Leaderboard.new('map_4_xp') | |
| => #<Leaderboard:0x1018c735 |