about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/pages_controller.rb33
1 files changed, 16 insertions, 17 deletions
diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb
index 76fe03a..7ae535b 100644
--- a/app/controllers/pages_controller.rb
+++ b/app/controllers/pages_controller.rb
@@ -11,23 +11,22 @@ class PagesController < ApplicationController
 
   private
     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
-        when 5
-          [r.shift(3), r.shift(2)]
-        when 6
-          r.each_slice(3)
-        when 9
-          r.each_slice(3)
-        when 10
-          [r.shift(4), r.shift(3), r.shift(3)]
-        when 13
-          [r.shift(4), r.shift(3), r.shift(3), r.shift(3)]
-        when 14
-          [r.shift(4), r.shift(4), r.shift(3), r.shift(3)]
-        else
-          r.each_slice(4)
-        end
+	  teams = []
+	  while r.size > 0
+		if r.size % 4 == 0
+		  teams << r.shift(4)
+		elsif r.size % 4 == 2
+		  teams << r.shift(3)
+		  teams << r.shift(3)
+		elsif r.size % 4 == 3
+		  teams << r.shift(3)
+		else
+		  teams << r.shift(4)
+		end
+	  end
+
+	  teams.sort { |a, b| b.length <=> a.length }
     end
 end