Files
site/app/workers/wow_character_detail_worker.rb
2021-05-30 17:49:54 +02:00

51 lines
1.9 KiB
Ruby

# frozen_string_literal: true
class WowCharacterDetailWorker < WowSidekiqWorker
def perform(wow_character_id)
return unless (wow_character = WowCharacter.find_by(character_id: wow_character_id))
RBattlenet.authenticate(client_id: ENV['BLIZZARD_API_CLIENT_ID'], client_secret: ENV['BLIZZARD_API_CLIENT_SECRET'])
RBattlenet.set_options(locale: 'en_GB')
# Public data
params = {
name: wow_character.name.downcase,
realm: wow_character.wow_realm.slug
}
result = RBattlenet::Wow::Character.find(params)
return unless result.status_code == 200
wow_character.achievements_points = result.achievement_points
wow_character.last_login_timestamp = Time.at(result.last_login_timestamp.to_s[0..-4].to_i).utc
wow_character.average_item_level = result.average_item_level
wow_character.equipped_item_level = result.equipped_item_level
wow_character.wow_character_title = WowCharacterTitle.find_by(title_id: result.active_title.id) if result.active_title
wow_character.wow_guild = find_or_create_wow_guild(result.guild) if result.guild
wow_character.save
update_covenant_progress(wow_character, result.covenant_progress) if result.covenant_progress
end
def find_or_create_wow_guild(guild)
wow_guild = WowGuild.find_or_initialize_by(guild_id: guild.id)
wow_guild.name = guild.name
wow_guild.wow_realm = WowRealm.find_by(realm_id: guild.realm.id)
wow_guild.save
wow_guild.persisted? ? wow_guild : nil
end
def update_covenant_progress(wow_character, covenant_progress)
wow_covenant_progress = wow_character.wow_covenant_progress || WowCovenantProgress.new(wow_character: wow_character)
wow_covenant_progress.renown_level = covenant_progress.renown_level
wow_covenant_progress.wow_covenant = WowCovenant.find_by(covenant_id: covenant_progress.chosen_covenant.id)
wow_covenant_progress.save
end
end