summaryrefslogtreecommitdiff
path: root/app/controllers/matches_controller.rb
blob: 81ffcd850a7cb4eecec88c3d72c96d9875fda1d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class MatchesController < ApplicationController
	require 'httparty'
	require 'json'
	require 'delayed_job'

	before_action :set_tournament, only: [:index]

	# GET /tournaments/1/matches
	# GET /tournaments/1/matches.json
	def index
	end

	# GET /tournaments/1/matches/1
	# GET /tournaments/1/matches/1.json
	def show
		if @match.tournament_stage.tournament.game_id == 1
			file_blue = "blue.yaml"
			file_purple = "purple.yaml"
			@blue2 = YAML.load_file(file_blue)
			@purp2 = YAML.load_file(file_purple)
		end
	end

	# PATCH/PUT /tournaments/1/matches/1
	# PATCH/PUT /tournaments/1/matches/1.json
	def update
		case @match.status
		when 0
			# Created, waiting to be scheduled
		when 1
			# Scheduled, waiting to start
			if (@tournament.hosts.include? current_user) and (params[:update_action] == "start")
				@match.status = 2
				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
				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 }
			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.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
						format.html { render action: 'show' }
						format.json { render json: @match.errors, status: :unprocessable_entity }
					end
				end
				return
			end
		else
			redirect_to tournament_match_path(@tournament, @match)
		end
	end

	private
	# Use callbacks to share common setup or constraints between actions.
	def set_match
		@match = Match.find(params[:id])
		@tournament = @match.tournament_stage.tournament
	end

	def set_tournament
		@tournament = Tournament.find(params[:tournament_id])
	end

	# Never trust parameters from the scary internet, only allow the white list through.
	def match_params
		params.require(:match).permit(:status, :tournament_stage_id, :winner_id, :remote_id, :submitted_peer_evaluations, :update_action)
	end

	# Turn of check_edit, since our #update is flexible
	def check_edit
		set_match
	end
end