From 8aefe73872571ac54738bde71d4da5611659a0cc Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 29 Apr 2014 10:53:28 -0400 Subject: fix match lifecycle --- app/controllers/matches_controller.rb | 44 +++++++++++++++++------------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index f713dff..dbd3e68 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -18,6 +18,7 @@ class MatchesController < ApplicationController # PATCH/PUT /tournaments/1/matches/1 # PATCH/PUT /tournaments/1/matches/1.json def update + notice = nil case @match.status when 0 # Created, waiting to be scheduled @@ -26,46 +27,45 @@ class MatchesController < ApplicationController if (@tournament.hosts.include? current_user) and (params[:update_action] == "start") @match.status = 2 @match.start_sampling - respond_to do |format| - if @match.save - format.html { redirect_to tournament_match_path(@tournament, @match), notice: 'Match has started.' } - format.json { head :no_content } - else - format.html { render action: 'show' } - format.json { render json: @match.errors, status: :unprocessable_entity } - end + if @match.save + notice = 'Match has started.' + else + respond_to do |format| + format.html { render action: 'show' } + format.json { render json: @match.errors, status: :unprocessable_entity } + end + return end - return end when 2 # Started, waiting to finish @match.handle_sampling(@current_user, params) # The @match.status will be updated by Statistic's after_save hook - respond_to do |format| - format.html { redirect_to tournament_match_path(@tournament, @match), notice: 'Match has finished.' } - format.json { head :no_content } + if @match.status == 3 + notice = 'Match has finished' end when 3 if (@tournament.hosts.include? current_user) and (params[:update_action] == "start") ok = true ActiveRecord::Base.transaction do - ok &= @match.statitistics.destroy_all - @match.status = 1 + ok &= @match.statistics.destroy_all + ok &- @match.status = 1 ok &= @match.save end - respond_to do |format| - if @match.save - format.html { redirect_to tournament_match_path(@tournament, @match), notice: 'Match has finished.' } - format.json { head :no_content } - else + if ok + notice = "Match has been reset" + else + respond_to do |format| format.html { render action: 'show' } format.json { render json: @match.errors, status: :unprocessable_entity } end + return end - return end - else - redirect_to tournament_match_path(@tournament, @match) + end + respond_to do |format| + format.html { redirect_to match_path(@match), notice: notice } + format.json { head :no_content } end end -- cgit v1.2.3