Permalink
Browse files

Merge pull request #32 from agoragames/version-3

Version 3
  • Loading branch information...
2 parents 771a5f4 + ab4b39f commit 94591a62347cc9a2802d16e8c6fd8462915e9af6 @czarneckid czarneckid committed Nov 30, 2012
Showing with 318 additions and 243 deletions.
  1. +0 −1 .rvmrc
  2. +15 −0 CHANGELOG.markdown
  3. +61 −26 README.markdown
  4. +2 −2 Rakefile
  5. +87 −76 lib/leaderboard.rb
  6. +1 −1 lib/leaderboard/version.rb
  7. +97 −76 spec/leaderboard_spec.rb
  8. +54 −60 spec/reverse_leaderboard_spec.rb
  9. +1 −1 spec/version_spec.rb
View
@@ -1,3 +1,2 @@
rvm --create 1.8.7@leaderboard_gem
-rvm --create 1.9.2@leaderboard_gem
rvm --create 1.9.3@leaderboard_gem
View
@@ -1,5 +1,20 @@
# CHANGELOG
+## leaderboard 3.0.0.rc3 ()
+
+* Added `rank_member_if` and `rank_member_if_in` methods that allow you to rank a member in the leaderboard based on execution of a lambda.
+
+## leaderboard 3.0.0.rc2 (2012-11-27)
+
+* No longer cast scores to a floating point automatically. If requesting a score for an unknown member in the leaderboard, return `nil`. Under the old behavior, a `nil` score gets returned as 0.0. This is misleading as 0.0 is a valid score.
+
+## leaderboard 3.0.0.rc1 (2012-11-08)
+
+* Removes `:use_zero_index_for_rank_option` as valid option for requesting data from the leaderboard. [Original proposal](https://github.com/agoragames/leaderboard/pull/27)
+* Optional member data is stored in a single hash. [Original proposal](https://github.com/agoragames/leaderboard/pull/26)
+* Adds `:sort_by` as valid option for requesting data from the leaderboard. [Original proposal](https://github.com/agoragames/leaderboard/pull/30)
+* Removes `:with_scores` and `:with_ranks` as valid options for requesting data from the leaderboard.
+
## leaderboard 2.5.0 (2012-10-12)
* Added `members_from_rank_range` and `members_from_rank_range_in` methods to be able to retrieve members from a leaderboard in a given rank range.
View
@@ -119,30 +119,34 @@ Get some information about your leaderboard:
=> 1
```
-The `rank_member` call will also accept an optional hash of 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. Example:
+The `rank_member` call will also accept an optional string 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:
```ruby
-highscore_lb.rank_member('84849292', 1, {'username' => 'member_name'})
+require 'json'
+highscore_lb.rank_member('84849292', 1, JSON.generate({'username' => 'member_name'})
```
You can retrieve, update and remove the optional member data using the
`member_data_for`, `update_member_data` and `remove_member_data` calls. Example:
```ruby
-highscore_lb.member_data_for('84849292')
+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'})
- => "OK"
-highscore_lb.member_data_for('84849292')
+highscore_lb.update_member_data('84849292', JSON.generate({'last_updated' => Time.now, 'username' => 'updated_member_name'}))
+ => "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.
+
Get some information about a specific member(s) in the leaderboard:
```ruby
@@ -162,23 +166,10 @@ 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`, `around_me` and `ranked_in_list`.
-Valid options are `:with_scores`, `:with_rank`, `:with_member_data`, `:use_zero_index_for_rank`
-and `:page_size`. Below is an example of retrieving the first page in the leaderboard
-without ranks:
-
-```ruby
- highscore_lb.leaders(1, :with_rank => false)
- => [{:member=>"member_10", :score=>9.0}, {:member=>"member_9", :score=>7.0}, {:member=>"member_8", :score=>5.0}, {:member=>"member_7", :score=>3.0}, {:member=>"member_6", :score=>1.0}, {:member=>"member_5", :score=>0.0}, {:member=>"member_4", :score=>0.0}, {:member=>"member_3", :score=>0.0}, {:member=>"member_2", :score=>0.0}, {:member=>"member_1", :score=>0.0}]
-```
-
-Below is an example of retrieving the first page in the leaderboard without scores or ranks:
-
-```ruby
- highscore_lb.leaders(1, :with_scores => false, :with_rank => false)
- => [{:member=>"member_10"}, {:member=>"member_9"}, {:member=>"member_8"}, {:member=>"member_7"}, {:member=>"member_6"}, {:member=>"member_5"}, {:member=>"member_4"}, {:member=>"member_3"}, {:member=>"member_2"}, {:member=>"member_1"}]
-```
+
+You can pass various options to the calls `leaders`, `around_me` and `ranked_in_list`.
+Valid options are `:with_member_data`, `:page_size` and `:sort_option`. Valid values for
+`:sort_option` are `:none` (default), `:score` and `:rank`.
You can also use the `members` and `members_in` methods as aliases for the `leaders` and `leaders_in` methods.
@@ -231,6 +222,50 @@ 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_option` 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:
+
+```ruby
+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}]
+```
+
+```ruby
+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}]
+```
+
+### Conditionally rank a member in the leaderboard
+
+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 be `nil` 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
+
+```ruby
+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.
+
### Ranking multiple members in a leaderboard at once
Insert multiple data items for members and their associated scores:
View
@@ -10,7 +10,7 @@ end
task :default => :spec
-desc "Run the specs against Ruby 1.8.7, 1.9.2 and 1.9.3"
+desc "Run the specs against Ruby 1.8.7 and 1.9.3"
task :test_rubies do
- system "rvm 1.8.7@leaderboard_gem,1.9.2@leaderboard_gem,1.9.3@leaderboard_gem do rake spec"
+ system "rvm 1.8.7@leaderboard_gem,1.9.3@leaderboard_gem do rake spec"
end
Oops, something went wrong.

0 comments on commit 94591a6

Please sign in to comment.