add WowGuild model
This commit is contained in:
@@ -9,6 +9,7 @@ class WowCharacter < ApplicationRecord
|
||||
belongs_to :wow_class
|
||||
belongs_to :wow_race
|
||||
belongs_to :wow_character_title, optional: true
|
||||
belongs_to :wow_guild, optional: true
|
||||
belongs_to :last_position, class_name: 'WowGeoPosition', optional: true
|
||||
belongs_to :bind_position, class_name: 'WowGeoPosition', optional: true
|
||||
has_one :wow_character_medium, dependent: :nullify
|
||||
|
||||
12
app/models/wow_guild.rb
Normal file
12
app/models/wow_guild.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class WowGuild < ApplicationRecord
|
||||
extend Mobility
|
||||
translates :translated_faction
|
||||
|
||||
belongs_to :wow_realm
|
||||
has_many :wow_characters, dependent: :nullify
|
||||
|
||||
validates :name, presence: true
|
||||
validates :guild_id, presence: true, uniqueness: true
|
||||
end
|
||||
@@ -8,7 +8,11 @@ class WowCharacterDetailWorker < WowSidekiqWorker
|
||||
RBattlenet.set_options(locale: 'en_GB')
|
||||
|
||||
# Public data
|
||||
result = RBattlenet::Wow::Character.find({ name: wow_character.name.downcase, realm: wow_character.wow_realm.slug })
|
||||
params = {
|
||||
name: wow_character.name.downcase,
|
||||
realm: wow_character.wow_realm.slug
|
||||
}
|
||||
result = RBattlenet::Wow::Character.find(params)
|
||||
|
||||
return unless result.status_code == 200
|
||||
|
||||
@@ -17,7 +21,22 @@ class WowCharacterDetailWorker < WowSidekiqWorker
|
||||
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
|
||||
end
|
||||
|
||||
def find_or_create_wow_guild(guild)
|
||||
WowGuild.find_by(guild_id: guild.id)
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
return unless (wow_realm = WowRealm.find_by(realm_id: guild.realm.id))
|
||||
|
||||
wow_guild = WowGuild.create(
|
||||
guild_id: guild.id,
|
||||
name: guild.name,
|
||||
wow_relam: wow_realm
|
||||
)
|
||||
WowGuildDetailWorker.perform_async(wow_guild.guild_id)
|
||||
wow_guild
|
||||
end
|
||||
end
|
||||
|
||||
32
app/workers/wow_guild_detail_worker.rb
Normal file
32
app/workers/wow_guild_detail_worker.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class WowGuildDetailWorker < WowSidekiqWorker
|
||||
def perform(wow_guild_id)
|
||||
return unless (wow_guild = WowGuild.find_by(guild_id: wow_guild_id))
|
||||
|
||||
RBattlenet.authenticate(client_id: ENV['BLIZZARD_API_CLIENT_ID'], client_secret: ENV['BLIZZARD_API_CLIENT_SECRET'])
|
||||
RBattlenet.set_options(locale: 'all')
|
||||
|
||||
# Public data
|
||||
params = {
|
||||
name: wow_guild.name.downcase.tr(' ', '-'),
|
||||
realm: wow_guild.wow_realm.slug
|
||||
}
|
||||
result = RBattlenet::Wow::Guild.find(params)
|
||||
|
||||
return unless result.status_code == 200
|
||||
|
||||
wow_guild.achievement_points = result.achievement_points
|
||||
wow_guild.member_count = result.member_count
|
||||
wow_guild.faction = result.faction.type
|
||||
|
||||
# Localisation data
|
||||
locales.each do |locale|
|
||||
Mobility.with_locale(locale[0]) do
|
||||
wow_guild.translated_faction = result.faction.name[locale[1]]
|
||||
end
|
||||
end
|
||||
|
||||
wow_guild.save
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user