@@ -145,37 +145,9 @@ def meta_analysis(role:, patch:)
145145 # @param patch [String] Patch version
146146 # @return [Array<Hash>] Suggested counters with winrate
147147 def suggest_counters ( opponent_pick :, role :, patch :)
148- # Find matches where opponent_pick was played
149- matches = CompetitiveMatch . recent ( 30 )
150- matches = matches . by_patch ( patch ) if patch . present?
151-
152- counters = Hash . new { |h , k | h [ k ] = { wins : 0 , total : 0 } }
153-
154- matches . each do |match |
155- # Check if opponent picked this champion in this role
156- opponent_champion = match . opponent_picks . find do |p |
157- p [ 'champion' ] == opponent_pick && p [ 'role' ] &.downcase == role . downcase
158- end
159-
160- next unless opponent_champion
161-
162- # Find what was picked against it in same role
163- our_champion = match . our_picks . find { |p | p [ 'role' ] &.downcase == role . downcase }
164- next unless our_champion && our_champion [ 'champion' ]
165-
166- counter_name = our_champion [ 'champion' ]
167- counters [ counter_name ] [ :total ] += 1
168- counters [ counter_name ] [ :wins ] += 1 if match . victory?
169- end
170-
171- # Calculate winrates and sort
172- counters . map do |champion , stats |
173- {
174- champion : champion ,
175- games : stats [ :total ] ,
176- winrate : ( ( stats [ :wins ] . to_f / stats [ :total ] ) * 100 ) . round ( 2 )
177- }
178- end . sort_by { |c | -c [ :winrate ] } . first ( 5 )
148+ matches = fetch_matches_for_patch ( patch )
149+ counters = build_counters_hash ( matches , opponent_pick : opponent_pick , role : role )
150+ format_counters ( counters )
179151 end
180152
181153 private
@@ -203,4 +175,43 @@ def extract_picks_and_bans(matches, role)
203175
204176 [ picks , bans ]
205177 end
178+
179+ def fetch_matches_for_patch ( patch )
180+ matches = CompetitiveMatch . recent ( 30 )
181+ patch . present? ? matches . by_patch ( patch ) : matches
182+ end
183+
184+ def build_counters_hash ( matches , opponent_pick :, role :)
185+ counters = Hash . new { |h , k | h [ k ] = { wins : 0 , total : 0 } }
186+ matches . each { |match | accumulate_counter ( counters , match , opponent_pick : opponent_pick , role : role ) }
187+ counters
188+ end
189+
190+ def accumulate_counter ( counters , match , opponent_pick :, role :)
191+ opponent_champion = find_opponent_pick ( match , opponent_pick : opponent_pick , role : role )
192+ return unless opponent_champion
193+
194+ our_champion = match . our_picks . find { |p | p [ 'role' ] &.downcase == role . downcase }
195+ return unless our_champion && our_champion [ 'champion' ]
196+
197+ counter_name = our_champion [ 'champion' ]
198+ counters [ counter_name ] [ :total ] += 1
199+ counters [ counter_name ] [ :wins ] += 1 if match . victory?
200+ end
201+
202+ def find_opponent_pick ( match , opponent_pick :, role :)
203+ match . opponent_picks . find do |p |
204+ p [ 'champion' ] == opponent_pick && p [ 'role' ] &.downcase == role . downcase
205+ end
206+ end
207+
208+ def format_counters ( counters )
209+ counters . map do |champion , stats |
210+ {
211+ champion : champion ,
212+ games : stats [ :total ] ,
213+ winrate : ( ( stats [ :wins ] . to_f / stats [ :total ] ) * 100 ) . round ( 2 )
214+ }
215+ end . sort_by { |c | -c [ :winrate ] } . first ( 5 )
216+ end
206217end
0 commit comments