From f20dc8df17187027c1881803dbc72b1d3802ff1a Mon Sep 17 00:00:00 2001 From: guntasgrewal Date: Mon, 7 Apr 2014 03:48:51 -0400 Subject: Tournament advancement stuff. IT'S 4 AM --- app/controllers/matches_controller.rb | 31 +++--- app/models/tournament.rb | 10 ++ app/views/matches/index.html.erb | 35 +++---- app/views/matches/show.html.erb | 189 +++++++++++++++------------------- 4 files changed, 122 insertions(+), 143 deletions(-) diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index 8e7cc9e..7d6bf43 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -144,9 +144,6 @@ class MatchesController < ApplicationController handle_asynchronously :is_match_over def show - if (@match.status == 1) - @scores = @match.scores - end file_blue = "blue.yaml" file_purple = "purple.yaml" @blue2 = YAML.load_file(file_blue) @@ -167,12 +164,16 @@ class MatchesController < ApplicationController format.json { render json: "Permission denied", status: :forbidden } end end - when "score" + when "finish" + @match.status = 2 + + # Individual scores scores = params["scores"] scores.each do |user_name, score| Score.create(user: User.find_by_user_name(user_name), match: @match, value: score.to_i) end + # Team scores team_scores = {} @match.teams.each do |team| team_scores[team] = 0 @@ -180,10 +181,15 @@ class MatchesController < ApplicationController team_scores[team] += scores[user.user_name].to_i end end - teams = team_scores.invert @match.winner = teams[teams.keys.sort.last] + # Schedule next match + cur_match_num = @tournament.matches_ordered.invert[@match] + unless cur_match_num == 1 + @match.winner.matches.push(@tournament.matches_ordered[cur_match_num/2]) + end + respond_to do |format| if @match.save format.html { redirect_to tournament_match_path(@tournament, @match), notice: 'Peer evaluation started.' } @@ -194,7 +200,7 @@ class MatchesController < ApplicationController end end when "peer" - @match.status = 2; + @match.status = 3; respond_to do |format| if @match.save format.html { redirect_to tournament_match_path(@tournament, @match), notice: 'Scores Submitted' } @@ -204,17 +210,6 @@ class MatchesController < ApplicationController format.json { render json: "Permission denied", status: :forbidden } end end - when "finish" - @match.status = 3 - respond_to do |format| - if @match.save - format.html { redirect_to tournament_match_path(@tournament, @match), notice: 'Peer Review Submitted' } - format.json { head :no_content } - else - format.html { redirect_to @tournament, notice: "You don't have permission to start this match." } - format.json { render json: "Permission denied", status: :forbidden } - end - end when "reset" @match.status = 0 respond_to do |format| @@ -226,7 +221,6 @@ class MatchesController < ApplicationController format.json { render json: "Permission denied", status: :forbidden } end end - else respond_to do |format| format.html { redirect_to @tournament, notice: "Invalid action", status: :unprocessable_entity } @@ -241,7 +235,6 @@ class MatchesController < ApplicationController def set_match set_tournament @match = @tournament.matches.find(params[:id]) - @first = @match.teams.first.users.first.user_name.downcase end def set_tournament @tournament = Tournament.find(params[:tournament_id]) diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 10d7b30..aa331f0 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -5,6 +5,16 @@ class Tournament < ActiveRecord::Base has_and_belongs_to_many :players, class_name: "User", association_foreign_key: "player_id", join_table: "players_tournaments" has_and_belongs_to_many :hosts, class_name: "User", association_foreign_key: "host_id", join_table: "hosts_tournaments" + def matches_ordered + h = {} + i = 1 + matches.order(:id).each do |m| + h[i] = m + i += 1 + end + return h + end + def preferences @preferences ||= Preferences.new(self) end diff --git a/app/views/matches/index.html.erb b/app/views/matches/index.html.erb index 80d12ab..6c93321 100644 --- a/app/views/matches/index.html.erb +++ b/app/views/matches/index.html.erb @@ -11,24 +11,23 @@ - - <% @tournament.matches.order(:id).reverse.each do |match| %> - - <%= match.name %> - <%= match.status %> - <%= (match.winner.nil? ? "-" : "Team #{match.winner.id}") %> - <%= link_to "Show", tournament_match_path(@tournament, match) %> - <%# If user is the host, let them start the tournment %> - <% if @tournament.hosts.include?(current_user) %> - <%= form_tag(tournament_match_path(@tournament, match), method: "put") do %> - - <%= submit_tag("Start Match", :dissabled => match.teams.count < @tournament.min_teams_per_match) %> - <% end %> - <% end %> - - - <% end %> - + + <% @tournament.matches.order(:id).reverse.each do |match| %> + <%= match.name %> + <%= match.status %> + <%= (match.winner.nil? ? "-" : "Team #{match.winner.id}") %> + <%= link_to "Show", tournament_match_path(@tournament, match) %> + <%# If user is the host, let them start the tournment %> + <% if @tournament.hosts.include?(current_user) %> + <%= form_tag(tournament_match_path(@tournament, match), method: "put") do %> + + <% @startable = (match.status == 0) and (match.teams.count >= @tournament.min_teams_per_match) %> + <%= submit_tag("Start Match", :disabled => ! @startable) %> + <% end %> + <% end %> + + <% end %> +
diff --git a/app/views/matches/show.html.erb b/app/views/matches/show.html.erb index cd30926..a832d17 100644 --- a/app/views/matches/show.html.erb +++ b/app/views/matches/show.html.erb @@ -1,21 +1,21 @@

