about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/machines_controller.rb4
-rw-r--r--app/controllers/pages_controller.rb35
-rw-r--r--app/controllers/players_controller.rb4
-rw-r--r--app/helpers/pages_helper.rb3
-rw-r--r--app/views/layouts/application.html.erb6
-rw-r--r--app/views/machines/index.html.erb3
-rw-r--r--app/views/machines/show.html.erb4
-rw-r--r--app/views/pages/index.html.erb2
-rw-r--r--app/views/pages/randomize.html.erb10
-rw-r--r--app/views/players/index.html.erb3
-rw-r--r--app/views/players/show.html.erb4
11 files changed, 64 insertions, 14 deletions
diff --git a/app/controllers/machines_controller.rb b/app/controllers/machines_controller.rb
index 3ef5893..33dcd3b 100644
--- a/app/controllers/machines_controller.rb
+++ b/app/controllers/machines_controller.rb
@@ -25,7 +25,7 @@ class MachinesController < ApplicationController
 
     respond_to do |format|
       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 }
       else
         format.html { render :new, status: :unprocessable_entity }
@@ -38,7 +38,7 @@ class MachinesController < ApplicationController
   def update
     respond_to do |format|
       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 }
       else
         format.html { render :show, status: :unprocessable_entity }
diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb
new file mode 100644
index 0000000..91b5ebc
--- /dev/null
+++ b/app/controllers/pages_controller.rb
@@ -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
diff --git a/app/controllers/players_controller.rb b/app/controllers/players_controller.rb
index 44034e5..cebe2a1 100644
--- a/app/controllers/players_controller.rb
+++ b/app/controllers/players_controller.rb
@@ -22,7 +22,7 @@ class PlayersController < ApplicationController
 
     respond_to do |format|
       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 }
       else
         format.html { render :new, status: :unprocessable_entity }
@@ -35,7 +35,7 @@ class PlayersController < ApplicationController
   def update
     respond_to do |format|
       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 }
       else
         format.html { render :show, status: :unprocessable_entity }
diff --git a/app/helpers/pages_helper.rb b/app/helpers/pages_helper.rb
new file mode 100644
index 0000000..b7429d2
--- /dev/null
+++ b/app/helpers/pages_helper.rb
@@ -0,0 +1,3 @@
+# frozen_string_literal: true
+module PagesHelper
+end
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index a21e612..cc2f6c2 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -12,8 +12,10 @@
 
   <body>
     <nav>
-      <%= link_to "Pins", machines_url %>
-      <%= link_to "Players", players_url %>
+      <%= link_to 'Home', '/' %>
+      <%= link_to 'Pins', machines_url %>
+      <%= link_to 'Players', players_url %>
+      <%= link_to 'Randomize', controller: 'pages', action: 'randomize' %>
     </nav>
 
     <%= yield %>
diff --git a/app/views/machines/index.html.erb b/app/views/machines/index.html.erb
index 2c99524..8237518 100644
--- a/app/views/machines/index.html.erb
+++ b/app/views/machines/index.html.erb
@@ -8,4 +8,5 @@
   <% end %>
 </div>
 
-<%= link_to "New machine", new_machine_path %>
+<hr>
+<%= link_to "Add a pin", new_machine_path %>
diff --git a/app/views/machines/show.html.erb b/app/views/machines/show.html.erb
index 7870783..19e5f19 100644
--- a/app/views/machines/show.html.erb
+++ b/app/views/machines/show.html.erb
@@ -3,7 +3,5 @@
 <%= render "form", machine: @machine %>
 
 <div>
-  <%= link_to "Back to machines", machines_path %>
-
-  <%= button_to "Destroy this machine", @machine, method: :delete %>
+  <%= button_to "Delete #{@machine.name}", @machine, method: :delete %>
 </div>
diff --git a/app/views/pages/index.html.erb b/app/views/pages/index.html.erb
new file mode 100644
index 0000000..809ea04
--- /dev/null
+++ b/app/views/pages/index.html.erb
@@ -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>
diff --git a/app/views/pages/randomize.html.erb b/app/views/pages/randomize.html.erb
new file mode 100644
index 0000000..1505ab5
--- /dev/null
+++ b/app/views/pages/randomize.html.erb
@@ -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 %>
diff --git a/app/views/players/index.html.erb b/app/views/players/index.html.erb
index 5a2b9ef..c067483 100644
--- a/app/views/players/index.html.erb
+++ b/app/views/players/index.html.erb
@@ -8,4 +8,5 @@
   <% end %>
 </div>
 
-<%= link_to "New player", new_player_path %>
+<hr>
+<%= link_to "Add a player", new_player_path %>
diff --git a/app/views/players/show.html.erb b/app/views/players/show.html.erb
index 42dce9c..e031050 100644
--- a/app/views/players/show.html.erb
+++ b/app/views/players/show.html.erb
@@ -3,7 +3,5 @@
 <%= render "form", player: @player %>
 
 <div>
-  <%= link_to "Back to players", players_path %>
-
-  <%= button_to "Destroy this player", @player, method: :delete %>
+  <%= button_to "Delete #{@player.name}", @player, method: :delete %>
 </div>