summaryrefslogtreecommitdiff
path: root/app/controllers/matches_controller.rb
blob: 4c92e67a0176970a5a202e3a7fba243eeeebc9a1 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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 params[:update_action]
		when "start"
			#
			# Redirect to the current match page for this tournament with the correct sampling view rendered
			#

			@match.status = 1
			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 { redirect_to @tournament, notice: "You don't have permission to start this match." }
					format.json { render json: "Permission denied", status: :forbidden }
				end
			end
		when "finish"
			#
			# Get the winner and blowout status from the params given by the correct sampling view
			#

			unless @match.tournament_stage.tournament.sampling.sampling_done?
				@match.tournament_stage.tournament.sampling.handle_user_interaction(@match, current_user, params)
			end

			# Skip peer evaluation if there aren't enough players per team
			peer = false
			@match.teams.each do |team|
				if team.users.count > 2
					peer = true
				end
			end
			@match.status = peer ? 2 : 3

			respond_to do |format|
				if @match.save
					format.html { redirect_to tournament_match_path(@tournament, @match), notice: 'Peer evaluation started.' }
					format.json { head :no_content }
				else
					format.html { redirect_to @tournament, notice: "Permission denied" }
					format.json { render json: "Permission denied", status: :forbidden }
				end
			end		
		when "peer"
			#
			# Update user scores via scoring method
			#

			#update this to use scoring interface

			order = params[:review_action]
			base_score = 2
			next_score = 3
			order.split(",").reverse.each do |elem|
				player_score = base_score
				if @match.winner.user.include?(@current_user)
					player_score += 10
				else
					player_score += 7
				end
				Score.create(user: elem, match: @match, value: player_score )
				base_score = next_score
				next_score += base_score  
			end

			@match.submitted_peer_evaluations += 1
			players = []; @match.teams.each{|t| players.concat(t.users.all)}
			if (@match.submitted_peer_evaluations == players.count) 
				@match.status = 3
			end


			respond_to do |format|
				if @match.save
					format.html { redirect_to tournament_match_path(@tournament, @match), notice: 'Scores 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"
			#
			# Reset Match Status to 1 in case something needs to be replayed.
			#

			@match.status = 1
			respond_to do |format|
				if @match.save
					format.html { redirect_to tournament_match_path(@tournament, @match), notice: 'Match Status Reset to 1' }
					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
		else
			respond_to do |format|
				format.html { redirect_to @tournament, notice: "Invalid action", status: :unprocessable_entity }
				format.json { render json: @tournament.errors, status: :unprocessable_entity }
			end
		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