about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2021-10-11 13:41:08 -0400
committerBen Harris <ben@tilde.team>2021-10-11 13:41:08 -0400
commitc0a2217052dec8fc995949732c9e28a15c9319fd (patch)
tree5d7f36bfea48c16c9e30e8cd63dd4c56ad9f2287
parent848c59f15e83c0571be956726284d8d9180679b4 (diff)
maketeams is now generic for any number of players
-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