add team picker

This commit is contained in:
Ben Harris 2021-08-23 00:52:23 -04:00
parent 9a2d1bbf9c
commit 5cbc48b9a0
9 changed files with 57 additions and 19 deletions

View File

@ -1,8 +1,35 @@
class PagesController < ApplicationController class PagesController < ApplicationController
def index def index
@players = Player.all @players = Player.all
group_size = Player.count
@groups = Player.order(Arel.sql("RANDOM()")).each_slice
end end
def teampicker
@groups = maketeams
end
private
def random
Player.where("strikes < 4").order(Arel.sql("RANDOM()"))
end
def maketeams
r = random.to_a
case Player.count { |p| p.active? }
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
end
end end

View File

@ -1,2 +1,7 @@
class Player < ApplicationRecord class Player < ApplicationRecord
validates :name, presence: true
def active?
self.strikes < 4 && self.paid
end
end end

View File

@ -15,7 +15,8 @@
<%= link_to "Home", root_path %> ~ <%= link_to "Home", root_path %> ~
<%= link_to "Tables", machines_path %> ~ <%= link_to "Tables", machines_path %> ~
<%= link_to "Players", players_path %> ~ <%= link_to "Players", players_path %> ~
<%= link_to "Pick Random", controller: "machines", action: "random" %> <%= link_to "Pick Table", controller: "machines", action: "random" %> ~
<%= link_to "Pick Teams", controller: "pages", action: "teampicker" %>
</nav> </nav>
<hr> <hr>

View File

@ -10,22 +10,10 @@
<tbody> <tbody>
<% @players.order("strikes").each do |player| %> <% @players.order("strikes").each do |player| %>
<tr> <tr>
<td><%= player.name %></td> <td><%= link_to player.name, edit_player_path(player) %></td>
<td><%= player.paid %></td> <td><%= player.paid %></td>
<td><%= player.strikes %></td> <td><%= player.strikes %></td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
<h2>teampicker</h2>
<% @players.order(Arel.sql("RANDOM()")).each_slice(@players.count % 4 == 0 ? 4 : 3).each.with_index(1) do |slice, i| %>
<h3>Team <%= i %></h3>
<ul>
<% slice.each do |player| %>
<li><%= player.name %></li>
<% end %>
</ul>
<% end %>

View File

@ -0,0 +1,10 @@
<h2>teampicker</h2>
<% @groups.each.with_index(1) do |slice, i| %>
<h3>Team <%= i %></h3>
<ul>
<% slice.each do |player| %>
<li><%= link_to player.name, edit_player_path(player) %></li>
<% end %>
</ul>
<% end %>

View File

@ -1,4 +1,4 @@
<h1>Edit Player</h1> <h1>Edit <%= @player.name %></h1>
<%= render "form", machine: @player %> <%= render "form", machine: @player %>

View File

@ -1,9 +1,10 @@
<h1>Pinball Players</h1> <h1>Pinball Players</h1>
<p><%= @players.count %> available players</p>
<ul> <ul>
<% @players.each do |player| %> <% @players.each do |player| %>
<li> <li>
<%= link_to player.name, player %> <%= link_to player.name, edit_player_path(player) %>
</li> </li>
<% end %> <% end %>
</ul> </ul>

View File

@ -1,5 +1,6 @@
Rails.application.routes.draw do Rails.application.routes.draw do
root 'pages#index' root 'pages#index'
get 'teampicker', to: 'pages#teampicker'
get 'random', to: 'machines#random' get 'random', to: 'machines#random'
resources :machines resources :machines
resources :players resources :players

View File

@ -0,0 +1,5 @@
class SetPlayerStrikesDefaultToZero < ActiveRecord::Migration[6.0]
def change
change_column_default :players, :strikes, from: nil, to: 0
end
end