@@ -115,15 +115,41 @@ def create_player_match_stats(match, participants, organization)
115115 puts "SyncMatchJob: Our players in match: #{ our_participants . size } "
116116
117117 team_totals = calculate_team_totals ( participants , our_participants , is_competitive )
118+ opponent_map = build_opponent_map ( participants )
118119
119120 participants . each do |participant_data |
120121 player = organization . players . find_by ( riot_puuid : participant_data [ :puuid ] )
121122 next unless player
122123
123- create_stat_for_participant ( match , player , participant_data , team_totals )
124+ create_stat_for_participant ( match , player , participant_data , team_totals , opponent_map )
124125 end
125126 end
126127
128+ # Builds a hash mapping each participant's puuid to the champion name of their
129+ # lane opponent (same teamPosition on the opposing team).
130+ # Returns an empty hash when the match has an unexpected team structure.
131+ def build_opponent_map ( participants )
132+ by_team = participants . group_by { |p | p [ :team_id ] }
133+ teams = by_team . keys
134+ return { } unless teams . size == 2
135+
136+ result = { }
137+ teams . each do |team_id |
138+ other_team_id = teams . find { |t | t != team_id }
139+ other_team = by_team [ other_team_id ] || [ ]
140+
141+ by_team [ team_id ] . each do |participant |
142+ role = participant [ :role ]
143+ next if role . blank?
144+
145+ opponent = other_team . find { |o | o [ :role ] == role }
146+ result [ participant [ :puuid ] ] = opponent &.dig ( :champion_name )
147+ end
148+ end
149+
150+ result
151+ end
152+
127153 def calculate_team_totals ( participants , our_participants , is_competitive )
128154 source = is_competitive ? our_participants : participants
129155 source . group_by { |p | p [ :team_id ] } . transform_values do |team_participants |
@@ -137,7 +163,7 @@ def calculate_team_totals(participants, our_participants, is_competitive)
137163 end
138164 end
139165
140- def create_stat_for_participant ( match , player , participant_data , team_totals )
166+ def create_stat_for_participant ( match , player , participant_data , team_totals , opponent_map = { } )
141167 team_stats = team_totals [ participant_data [ :team_id ] ]
142168 damage_share = calc_share ( participant_data [ :total_damage_dealt ] , team_stats &.dig ( :total_damage ) )
143169 gold_share = calc_share ( participant_data [ :gold_earned ] , team_stats &.dig ( :total_gold ) )
@@ -148,6 +174,7 @@ def create_stat_for_participant(match, player, participant_data, team_totals)
148174 player : player ,
149175 role : normalize_role ( participant_data [ :role ] ) ,
150176 champion : participant_data [ :champion_name ] ,
177+ opponent_champion : opponent_map [ participant_data [ :puuid ] ] ,
151178 kills : participant_data [ :kills ] ,
152179 deaths : participant_data [ :deaths ] ,
153180 assists : participant_data [ :assists ] ,
@@ -160,6 +187,8 @@ def create_stat_for_participant(match, player, participant_data, team_totals)
160187 wards_placed : participant_data [ :wards_placed ] ,
161188 wards_destroyed : participant_data [ :wards_killed ] ,
162189 first_blood : participant_data [ :first_blood_kill ] ,
190+ first_tower : participant_data [ :first_tower_kill ] ,
191+ control_wards_purchased : participant_data [ :control_wards_purchased ] ,
163192 double_kills : participant_data [ :double_kills ] ,
164193 triple_kills : participant_data [ :triple_kills ] ,
165194 quadra_kills : participant_data [ :quadra_kills ] ,
0 commit comments