add randomizer

This commit is contained in:
Ben Harris 2022-01-15 14:08:29 -05:00
parent 1d9a622303
commit c5b9fe77a8
Signed by: ben
GPG Key ID: 4E0AF802FFF7960C
15 changed files with 88 additions and 44 deletions

View File

@ -25,7 +25,7 @@ class MachinesController < ApplicationController
respond_to do |format| respond_to do |format|
if @machine.save if @machine.save
format.html { redirect_to machines_url, notice: 'Machine was successfully created.' } format.html { redirect_to machines_url, notice: "Added #{@machine.name} #{@machine.edition}." }
format.json { render :show, status: :created, location: @machine } format.json { render :show, status: :created, location: @machine }
else else
format.html { render :new, status: :unprocessable_entity } format.html { render :new, status: :unprocessable_entity }
@ -38,7 +38,7 @@ class MachinesController < ApplicationController
def update def update
respond_to do |format| respond_to do |format|
if @machine.update(machine_params) if @machine.update(machine_params)
format.html { redirect_to machines_url, notice: 'Machine was successfully updated.' } format.html { redirect_to machines_url, notice: "Updated #{@machine.name} #{@machine.edition}." }
format.json { render :show, status: :ok, location: @machine } format.json { render :show, status: :ok, location: @machine }
else else
format.html { render :show, status: :unprocessable_entity } format.html { render :show, status: :unprocessable_entity }

View File

@ -0,0 +1,35 @@
# frozen_string_literal: true
class PagesController < ApplicationController
def index; end
def randomize
@groups = make_teams
@machines = Machine.limit(@groups.size).order(Arel.sql('RANDOM()'))
end
private
def make_teams
r = Player.order(Arel.sql('RANDOM()')).to_a
teams = []
until r.empty?
if (r.size % 4).zero?
teams << r.shift(4)
elsif r.size.even? && (r.size > 2)
teams << r.shift(3)
teams << r.shift(3)
elsif (r.size % 3).zero?
teams << r.shift(3)
elsif r.size == 5
teams << r.shift(3)
teams << r.shift(2)
else
teams << r.shift(4)
end
end
teams.sort { |a, b| b.length <=> a.length }
end
end

View File

@ -22,7 +22,7 @@ class PlayersController < ApplicationController
respond_to do |format| respond_to do |format|
if @player.save if @player.save
format.html { redirect_to players_url, notice: 'Player was successfully created.' } format.html { redirect_to players_url, notice: "Added #{@player.name}." }
format.json { render :show, status: :created, location: @player } format.json { render :show, status: :created, location: @player }
else else
format.html { render :new, status: :unprocessable_entity } format.html { render :new, status: :unprocessable_entity }
@ -35,7 +35,7 @@ class PlayersController < ApplicationController
def update def update
respond_to do |format| respond_to do |format|
if @player.update(player_params) if @player.update(player_params)
format.html { redirect_to players_url, notice: 'Player was successfully updated.' } format.html { redirect_to players_url, notice: "Updated #{@player.name}." }
format.json { render :show, status: :ok, location: @player } format.json { render :show, status: :ok, location: @player }
else else
format.html { render :show, status: :unprocessable_entity } format.html { render :show, status: :unprocessable_entity }

View File

@ -0,0 +1,3 @@
# frozen_string_literal: true
module PagesHelper
end

View File

@ -12,8 +12,10 @@
<body> <body>
<nav> <nav>
<%= link_to "Pins", machines_url %> <%= link_to 'Home', '/' %>
<%= link_to "Players", players_url %> <%= link_to 'Pins', machines_url %>
<%= link_to 'Players', players_url %>
<%= link_to 'Randomize', controller: 'pages', action: 'randomize' %>
</nav> </nav>
<%= yield %> <%= yield %>

View File

@ -8,4 +8,5 @@
<% end %> <% end %>
</div> </div>
<%= link_to "New machine", new_machine_path %> <hr>
<%= link_to "Add a pin", new_machine_path %>

View File

@ -3,7 +3,5 @@
<%= render "form", machine: @machine %> <%= render "form", machine: @machine %>
<div> <div>
<%= link_to "Back to machines", machines_path %> <%= button_to "Delete #{@machine.name}", @machine, method: :delete %>
<%= button_to "Destroy this machine", @machine, method: :delete %>
</div> </div>

View File

@ -0,0 +1,2 @@
<h1>Knockout</h1>
<p>To start the tournament, add players and make sure that the pins are up to date. Then hit randomize to automatically pick teams!</p>

View File

@ -0,0 +1,10 @@
<h1>Randomize</h1>
<% @groups.each.with_index do |slice, i| %>
<h2><%= @machines[i].name %> <small><%= @machines[i].edition %></small></h2>
<ol>
<% slice.each do |player| %>
<li><%= link_to player.name, player_path(player) %></li>
<% end %>
</ol>
<% end %>

View File

