Skip to content

Commit

Permalink
Refactor TeamPicker.calculate_fair_players
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesshipton committed Mar 17, 2012
1 parent 4fa8825 commit 019a2a4
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/football-manager/team_picker.rb
@@ -1,17 +1,9 @@
module FootballManager
class TeamPicker
def self.calculate_fair_teams(players)
# players_in_descending_order = players.sort { |a,b| b.skill <=> a.skill }
players_in_descending_order = []

5.downto(1) do |i|
players_in_descending_order <<
players.select { |player| player.skill == i }.sort_by {rand}
end

team_a, team_b = FootballManager::Team.new, FootballManager::Team.new

players_in_descending_order.flatten.partition do |player|
players_in_descending_order(players).each do |player|
if team_a.empty? || team_a.points <= team_b.points
team_a << player
else
Expand All @@ -21,6 +13,13 @@ def self.calculate_fair_teams(players)

return [team_a, team_b]
end

private
def self.players_in_descending_order(players)
5.downto(1).collect do |i|
players.select { |player| player.skill == i }.sort_by {rand}
end.flatten!
end
end
end

0 comments on commit 019a2a4

Please sign in to comment.