@@ -120,7 +120,7 @@ def stats
120120 end ,
121121 comparison : {
122122 total_gold : stats . sum ( :gold_earned ) ,
123- total_damage : stats . sum ( :total_damage_dealt ) ,
123+ total_damage : stats . sum ( :damage_dealt_total ) ,
124124 total_vision_score : stats . sum ( :vision_score ) ,
125125 avg_kda : calculate_avg_kda ( stats )
126126 }
@@ -138,63 +138,22 @@ def import
138138 unless player . riot_puuid . present?
139139 return render_error (
140140 message : 'Player does not have a Riot PUUID. Please sync player from Riot first.' ,
141- code : 'VALIDATION_ERROR ' ,
142- status : :unprocessable_entity
141+ code : 'MISSING_PUUID ' ,
142+ status : :bad_request
143143 )
144144 end
145145
146- begin
147- riot_service = RiotApiService . new
148- region = player . region || 'BR'
149-
150- match_ids = riot_service . get_match_history (
151- puuid : player . riot_puuid ,
152- region : region ,
153- count : count
154- )
155-
156- imported_count = 0
157- match_ids . each do |match_id |
158- next if Match . exists? ( riot_match_id : match_id )
146+ job_id = ImportPlayerMatchesJob . perform_later (
147+ player . id ,
148+ current_organization . id ,
149+ count
150+ ) . job_id
159151
160- SyncMatchJob . perform_later ( match_id , current_organization . id , region )
161- imported_count += 1
162- end
163-
164- render_success ( {
165- message : "Queued #{ imported_count } matches for import" ,
166- total_matches_found : match_ids . count ,
167- already_imported : match_ids . count - imported_count ,
168- player : PlayerSerializer . render_as_hash ( player )
169- } )
170- rescue RedisClient ::CannotConnectError , Redis ::CannotConnectError => e
171- Rails . logger . error "Redis connection failed during match import: #{ e . message } "
172-
173- render_error (
174- message : 'Background job service is temporarily unavailable. Please try again later.' ,
175- code : 'BACKGROUND_SERVICE_UNAVAILABLE' ,
176- status : :service_unavailable ,
177- details : {
178- hint : 'The import service is currently down. Contact your administrator if this persists.' ,
179- player_id : player . id
180- }
181- )
182- rescue RiotApiService ::RiotApiError => e
183- render_error (
184- message : "Failed to fetch matches from Riot API: #{ e . message } " ,
185- code : 'RIOT_API_ERROR' ,
186- status : :bad_gateway
187- )
188- rescue StandardError => e
189- Rails . logger . error "Unexpected error during match import: #{ e . class } - #{ e . message } "
190- Rails . logger . error e . backtrace . first ( 5 ) . join ( "\n " )
191-
192- render_error (
193- message : "Failed to import matches: #{ e . message } " ,
194- code : 'IMPORT_ERROR' ,
195- status : :internal_server_error
196- )
197- end
152+ render_success ( {
153+ job_id : job_id ,
154+ player_id : player . id . to_s ,
155+ count : count
156+ } , message : 'Match import queued successfully' )
198157 end
199158
200159 private
@@ -265,7 +224,7 @@ def calculate_matches_summary(matches)
265224 victories : matches . victories . count ,
266225 defeats : matches . defeats . count ,
267226 win_rate : calculate_win_rate ( matches ) ,
268- by_type : matches . group ( :match_type ) . count ,
227+ by_type : matches . unscope ( :order ) . group ( :match_type ) . count ,
269228 avg_duration : matches . average ( :game_duration ) &.round ( 0 )
270229 }
271230 end
@@ -276,8 +235,8 @@ def calculate_team_stats(stats)
276235 total_deaths : stats . sum ( :deaths ) ,
277236 total_assists : stats . sum ( :assists ) ,
278237 total_gold : stats . sum ( :gold_earned ) ,
279- total_damage : stats . sum ( :total_damage_dealt ) ,
280- total_cs : stats . sum ( :minions_killed ) ,
238+ total_damage : stats . sum ( :damage_dealt_total ) ,
239+ total_cs : stats . sum ( :cs ) ,
281240 total_vision_score : stats . sum ( :vision_score )
282241 }
283242 end
0 commit comments