leaderboard 3.4.0 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/CHANGELOG.markdown +4 -0
- data/LICENSE.txt +1 -1
- data/README.markdown +18 -17
- data/lib/leaderboard.rb +21 -10
- data/lib/leaderboard/version.rb +1 -1
- data/spec/leaderboard_spec.rb +24 -0
- data/spec/version_spec.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZjY2OTAyMGJjZTE2NmFiNjQyYTdjZjUyNmUwMGUzZDc4NGM1ZDY5NQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MDJhZTE4MTAwZTU0ZjM4OTUzNGJhY2ZjMGVmZTZlMzE0ZGNjNjY5Yw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NmQ2ZThlMDBjNDA4YmNhOWZlM2JlOWMyMWZlMTYzN2ZlZmMyM2FiMWIzMTAx
|
10
|
+
ODJhMzBhMjNkYzAzZGI5YTZmNWQ3YTRjM2RiYTFhMjRlMDkxNjU4M2NlMGI2
|
11
|
+
ZDExOWJjMjliODdhMTY4NTBjYjUwNGNmNTYyNGYxZmU2MTg5MDc=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NTM4MTM3ZmNiYWYyMTE1MGRmMGJhZmQzNjIxNGM2NGI1MzM4NTg4NzM5YWRk
|
14
|
+
MGVkNDg5N2U5ODY1M2IyNzVjYjNmZDYyODMzNGRjMzZlYjcwMzYxZDRkYjI2
|
15
|
+
MjI4MzBkMDA4NDNlMTNmN2IwZjcyNWMyNzFmZDg1MzhhMWVlYWY=
|
data/CHANGELOG.markdown
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 3.5.0 (2014-01-24)
|
4
|
+
|
5
|
+
* Allow for custom keys to be set for customizing the data returned from calls like `leaders` or `around_me` [#44](https://github.com/agoragames/leaderboard/pull/44)
|
6
|
+
|
3
7
|
## 3.4.0 (2013-11-12)
|
4
8
|
|
5
9
|
* Added `score_for_percentile` method to be able to calculate the score for a given percentile value in the leaderboard.
|
data/LICENSE.txt
CHANGED
data/README.markdown
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Leaderboards backed by [Redis](http://redis.io) in Ruby.
|
4
4
|
|
5
|
-
Builds off ideas proposed in http://
|
5
|
+
Builds off ideas proposed in http://www.agoragames.com/blog/2011/01/01/creating-high-score-tables-leaderboards-using-redis/.
|
6
6
|
|
7
7
|
[](http://travis-ci.org/agoragames/leaderboard)
|
8
8
|
|
@@ -23,8 +23,6 @@ check out the [Redis documentation](http://redis.io/documentation).
|
|
23
23
|
|
24
24
|
The gem has been built and tested under Ruby 1.8.7, Ruby 1.9.2 and Ruby 1.9.3.
|
25
25
|
|
26
|
-
The gem is compatible with Redis 2.4.x and Redis 2.6.x.
|
27
|
-
|
28
26
|
## Usage
|
29
27
|
|
30
28
|
### Creating a leaderboard
|
@@ -42,15 +40,6 @@ Create a new leaderboard or attach to an existing leaderboard named 'highscores'
|
|
42
40
|
=> #<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)>>
|
43
41
|
```
|
44
42
|
|
45
|
-
If you need to pass in options for Redis, you can do this in the initializer:
|
46
|
-
|
47
|
-
```ruby
|
48
|
-
redis_options = {:host => 'localhost', :port => 6379, :db => 1}
|
49
|
-
=> {:host=>"localhost", :port=>6379, :db=>1}
|
50
|
-
highscore_lb = Leaderboard.new('highscores', Leaderboard::DEFAULT_OPTIONS, redis_options)
|
51
|
-
=> #<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)>>
|
52
|
-
```
|
53
|
-
|
54
43
|
### Defining leaderboard options
|
55
44
|
|
56
45
|
The `Leaderboard::DEFAULT_OPTIONS` are as follows:
|
@@ -58,16 +47,28 @@ The `Leaderboard::DEFAULT_OPTIONS` are as follows:
|
|
58
47
|
```ruby
|
59
48
|
DEFAULT_OPTIONS = {
|
60
49
|
:page_size => DEFAULT_PAGE_SIZE,
|
61
|
-
:reverse => false
|
50
|
+
:reverse => false,
|
51
|
+
:member_key => :member,
|
52
|
+
:rank_key => :rank,
|
53
|
+
:score_key => :score,
|
54
|
+
:member_data_key => :member_data
|
62
55
|
}
|
63
56
|
```
|
64
57
|
|
65
58
|
The `DEFAULT_PAGE_SIZE` is 25.
|
66
59
|
|
67
|
-
You would use the option, `:reverse => true`, if you wanted a leaderboard sorted from lowest-to-highest score. You
|
68
|
-
|
60
|
+
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`.
|
61
|
+
|
62
|
+
If you need to pass in options for Redis, you can do this in the initializer:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
redis_options = {:host => 'localhost', :port => 6379, :db => 1}
|
66
|
+
=> {:host=>"localhost", :port=>6379, :db=>1}
|
67
|
+
highscore_lb = Leaderboard.new('highscores', Leaderboard::DEFAULT_OPTIONS, redis_options)
|
68
|
+
=> #<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)>>
|
69
|
+
```
|
69
70
|
|
70
|
-
You can pass in an existing connection to Redis using `:redis_connection` in the
|
71
|
+
You can pass in an existing connection to Redis using `:redis_connection` in the `redis_options` hash:
|
71
72
|
|
72
73
|
```ruby
|
73
74
|
redis = Redis.new
|
@@ -446,5 +447,5 @@ Unofficially supported (they need some feature parity love):
|
|
446
447
|
|
447
448
|
## Copyright
|
448
449
|
|
449
|
-
Copyright (c) 2011-
|
450
|
+
Copyright (c) 2011-2014 David Czarnecki. See LICENSE.txt for further details.
|
450
451
|
|
data/lib/leaderboard.rb
CHANGED
@@ -10,7 +10,11 @@ class Leaderboard
|
|
10
10
|
# highest-to-lowest order.
|
11
11
|
DEFAULT_OPTIONS = {
|
12
12
|
:page_size => DEFAULT_PAGE_SIZE,
|
13
|
-
:reverse => false
|
13
|
+
:reverse => false,
|
14
|
+
:member_key => :member,
|
15
|
+
:rank_key => :rank,
|
16
|
+
:score_key => :score,
|
17
|
+
:member_data_key => :member_data
|
14
18
|
}
|
15
19
|
|
16
20
|
# Default Redis host: localhost
|
@@ -60,13 +64,20 @@ class Leaderboard
|
|
60
64
|
# leaderboard = Leaderboard.new('highscores')
|
61
65
|
# leaderboard = Leaderboard.new('highscores', {:page_size => 10})
|
62
66
|
def initialize(leaderboard_name, options = DEFAULT_OPTIONS, redis_options = DEFAULT_REDIS_OPTIONS)
|
67
|
+
leaderboard_options = DEFAULT_OPTIONS.dup
|
68
|
+
leaderboard_options.merge!(options)
|
69
|
+
|
63
70
|
@leaderboard_name = leaderboard_name
|
64
71
|
|
65
|
-
@reverse =
|
66
|
-
@page_size =
|
72
|
+
@reverse = leaderboard_options[:reverse]
|
73
|
+
@page_size = leaderboard_options[:page_size]
|
67
74
|
if @page_size.nil? || @page_size < 1
|
68
75
|
@page_size = DEFAULT_PAGE_SIZE
|
69
76
|
end
|
77
|
+
@member_key = leaderboard_options[:member_key]
|
78
|
+
@rank_key = leaderboard_options[:rank_key]
|
79
|
+
@score_key = leaderboard_options[:score_key]
|
80
|
+
@member_data_key = leaderboard_options[:member_data_key]
|
70
81
|
|
71
82
|
@redis_connection = redis_options[:redis_connection]
|
72
83
|
unless @redis_connection.nil?
|
@@ -440,7 +451,7 @@ class Leaderboard
|
|
440
451
|
responses[0] = responses[0].to_f if responses[0]
|
441
452
|
responses[1] = responses[1] + 1 rescue nil
|
442
453
|
|
443
|
-
{
|
454
|
+
{@member_key => member, @score_key => responses[0], @rank_key => responses[1]}
|
444
455
|
end
|
445
456
|
|
446
457
|
# Remove members from the leaderboard in a given score range.
|
@@ -912,14 +923,14 @@ class Leaderboard
|
|
912
923
|
|
913
924
|
members.each_with_index do |member, index|
|
914
925
|
data = {}
|
915
|
-
data[
|
926
|
+
data[@member_key] = member
|
916
927
|
unless leaderboard_options[:members_only]
|
917
|
-
data[
|
918
|
-
data[
|
928
|
+
data[@rank_key] = responses[index * 2] + 1 rescue nil
|
929
|
+
data[@score_key] = responses[index * 2 + 1].to_f if responses[index * 2 + 1]
|
919
930
|
end
|
920
931
|
|
921
932
|
if leaderboard_options[:with_member_data]
|
922
|
-
data[
|
933
|
+
data[@member_data_key] = member_data_for_in(leaderboard_name, member)
|
923
934
|
end
|
924
935
|
|
925
936
|
ranks_for_members << data
|
@@ -927,9 +938,9 @@ class Leaderboard
|
|
927
938
|
|
928
939
|
case leaderboard_options[:sort_by]
|
929
940
|
when :rank
|
930
|
-
ranks_for_members = ranks_for_members.sort_by { |member| member[
|
941
|
+
ranks_for_members = ranks_for_members.sort_by { |member| member[@rank_key] }
|
931
942
|
when :score
|
932
|
-
ranks_for_members = ranks_for_members.sort_by { |member| member[
|
943
|
+
ranks_for_members = ranks_for_members.sort_by { |member| member[@score_key] }
|
933
944
|
end
|
934
945
|
|
935
946
|
ranks_for_members
|
data/lib/leaderboard/version.rb
CHANGED
data/spec/leaderboard_spec.rb
CHANGED
@@ -735,4 +735,28 @@ describe 'Leaderboard' do
|
|
735
735
|
@leaderboard.leaders_in('highscores', 1).size.should eql(1)
|
736
736
|
@leaderboard.leaders_in('more_highscores', 1).size.should eql(1)
|
737
737
|
end
|
738
|
+
|
739
|
+
it 'should allow you to set custom keys for member, score, rank and member_data' do
|
740
|
+
@leaderboard = Leaderboard.new('name',
|
741
|
+
{
|
742
|
+
:member_key => :custom_member_key,
|
743
|
+
:rank_key => :custom_rank_key,
|
744
|
+
:score_key => :custom_score_key,
|
745
|
+
:member_data_key => :custom_member_data_key
|
746
|
+
},
|
747
|
+
{:host => "127.0.0.1", :db => 15})
|
748
|
+
|
749
|
+
rank_members_in_leaderboard(5)
|
750
|
+
leaders = @leaderboard.leaders(1, :with_member_data => true)
|
751
|
+
leaders.each do |leader|
|
752
|
+
leader[:custom_member_key].should_not be_nil
|
753
|
+
leader[:member].should be_nil
|
754
|
+
leader[:custom_score_key].should_not be_nil
|
755
|
+
leader[:score].should be_nil
|
756
|
+
leader[:custom_rank_key].should_not be_nil
|
757
|
+
leader[:rank].should be_nil
|
758
|
+
leader[:custom_member_data_key].should_not be_nil
|
759
|
+
leader[:member_data].should be_nil
|
760
|
+
end
|
761
|
+
end
|
738
762
|
end
|
data/spec/version_spec.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: leaderboard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Czarnecki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
96
|
version: '0'
|
97
97
|
requirements: []
|
98
98
|
rubyforge_project: leaderboard
|
99
|
-
rubygems_version: 2.
|
99
|
+
rubygems_version: 2.2.0
|
100
100
|
signing_key:
|
101
101
|
specification_version: 4
|
102
102
|
summary: Leaderboards backed by Redis in Ruby
|