Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions app/jobs/sync_player_from_riot_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'],
Expand All @@ -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!({
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
24 changes: 23 additions & 1 deletion app/modules/players/jobs/sync_player_from_riot_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down