diff --git a/app/jobs/sync_player_from_riot_job.rb b/app/jobs/sync_player_from_riot_job.rb index e6f2b39..9c577d8 100644 --- a/app/jobs/sync_player_from_riot_job.rb +++ b/app/jobs/sync_player_from_riot_job.rb @@ -4,14 +4,12 @@ class SyncPlayerFromRiotJob < ApplicationJob def perform(player_id) player = Player.find(player_id) - # Check if player has necessary data unless player.riot_puuid.present? || player.summoner_name.present? player.update(sync_status: 'error', last_sync_at: Time.current) Rails.logger.error "Player #{player_id} missing Riot info" return end - # Get Riot API key riot_api_key = ENV['RIOT_API_KEY'] unless riot_api_key.present? player.update(sync_status: 'error', last_sync_at: Time.current) @@ -20,20 +18,18 @@ def perform(player_id) end begin - # Use player's region or default to BR1 region = player.region.presence&.downcase || 'br1' - # Fetch summoner data if player.riot_puuid.present? summoner_data = fetch_summoner_by_puuid(player.riot_puuid, region, riot_api_key) else summoner_data = fetch_summoner_by_name(player.summoner_name, region, riot_api_key) end - # Fetch ranked stats - ranked_data = fetch_ranked_stats(summoner_data['id'], region, riot_api_key) + # Use PUUID for league endpoint (workaround for Riot API bug where summoner_data['id'] is nil) + # See: https://github.com/RiotGames/developer-relations/issues/1092 + ranked_data = fetch_ranked_stats_by_puuid(player.riot_puuid, region, riot_api_key) - # Update player data update_data = { riot_puuid: summoner_data['puuid'], riot_summoner_id: summoner_data['id'], @@ -43,7 +39,6 @@ def perform(player_id) last_sync_at: Time.current } - # Update ranked stats if available solo_queue = ranked_data.find { |q| q['queueType'] == 'RANKED_SOLO_5x5' } if solo_queue update_data.merge!({ @@ -85,7 +80,6 @@ def fetch_summoner_by_name(summoner_name, region, api_key) game_name, tag_line = summoner_name.split('#') tag_line ||= region.upcase - # Get PUUID from Riot ID account_url = "https://americas.api.riotgames.com/riot/account/v1/accounts/by-riot-id/#{URI.encode_www_form_component(game_name)}/#{URI.encode_www_form_component(tag_line)}" account_uri = URI(account_url) account_request = Net::HTTP::Get.new(account_uri) @@ -144,4 +138,24 @@ def fetch_ranked_stats(summoner_id, region, api_key) JSON.parse(response.body) end + + def fetch_ranked_stats_by_puuid(puuid, region, api_key) + require 'net/http' + require 'json' + + url = "https://#{region}.api.riotgames.com/lol/league/v4/entries/by-puuid/#{puuid}" + uri = URI(url) + request = Net::HTTP::Get.new(uri) + request['X-Riot-Token'] = api_key + + response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| + http.request(request) + end + + unless response.is_a?(Net::HTTPSuccess) + raise "Riot API Error: #{response.code} - #{response.body}" + end + + JSON.parse(response.body) + end end diff --git a/app/modules/players/jobs/sync_player_from_riot_job.rb b/app/modules/players/jobs/sync_player_from_riot_job.rb index e2863c8..9c577d8 100644 --- a/app/modules/players/jobs/sync_player_from_riot_job.rb +++ b/app/modules/players/jobs/sync_player_from_riot_job.rb @@ -26,7 +26,9 @@ def perform(player_id) summoner_data = fetch_summoner_by_name(player.summoner_name, region, riot_api_key) end - ranked_data = fetch_ranked_stats(summoner_data['id'], region, riot_api_key) + # Use PUUID for league endpoint (workaround for Riot API bug where summoner_data['id'] is nil) + # See: https://github.com/RiotGames/developer-relations/issues/1092 + ranked_data = fetch_ranked_stats_by_puuid(player.riot_puuid, region, riot_api_key) update_data = { riot_puuid: summoner_data['puuid'], @@ -136,4 +138,24 @@ def fetch_ranked_stats(summoner_id, region, api_key) JSON.parse(response.body) end + + def fetch_ranked_stats_by_puuid(puuid, region, api_key) + require 'net/http' + require 'json' + + url = "https://#{region}.api.riotgames.com/lol/league/v4/entries/by-puuid/#{puuid}" + uri = URI(url) + request = Net::HTTP::Get.new(uri) + request['X-Riot-Token'] = api_key + + response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| + http.request(request) + end + + unless response.is_a?(Net::HTTPSuccess) + raise "Riot API Error: #{response.code} - #{response.body}" + end + + JSON.parse(response.body) + end end diff --git a/docker-compose.yml b/docker-compose.yml index b88ce11..40a68b6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -67,7 +67,7 @@ services: condition: service_healthy redis: condition: service_healthy - command: bundle exec sidekiq + command: bundle exec sidekiq -C config/sidekiq.yml volumes: postgres_data: