summaryrefslogtreecommitdiff
path: root/app/controllers/matches_controller.rb
blob: bb5a8761728f7e23a8117d378eb2d3cad4ce8962 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
class MatchesController < ApplicationController
	before_action :set_tournament, only: [:index, :new, :create]
	before_action :set_match, only: [:show, :edit, :update, :destroy]
	# GET /matches
	# GET /matches.json
  require 'httparty'
  require 'json'

  def index
    @matches = @tournament.matches
  end

  def get_riot_info
	if signed_in?

			#current user information
			response = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/by-name/#{current_user.user_name}?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")

			id = response["#{current_user.user_name.downcase}"]['id']

			#recent game information
			recent = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/game/by-summoner/#{response["#{current_user.user_name.downcase}"]['id']}/recent?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")

			game_id = recent["games"][0]["gameId"]

			#remote_user_id = 6651654651354
			#remove_user_name = TeslasMind
			#How to Add
			#how do I access

			#members of most recent game id's
			player1 = recent["games"][0]["fellowPlayers"][0]["summonerId"]
			player2 = recent["games"][0]["fellowPlayers"][1]["summonerId"]
			player3 = recent["games"][0]["fellowPlayers"][2]["summonerId"]
			player4 = recent["games"][0]["fellowPlayers"][3]["summonerId"]
			player5 = recent["games"][0]["fellowPlayers"][4]["summonerId"]
			player6 = recent["games"][0]["fellowPlayers"][5]["summonerId"]
			player7 = recent["games"][0]["fellowPlayers"][6]["summonerId"]
			player8 = recent["games"][0]["fellowPlayers"][7]["summonerId"]
			player9 = recent["games"][0]["fellowPlayers"][8]["summonerId"]

			players_by_id = [player1, player2, player3, player4, player5, player6, player7, player8, player9]

			#collect summoner names
			memb1 = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/#{player1}/name?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")
			memb1 = memb1["#{player1}"]
			sleep(1);

			memb2 = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/#{player2}/name?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")
			memb2 = memb2["#{player2}"]
			sleep(1);

			memb3 = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/#{player3}/name?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")
			memb3 = memb3["#{player3}"]
			sleep(1);

			memb4 = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/#{player4}/name?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")
			memb4 = memb4["#{player4}"]
			sleep(1);

			memb5 = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/#{player5}/name?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")
			memb5 = memb5["#{player5}"]
			sleep(1);

			memb6 = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/#{player6}/name?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")
			memb6 = memb6["#{player6}"]
			sleep(1);

			memb7 = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/#{player7}/name?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")
			memb7 = memb7["#{player7}"]
			sleep(1);

			memb8 = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/#{player8}/name?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")
			memb8 = memb8["#{player8}"]
			sleep(1);

			memb9 = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/#{player9}/name?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")
			memb9 = memb9["#{player9}"]
			sleep(1);

			memb10 = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/#{id}/name?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")
			memb10 = memb10["#{id}"]

			players = ["#{memb1}", "#{memb2}", "#{memb3}", "#{memb4}", "#{memb5}", "#{memb6}", "#{memb7}", "#{memb8}", "#{memb9}", "#{memb10}"]

			sleep(5);

			blue = Hash.new
			purple = Hash.new

			for i in 0..8
				current_player = players_by_id[i]
				place = players[i]
				info = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/game/by-summoner/#{current_player}/recent?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")

				if 100 == info["games"][0]["stats"]["team"]
					blue.merge!("#{place}" => info["games"][0]["stats"])
				else
					purple.merge!("#{place}" => info["games"][0]["stats"])
				end
				sleep(1)
			end					

			if 100 == recent["games"][0]["stats"]["team"]
				blue.merge!("#{players[9]}" => recent["games"][0]["stats"])
			else
				purple.merge!("#{players[9]}" => recent["games"][0]["stats"])
			end

			@purp = purple
			@blue = blue

	end #end if
  end #end def
	# GET /matches/1
	# GET /matches/1.json
	def show
	end

	# GET /matches/new
	def new
	end

	# GET /matches/1/edit
	def edit
	end

	# POST /matches
	# POST /matches.json
	def create
		@match = @tournament.matches.build(match_params)

		respond_to do |format|
			if @match.save
				format.html { redirect_to tournament_matches_path, notice: 'Match was successfully created.' }
				format.json { render action: 'show', status: :created, location: @match }
			else
				format.html { render action: 'new' }
				format.json { render json: @match.errors, status: :unprocessable_entity }
			end
		end
	end

	# PATCH/PUT /matches/1
	# PATCH/PUT /matches/1.json
	def update
		respond_to do |format|
			if @match.update(match_params)
				format.html { redirect_to [@tournament, @match], notice: 'Match was successfully updated.' }
				format.json { head :no_content }
			else
				format.html { render action: 'edit' }
				format.json { render json: @match.errors, status: :unprocessable_entity }
			end
		end
	end

	# DELETE /matches/1
	# DELETE /matches/1.json
	def destroy
		@match.destroy
		respond_to do |format|
			format.html { redirect_to tournament_matches_path }
			format.json { head :no_content }
		end
	end

	private
	# Use callbacks to share common setup or constraints between actions.
	def set_match
		@tournament = Tournament.find(params[:tournament_id])
		@match = @tournament.matches.find(params[:id]);
	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_id, :name, :winner_id, :remote_id)
	end
end