|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Fetches recent match history for a scouting target and aggregates |
| 4 | +# per-champion and overall performance stats. |
| 5 | +# |
| 6 | +# Used by both SyncScoutingTargetJob (background) and the inline sync |
| 7 | +# action in Scouting::PlayersController (synchronous response). |
| 8 | +class PerformanceAggregator |
| 9 | + MATCH_COUNT = 20 |
| 10 | + |
| 11 | + def initialize(riot_service:) |
| 12 | + @riot = riot_service |
| 13 | + end |
| 14 | + |
| 15 | + # Returns a hash ready to be stored in target.recent_performance. |
| 16 | + # Returns nil if the PUUID is missing or no match data is available. |
| 17 | + def call(puuid:, region:) |
| 18 | + return nil if puuid.blank? |
| 19 | + |
| 20 | + match_ids = @riot.get_match_history(puuid: puuid, region: region, count: MATCH_COUNT) |
| 21 | + return nil if match_ids.empty? |
| 22 | + |
| 23 | + stats = collect_stats(match_ids, puuid, region) |
| 24 | + return nil if stats.empty? |
| 25 | + |
| 26 | + build_summary(stats) |
| 27 | + rescue RiotApiService::RiotApiError => e |
| 28 | + Rails.logger.warn("[PerformanceAggregator] Skipping match fetch: #{e.message}") |
| 29 | + nil |
| 30 | + end |
| 31 | + |
| 32 | + private |
| 33 | + |
| 34 | + def collect_stats(match_ids, puuid, region) |
| 35 | + match_ids.filter_map do |match_id| |
| 36 | + details = @riot.get_match_details(match_id: match_id, region: region) |
| 37 | + details[:participants].find { |p| p[:puuid] == puuid } |
| 38 | + rescue RiotApiService::RiotApiError => e |
| 39 | + Rails.logger.warn("[PerformanceAggregator] Could not fetch #{match_id}: #{e.message}") |
| 40 | + nil |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + def build_summary(stats) |
| 45 | + aggregate_overall(stats).merge( |
| 46 | + champion_pool_stats: aggregate_per_champion(stats), |
| 47 | + matches_analyzed: stats.size |
| 48 | + ) |
| 49 | + end |
| 50 | + |
| 51 | + def aggregate_overall(stats) |
| 52 | + totals = sum_stats(stats) |
| 53 | + wins = stats.count { |p| p[:win] } |
| 54 | + total = stats.size |
| 55 | + |
| 56 | + overall_hash(totals, wins, total) |
| 57 | + end |
| 58 | + |
| 59 | + def overall_hash(totals, wins, total) # rubocop:disable Metrics/AbcSize |
| 60 | + { |
| 61 | + games_played: total, |
| 62 | + win_rate: (wins.to_f / total * 100).round(1), |
| 63 | + avg_kda: kda_ratio(totals[:kills], totals[:deaths], totals[:assists], total).round(2), |
| 64 | + avg_kills: (totals[:kills].to_f / total).round(1), |
| 65 | + avg_deaths: (totals[:deaths].to_f / total).round(1), |
| 66 | + avg_assists: (totals[:assists].to_f / total).round(1), |
| 67 | + avg_vision_score: (totals[:vision].to_f / total).round(1), |
| 68 | + avg_cs_per_min: (totals[:cs].to_f / total).round(1) |
| 69 | + } |
| 70 | + end |
| 71 | + |
| 72 | + def aggregate_per_champion(stats) |
| 73 | + stats.group_by { |p| p[:champion_name] } |
| 74 | + .map { |champion, games| champion_row(champion, games) } |
| 75 | + .sort_by { |c| -c[:games] } |
| 76 | + end |
| 77 | + |
| 78 | + def champion_row(champion, games) # rubocop:disable Metrics/AbcSize |
| 79 | + totals = sum_stats(games) |
| 80 | + wins = games.count { |p| p[:win] } |
| 81 | + total = games.size |
| 82 | + |
| 83 | + { |
| 84 | + champion: champion, |
| 85 | + games: total, |
| 86 | + wins: wins, |
| 87 | + winrate: (wins.to_f / total * 100).round(1), |
| 88 | + kda_ratio: kda_ratio(totals[:kills], totals[:deaths], totals[:assists], total).round(2), |
| 89 | + avg_kills: (totals[:kills].to_f / total).round(1), |
| 90 | + avg_deaths: (totals[:deaths].to_f / total).round(1), |
| 91 | + avg_assists: (totals[:assists].to_f / total).round(1), |
| 92 | + avg_cs_per_min: (totals[:cs].to_f / total).round(1) |
| 93 | + } |
| 94 | + end |
| 95 | + |
| 96 | + def sum_stats(stats) |
| 97 | + { |
| 98 | + kills: stats.sum { |p| p[:kills].to_i }, |
| 99 | + deaths: stats.sum { |p| p[:deaths].to_i }, |
| 100 | + assists: stats.sum { |p| p[:assists].to_i }, |
| 101 | + vision: stats.sum { |p| p[:vision_score].to_i }, |
| 102 | + cs: stats.sum { |p| p[:minions_killed].to_i + p[:neutral_minions_killed].to_i } |
| 103 | + } |
| 104 | + end |
| 105 | + |
| 106 | + def kda_ratio(kills, deaths, assists, total) |
| 107 | + avg_deaths = deaths.to_f / total |
| 108 | + return (kills + assists).to_f / total if avg_deaths.zero? |
| 109 | + |
| 110 | + (kills + assists).to_f / deaths |
| 111 | + end |
| 112 | +end |
0 commit comments