@@ -84,6 +84,8 @@ def opponents
8484 status : :internal_server_error )
8585 end
8686
87+ PERFORMANCE_ROLES = %w[ top jungle mid adc support ] . freeze
88+
8789 # ── Private helpers ────────────────────────────────────────────
8890 private
8991
@@ -165,38 +167,41 @@ def build_side_performance(rows)
165167 end
166168 end
167169
168- def build_role_performance ( rows ) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
169- roles = %w[ top jungle mid adc support ]
170- role_stats = roles . each_with_object ( { } ) do | r , h |
171- h [ r ] = { games : 0 , wins : 0 , champions : Hash . new ( 0 ) }
172- end
170+ def build_role_performance ( rows )
171+ role_stats = initial_role_stats
172+ rows . each { | match | accumulate_match_picks ( role_stats , match ) }
173+ role_stats . map { | role , stats | format_role_stat ( role , stats ) }
174+ end
173175
174- rows . each do |match |
175- won = match . victory
176- ( match . our_picks || [ ] ) . each do |pick |
177- role = pick [ 'role' ] &.downcase
178- champ = pick [ 'champion' ]
179- next unless role_stats . key? ( role ) && champ . present?
176+ def initial_role_stats
177+ PERFORMANCE_ROLES . each_with_object ( { } ) { |r , h | h [ r ] = { games : 0 , wins : 0 , champions : Hash . new ( 0 ) } }
178+ end
180179
181- role_stats [ role ] [ :games ] += 1
182- role_stats [ role ] [ :wins ] += 1 if won
183- role_stats [ role ] [ :champions ] [ champ ] += 1
184- end
185- end
180+ def accumulate_match_picks ( role_stats , match )
181+ won = match . victory
182+ ( match . our_picks || [ ] ) . each do |pick |
183+ role = pick [ 'role' ] &.downcase
184+ champ = pick [ 'champion' ]
185+ next unless role_stats . key? ( role ) && champ . present?
186186
187- role_stats . map do |role , s |
188- most_played = s [ :champions ] . max_by { |_ , c | c } &.first || 'N/A'
189- {
190- role : role ,
191- games : s [ :games ] ,
192- wins : s [ :wins ] ,
193- win_rate : s [ :games ] . positive? ? ( s [ :wins ] . to_f / s [ :games ] * 100 ) . round ( 1 ) : 0 ,
194- most_played_champion : most_played ,
195- champion_pool_size : s [ :champions ] . size
196- }
187+ role_stats [ role ] [ :games ] += 1
188+ role_stats [ role ] [ :wins ] += 1 if won
189+ role_stats [ role ] [ :champions ] [ champ ] += 1
197190 end
198191 end
199192
193+ def format_role_stat ( role , stats )
194+ most_played = stats [ :champions ] . max_by { |_ , c | c } &.first || 'N/A'
195+ {
196+ role : role ,
197+ games : stats [ :games ] ,
198+ wins : stats [ :wins ] ,
199+ win_rate : stats [ :games ] . positive? ? ( stats [ :wins ] . to_f / stats [ :games ] * 100 ) . round ( 1 ) : 0 ,
200+ most_played_champion : most_played ,
201+ champion_pool_size : stats [ :champions ] . size
202+ }
203+ end
204+
200205 def extract_meta_champions ( matches )
201206 matches . where . not ( meta_champions : nil )
202207 . pluck ( :meta_champions )
0 commit comments