diff options
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/machines_controller.rb | 4 | ||||
-rw-r--r-- | app/controllers/pages_controller.rb | 35 | ||||
-rw-r--r-- | app/controllers/players_controller.rb | 4 |
3 files changed, 39 insertions, 4 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 } |