Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scores_percent and scores_percent_in #66

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,3 @@ Unofficially supported (they need some feature parity love):
## Copyright

Copyright (c) 2011-2017 David Czarnecki. See LICENSE.txt for further details.

19 changes: 19 additions & 0 deletions lib/leaderboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,25 @@ def total_scores_in(leaderboard_name)
all_leaders_from(leaderboard_name).map{|hash| hash[:score] }.inject(0, :+)
end

# Percent of total_scores in leaderboard for this member, slice of the pie
# @param member [String] Member name.
#
# @return Percentage of total of scores
def scores_percent(member)
scores_percent_in(@leaderboard_name, member)
end

# Percent of total_scores in named leaderboard for this member, slice of the pie
# @param member [String] Member name.
# @param leaderboard_name Name of the leaderboard.
#
# @return Percentage of total of scores
def scores_percent_in(leaderboard_name, member)
member_score = score_for_in(leaderboard_name, member)
total_scores = total_scores_in(leaderboard_name)
(member_score / total_scores) * 100
end

# Change the score for a member in the leaderboard by a score delta which can be positive or negative.
#
# @param member [String] Member name.
Expand Down
8 changes: 8 additions & 0 deletions spec/leaderboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -861,4 +861,12 @@

expect(@leaderboard.total_scores()).to eq(325)
end

it 'should return the percentage of all scores in the leaderboard' do
rank_members_in_leaderboard(5)

expect(@leaderboard.scores_percent('member_1')).to eq(6.666666666666667)
expect(@leaderboard.scores_percent('member_2')).to eq(13.333333333333334)
end

end