maketeams is now generic for any number of players

This commit is contained in:
Ben Harris 2021-10-11 13:41:08 -04:00
parent 848c59f15e
commit c0a2217052
1 changed files with 16 additions and 17 deletions

View File

@ -11,23 +11,22 @@ class PagesController < ApplicationController
private private
def maketeams def maketeams
r = Player.where("strikes < 3").order(Arel.sql("RANDOM()")).to_a r = Player.where(strikes: 0...3).order(Arel.sql("RANDOM()")).to_a
groups = case r.size teams = []
when 5 while r.size > 0
[r.shift(3), r.shift(2)] if r.size % 4 == 0
when 6 teams << r.shift(4)
r.each_slice(3) elsif r.size % 4 == 2
when 9 teams << r.shift(3)
r.each_slice(3) teams << r.shift(3)
when 10 elsif r.size % 4 == 3
[r.shift(4), r.shift(3), r.shift(3)] teams << r.shift(3)
when 13 else
[r.shift(4), r.shift(3), r.shift(3), r.shift(3)] teams << r.shift(4)
when 14 end
[r.shift(4), r.shift(4), r.shift(3), r.shift(3)] end
else
r.each_slice(4) teams.sort { |a, b| b.length <=> a.length }
end
end end
end end