|
1 | | -class SyncMatchJob < ApplicationJob |
2 | | - queue_as :default |
3 | | - |
4 | | - retry_on RiotApiService::RateLimitError, wait: :polynomially_longer, attempts: 5 |
5 | | - retry_on RiotApiService::RiotApiError, wait: 1.minute, attempts: 3 |
6 | | - |
7 | | - def perform(match_id, organization_id, region = 'BR') |
8 | | - organization = Organization.find(organization_id) |
9 | | - riot_service = RiotApiService.new |
10 | | - |
11 | | - match_data = riot_service.get_match_details( |
12 | | - match_id: match_id, |
13 | | - region: region |
14 | | - ) |
15 | | - |
16 | | - # Check if match already exists |
17 | | - match = Match.find_by(riot_match_id: match_data[:match_id]) |
18 | | - if match.present? |
19 | | - Rails.logger.info("Match #{match_id} already exists") |
20 | | - return |
21 | | - end |
22 | | - |
23 | | - # Create match record |
24 | | - match = create_match_record(match_data, organization) |
25 | | - |
26 | | - # Create player match stats |
27 | | - create_player_match_stats(match, match_data[:participants], organization) |
28 | | - |
29 | | - Rails.logger.info("Successfully synced match #{match_id}") |
30 | | - |
31 | | - rescue RiotApiService::NotFoundError => e |
32 | | - Rails.logger.error("Match not found in Riot API: #{match_id} - #{e.message}") |
33 | | - rescue StandardError => e |
34 | | - Rails.logger.error("Failed to sync match #{match_id}: #{e.message}") |
35 | | - raise |
36 | | - end |
37 | | - |
38 | | - private |
39 | | - |
40 | | - def create_match_record(match_data, organization) |
41 | | - Match.create!( |
42 | | - organization: organization, |
43 | | - riot_match_id: match_data[:match_id], |
44 | | - match_type: determine_match_type(match_data[:game_mode]), |
45 | | - game_start: match_data[:game_creation], |
46 | | - game_end: match_data[:game_creation] + match_data[:game_duration].seconds, |
47 | | - game_duration: match_data[:game_duration], |
48 | | - game_version: match_data[:game_version], |
49 | | - victory: determine_team_victory(match_data[:participants], organization) |
50 | | - ) |
51 | | - end |
52 | | - |
53 | | - def create_player_match_stats(match, participants, organization) |
54 | | - Rails.logger.info "Creating stats for #{participants.count} participants" |
55 | | - created_count = 0 |
56 | | - |
57 | | - participants.each do |participant_data| |
58 | | - # Find player by PUUID |
59 | | - player = organization.players.find_by(riot_puuid: participant_data[:puuid]) |
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}" |
67 | | - |
68 | | - PlayerMatchStat.create!( |
69 | | - match: match, |
70 | | - player: player, |
71 | | - role: normalize_role(participant_data[:role]), |
72 | | - champion: participant_data[:champion_name], |
73 | | - kills: participant_data[:kills], |
74 | | - deaths: participant_data[:deaths], |
75 | | - assists: participant_data[:assists], |
76 | | - gold_earned: participant_data[:gold_earned], |
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, |
81 | | - vision_score: participant_data[:vision_score], |
82 | | - wards_placed: participant_data[:wards_placed], |
83 | | - wards_destroyed: participant_data[:wards_killed], |
84 | | - first_blood: participant_data[:first_blood_kill], |
85 | | - double_kills: participant_data[:double_kills], |
86 | | - triple_kills: participant_data[:triple_kills], |
87 | | - quadra_kills: participant_data[:quadra_kills], |
88 | | - penta_kills: participant_data[:penta_kills], |
89 | | - performance_score: calculate_performance_score(participant_data) |
90 | | - ) |
91 | | - created_count += 1 |
92 | | - Rails.logger.info "Stat created successfully for #{player.summoner_name}" |
93 | | - end |
94 | | - |
95 | | - Rails.logger.info "Created #{created_count} player match stats" |
96 | | - end |
97 | | - |
98 | | - def determine_match_type(game_mode) |
99 | | - case game_mode.upcase |
100 | | - when 'CLASSIC' then 'official' |
101 | | - when 'ARAM' then 'scrim' |
102 | | - else 'scrim' |
103 | | - end |
104 | | - end |
105 | | - |
106 | | - def determine_team_victory(participants, organization) |
107 | | - # Find our players in the match |
108 | | - our_player_puuids = organization.players.pluck(:riot_puuid).compact |
109 | | - our_participants = participants.select { |p| our_player_puuids.include?(p[:puuid]) } |
110 | | - |
111 | | - return nil if our_participants.empty? |
112 | | - |
113 | | - # Check if our players won |
114 | | - our_participants.first[:win] |
115 | | - end |
116 | | - |
117 | | - def normalize_role(role) |
118 | | - role_mapping = { |
119 | | - 'top' => 'top', |
120 | | - 'jungle' => 'jungle', |
121 | | - 'middle' => 'mid', |
122 | | - 'mid' => 'mid', |
123 | | - 'bottom' => 'adc', |
124 | | - 'adc' => 'adc', |
125 | | - 'utility' => 'support', |
126 | | - 'support' => 'support' |
127 | | - } |
128 | | - |
129 | | - role_mapping[role&.downcase] || 'mid' |
130 | | - end |
131 | | - |
132 | | - def calculate_performance_score(participant_data) |
133 | | - # Simple performance score calculation |
134 | | - # This can be made more sophisticated |
135 | | - kda = calculate_kda( |
136 | | - kills: participant_data[:kills], |
137 | | - deaths: participant_data[:deaths], |
138 | | - assists: participant_data[:assists] |
139 | | - ) |
140 | | - |
141 | | - base_score = kda * 10 |
142 | | - damage_score = (participant_data[:total_damage_dealt] / 1000.0) |
143 | | - vision_score = participant_data[:vision_score] || 0 |
144 | | - |
145 | | - (base_score + damage_score * 0.1 + vision_score).round(2) |
146 | | - end |
147 | | - |
148 | | - def calculate_kda(kills:, deaths:, assists:) |
149 | | - total = (kills + assists).to_f |
150 | | - return total if deaths.zero? |
151 | | - |
152 | | - total / deaths |
153 | | - end |
154 | | -end |
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class SyncMatchJob < ApplicationJob |
| 4 | + queue_as :default |
| 5 | + |
| 6 | + retry_on RiotApiService::RateLimitError, wait: :polynomially_longer, attempts: 5 |
| 7 | + retry_on RiotApiService::RiotApiError, wait: 1.minute, attempts: 3 |
| 8 | + |
| 9 | + def perform(match_id, organization_id, region = 'BR') |
| 10 | + organization = Organization.find(organization_id) |
| 11 | + riot_service = RiotApiService.new |
| 12 | + |
| 13 | + match_data = riot_service.get_match_details( |
| 14 | + match_id: match_id, |
| 15 | + region: region |
| 16 | + ) |
| 17 | + |
| 18 | + # Check if match already exists |
| 19 | + match = Match.find_by(riot_match_id: match_data[:match_id]) |
| 20 | + if match.present? |
| 21 | + Rails.logger.info("Match #{match_id} already exists") |
| 22 | + return |
| 23 | + end |
| 24 | + |
| 25 | + # Create match record |
| 26 | + match = create_match_record(match_data, organization) |
| 27 | + |
| 28 | + # Create player match stats |
| 29 | + create_player_match_stats(match, match_data[:participants], organization) |
| 30 | + |
| 31 | + Rails.logger.info("Successfully synced match #{match_id}") |
| 32 | + rescue RiotApiService::NotFoundError => e |
| 33 | + Rails.logger.error("Match not found in Riot API: #{match_id} - #{e.message}") |
| 34 | + rescue StandardError => e |
| 35 | + Rails.logger.error("Failed to sync match #{match_id}: #{e.message}") |
| 36 | + raise |
| 37 | + end |
| 38 | + |
| 39 | + private |
| 40 | + |
| 41 | + def create_match_record(match_data, organization) |
| 42 | + Match.create!( |
| 43 | + organization: organization, |
| 44 | + riot_match_id: match_data[:match_id], |
| 45 | + match_type: determine_match_type(match_data[:game_mode]), |
| 46 | + game_start: match_data[:game_creation], |
| 47 | + game_end: match_data[:game_creation] + match_data[:game_duration].seconds, |
| 48 | + game_duration: match_data[:game_duration], |
| 49 | + game_version: match_data[:game_version], |
| 50 | + victory: determine_team_victory(match_data[:participants], organization) |
| 51 | + ) |
| 52 | + end |
| 53 | + |
| 54 | + def create_player_match_stats(match, participants, organization) |
| 55 | + Rails.logger.info "Creating stats for #{participants.count} participants" |
| 56 | + created_count = 0 |
| 57 | + |
| 58 | + participants.each do |participant_data| |
| 59 | + # Find player by PUUID |
| 60 | + player = organization.players.find_by(riot_puuid: participant_data[:puuid]) |
| 61 | + |
| 62 | + if player.nil? |
| 63 | + Rails.logger.debug "Participant PUUID #{participant_data[:puuid][0..20]}... not found in organization" |
| 64 | + next |
| 65 | + end |
| 66 | + |
| 67 | + Rails.logger.info "Creating stat for player: #{player.summoner_name}" |
| 68 | + |
| 69 | + PlayerMatchStat.create!( |
| 70 | + match: match, |
| 71 | + player: player, |
| 72 | + role: normalize_role(participant_data[:role]), |
| 73 | + champion: participant_data[:champion_name], |
| 74 | + kills: participant_data[:kills], |
| 75 | + deaths: participant_data[:deaths], |
| 76 | + assists: participant_data[:assists], |
| 77 | + gold_earned: participant_data[:gold_earned], |
| 78 | + damage_dealt_champions: participant_data[:total_damage_dealt], |
| 79 | + damage_dealt_total: participant_data[:total_damage_dealt], |
| 80 | + damage_taken: participant_data[:total_damage_taken], |
| 81 | + cs: participant_data[:minions_killed].to_i + participant_data[:neutral_minions_killed].to_i, |
| 82 | + vision_score: participant_data[:vision_score], |
| 83 | + wards_placed: participant_data[:wards_placed], |
| 84 | + wards_destroyed: participant_data[:wards_killed], |
| 85 | + first_blood: participant_data[:first_blood_kill], |
| 86 | + double_kills: participant_data[:double_kills], |
| 87 | + triple_kills: participant_data[:triple_kills], |
| 88 | + quadra_kills: participant_data[:quadra_kills], |
| 89 | + penta_kills: participant_data[:penta_kills], |
| 90 | + performance_score: calculate_performance_score(participant_data) |
| 91 | + ) |
| 92 | + created_count += 1 |
| 93 | + Rails.logger.info "Stat created successfully for #{player.summoner_name}" |
| 94 | + end |
| 95 | + |
| 96 | + Rails.logger.info "Created #{created_count} player match stats" |
| 97 | + end |
| 98 | + |
| 99 | + def determine_match_type(game_mode) |
| 100 | + case game_mode.upcase |
| 101 | + when 'CLASSIC' then 'official' |
| 102 | + when 'ARAM' then 'scrim' |
| 103 | + else 'scrim' |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + def determine_team_victory(participants, organization) |
| 108 | + # Find our players in the match |
| 109 | + our_player_puuids = organization.players.pluck(:riot_puuid).compact |
| 110 | + our_participants = participants.select { |p| our_player_puuids.include?(p[:puuid]) } |
| 111 | + |
| 112 | + return nil if our_participants.empty? |
| 113 | + |
| 114 | + # Check if our players won |
| 115 | + our_participants.first[:win] |
| 116 | + end |
| 117 | + |
| 118 | + def normalize_role(role) |
| 119 | + role_mapping = { |
| 120 | + 'top' => 'top', |
| 121 | + 'jungle' => 'jungle', |
| 122 | + 'middle' => 'mid', |
| 123 | + 'mid' => 'mid', |
| 124 | + 'bottom' => 'adc', |
| 125 | + 'adc' => 'adc', |
| 126 | + 'utility' => 'support', |
| 127 | + 'support' => 'support' |
| 128 | + } |
| 129 | + |
| 130 | + role_mapping[role&.downcase] || 'mid' |
| 131 | + end |
| 132 | + |
| 133 | + def calculate_performance_score(participant_data) |
| 134 | + # Simple performance score calculation |
| 135 | + # This can be made more sophisticated |
| 136 | + kda = calculate_kda( |
| 137 | + kills: participant_data[:kills], |
| 138 | + deaths: participant_data[:deaths], |
| 139 | + assists: participant_data[:assists] |
| 140 | + ) |
| 141 | + |
| 142 | + base_score = kda * 10 |
| 143 | + damage_score = (participant_data[:total_damage_dealt] / 1000.0) |
| 144 | + vision_score = participant_data[:vision_score] || 0 |
| 145 | + |
| 146 | + (base_score + damage_score * 0.1 + vision_score).round(2) |
| 147 | + end |
| 148 | + |
| 149 | + def calculate_kda(kills:, deaths:, assists:) |
| 150 | + total = (kills + assists).to_f |
| 151 | + return total if deaths.zero? |
| 152 | + |
| 153 | + total / deaths |
| 154 | + end |
| 155 | +end |
0 commit comments