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 +---
lib/sampling/riot_api.rb | 143 +++++++++++++-------------
7 files changed, 220 insertions(+), 231 deletions(-)
delete mode 100644 app/views/tournaments/_selected.html.erb
delete mode 100644 app/views/tournaments/_stages.html.erb
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 %>
diff --git a/lib/sampling/riot_api.rb b/lib/sampling/riot_api.rb
index 3de4185..d5e9c72 100644
--- a/lib/sampling/riot_api.rb
+++ b/lib/sampling/riot_api.rb
@@ -1,10 +1,72 @@
module Sampling
module RiotApi
+ protected
+ def self.api_name
+ "prod.api.pvp.net/api/lol"
+ end
+
+ protected
+ def self.api_key
+ ENV["RIOT_API_KEY"]
+ end
+
+ protected
+ def self.region
+ ENV["RIOT_API_REGION"]
+ end
+
+ protected
+ def self.url(request, args={})
+ "https://prod.api.pvp.net/api/lol/#{region}/#{request % args.merge(args){|k,v|url_escape(v)}}?#{api_key}"
+ end
+
+ protected
+ def self.url_escape(string)
+ URI::escape(string.to_s, /[^a-zA-Z0-9._~!$&'()*+,;=:@-]/)
+ end
+
+ protected
+ def self.standardize(summoner_name)
+ summoner_name.to_s.downcase.gsub(' ', '')
+ end
+
+ protected
+ class Job < ThrottledApiRequest
+ def initialize(request, args={})
+ @url = Sampling::RiotApi::url(request, args)
+ super(api_name, 10.seconds, 10)
+ end
+
+ def perform
+ response = open(@url)
+ status = response.status
+ data = JSON::parse(response.read)
+
+ # Error codes that RIOT uses:
+ # "400"=>"Bad request"
+ # "401"=>"Unauthorized"
+ # "429"=>"Rate limit exceeded"
+ # "500"=>"Internal server error"
+ # "503"=>"Service unavailable"
+ # "404"=>"Not found"
+ # Should probably handle these better
+ if status[0] != "200"
+ raise "GET #{@url} => #{status.join(" ")}"
+ end
+ self.handle(data)
+ end
+
+ def handle(data)
+ end
+ end
+
+ ########################################################################
+
##
# Return whether or not this sampling method works with the specified game.
# Spoiler: It only works with League of Legends (or subclasses of it).
public
- def works_with?(game)
+ def self.works_with?(game)
if api_key.nil? or region.nil?
return false
end
@@ -19,17 +81,17 @@ module Sampling
##
# This sampling method uses remote IDs
public
- def uses_remote?
+ def self.uses_remote?
return true
end
##
# When given a summoner name for a user, figure out the summoner ID.
public
- def set_remote_name(user, game, summoner_name)
+ def self.set_remote_name(user, game, summoner_name)
Delayed::Job.enqueue(UsernameJob.new(user, game, summoner_name), :queue => api_name)
end
- private
+ protected
class UsernameJob < Job
def initialize(user, game, summoner_name)
@user_id = user.id
@@ -57,21 +119,21 @@ module Sampling
# When given data from RemoteUsername#value, give back a readable name to display.
# Here, this is the summoner name.
public
- def get_remote_name(data)
+ def self.get_remote_name(data)
data["name"]
end
##
# Fetch all the statistics for a match.
public
- def sampling_start(match)
+ def self.sampling_start(match)
@match.teams.each do |team|
team.users.each do |user|
Delayed::Job.enqueue(MatchJob.new(user, match), :queue => api_name)
end
end
end
- private
+ protected
class FetchStatisticsJob < Job
def initialize(user, match)
@user_id = user.id
@@ -92,78 +154,17 @@ module Sampling
end
public
- def sampling_done?(match)
+ def self.sampling_done?(match)
# TODO
end
public
- def render_user_interaction(match, user)
+ def self.render_user_interaction(match, user)
return ""
end
public
- def handle_user_interaction(match, user)
- end
-
- ########################################################################
-
- private
- def api_name
- "prod.api.pvp.net/api/lol"
- end
-
- private
- def api_key
- ENV["RIOT_API_KEY"]
- end
-
- private
- def region
- ENV["RIOT_API_REGION"]
- end
-
- private
- def url(request, args={})
- "https://prod.api.pvp.net/api/lol/#{region}/#{request % args.merge(args){|k,v|url_escape(v)}}?#{api_key}"
- end
-
- private
- def url_escape(string)
- URI::escape(string.to_s, /[^a-zA-Z0-9._~!$&'()*+,;=:@-]/)
- end
-
- private
- def standardize(summoner_name)
- summoner_name.to_s.downcase.gsub(' ', '')
- end
-
- private
- class Job < ThrottledApiRequest.new(api_name, 10.seconds, 10)
- def initialize(request, args={})
- @url = Sampling::RiotApi::url(request, args)
- end
-
- def perform
- response = open(@url)
- status = response.status
- data = JSON::parse(response.read)
-
- # Error codes that RIOT uses:
- # "400"=>"Bad request"
- # "401"=>"Unauthorized"
- # "429"=>"Rate limit exceeded"
- # "500"=>"Internal server error"
- # "503"=>"Service unavailable"
- # "404"=>"Not found"
- # Should probably handle these better
- if status[0] != "200"
- raise "GET #{@url} => #{status.join(" ")}"
- end
- self.handle(data)
- end
-
- def handle(data)
- end
+ def self.handle_user_interaction(match, user)
end
end
end
--
cgit v1.1-4-g5e80