From f8c539bb87ca42bafe7f15a6c2d6455ee7aa35de Mon Sep 17 00:00:00 2001 From: Etienne Ischer Date: Fri, 28 May 2021 17:04:31 +0200 Subject: [PATCH] add WowGuild model --- app/models/wow_character.rb | 1 + app/models/wow_guild.rb | 12 +++++++ app/workers/wow_character_detail_worker.rb | 21 +++++++++++- app/workers/wow_guild_detail_worker.rb | 32 +++++++++++++++++++ .../20210528125553_create_wow_guilds.rb | 21 ++++++++++++ db/schema.rb | 18 ++++++++++- spec/models/wow_guild_spec.rb | 5 +++ 7 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 app/models/wow_guild.rb create mode 100644 app/workers/wow_guild_detail_worker.rb create mode 100644 db/migrate/20210528125553_create_wow_guilds.rb create mode 100644 spec/models/wow_guild_spec.rb diff --git a/app/models/wow_character.rb b/app/models/wow_character.rb index 471d653..6ea9b79 100644 --- a/app/models/wow_character.rb +++ b/app/models/wow_character.rb @@ -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 diff --git a/app/models/wow_guild.rb b/app/models/wow_guild.rb new file mode 100644 index 0000000..c35e0f0 --- /dev/null +++ b/app/models/wow_guild.rb @@ -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 diff --git a/app/workers/wow_character_detail_worker.rb b/app/workers/wow_character_detail_worker.rb index 670fed9..c0ce445 100644 --- a/app/workers/wow_character_detail_worker.rb +++ b/app/workers/wow_character_detail_worker.rb @@ -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 diff --git a/app/workers/wow_guild_detail_worker.rb b/app/workers/wow_guild_detail_worker.rb new file mode 100644 index 0000000..19ee83b --- /dev/null +++ b/app/workers/wow_guild_detail_worker.rb @@ -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 diff --git a/db/migrate/20210528125553_create_wow_guilds.rb b/db/migrate/20210528125553_create_wow_guilds.rb new file mode 100644 index 0000000..255d138 --- /dev/null +++ b/db/migrate/20210528125553_create_wow_guilds.rb @@ -0,0 +1,21 @@ +class CreateWowGuilds < ActiveRecord::Migration[6.1] + def change + create_table :wow_guilds do |t| + t.string :name + t.integer :guild_id + t.integer :achievement_points + t.integer :member_count + t.string :faction + t.jsonb :translated_faction + t.belongs_to :wow_realm + + t.timestamps + end + + add_index :wow_guilds, :guild_id, unique: true + + change_table :wow_characters do |t| + t.belongs_to :wow_guild + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 21112fb..6a32e73 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_04_30_213202) do +ActiveRecord::Schema.define(version: 2021_05_28_125553) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -129,10 +129,12 @@ ActiveRecord::Schema.define(version: 2021_04_30_213202) do t.datetime "updated_at", precision: 6, null: false t.bigint "last_position_id" t.bigint "bind_position_id" + t.bigint "wow_guild_id" t.index ["character_id"], name: "index_wow_characters_on_character_id", unique: true t.index ["user_id"], name: "index_wow_characters_on_user_id" t.index ["wow_character_title_id"], name: "index_wow_characters_on_wow_character_title_id" t.index ["wow_class_id"], name: "index_wow_characters_on_wow_class_id" + t.index ["wow_guild_id"], name: "index_wow_characters_on_wow_guild_id" t.index ["wow_race_id"], name: "index_wow_characters_on_wow_race_id" t.index ["wow_realm_id"], name: "index_wow_characters_on_wow_realm_id" end @@ -178,6 +180,20 @@ ActiveRecord::Schema.define(version: 2021_04_30_213202) do t.index ["zone_id"], name: "index_wow_geo_zones_on_zone_id", unique: true end + create_table "wow_guilds", force: :cascade do |t| + t.string "name" + t.integer "guild_id" + t.integer "achievement_points" + t.integer "member_count" + t.string "faction" + t.jsonb "translated_faction" + t.bigint "wow_realm_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["guild_id"], name: "index_wow_guilds_on_guild_id", unique: true + t.index ["wow_realm_id"], name: "index_wow_guilds_on_wow_realm_id" + end + create_table "wow_mounts", force: :cascade do |t| t.jsonb "name" t.string "source_type" diff --git a/spec/models/wow_guild_spec.rb b/spec/models/wow_guild_spec.rb new file mode 100644 index 0000000..d259238 --- /dev/null +++ b/spec/models/wow_guild_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe WowGuild, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end