summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <shumakl@purdue.edu>2014-04-27 02:46:11 -0400
committerLuke Shumaker <shumakl@purdue.edu>2014-04-27 02:46:11 -0400
commit6c615c4eaa973d83778fc6d34157f823880765f1 (patch)
tree112cae9c020e34fdbd3620eea13b3c931e24e71c
parent353e0b387b8e76d407f9451c236efbb4bd3a19ef (diff)
Work on the tournament creation page
-rw-r--r--app/controllers/tournaments_controller.rb37
-rw-r--r--app/models/tournament.rb32
-rw-r--r--app/views/tournaments/_form.html.erb161
-rw-r--r--app/views/tournaments/_selected.html.erb23
-rw-r--r--app/views/tournaments/_stages.html.erb36
-rw-r--r--app/views/tournaments/new.html.erb19
-rw-r--r--lib/sampling/riot_api.rb143
7 files changed, 220 insertions, 231 deletions
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? %>
- <div id="error_explanation">
- <h2><%= pluralize(@tournament.errors.count, "error") %> prohibited this tournament from being saved:</h2>
-
- <ul>
- <% @tournament.errors.full_messages.each do |msg| %>
- <li><%= msg %></li>
- <% end %>
- </ul>
- </div>
- <% end %>
-
- <div class="field">
- <%= f.label :game_id %><br>
- <%= f.text_field :game_id %>
- </div>
- <div class="field">
- <%= f.label :status %><br>
- <%= f.number_field :status %>
- </div>
- <div class="field">
- <%= f.label :name %><br>
- <%= f.text_field :name %>
- </div>
- <div class="field">
- <%= f.label :min_players_per_team %><br>
- <%= f.number_field :min_players_per_team %>
- </div>
- <div class="field">
- <%= f.label :max_players_per_team %><br>
- <%= f.number_field :max_players_per_team %>
- </div>
- <div class="field">
- <%= f.label :min_teams_per_match %><br>
- <%= f.number_field :min_teams_per_match %>
- </div>
- <div class="field">
- <%= f.label :max_teams_per_match %><br>
- <%= f.number_field :max_teams_per_match %>
- </div>
- <div class="field">
- <%= f.label :set_rounds %><br>
- <%= f.number_field :set_rounds %>
- </div>
- <div class="field">
- <%= f.label :randomized_teams %><br>
- <%= f.check_box :randomized_teams %>
- </div>
- <div class="field">
- <%= f.label :sampling_method %><br>
- <%= f.text_field :sampling_method %>
- </div>
- <div class="field">
- <%= f.label :scoring_method %><br>
- <%= f.text_field :scoring_method %>
- </div>
- <div class="actions">
- <%= f.submit %>
- </div>
+<%= 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 %>
+
+ <fieldset><legend>Attributes</legend>
+ <p>
+ <%= f.label :game_id, "Select a game type" %>
+ <%= f.select(:game_id, Game.all.map{|game| [game.name, game.id]}) %>
+ </p>
+
+ <% if @tournament.game.nil? %>
+ <%= submit_tag("Select", :class => "btn") %>
+ </fieldset>
+ <% else %>
+
+ <p>
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+ </p>
+
+ <table><tbody>
+ <tr>
+ <td></td>
+ <th>Minimum</th>
+ <th>Maximum</th></tr>
+ <tr>
+ <th>Players per team</th>
+ <td><%= f.text_field(:min_players_per_team, type: :number, min: 1) %></td>
+ <td><%= f.text_field(:max_players_per_team, type: :number, min: 1) %></td>
+ </tr>
+ <tr>
+ <th>Teams per match</th>
+ <td><%= f.text_field(:min_teams_per_match, type: :number, min: 1) %></td>
+ <td><%= f.text_field(:max_teams_per_match, type: :number, min: 1) %></td>
+ </tr>
+ </tbody></table>
+
+ <p>
+ <%= f.label :scoring_method %>
+ <%= f.select(:scoring_method, @tournament.scoring_methods.map{|method| [method.humanize, method]}) %>
+ </p>
+
+ <p>
+ <%= f.label :sampling_method %>
+ <%= f.select(:sampling_method, @tournament.sampling_methods.map{|method| [method.humanize, method]}) %>
+ </p>
+
+ </fieldset>
+
+ <fieldset><legend>Settings</legend>
+ <%= fields_for "tournament[settings]", @tournament.settings do |setting_fields| %>
+ <% (@tournament.settings.empty? ? @tournament.game.settings : @tournament.settings).each do |setting| %><p>
+ <% name = setting.name.parameterize.underscore %>
+ <%= setting_fields.label name %>
+ <br>
+ <% case setting.vartype %>
+ <% when 0 %>
+ <%= setting_fields.text_field( name ) %>
+ <% when 1 %>
+ <%= setting_fields.text_area( name ) %>
+ <% when 2 %>
+ <ul>
+ <% setting.type_opt.split(',').each do |option|%>
+ <li><label><%= setting_fields.radio_button( name, option ) %><%= option %></label></li>
+ <% end %>
+ </ul>
+ <% when 3 %>
+ <ul>
+ <% setting.type_opt.split(',').each do |option|%>
+ <li><label><%= check_box_tag(name, value = option, checked = false, options = {}) %><%= option %></label></li>
+ <% end %>
+ </ul>
+ <% when 4 %>
+ <%= setting_fields.radio_button( name, "true" ) %> True
+ <%= setting_fields.radio_button( name, "false" ) %> False
+ <% when 5 %>
+ <%= setting_fields.select( name, setting.type_opt.split(',') ) %>
+ <% end %>
+ </p><% end %>
+ <% end %>
+ </fieldset>
+
+ <fieldset><legend>Stages</legend>
+ <label for="num_stages">Number of tournament stages</label>
+ <input type="number" name="num_stages" min="1">
+ <% for i in 1..(params[:num_stages].to_i) do %>
+ <%= fields_for "tournament[stages][#{i}]", @tournament.stages do |stage_fields| %>
+ <fieldset><legend>Stage <%= i %></legend>
+ <%= stage_fields.label :scheduling_method %>
+ <%= stage_fields.select(:scheduling_method, @tournament.scheduling_methods.map{|method| [method.humanize, method]}) %>
+ <%= stage_fields.label :seeding_method %>
+ <%= stage_fields.select(:seeding_method, @tournament.seeding_methods.map{|method| [method.humanize, method]}) %>
+ </fieldset>
+ <% end %>
+ <% end %>
+ </fieldset>
+ <%= 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 %>
- <p>
- <%= f.label name %><br>
- <% 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 %>
- </p>
- <% 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| %>
- <p>
- <%= setting_fields.label setting.name %>
- <br>
- <% 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 %> <br>
- <% end %>
- <% when 3 %>
- <% setting.type_opt.split(',').each do |option|%>
- <%= check_box_tag(setting.name, value = option, checked = false, options = {}) %> <%= option %> <br>
- <% 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 %>
- </p>
- <% 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 @@
<h1>New Tournament</h1>
-<%= 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 %>
-
-<div id='ajax-form'>
- <% if not @tournament.game.nil? %>
- <%= render 'selected' %>
- <% end %>
-</div>
-
-<div id='ajax-form'>
- <% if not @tournament.game.nil? %>
- <%= render 'stages' %>
- <% end %>
-</div>
+<%= 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