- Status: - <%= @match.status %> + Status: + <%= @match.status %>

- Tournament: - <%= @match.tournament.id %> + Tournament: + <%= @match.tournament.id %>

- Name: - <%= @match.name %> + Name: + <%= @match.name %>

-<% if (@match.status== 0) || !@tournament.players.include?(current_user) %> +
+

Teams/users

+ <% end %> - - - -<% elsif (@match.status==1) %> - <% if @tournament.hosts.include?(current_user) && @scores.empty? %> - <%= form_tag(tournament_match_path(@tournament, @match), method: "put") do %> - - <% @match.teams.each do |team| %> -
Team <%= team.id.to_s %> - <% team.users.collect{|u| u.user_name}.each do |k| %> - - <% end %> -
- <% end %> - <%= submit_tag("Enter Scores") %> - <% end %> - <% else %> - <% if @scores.empty? %> -

The host has yet to post the scores of the match

- <% @match.teams.each do |team| %> -
    - <% team.users.collect{|u| u.user_name}.each do |k| %> -
  1. <%= k %>
  2. - <% end %> -
- <% end %> - <% else %> - <% @match.teams.each do |team| %> -
    - <% team.users.each do |user| %> -
  1. <%= user.user_name %> - SCORE: <%= @scores.select{|s| s.user == user}.first.value %>
  2. - <% end %> -
- <% end %> - <% end %> - <% end %> - - -<% elsif (@match.status==2) %> - <% if (@tournament.players.include?(current_user)) %> - <% @match.teams.each do |team| %> - <% if team.users.include?(current_user) %> -
    - <% team.users.collect{|u| u.user_name}.each do |k| %> -
  1. <%= k%> -
    - <% if (@tournament.game_id == 1) %> - <%= if @blue2["#{k}"] == nil - "Level: #{@purp2["#{k}"]["level"]} K/D/A: #{@purp2["#{k}"]["championsKilled"]}/#{@purp2["#{k}"]["numDeaths"]}/#{@purp2["#{k}"]["assists"]} Gold:#{@purp2["#{k}"]["goldEarned"]}" - else - "Level: #{@blue2["#{k}"]["level"]} K/D/A: #{@blue2["#{k}"]["championsKilled"]}/#{@blue2["#{k}"]["numDeaths"]}/#{@blue2["#{k}"]["assists"]} Gold:#{@blue2["#{k}"]["goldEarned"]}" - end %> - <% end %> -
  2. - <% end %> -
- <% end %> - <% end %> - <% elsif (@tournament.hosts.include?(current_user)) %> - - <% end %> -<% end %> + +
<% unless @match.winner.nil? %>

@@ -114,25 +51,65 @@

<% end %> -<% if @tournament.players.include?(current_user) %> -
-
- <%= form_tag(tournament_match_path(@tournament, @match), method: "put") do %> - <% case @match.status %> +
+ <%= form_tag(tournament_match_path(@tournament, @match), method: "put") do %> + <% case @match.status %> <% when 0 %> - - <%= submit_tag("Start Match") %> - <% when 1 %> - - <%= submit_tag("Begin Peer Evaluation") %> - <% when 2 %> - - <%= submit_tag("End Match") %> - <% when 3 %> - - <%= submit_tag("Reset Status") %> - <% end %> - <% end %> - + + <% if @tournament.hosts.include? current_user %> + + <%= submit_tag("Start Match", :disabled => @match.teams.count < @tournament.min_teams_per_match) %> + <% else %> +

Match is waiting to start.

+ <% end %> + <% when 1 %> + + <% if @tournament.hosts.include? current_user %> + + <% @match.teams.each do |team| %> +
Team <%= team.id.to_s %> + <% team.users.collect{|u| u.user_name}.each do |k| %><% end %> +
+ <% end %> + <%= submit_tag("Finish match") %> + <% else %> +

The match is running; the host has yet to post the scores of the match.

+ <% end %> + <% when 2 %> + + + <% users = []; @match.teams.each{|t| users.concat(t.users)}; %> + <% if users.include? current_user %> + <% @match.teams.each do |team| %> + <% if team.users.include?(current_user) %> +
    + <% team.users.collect{|u| u.user_name}.each do |k| %> +
  1. <%= k%> +
    + <% if (@tournament.game_id == 1) %> + <%= if @blue2["#{k}"] == nil + "Level: #{@purp2["#{k}"]["level"]} K/D/A: #{@purp2["#{k}"]["championsKilled"]}/#{@purp2["#{k}"]["numDeaths"]}/#{@purp2["#{k}"]["assists"]} Gold:#{@purp2["#{k}"]["goldEarned"]}" + else + "Level: #{@blue2["#{k}"]["level"]} K/D/A: #{@blue2["#{k}"]["championsKilled"]}/#{@blue2["#{k}"]["numDeaths"]}/#{@blue2["#{k}"]["assists"]} Gold:#{@blue2["#{k}"]["goldEarned"]}" + end %> + <% end %> +
  2. + <% end %> +
+ <% end %> + <% end %> + <%= submit_tag("Submit peer evaluation") %> + <% else %> + Waiting for peer evaluations to be submitted. + <% end %> + <% when 3 %> + + This match is done. + + <%= submit_tag("Reset Status") %> + <% end # case %> + <% end # form %>
-<% end %> -- cgit v1.2.3-2-g168b