@ -8,4 +8,5 @@
<% end %> <% end %>
</div> </div>
<%= link_to "New player", new_player_path %> <hr>
<%= link_to "Add a player", new_player_path %>

View File

@ -3,7 +3,5 @@
<%= render "form", player: @player %> <%= render "form", player: @player %>
<div> <div>
<%= link_to "Back to players", players_path %> <%= button_to "Delete #{@player.name}", @player, method: :delete %>
<%= button_to "Destroy this player", @player, method: :delete %>
</div> </div>

View File

@ -1,10 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
Rails.application.routes.draw do Rails.application.routes.draw do
get 'randomize', to: 'pages#randomize'
resources :machines resources :machines
resources :players resources :players
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Defines the root path route ("/") root 'pages#index'
# root "articles#index"
end end

25
db/schema.rb generated
View File

@ -1,3 +1,4 @@
# frozen_string_literal: true
# This file is auto-generated from the current state of the database. Instead # This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to # of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition. # incrementally modify your database, and then regenerate this schema definition.
@ -10,21 +11,21 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_01_15_164820) do ActiveRecord::Schema.define(version: 20_220_115_164_820) do
create_table "machines", force: :cascade do |t| create_table 'machines', force: :cascade do |t|
t.string "name" t.string 'name'
t.string "edition" t.string 'edition'
t.datetime "created_at", precision: 6, null: false t.datetime 'created_at', precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false t.datetime 'updated_at', precision: 6, null: false
end end
create_table "players", force: :cascade do |t| create_table 'players', force: :cascade do |t|
t.string "name" t.string 'name'
t.boolean "paid" t.boolean 'paid'
t.integer "strikes" t.integer 'strikes'
t.datetime "created_at", precision: 6, null: false t.datetime 'created_at', precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false t.datetime 'updated_at', precision: 6, null: false
end end
end end

View File

@ -9,36 +9,33 @@ class MachinesTest < ApplicationSystemTestCase
test 'visiting the index' do test 'visiting the index' do
visit machines_url visit machines_url
assert_selector 'h1', text: 'Machines' assert_selector 'h1', text: 'Pins'
end end
test 'should create machine' do test 'should create machine' do
visit machines_url visit machines_url
click_on 'New machine' click_on 'Add a pin'
fill_in 'Edition', with: @machine.edition fill_in 'Edition', with: @machine.edition
fill_in 'Name', with: @machine.name fill_in 'Name', with: @machine.name
click_on 'Create Machine' click_on 'Create Machine'
assert_text 'Machine was successfully created' assert_text "Added #{@machine.name} #{@machine.edition}."
click_on 'Back'
end end
test 'should update Machine' do test 'should update Machine' do
visit machine_url(@machine) visit machine_url(@machine)
click_on 'Edit this machine', match: :first
fill_in 'Edition', with: @machine.edition fill_in 'Edition', with: @machine.edition
fill_in 'Name', with: @machine.name fill_in 'Name', with: @machine.name
click_on 'Update Machine' click_on 'Update Machine'
assert_text 'Machine was successfully updated' assert_text "Updated #{@machine.name} #{@machine.edition}."
click_on 'Back'
end end
test 'should destroy Machine' do test 'should destroy Machine' do
visit machine_url(@machine) visit machine_url(@machine)
click_on 'Destroy this machine', match: :first click_on "Delete #{@machine.name}", match: :first
assert_text 'Machine was successfully destroyed' assert_text 'Machine was successfully destroyed'
end end

View File

@ -14,33 +14,30 @@ class PlayersTest < ApplicationSystemTestCase
test 'should create player' do test 'should create player' do
visit players_url visit players_url
click_on 'New player' click_on 'Add a player'
fill_in 'Name', with: @player.name fill_in 'Name', with: @player.name
check 'Paid' if @player.paid check 'Paid' if @player.paid
fill_in 'Strikes', with: @player.strikes fill_in 'Strikes', with: @player.strikes
click_on 'Create Player' click_on 'Create Player'
assert_text 'Player was successfully created' assert_text "Added #{@player.name}."
click_on 'Back'
end end
test 'should update Player' do test 'should update Player' do
visit player_url(@player) visit player_url(@player)
click_on 'Edit this player', match: :first
fill_in 'Name', with: @player.name fill_in 'Name', with: @player.name
check 'Paid' if @player.paid check 'Paid' if @player.paid
fill_in 'Strikes', with: @player.strikes fill_in 'Strikes', with: @player.strikes
click_on 'Update Player' click_on 'Update Player'
assert_text 'Player was successfully updated' assert_text "Updated #{@player.name}."
click_on 'Back'
end end
test 'should destroy Player' do test 'should destroy Player' do
visit player_url(@player) visit player_url(@player)
click_on 'Destroy this player', match: :first click_on "Delete #{@player.name}", match: :first
assert_text 'Player was successfully destroyed' assert_text 'Player was successfully destroyed'
end end