leaderboard 3.7.0 → 3.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.markdown +4 -0
- data/README.markdown +3 -3
- data/lib/leaderboard/version.rb +1 -1
- data/lib/tie_ranking_leaderboard.rb +21 -0
- data/spec/competition_ranking_leaderboard_spec.rb +17 -0
- data/spec/reverse_competition_ranking_leaderboard_spec.rb +18 -0
- data/spec/reverse_tie_ranking_leaderboard_spec.rb +34 -0
- data/spec/tie_ranking_leaderboard_spec.rb +34 -0
- data/spec/version_spec.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7bde8fa5f70279295659c4d1d2eff551c105ccf8
|
4
|
+
data.tar.gz: 0de720bc5a777acb6c568ebcd5a62d99490b799f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 237db7f422c2df67dc63040304a49efc806ce716e4976d5f2b68c36c70c6fa42b9fdd349237376a998430fd9ccd5faed03c0bd2ba075a1650dcb34f0aa153d66
|
7
|
+
data.tar.gz: 26c645596ea59bc82c738e16d1c8313b2c87753ccc1936dff64d4a2a70277683eac8e5479844467815a7d68824432736e95ebbdf7960aadb8846ac39d012a6bf
|
data/CHANGELOG.markdown
CHANGED
data/README.markdown
CHANGED
@@ -131,7 +131,7 @@ encode a Hash of member data. Example:
|
|
131
131
|
|
132
132
|
```ruby
|
133
133
|
require 'json'
|
134
|
-
highscore_lb.rank_member('84849292', 1, JSON.generate({'username' => 'member_name'})
|
134
|
+
highscore_lb.rank_member('84849292', 1, JSON.generate({'username' => 'member_name'}))
|
135
135
|
```
|
136
136
|
|
137
137
|
You can retrieve, update and remove the optional member data using the
|
@@ -182,7 +182,7 @@ You can pass various options to the calls `leaders`, `all_leaders`, `around_me`,
|
|
182
182
|
* `:with_member_data` - `true` or `false` (default) to return the optional member data.
|
183
183
|
* `:page_size` - An integer value to change the page size for that call.
|
184
184
|
* `:members_only` - `true` or `false` (default) to return only the members without their score and rank.
|
185
|
-
* `:
|
185
|
+
* `:sort_by` - Valid values for `:sort_by` are `:none` (default), `:score` and `:rank`.
|
186
186
|
|
187
187
|
You can also use the `members` and `members_in` methods as aliases for the `leaders` and `leaders_in` methods.
|
188
188
|
|
@@ -234,7 +234,7 @@ members = highscore_lb.members_from_rank_range(1, 5)
|
|
234
234
|
=> [{: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}]
|
235
235
|
```
|
236
236
|
|
237
|
-
The option `:
|
237
|
+
The option `:sort_by` is useful for retrieving an arbitrary list of
|
238
238
|
members from a given leaderboard where you would like the data sorted
|
239
239
|
when returned. The follow examples demonstrate its use:
|
240
240
|
|
data/lib/leaderboard/version.rb
CHANGED
@@ -45,6 +45,27 @@ class TieRankingLeaderboard < Leaderboard
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
+
# Change the score for a member in the named leaderboard by a delta which can be positive or negative.
|
49
|
+
#
|
50
|
+
# @param leaderboard_name [String] Name of the leaderboard.
|
51
|
+
# @param member [String] Member name.
|
52
|
+
# @param delta [float] Score change.
|
53
|
+
def change_score_for_member_in(leaderboard_name, member, delta)
|
54
|
+
previous_score = score_for(member)
|
55
|
+
new_score = previous_score + delta
|
56
|
+
|
57
|
+
total_members_at_previous_score = @redis_connection.zrevrangebyscore(leaderboard_name, previous_score, previous_score)
|
58
|
+
|
59
|
+
@redis_connection.multi do |transaction|
|
60
|
+
transaction.zadd(leaderboard_name, new_score, member)
|
61
|
+
transaction.zadd(ties_leaderboard_key(leaderboard_name), new_score, new_score.to_f.to_s)
|
62
|
+
end
|
63
|
+
|
64
|
+
if total_members_at_previous_score.length == 1
|
65
|
+
@redis_connection.zrem(ties_leaderboard_key(leaderboard_name), previous_score.to_f.to_s)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
48
69
|
# Rank a member in the named leaderboard.
|
49
70
|
#
|
50
71
|
# @param leaderboard_name [String] Name of the leaderboard.
|
@@ -106,5 +106,22 @@ describe 'CompetitionRankingLeaderboard' do
|
|
106
106
|
|
107
107
|
leaderboard.disconnect
|
108
108
|
end
|
109
|
+
|
110
|
+
it 'should have the correct rankings and scores when using change_score_for' do
|
111
|
+
leaderboard = CompetitionRankingLeaderboard.new('ties', Leaderboard::DEFAULT_OPTIONS, {:host => "127.0.0.1", :db => 15})
|
112
|
+
|
113
|
+
leaderboard.rank_member('member_1', 50)
|
114
|
+
leaderboard.rank_member('member_2', 50)
|
115
|
+
leaderboard.rank_member('member_3', 30)
|
116
|
+
leaderboard.rank_member('member_4', 30)
|
117
|
+
leaderboard.rank_member('member_5', 10)
|
118
|
+
leaderboard.change_score_for('member_3', 10)
|
119
|
+
|
120
|
+
expect(leaderboard.rank_for('member_3')).to eq(3)
|
121
|
+
expect(leaderboard.rank_for('member_4')).to eq(4)
|
122
|
+
expect(leaderboard.score_for('member_3')).to eq(40.0)
|
123
|
+
|
124
|
+
leaderboard.disconnect
|
125
|
+
end
|
109
126
|
end
|
110
127
|
end
|
@@ -112,5 +112,23 @@ describe 'CompetitionRankingLeaderboard (reverse option)' do
|
|
112
112
|
|
113
113
|
leaderboard.disconnect
|
114
114
|
end
|
115
|
+
|
116
|
+
it 'should have the correct rankings and scores when using change_score_for' do
|
117
|
+
leaderboard = CompetitionRankingLeaderboard.new('ties', {:reverse => true}, {:host => "127.0.0.1", :db => 15})
|
118
|
+
|
119
|
+
leaderboard.rank_member('member_1', 50)
|
120
|
+
leaderboard.rank_member('member_2', 50)
|
121
|
+
leaderboard.rank_member('member_3', 30)
|
122
|
+
leaderboard.rank_member('member_4', 30)
|
123
|
+
leaderboard.rank_member('member_5', 10)
|
124
|
+
leaderboard.change_score_for('member_3', 10)
|
125
|
+
|
126
|
+
expect(leaderboard.rank_for('member_3')).to eq(3)
|
127
|
+
expect(leaderboard.rank_for('member_4')).to eq(2)
|
128
|
+
expect(leaderboard.score_for('member_3')).to eq(40.0)
|
129
|
+
|
130
|
+
leaderboard.disconnect
|
131
|
+
end
|
132
|
+
|
115
133
|
end
|
116
134
|
end
|
@@ -137,5 +137,39 @@ describe 'TieRankingLeaderboard (reverse option)' do
|
|
137
137
|
|
138
138
|
leaderboard.disconnect
|
139
139
|
end
|
140
|
+
|
141
|
+
it 'should have the correct rankings and scores when using change_score_for' do
|
142
|
+
leaderboard = TieRankingLeaderboard.new('ties', {:reverse => true}, {:host => "127.0.0.1", :db => 15})
|
143
|
+
|
144
|
+
leaderboard.rank_member('member_1', 50)
|
145
|
+
leaderboard.rank_member('member_2', 50)
|
146
|
+
leaderboard.rank_member('member_3', 30)
|
147
|
+
leaderboard.rank_member('member_4', 30)
|
148
|
+
leaderboard.rank_member('member_5', 10)
|
149
|
+
leaderboard.change_score_for('member_3', 10)
|
150
|
+
|
151
|
+
expect(leaderboard.rank_for('member_3')).to eq(3)
|
152
|
+
expect(leaderboard.rank_for('member_4')).to eq(2)
|
153
|
+
expect(leaderboard.score_for('member_3')).to eq(40.0)
|
154
|
+
|
155
|
+
leaderboard.disconnect
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'should have the correct rankings and scores when using change_score_for (varying scores)' do
|
159
|
+
leaderboard = TieRankingLeaderboard.new('ties', {:reverse => true}, {:host => "127.0.0.1", :db => 15})
|
160
|
+
|
161
|
+
leaderboard.rank_member('member_1', 5)
|
162
|
+
leaderboard.rank_member('member_2', 4)
|
163
|
+
leaderboard.rank_member('member_3', 3)
|
164
|
+
leaderboard.rank_member('member_4', 2)
|
165
|
+
leaderboard.rank_member('member_5', 1)
|
166
|
+
leaderboard.change_score_for('member_3', 0.5)
|
167
|
+
|
168
|
+
expect(leaderboard.rank_for('member_3')).to eq(3)
|
169
|
+
expect(leaderboard.rank_for('member_4')).to eq(2)
|
170
|
+
expect(leaderboard.score_for('member_3')).to eq(3.5)
|
171
|
+
|
172
|
+
leaderboard.disconnect
|
173
|
+
end
|
140
174
|
end
|
141
175
|
end
|
@@ -138,6 +138,40 @@ describe 'TieRankingLeaderboard' do
|
|
138
138
|
leaderboard.disconnect
|
139
139
|
end
|
140
140
|
|
141
|
+
it 'should have the correct rankings and scores when using change_score_for' do
|
142
|
+
leaderboard = TieRankingLeaderboard.new('ties', Leaderboard::DEFAULT_OPTIONS, {:host => "127.0.0.1", :db => 15})
|
143
|
+
|
144
|
+
leaderboard.rank_member('member_1', 50)
|
145
|
+
leaderboard.rank_member('member_2', 50)
|
146
|
+
leaderboard.rank_member('member_3', 30)
|
147
|
+
leaderboard.rank_member('member_4', 30)
|
148
|
+
leaderboard.rank_member('member_5', 10)
|
149
|
+
leaderboard.change_score_for('member_3', 10)
|
150
|
+
|
151
|
+
expect(leaderboard.rank_for('member_3')).to eq(2)
|
152
|
+
expect(leaderboard.rank_for('member_4')).to eq(3)
|
153
|
+
expect(leaderboard.score_for('member_3')).to eq(40.0)
|
154
|
+
|
155
|
+
leaderboard.disconnect
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'should have the correct rankings and scores when using change_score_for (varying scores)' do
|
159
|
+
leaderboard = TieRankingLeaderboard.new('ties', Leaderboard::DEFAULT_OPTIONS, {:host => "127.0.0.1", :db => 15})
|
160
|
+
|
161
|
+
leaderboard.rank_member('member_1', 5)
|
162
|
+
leaderboard.rank_member('member_2', 4)
|
163
|
+
leaderboard.rank_member('member_3', 3)
|
164
|
+
leaderboard.rank_member('member_4', 2)
|
165
|
+
leaderboard.rank_member('member_5', 1)
|
166
|
+
leaderboard.change_score_for('member_3', 0.5)
|
167
|
+
|
168
|
+
expect(leaderboard.rank_for('member_3')).to eq(3)
|
169
|
+
expect(leaderboard.rank_for('member_4')).to eq(4)
|
170
|
+
expect(leaderboard.score_for('member_3')).to eq(3.5)
|
171
|
+
|
172
|
+
leaderboard.disconnect
|
173
|
+
end
|
174
|
+
|
141
175
|
it 'should allow you to remove members in a given score range using #remove_members_in_score_range' do
|
142
176
|
@leaderboard = TieRankingLeaderboard.new('ties', Leaderboard::DEFAULT_OPTIONS, {:host => "127.0.0.1", :db => 15})
|
143
177
|
|
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.8.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: 2014-
|
11
|
+
date: 2014-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|