Skip to content

Commit af2e2ff

Browse files
committed
chore: improve match details
1 parent 36533c8 commit af2e2ff

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

app/modules/analytics/controllers/champions_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def build_match_summary(stat)
142142
date: stat.match.game_start&.strftime('%Y-%m-%d %H:%M'),
143143
victory: stat.match.victory?,
144144
game_duration: stat.match.game_duration.to_i,
145-
role: stat.role
145+
role: stat.role,
146+
opponent_champion: stat.opponent_champion
146147
}
147148
end
148149

app/modules/matches/jobs/sync_match_job.rb

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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],

app/modules/riot_integration/services/riot_api_service.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ def parse_participant(participant)
238238
summoner_spell_2_casts: participant['summoner2Casts'],
239239
cs_at_10: challenges['laneMinionsFirst10Minutes'],
240240
turret_plates_destroyed: challenges['turretPlatesTaken'],
241+
first_tower_kill: participant['firstTowerKill'],
242+
control_wards_purchased: participant['visionWardsBoughtInGame'],
241243
pings: extract_pings(participant)
242244
}
243245
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
# Adds opponent_champion to player_match_stats for laning matchup context.
4+
# Populated during match sync by finding the participant on the opposing team
5+
# with the same teamPosition (role) as the tracked player.
6+
class AddOpponentChampionToPlayerMatchStats < ActiveRecord::Migration[7.1]
7+
def change
8+
add_column :player_match_stats, :opponent_champion, :string
9+
10+
add_index :player_match_stats, :opponent_champion,
11+
name: 'idx_pms_opponent_champion'
12+
end
13+
end

0 commit comments

Comments
 (0)