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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73f4fb333c04f1aef154ecf0469cdea2f3602c24
4
- data.tar.gz: 6ada4c6ec175b491b2dec536b4961e3bd472eb54
3
+ metadata.gz: 7bde8fa5f70279295659c4d1d2eff551c105ccf8
4
+ data.tar.gz: 0de720bc5a777acb6c568ebcd5a62d99490b799f
5
5
  SHA512:
6
- metadata.gz: 104e46c727ed24d53883a4f45260af84fc3518d863ef662ccafab806152aea83aea40a5a740c80452535c2dcf25853cd54ce52aa7b398f9b019638654f44e99c
7
- data.tar.gz: 96f0f5756c4a17bd0dd9e573a3e8b682f961168f7a47e2fb0f0100cd6ec427457afdacb194367bf01a6f38eb5032bdcd3458f344a8683450dc8697a5a31d62c6
6
+ metadata.gz: 237db7f422c2df67dc63040304a49efc806ce716e4976d5f2b68c36c70c6fa42b9fdd349237376a998430fd9ccd5faed03c0bd2ba075a1650dcb34f0aa153d66
7
+ data.tar.gz: 26c645596ea59bc82c738e16d1c8313b2c87753ccc1936dff64d4a2a70277683eac8e5479844467815a7d68824432736e95ebbdf7960aadb8846ac39d012a6bf
data/CHANGELOG.markdown CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 3.8.0 (2014-11-07)
4
+
5
+ * Add support for `change_score_for(...)` in the `TieRankingLeaderboard` class.
6
+
3
7
  ## 3.7.0 (2014-07-28)
4
8
 
5
9
  * Add support for tie handling in leaderboards [#46](https://github.com/agoragames/leaderboard/pull/46)
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
- * `:sort_option` - Valid values for `:sort_option` are `:none` (default), `:score` and `:rank`.
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 `:sort_option` is useful for retrieving an arbitrary list of
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
 
@@ -1,3 +1,3 @@
1
1
  class Leaderboard
2
- VERSION = '3.7.0'.freeze
2
+ VERSION = '3.8.0'.freeze
3
3
  end
@@ -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
@@ -2,6 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  describe 'Leaderboard::VERSION' do
4
4
  it 'should be the correct version' do
5
- expect(Leaderboard::VERSION).to eq('3.7.0')
5
+ expect(Leaderboard::VERSION).to eq('3.8.0')
6
6
  end
7
7
  end
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.7.0
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-07-28 00:00:00.000000000 Z
11
+ date: 2014-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis