From 6c615c4eaa973d83778fc6d34157f823880765f1 Mon Sep 17 00:00:00 2001
From: Luke Shumaker
Date: Sun, 27 Apr 2014 02:46:11 -0400
Subject: Work on the tournament creation page
---
app/controllers/tournaments_controller.rb | 37 +++++--
app/models/tournament.rb | 32 +++---
app/views/tournaments/_form.html.erb | 161 +++++++++++++++++++-----------
app/views/tournaments/_selected.html.erb | 23 -----
app/views/tournaments/_stages.html.erb | 36 -------
app/views/tournaments/new.html.erb | 19 +---
6 files changed, 148 insertions(+), 160 deletions(-)
delete mode 100644 app/views/tournaments/_selected.html.erb
delete mode 100644 app/views/tournaments/_stages.html.erb
(limited to 'app')
diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb
index 734253a..62d32b1 100644
--- a/app/controllers/tournaments_controller.rb
+++ b/app/controllers/tournaments_controller.rb
@@ -31,7 +31,6 @@ class TournamentsController < ApplicationController
# GET /tournaments/new
def new
- @games = Game.all
@tournament = Tournament.new(tournament_attribute_params)
end
@@ -43,18 +42,20 @@ class TournamentsController < ApplicationController
# POST /tournaments
# POST /tournaments.json
def create
- require 'pp'
- puts "----attributes:"
- pp tournament_attribute_params
@tournament = Tournament.new(tournament_attribute_params)
@tournament.status = 0
ok = true
- ActiveRecord::Base.transaction do
- ok &= @tournament.save
- puts "----settings:"
- pp tournament_setting_params
- ok &= @tournament.update(tournament_setting_params)
- ok &= @tournament.hosts.push(current_user)
+ begin
+ ActiveRecord::Base.transaction do
+ ok &= @tournament.save
+ ok &= @tournament.update(tournament_setting_params)
+ ok &= @tournament.hosts.push(current_user)
+ for i in 1..(params[:num_stages].to_i) do
+ ok &= @tournament.stages.create(tournament_stage_params(i))
+ end
+ end
+ rescue
+ ok = false
end
respond_to do |format|
if ok
@@ -171,7 +172,17 @@ class TournamentsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def tournament_attribute_params
if params[:tournament]
- params.require(:tournament).permit(:game_id, :status, :name, :min_players_per_team, :max_players_per_team, :min_teams_per_match, :max_teams_per_match, :set_rounds, :randomized_teams, :sampling_method, :scoring_method)
+ p = params.require(:tournament).permit(:game_id, :status, :name, :min_players_per_team, :max_players_per_team, :min_teams_per_match, :max_teams_per_match, :set_rounds, :randomized_teams, :sampling_method, :scoring_method)
+ if p[:game_id]
+ game = Game.find(p[:game_id])
+ p[:min_players_per_team] ||= game.min_players_per_team
+ p[:max_players_per_team] ||= game.max_players_per_team
+ p[:min_teams_per_match] ||= game.min_teams_per_match
+ p[:max_teams_per_match] ||= game.max_teams_per_match
+ p[:sampling_method] ||= game.sampling_method
+ p[:scoring_method] ||= game.scoring_method
+ end
+ return p
else
return {}
end
@@ -186,6 +197,10 @@ class TournamentsController < ApplicationController
end
end
+ def tournament_stage_params(i)
+ params.require(:tournament).require(i.to_sym).permit(:scheduling_method, :seeding_method)
+ end
+
def is_owner?(object)
object.hosts.include?(current_user)
end
diff --git a/app/models/tournament.rb b/app/models/tournament.rb
index 2d4d6b6..900b6d6 100644
--- a/app/models/tournament.rb
+++ b/app/models/tournament.rb
@@ -50,6 +50,11 @@ class Tournament < ActiveRecord::Base
@tournament.settings_raw.all.collect { |x| x.name }
end
+ def empty?() keys.empty? end
+ def count() keys.count end
+ def length() count end
+ def size() count end
+
def method_missing(name, *args)
if name.to_s.ends_with?('=')
self[name.to_s.sub(/=$/, '').to_sym] = args.first
@@ -95,27 +100,30 @@ class Tournament < ActiveRecord::Base
end
# YISSSSSS
- def self.make_methods(dir)
- @methods ||= {}
- if @methods[dir].nil? or Rails.env.development?
- @methods[dir] = Dir.glob("#{Rails.root}/lib/#{dir}/*.rb").map{|filename| File.basename(filename, ".rb").humanize }
- end
- return @methods[dir]
- end
- def self.scoring_methods
+ def scoring_methods
make_methods "scoring"
end
- def self.sampling_methods
- make_methods "sampling"
+ def sampling_methods
+ make_methods("sampling").collect do |name|
+ "Sampling::#{name.camelcase}".constantize.works_with?(self.game)
+ end
end
- def self.scheduling_methods
+ def scheduling_methods
make_methods "scheduling"
end
- def self.seeding_methods
+ def seeding_methods
make_methods "seeding"
end
+
+ def make_methods(dir)
+ @@methods ||= {}
+ if @@methods[dir].nil? or Rails.env.development?
+ @@methods[dir] = Dir.glob("#{Rails.root}/lib/#{dir}/*.rb").map{|filename| File.basename(filename, ".rb") }
+ end
+ return @@methods[dir]
+ end
end
diff --git a/app/views/tournaments/_form.html.erb b/app/views/tournaments/_form.html.erb
index 7127d38..78ceca4 100644
--- a/app/views/tournaments/_form.html.erb
+++ b/app/views/tournaments/_form.html.erb
@@ -1,61 +1,102 @@
-<%= form_for(@tournament) do |f| %>
- <% if @tournament.errors.any? %>
-
-
<%= pluralize(@tournament.errors.count, "error") %> prohibited this tournament from being saved:
-
-
- <% @tournament.errors.full_messages.each do |msg| %>
- - <%= msg %>
- <% end %>
-
-
- <% end %>
-
-
- <%= f.label :game_id %>
- <%= f.text_field :game_id %>
-
-
- <%= f.label :status %>
- <%= f.number_field :status %>
-
-
- <%= f.label :name %>
- <%= f.text_field :name %>
-
-
- <%= f.label :min_players_per_team %>
- <%= f.number_field :min_players_per_team %>
-
-
- <%= f.label :max_players_per_team %>
- <%= f.number_field :max_players_per_team %>
-
-
- <%= f.label :min_teams_per_match %>
- <%= f.number_field :min_teams_per_match %>
-
-
- <%= f.label :max_teams_per_match %>
- <%= f.number_field :max_teams_per_match %>
-
-
- <%= f.label :set_rounds %>
- <%= f.number_field :set_rounds %>
-
-
- <%= f.label :randomized_teams %>
- <%= f.check_box :randomized_teams %>
-
-
- <%= f.label :sampling_method %>
- <%= f.text_field :sampling_method %>
-
-
- <%= f.label :scoring_method %>
- <%= f.text_field :scoring_method %>
-
-
- <%= f.submit %>
-
+<%= form_for(@tournament,
+ url: (@tournament.game.nil? ? new_tournament_path : tournaments_path),
+ method: (@tournament.game.nil? ? "get" : "post")) do |f| %>
+ <%= render "common/error_messages", :target => @tournament %>
+
+
+ <% else %>
+
+
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+
+
+
+
+ |
+ Minimum |
+ Maximum |
+
+ Players per team |
+ <%= f.text_field(:min_players_per_team, type: :number, min: 1) %> |
+ <%= f.text_field(:max_players_per_team, type: :number, min: 1) %> |
+
+
+ Teams per match |
+ <%= f.text_field(:min_teams_per_match, type: :number, min: 1) %> |
+ <%= f.text_field(:max_teams_per_match, type: :number, min: 1) %> |
+
+
+
+
+ <%= f.label :scoring_method %>
+ <%= f.select(:scoring_method, @tournament.scoring_methods.map{|method| [method.humanize, method]}) %>
+
+
+
+ <%= f.label :sampling_method %>
+ <%= f.select(:sampling_method, @tournament.sampling_methods.map{|method| [method.humanize, method]}) %>
+
+
+
+
+
<% end %>
+ <% end %>
+
+
+
+ <%= f.submit %>
+
+ <%# render 'stages' %>
+ <% end %>
<% end %>
diff --git a/app/views/tournaments/_selected.html.erb b/app/views/tournaments/_selected.html.erb
deleted file mode 100644
index e89550e..0000000
--- a/app/views/tournaments/_selected.html.erb
+++ /dev/null
@@ -1,23 +0,0 @@
-<%= form_for(@tournament) do |f| %>
- <%= render "common/error_messages", :target => @tournament %>
- <%= f.hidden_field(:game_id) %>
-
- <% @tournament.attributes.each do |name, value| %>
- <% if (name == "randomized_teams") or(name == "max_teams_per_match") or (name == "max_players_per_team") or (name == "id") or (name =~ /.*_at$/) or (name == "game_id") or (name == "status") or (name == "set_rounds") %>
- <% next %>
- <% end %>
-
- <%= f.label name %>
- <% unless @tournament.game.attributes[name].nil? %>
- <% if name == "sampling_method" %>
- <%= f.select( name, Tournament.sampling_methods) %>
- <% else %>
- <%= f.text_field(name, :value => @tournament.game.attributes[name] ) %>
- <% end %>
- <% else %>
- <%= f.select( name, Tournament.scoring_methods) %>
- <% end %>
-
- <% end %>
- <%= f.submit %>
-<% end %>
diff --git a/app/views/tournaments/_stages.html.erb b/app/views/tournaments/_stages.html.erb
deleted file mode 100644
index 20c7b3f..0000000
--- a/app/views/tournaments/_stages.html.erb
+++ /dev/null
@@ -1,36 +0,0 @@
- <%= form_for(@tournament) do |f| %>
- <%= render "common/error_messages", :target => @tournament %>
- <%= f.hidden_field(:game_id) %>
- <%= fields_for "tournament[settings]", @tournament.settings do |setting_fields| %>
-
- <% @tournament.game.settings.each do |setting| %>
-
- <%= setting_fields.label setting.name %>
-
- <% case setting.vartype %>
- <% when 0 %>
- <%= setting_fields.text_field( setting.name ) %>
- <% when 1 %>
- <%= setting_fields.text_area( setting.name ) %>
- <% when 2 %>
- <% setting.type_opt.split(',').each do |option|%>
- <%= setting_fields.radio_button( setting.name, option ) %> <%= option %>
- <% end %>
- <% when 3 %>
- <% setting.type_opt.split(',').each do |option|%>
- <%= check_box_tag(setting.name, value = option, checked = false, options = {}) %> <%= option %>
- <% end %>
- <% when 4 %>
- <%# setting_fields.label "true" %>
- <%= setting_fields.radio_button( setting.name, "true" ) %> True
- <%# setting_fields.label "false" %>
- <%= setting_fields.radio_button( setting.name, "false" ) %> False
- <% when 5 %>
- <%= setting_fields.select( setting.name, setting.type_opt.split(',') ) %>
- <% end %>
- <% end %>
-
- <% end %>
-
- <%= f.submit %>
-<% end %>
diff --git a/app/views/tournaments/new.html.erb b/app/views/tournaments/new.html.erb
index af74ea8..9c741e0 100644
--- a/app/views/tournaments/new.html.erb
+++ b/app/views/tournaments/new.html.erb
@@ -1,22 +1,5 @@
New Tournament
-<%= form_tag(new_tournament_path, method: "get") do %>
- <%= select_tag('tournament[game_id]',
- options_from_collection_for_select(@games, 'id', 'name', @tournament.game.nil? || @tournament.game.id),
- :prompt => "Select a Game Type") %>
- <%= submit_tag("Select", :class => "btn") %>
-<% end %>
-
-
- <% if not @tournament.game.nil? %>
- <%= render 'selected' %>
- <% end %>
-
-
-
- <% if not @tournament.game.nil? %>
- <%= render 'stages' %>
- <% end %>
-
+<%= render 'form' %>
<%= link_to 'Back', tournaments_path %>
--
cgit v1.2.3-2-g168b