-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make User.find_by_slack_mention a bang method that fails if the user …
…is not found\n refactor Leaderboard into its own class
- Loading branch information
1 parent
1c195b5
commit f6315ba
Showing
10 changed files
with
130 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
require 'models/challenge_state' | ||
require 'models/challenge' | ||
require 'models/match' | ||
require 'models/leaderboard' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Leaderboard | ||
def self.standings(max = nil) | ||
players = User.all.any_of({ :wins.gt => 0 }, :losses.gt => 0) | ||
players = players.limit(max) if max && max > 0 | ||
players = players.desc(:elo).asc(:_id).to_a | ||
rank = 1 | ||
standings = [] | ||
players.each_with_index do |player, index| | ||
rank += 1 if index > 0 && players[index - 1].elo != player.elo | ||
standings << [rank, player] | ||
end | ||
standings | ||
end | ||
|
||
def self.leaderboard(max = 3) | ||
stringify_leaderboard(standings(max)) | ||
end | ||
|
||
def self.rank_section(users) | ||
leaderboard = standings.drop_while { |row| !users.member? row.last } | ||
leaderboard = leaderboard.reverse.drop_while { |row| !users.member? row.last }.reverse | ||
stringify_leaderboard(leaderboard) | ||
end | ||
|
||
def self.stringify_leaderboard(leaderboard) | ||
leaderboard.any? ? leaderboard.map { |a| a.join('. ') }.join("\n") : 'No players.' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'spec_helper' | ||
|
||
describe Leaderboard do | ||
context '#standings' do | ||
it 'returns an empty list' do | ||
expect(Leaderboard.standings).to eq [] | ||
end | ||
it 'ranks incrementally' do | ||
user1 = Fabricate(:user, elo: 1, wins: 1, losses: 1) | ||
user2 = Fabricate(:user, elo: 2, wins: 1, losses: 1) | ||
expect(Leaderboard.standings).to eq [[1, user2], [2, user1]] | ||
end | ||
it 'ranks players with the same elo equally' do | ||
user1 = Fabricate(:user, elo: 1, wins: 1, losses: 1) | ||
user2 = Fabricate(:user, elo: 2, wins: 1, losses: 1) | ||
user3 = Fabricate(:user, elo: 1, wins: 1, losses: 1) | ||
expect(Leaderboard.standings).to eq [[1, user2], [2, user1], [2, user3]] | ||
end | ||
it 'limits to max' do | ||
Fabricate(:user, elo: 1, wins: 1, losses: 1) | ||
user2 = Fabricate(:user, elo: 2, wins: 1, losses: 1) | ||
Fabricate(:user, elo: 1, wins: 1, losses: 1) | ||
expect(Leaderboard.standings(1)).to eq [[1, user2]] | ||
end | ||
it 'ignores players without wins or losses' do | ||
user1 = Fabricate(:user, elo: 1, wins: 1, losses: 1) | ||
Fabricate(:user, elo: 2, wins: 0, losses: 0) | ||
expect(Leaderboard.standings).to eq [[1, user1]] | ||
end | ||
it 'first game' do | ||
user1 = Fabricate(:user, elo: 48, wins: 1, losses: 0) | ||
user2 = Fabricate(:user, elo: -48, wins: 0, losses: 1) | ||
expect(Leaderboard.standings).to eq [[1, user1], [2, user2]] | ||
end | ||
end | ||
context '#leaderboard' do | ||
it 'returns no players' do | ||
expect(Leaderboard.leaderboard).to eq 'No players.' | ||
end | ||
it 'returns formatted leaderboard' do | ||
user1 = Fabricate(:user, elo: 48, wins: 1, losses: 0) | ||
user2 = Fabricate(:user, elo: -48, wins: 0, losses: 1) | ||
expect(Leaderboard.leaderboard).to eq "1. #{user1}\n2. #{user2}" | ||
end | ||
end | ||
context '#rank_section' do | ||
it 'returns no players' do | ||
expect(Leaderboard.leaderboard).to eq 'No players.' | ||
end | ||
end | ||
context '#stringify_leaderboard' do | ||
it 'joins arrays of arrays' do | ||
expect(Leaderboard.stringify_leaderboard([[1, 'foo'], [2, 'bar']])).to eq "1. foo\n2. bar" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters