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