From ab25aac502a768b6a530ada5c46d6aabe6a56c4c Mon Sep 17 00:00:00 2001 From: AndrewMurrell Date: Sat, 26 Apr 2014 22:47:58 -0400 Subject: Added READMEs for scheduling and started seeding in lib. --- lib/scheduling/README.md | 13 +++++++++++++ lib/scheduling/elimination.rb | 25 ++++++++++++------------- lib/scheduling/roundrobin.rb | 31 +++++++++++++++++++++---------- lib/seeding/.keep | 0 lib/seeding/README.md | 3 +++ 5 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 lib/scheduling/README.md create mode 100644 lib/seeding/.keep create mode 100644 lib/seeding/README.md diff --git a/lib/scheduling/README.md b/lib/scheduling/README.md new file mode 100644 index 0000000..173b7be --- /dev/null +++ b/lib/scheduling/README.md @@ -0,0 +1,13 @@ +Files in this directory should implement the following interface: + + - `initialize(tournament_stage)` + construct new Scheduling object from tournament_stage + + - `create_matches` + creates all the matches of the current round + + - `finish_match(match)` + progresses the match through the schedule + + - `graph` + returns a string representation of an svg image of the current stage \ No newline at end of file diff --git a/lib/scheduling/elimination.rb b/lib/scheduling/elimination.rb index 074cb5c..4518cff 100644 --- a/lib/scheduling/elimination.rb +++ b/lib/scheduling/elimination.rb @@ -33,8 +33,7 @@ module Scheduling end end - def match_finished(match) - #what in the goddamn fuck does this mean + def finish_match(match) matches = match.tournament_stage.matches_ordered cur_match_num = matches.invert[match] unless cur_match_num == 1 @@ -55,17 +54,17 @@ module Scheduling height = [(matchHeight+50) * logBase**(depth-1) + 100, 500].max; str = <<-STRING - - - - - - - -STRING + + + + + + + + STRING base = 1 pBase = 1 (1..matches.count).each do |i| diff --git a/lib/scheduling/roundrobin.rb b/lib/scheduling/roundrobin.rb index e149860..7a9e257 100644 --- a/lib/scheduling/roundrobin.rb +++ b/lib/scheduling/roundrobin.rb @@ -14,6 +14,17 @@ module Scheduling end end + def finish_match(match) + #declare winner of match, and store that somehow + rotate + return "totes worked\n" + end + + def graph(current_user) + end + + private + def create_round_array #round robin should look like this. #NOTE: I DO NOT KNOW IF THIS IS HOW TO PROPERLY POPULATE THE ROUND ROBIN ARRAY WITH TEAMS @@ -28,15 +39,23 @@ module Scheduling end end - #this is called when a round has completed + def tournament_stage + @tournament_stage + end + + def tournament + tournament_stage.tournament + end + def rotate + #this is called when a round has completed + #remove first team hold = @team_pairs.shift #rotate by 1 element @team_pairs.rotate! #place first team the front of the array @team_pairs.unshift(hold) - end def mother_fuckin_winner @@ -50,14 +69,6 @@ module Scheduling scores[weiner] end - def match_finished(match) - #declare winner of match, and store that somehow - rotate - return "totes worked\n" - end - def graph(current_user) - - end end end diff --git a/lib/seeding/.keep b/lib/seeding/.keep new file mode 100644 index 0000000..e69de29 diff --git a/lib/seeding/README.md b/lib/seeding/README.md new file mode 100644 index 0000000..2f12622 --- /dev/null +++ b/lib/seeding/README.md @@ -0,0 +1,3 @@ +Files in this directory should implement the following interface: + +- -- cgit v1.2.3-54-g00ecf From add6074325e01b35f012f8fe783d5e4349c10a64 Mon Sep 17 00:00:00 2001 From: guntasgrewal Date: Sat, 26 Apr 2014 22:53:34 -0400 Subject: Changes to tournament_stage and tournament controller --- app/models/tournament.rb | 25 +++++++++++++++++++++++++ app/models/tournament_stage.rb | 23 ----------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 61b4700..09a1abb 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -93,4 +93,29 @@ class Tournament < ActiveRecord::Base def sampling @sampling ||= "Sampling::#{self.sampling_method.camelcase}".constantize end + + # YISSSSSS + def self.make_methods(dir) + if @methods[dir].nil? or Rails.env.development? + @methods[dir] = Dir.glob("#{Rails.root}/lib/#{dir}/*.rb").map{|filename| filename.split('/').last.sub(/\.rb$/, '')} + end + return @methods[dir] + end + + def self.scoring_methods + make_methods "scoring" + end + + def self.sampling_methods + make_methods "sampling" + end + + def self.scheduling_methods + make_methods "scheduling" + end + + def self.seeding_methods + make_methods "seeding" + end + end diff --git a/app/models/tournament_stage.rb b/app/models/tournament_stage.rb index 84519b9..9352137 100644 --- a/app/models/tournament_stage.rb +++ b/app/models/tournament_stage.rb @@ -25,29 +25,6 @@ class TournamentStage < ActiveRecord::Base return seeding.seed.pair(matches, players) end - def make_methods(dir) - if @methods[dir].nil? or Rails.env.development? - @methods[dir] = Dir.glob("#{Rails.root}/lib/#{dir}/*.rb").map{|filename| filename.sub(/.*\/(.*)\.rb/, /\1/)} - end - return @methods[dir] - end - - def scoring_methods - make_methods "scoring" - end - - def sampling_methods - make_methods "sampling" - end - - def scheduling_methods - make_methods "scheduling" - end - - def seeding_methods - make_methods "seeding" - end - # Accessors to the configured methods def scoring -- cgit v1.2.3-54-g00ecf From 4638a832b3b9da87bf076f4370e0d99bdf11ee78 Mon Sep 17 00:00:00 2001 From: AndrewMurrell Date: Sat, 26 Apr 2014 23:21:57 -0400 Subject: Fixed metaprogramming, I think. and added README for seeding --- app/models/tournament.rb | 4 ++-- lib/seeding/README.md | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 09a1abb..0a8ab8d 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -96,8 +96,8 @@ class Tournament < ActiveRecord::Base # YISSSSSS def self.make_methods(dir) - if @methods[dir].nil? or Rails.env.development? - @methods[dir] = Dir.glob("#{Rails.root}/lib/#{dir}/*.rb").map{|filename| filename.split('/').last.sub(/\.rb$/, '')} + if @methods.nil? or @methods[dir].nil? or Rails.env.development? + @methods[dir] = Dir.glob("#{Rails.root}/lib/#{dir}/*.rb").map{|filename| File.basename(filename) } end return @methods[dir] end diff --git a/lib/seeding/README.md b/lib/seeding/README.md index 2f12622..0afbb94 100644 --- a/lib/seeding/README.md +++ b/lib/seeding/README.md @@ -1,3 +1,4 @@ Files in this directory should implement the following interface: -- +- seed_matches(tournament) + take the matches of a tournament and the players in a tournament, assign players to teams, and teams to matches -- cgit v1.2.3-54-g00ecf From 40939795096c0b7a1791d71d920b84ff283fb550 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 25 Apr 2014 11:09:09 -0400 Subject: Sampling methods WIP --- app/controllers/users_controller.rb | 15 +--- lib/sampling/README.md | 28 ++++++ lib/sampling/double_bind.rb | 7 ++ lib/sampling/manual.rb | 35 ++++++++ lib/sampling/riot_api.rb | 169 ++++++++++++++++++++++++++++++++++++ lib/throttled_api_request.rb | 25 ++++++ 6 files changed, 265 insertions(+), 14 deletions(-) create mode 100644 lib/sampling/README.md create mode 100644 lib/sampling/double_bind.rb create mode 100644 lib/sampling/manual.rb create mode 100644 lib/sampling/riot_api.rb create mode 100644 lib/throttled_api_request.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 27b3c61..767d992 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -62,20 +62,7 @@ class UsersController < ApplicationController else params[:user][:remote_usernames].each do |game_name,user_name| game = Game.find_by_name(game_name) - remote_username = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/by-name/#{user_name.downcase}?api_key=ad539f86-22fd-474d-9279-79a7a296ac38") - - id = "#{remote_username["#{user_name.downcase}"]["id"]}".to_i - username = "#{remote_username["#{user_name.downcase}"]["name"]}" - - hash = {:username => username, :id => id} - - remote = @user.remote_usernames.where(:game => game).first - if remote.nil? - ok &= @user.remote_usernames.create(game: game, value: hash) - else - remote.value = hash - ok &= remote.save - end + Sampling::RiotApi::set_remote_name(@user, game, user_name) end end respond_to do |format| diff --git a/lib/sampling/README.md b/lib/sampling/README.md new file mode 100644 index 0000000..28c603e --- /dev/null +++ b/lib/sampling/README.md @@ -0,0 +1,28 @@ +Files in this directory should be modules implementing the following +interface: + + - `works_with?(Game) => Boolean` + Returns whether or not this sampling method works with the + specified game. + + - `uses_remote?() => Boolean` + Return whether or not this sampling method requires remote IDs for + users. + - `set_remote_name(User, Game, String)` + Set the remote ID for a user for the specified game. It is safe to + assume that this sampling method `works_with?` that game. + - `get_remote_name(Object)` + When given an object from `RemoteUsername#value`, give back a + human-readable/editable name to display. + + - `sampling_start(Match)` + Fetch the statistics for a match. + - `sampling_done?(Match) => Boolean` + Returns whether or not statistics have been completely collected + yet. + + - `render_user_interaction(Match, User) => String` + Returns HTML to render on a page. + - `handle_user_interaction(Match, User, Hash params)` + Handles params from the form generated by + `#user_interaction_render`. diff --git a/lib/sampling/double_bind.rb b/lib/sampling/double_bind.rb new file mode 100644 index 0000000..4a5201c --- /dev/null +++ b/lib/sampling/double_bind.rb @@ -0,0 +1,7 @@ +module Sampling + module DoubleBlind + def works_with?(game) + return true + end + end +end diff --git a/lib/sampling/manual.rb b/lib/sampling/manual.rb new file mode 100644 index 0000000..17c8104 --- /dev/null +++ b/lib/sampling/manual.rb @@ -0,0 +1,35 @@ +module Sampling + module HostEntry + def self.works_with?(game) + return true + end + + def self.uses_remote? + return false + end + + def self.set_remote_name(user, game, value) + raise "This sampling method doesn't use remote usernames." + end + + def self.get_remote_name(value) + raise "This sampling method doesn't use remote usernames." + end + + def self.sampling_start(match) + # TODO + end + + def self.sampling_done?(match) + # TODO + end + + def self.render_user_interaction(match, user) + + end + + def self.handle_user_interaction(match, user, sampling_params) + match.statistics.create(user: nil, name: "blowout", + end + end +end diff --git a/lib/sampling/riot_api.rb b/lib/sampling/riot_api.rb new file mode 100644 index 0000000..3de4185 --- /dev/null +++ b/lib/sampling/riot_api.rb @@ -0,0 +1,169 @@ +module Sampling + module RiotApi + ## + # 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) + if api_key.nil? or region.nil? + return false + end + if game.name == "League of Legends" + return true + end + unless game.parent.nil? + return works_with?(game.parent) + end + end + + ## + # This sampling method uses remote IDs + public + def 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) + Delayed::Job.enqueue(UsernameJob.new(user, game, summoner_name), :queue => api_name) + end + private + class UsernameJob < Job + def initialize(user, game, summoner_name) + @user_id = user.id + @game_id = game.id + # Escape any funny stuff + summoner_names = [summoner_name].map{|name|Sampling::RiotApi::standardize(name.gsub(',',''))} + # Generate the request + super("v1.3/summoner/by-name/%{summonerNames}", { :summonerNames => summoner_names.join(",") }) + end + def handle(data) + user = User.find(@user_id) + game = Game.find(@game_id) + + normalized_summoner_name = data.keys.first + remote_data = { + :id => data[normalized_summoner_name]["id"], + :name => data[normalized_summoner_name]["name"], + } + + user.set_remote_username(game, remote_data) + end + end + + ## + # 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) + data["name"] + end + + ## + # Fetch all the statistics for a match. + public + def 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 + class FetchStatisticsJob < Job + def initialize(user, match) + @user_id = user.id + @match_id = match.id + # Get the summoner id + summoner = user.get_remote_username(match.tournament_stage.tournament.game) + if summoner.nil? + raise "Someone didn't enter their summoner name" + end + # Generate the request + super("v1.3/game/by-summoner/%{summonerId}/recent", { :summonerId => summoner["id"] }) + end + def handle(data) + user = User.find(@user_id) + match = Match.find(@match_id) + Statistic.create(user: user, match: match, value: TODO) + end + end + + public + def sampling_done?(match) + # TODO + end + + public + def 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 + end + end +end diff --git a/lib/throttled_api_request.rb b/lib/throttled_api_request.rb new file mode 100644 index 0000000..3f30c56 --- /dev/null +++ b/lib/throttled_api_request.rb @@ -0,0 +1,25 @@ +class ThrottledApiRequest < Struct.new(:api_name, :unit_time, :requests_per) + def before(job) + loop do + sleep_for = -1 + ActiveRecord::Base.transaction do + ApiRequests.create(:api_name => self.api_name) + recent_requests = ApiRequets. + where(:api_name => self.api_name). + where("updated_at > ?", Time.now.utc - self.unit_time). + order(:updated_at) + if (recent_requests.count > self.requests_per) + sleep_for = Time.now.utc - recent_requests[recent_requests.count-self.requests_per].updated_at + raise ActiveRecord::Rollback + else + sleep_for = -1 + end + end + if sleep_for != -1 + sleep(sleep_for) + else + break + end + end + end +end -- cgit v1.2.3-54-g00ecf From cbdccb99bcad74ffc98f6964fe504e20cfe0cdc2 Mon Sep 17 00:00:00 2001 From: AndrewMurrell Date: Sat, 26 Apr 2014 23:37:59 -0400 Subject: Added my fix --- app/models/tournament.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 0a8ab8d..30a4895 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -96,8 +96,9 @@ class Tournament < ActiveRecord::Base # YISSSSSS def self.make_methods(dir) - if @methods.nil? or @methods[dir].nil? or Rails.env.development? - @methods[dir] = Dir.glob("#{Rails.root}/lib/#{dir}/*.rb").map{|filename| File.basename(filename) } + @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 -- cgit v1.2.3-54-g00ecf From 506486a6a38fc73b49dd35077cfaafbdf891c664 Mon Sep 17 00:00:00 2001 From: guntasgrewal Date: Sat, 26 Apr 2014 23:38:36 -0400 Subject: Some shit I did --- app/controllers/alerts_controller.rb | 7 ++++-- app/controllers/tournaments_controller.rb | 8 +++++++ app/models/tournament.rb | 6 ++--- app/views/tournaments/_selected.html.erb | 38 ++++--------------------------- app/views/tournaments/_stages.html.erb | 36 +++++++++++++++++++++++++++++ app/views/tournaments/new.html.erb | 6 +++++ 6 files changed, 62 insertions(+), 39 deletions(-) create mode 100644 app/views/tournaments/_stages.html.erb diff --git a/app/controllers/alerts_controller.rb b/app/controllers/alerts_controller.rb index 1d09864..77ca8b9 100644 --- a/app/controllers/alerts_controller.rb +++ b/app/controllers/alerts_controller.rb @@ -25,9 +25,12 @@ class AlertsController < ApplicationController @alert = Alert.new(alert_params) @alert.author = current_user users = {} - users = Users.all + users = User.all - #current_user.send_message(users, @alert.message, "Pay Attention!") + + for i in 0..users.length + current_user.send_message(users[i], @alert.message, "Pay Attention!") + end respond_to do |format| if @alert.save diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 60f8789..734253a 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -67,6 +67,14 @@ class TournamentsController < ApplicationController end end + def create_stage + + # stage = @tournament.stages.new + # stage.create(TODO:PARAMETERS) + # @tournament.stages.push(stage) + + end + # PATCH/PUT /tournaments/1 # PATCH/PUT /tournaments/1.json def update diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 0a8ab8d..2d4d6b6 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -96,8 +96,9 @@ class Tournament < ActiveRecord::Base # YISSSSSS def self.make_methods(dir) - if @methods.nil? or @methods[dir].nil? or Rails.env.development? - @methods[dir] = Dir.glob("#{Rails.root}/lib/#{dir}/*.rb").map{|filename| File.basename(filename) } + @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 @@ -117,5 +118,4 @@ class Tournament < ActiveRecord::Base def self.seeding_methods make_methods "seeding" end - end diff --git a/app/views/tournaments/_selected.html.erb b/app/views/tournaments/_selected.html.erb index 9240c49..e89550e 100644 --- a/app/views/tournaments/_selected.html.erb +++ b/app/views/tournaments/_selected.html.erb @@ -3,51 +3,21 @@ <%= f.hidden_field(:game_id) %> <% @tournament.attributes.each do |name, value| %> - <% if (name == "id") or (name =~ /.*_at$/) or (name == "game_id") or (name == "status") or (name == "set_rounds") %> + <% 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.game.sampling_method.split(',') ) %> + <%= f.select( name, Tournament.sampling_methods) %> <% else %> - <%= f.text_field(name, :value => @tournament.game.attributes[name] ) %> + <%= f.text_field(name, :value => @tournament.game.attributes[name] ) %> <% end %> <% else %> - <%= f.text_field name %> + <%= f.select( name, Tournament.scoring_methods) %> <% end %>

<% end %> - - <%= 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/_stages.html.erb b/app/views/tournaments/_stages.html.erb new file mode 100644 index 0000000..20c7b3f --- /dev/null +++ b/app/views/tournaments/_stages.html.erb @@ -0,0 +1,36 @@ + <%= 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 2837708..af74ea8 100644 --- a/app/views/tournaments/new.html.erb +++ b/app/views/tournaments/new.html.erb @@ -13,4 +13,10 @@ <% end %> +
+ <% if not @tournament.game.nil? %> + <%= render 'stages' %> + <% end %> +
+ <%= link_to 'Back', tournaments_path %> -- cgit v1.2.3-54-g00ecf