Skip to content

Commit e2a6f3d

Browse files
committed
fix: analytics performance formatting
- Added time_period parameter support (week/month/season) - Fixed win_rate format (decimal 0-1 instead of percentage) - Ensured all numeric values are floats
1 parent c7cfb30 commit e2a6f3d

File tree

3 files changed

+90
-11
lines changed

3 files changed

+90
-11
lines changed

app/controllers/api/v1/analytics/performance_controller.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ def index
77
# Date range filter
88
if params[:start_date].present? && params[:end_date].present?
99
matches = matches.in_date_range(params[:start_date], params[:end_date])
10+
elsif params[:time_period].present?
11+
# Handle time_period parameter from frontend
12+
days = case params[:time_period]
13+
when 'week' then 7
14+
when 'month' then 30
15+
when 'season' then 90
16+
else 30
17+
end
18+
matches = matches.where('game_start >= ?', days.days.ago)
1019
else
1120
matches = matches.recent(30) # Default to last 30 days
1221
end
@@ -19,6 +28,14 @@ def index
1928
match_type_breakdown: calculate_match_type_breakdown(matches)
2029
}
2130

31+
# Add individual player stats if player_id is provided
32+
if params[:player_id].present?
33+
player = organization_scoped(Player).find_by(id: params[:player_id])
34+
if player
35+
performance_data[:player_stats] = calculate_player_stats(player, matches)
36+
end
37+
end
38+
2239
render_success(performance_data)
2340
end
2441

@@ -135,4 +152,50 @@ def calculate_avg_kda(stats)
135152
deaths = total_deaths.zero? ? 1 : total_deaths
136153
((total_kills + total_assists).to_f / deaths).round(2)
137154
end
155+
156+
def calculate_player_stats(player, matches)
157+
stats = PlayerMatchStat.where(player: player, match: matches)
158+
159+
return nil if stats.empty?
160+
161+
total_kills = stats.sum(:kills)
162+
total_deaths = stats.sum(:deaths)
163+
total_assists = stats.sum(:assists)
164+
games_played = stats.count
165+
166+
# Calculate win rate as decimal (0-1) for frontend
167+
wins = stats.joins(:match).where(matches: { victory: true }).count
168+
win_rate = games_played.zero? ? 0.0 : (wins.to_f / games_played)
169+
170+
# Calculate KDA
171+
deaths = total_deaths.zero? ? 1 : total_deaths
172+
kda = ((total_kills + total_assists).to_f / deaths).round(2)
173+
174+
# Calculate CS per min
175+
total_cs = stats.sum(:cs)
176+
total_duration = matches.where(id: stats.pluck(:match_id)).sum(:game_duration)
177+
cs_per_min = total_duration.zero? ? 0.0 : (total_cs.to_f / (total_duration / 60.0)).round(1)
178+
179+
# Calculate gold per min
180+
total_gold = stats.sum(:gold_earned)
181+
gold_per_min = total_duration.zero? ? 0.0 : (total_gold.to_f / (total_duration / 60.0)).round(0)
182+
183+
# Calculate vision score
184+
vision_score = stats.average(:vision_score)&.round(1) || 0.0
185+
186+
{
187+
player_id: player.id,
188+
summoner_name: player.summoner_name,
189+
games_played: games_played,
190+
win_rate: win_rate,
191+
kda: kda,
192+
cs_per_min: cs_per_min,
193+
gold_per_min: gold_per_min,
194+
vision_score: vision_score,
195+
damage_share: 0.0, # Would need total team damage to calculate
196+
avg_kills: (total_kills.to_f / games_played).round(1),
197+
avg_deaths: (total_deaths.to_f / games_played).round(1),
198+
avg_assists: (total_assists.to_f / games_played).round(1)
199+
}
200+
end
138201
end

app/jobs/sync_match_job.rb

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,25 @@ def create_match_record(match_data, organization)
4545
game_start: match_data[:game_creation],
4646
game_end: match_data[:game_creation] + match_data[:game_duration].seconds,
4747
game_duration: match_data[:game_duration],
48-
patch_version: match_data[:game_version],
48+
game_version: match_data[:game_version],
4949
victory: determine_team_victory(match_data[:participants], organization)
5050
)
5151
end
5252

5353
def create_player_match_stats(match, participants, organization)
54+
Rails.logger.info "Creating stats for #{participants.count} participants"
55+
created_count = 0
56+
5457
participants.each do |participant_data|
5558
# Find player by PUUID
5659
player = organization.players.find_by(riot_puuid: participant_data[:puuid])
57-
next unless player
60+
61+
if player.nil?
62+
Rails.logger.debug "Participant PUUID #{participant_data[:puuid][0..20]}... not found in organization"
63+
next
64+
end
65+
66+
Rails.logger.info "Creating stat for player: #{player.summoner_name}"
5867

5968
PlayerMatchStat.create!(
6069
match: match,
@@ -65,22 +74,25 @@ def create_player_match_stats(match, participants, organization)
6574
deaths: participant_data[:deaths],
6675
assists: participant_data[:assists],
6776
gold_earned: participant_data[:gold_earned],
68-
total_damage_dealt: participant_data[:total_damage_dealt],
69-
total_damage_taken: participant_data[:total_damage_taken],
70-
minions_killed: participant_data[:minions_killed],
71-
jungle_minions_killed: participant_data[:neutral_minions_killed],
77+
damage_dealt_champions: participant_data[:total_damage_dealt],
78+
damage_dealt_total: participant_data[:total_damage_dealt],
79+
damage_taken: participant_data[:total_damage_taken],
80+
cs: participant_data[:minions_killed].to_i + participant_data[:neutral_minions_killed].to_i,
7281
vision_score: participant_data[:vision_score],
7382
wards_placed: participant_data[:wards_placed],
74-
wards_killed: participant_data[:wards_killed],
75-
champion_level: participant_data[:champion_level],
76-
first_blood_kill: participant_data[:first_blood_kill],
83+
wards_destroyed: participant_data[:wards_killed],
84+
first_blood: participant_data[:first_blood_kill],
7785
double_kills: participant_data[:double_kills],
7886
triple_kills: participant_data[:triple_kills],
7987
quadra_kills: participant_data[:quadra_kills],
8088
penta_kills: participant_data[:penta_kills],
8189
performance_score: calculate_performance_score(participant_data)
8290
)
91+
created_count += 1
92+
Rails.logger.info "Stat created successfully for #{player.summoner_name}"
8393
end
94+
95+
Rails.logger.info "Created #{created_count} player match stats"
8496
end
8597

8698
def determine_match_type(game_mode)

app/services/riot_api_service.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,15 @@ def check_rate_limit!
141141
end
142142

143143
def platform_for_region(region)
144-
REGIONS.dig(region.upcase, :platform) || raise(RiotApiError, "Unknown region: #{region}")
144+
# Handle both 'BR' and 'br1' formats
145+
clean_region = region.to_s.upcase.gsub(/\d+/, '')
146+
REGIONS.dig(clean_region, :platform) || raise(RiotApiError, "Unknown region: #{region}")
145147
end
146148

147149
def regional_route_for_region(region)
148-
REGIONS.dig(region.upcase, :region) || raise(RiotApiError, "Unknown region: #{region}")
150+
# Handle both 'BR' and 'br1' formats
151+
clean_region = region.to_s.upcase.gsub(/\d+/, '')
152+
REGIONS.dig(clean_region, :region) || raise(RiotApiError, "Unknown region: #{region}")
149153
end
150154

151155
def parse_summoner_response(response)

0 commit comments

Comments
 (0)