diff --git a/app/helpers/characters_helper.rb b/app/helpers/characters_helper.rb deleted file mode 100644 index c21fbc9..0000000 --- a/app/helpers/characters_helper.rb +++ /dev/null @@ -1,4 +0,0 @@ -# frozen_string_literal: true - -module CharactersHelper -end diff --git a/app/helpers/wow_characters_helper.rb b/app/helpers/wow_characters_helper.rb new file mode 100644 index 0000000..be7f68a --- /dev/null +++ b/app/helpers/wow_characters_helper.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module WowCharactersHelper + def wow_character_title_name(wow_character) + if wow_character.wow_character_title + case wow_character.gender + when 'FEMALE' + wow_character.wow_character_title.female_name.gsub('{name}', wow_character.name) + when 'MALE' + wow_character.wow_character_title.male_name.gsub('{name}', wow_character.name) + end + else + wow_character.name + end + end +end diff --git a/app/helpers/wow_classes_helper.rb b/app/helpers/wow_classes_helper.rb new file mode 100644 index 0000000..13c32b2 --- /dev/null +++ b/app/helpers/wow_classes_helper.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module WowClassesHelper + def gender_class_name(gender, wow_class) + case gender + when 'FEMALE' + wow_class.female_name + when 'MALE' + wow_class.male_name + end + end +end diff --git a/app/helpers/wow_races_helper.rb b/app/helpers/wow_races_helper.rb new file mode 100644 index 0000000..3999735 --- /dev/null +++ b/app/helpers/wow_races_helper.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module WowRacesHelper + def gender_race_name(gender, wow_race) + case gender + when 'FEMALE' + wow_race.female_name + when 'MALE' + wow_race.male_name + end + end +end diff --git a/app/models/wow_character.rb b/app/models/wow_character.rb index df5fffd..471d653 100644 --- a/app/models/wow_character.rb +++ b/app/models/wow_character.rb @@ -12,38 +12,9 @@ class WowCharacter < ApplicationRecord 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 + has_many :wow_standings, dependent: :nullify + has_many :wow_reputations, through: :wow_standings validates :name, presence: true validates :character_id, presence: true, uniqueness: true - - def gender_class_name - case gender - when 'FEMALE' - wow_class.female_name - when 'MALE' - wow_class.male_name - end - end - - def gender_race_name - case gender - when 'FEMALE' - wow_race.female_name - when 'MALE' - wow_race.male_name - end - end - - def title_name - if wow_character_title - case gender - when 'FEMALE' - wow_character_title.female_name.gsub('{name}', name) - when 'MALE' - wow_character_title.male_name.gsub('{name}', name) - end - else - name - end - end end diff --git a/app/models/wow_reputation.rb b/app/models/wow_reputation.rb new file mode 100644 index 0000000..ffdb69c --- /dev/null +++ b/app/models/wow_reputation.rb @@ -0,0 +1,13 @@ +class WowReputation < ApplicationRecord + extend Mobility + translates :name, :description, :translated_faction + + has_many :wow_standings, dependent: :destroy + has_many :wow_characters, through: :wow_standings + belongs_to :wow_reputation_tier, optional: true + has_many :sub_wow_reputations, class_name: 'WowReputation', foreign_key: 'meta_wow_reputation_id', dependent: :nullify, inverse_of: :meta_wow_reputation + belongs_to :meta_wow_reputation, class_name: 'WowReputation', optional: true + + validates :name, presence: true + validates :reputation_id, presence: true, uniqueness: true +end diff --git a/app/models/wow_reputation_tier.rb b/app/models/wow_reputation_tier.rb new file mode 100644 index 0000000..735ec5e --- /dev/null +++ b/app/models/wow_reputation_tier.rb @@ -0,0 +1,6 @@ +class WowReputationTier < ApplicationRecord + has_many :wow_reputations, dependent: :nullify + has_many :wow_reputation_tier_levels, dependent: :destroy + + validates :reputation_tier_id, presence: true, uniqueness: true +end diff --git a/app/models/wow_reputation_tier_level.rb b/app/models/wow_reputation_tier_level.rb new file mode 100644 index 0000000..8c21706 --- /dev/null +++ b/app/models/wow_reputation_tier_level.rb @@ -0,0 +1,9 @@ +class WowReputationTierLevel < ApplicationRecord + extend Mobility + translates :name + + belongs_to :wow_reputation_tier + + validates :name, presence: true + validates :order, presence: true, uniqueness: { scope: :wow_reputation_tier } +end diff --git a/app/models/wow_standing.rb b/app/models/wow_standing.rb new file mode 100644 index 0000000..e214151 --- /dev/null +++ b/app/models/wow_standing.rb @@ -0,0 +1,7 @@ +class WowStanding < ApplicationRecord + extend Mobility + translates :name + + belongs_to :wow_character + belongs_to :wow_reputation +end diff --git a/app/views/wow_characters/index.html.erb b/app/views/wow_characters/index.html.erb index f4c5cca..4f3cc41 100644 --- a/app/views/wow_characters/index.html.erb +++ b/app/views/wow_characters/index.html.erb @@ -22,8 +22,8 @@ <%= link_to character.name, character %> <%= character.translated_gender %> <%= character.wow_realm.name %> - <%= character.gender_race_name %> - <%= character.gender_class_name %> + <%= gender_race_name(character.gender, character.wow_race) %> + <%= gender_class_name(character.gender, character.wow_class) %> <%= character.translated_faction %> <%= character.level %> diff --git a/app/views/wow_characters/show.html.erb b/app/views/wow_characters/show.html.erb index f7c394c..fbccc0b 100644 --- a/app/views/wow_characters/show.html.erb +++ b/app/views/wow_characters/show.html.erb @@ -5,9 +5,9 @@ <% end %>
-
<%= @wow_character.title_name %>
-

<%= @wow_character.gender_race_name %>

-

<%= @wow_character.gender_class_name %>

+
<%= wow_character_title_name(@wow_character) %>
+

<%= gender_race_name(@wow_character.gender, @wow_character.wow_race) %>

+

<%= gender_class_name(@wow_character.gender, @wow_character.wow_class) %>

Last connection: <%= @wow_character.last_login_timestamp %>

Last position: Map: <%= @wow_character.last_position.wow_geo_map.name %> Zone: <%= @wow_character.last_position.wow_geo_zone.name %>

Bind position: Map: <%= @wow_character.bind_position.wow_geo_map.name %> Zone: <%= @wow_character.bind_position.wow_geo_zone.name %>

diff --git a/app/workers/wow_characters_worker.rb b/app/workers/wow_characters_worker.rb index 7a7706c..cc7f886 100644 --- a/app/workers/wow_characters_worker.rb +++ b/app/workers/wow_characters_worker.rb @@ -39,6 +39,7 @@ class WowCharactersWorker < WowSidekiqWorker WowCharacterMediaWorker.perform_async(wow_char.character_id) WowCharacterDetailWorker.perform_async(wow_char.character_id) WowCharacterPositionsWorker.perform_async(wow_char.character_id) + WowStandingWorker.perform_async(wow_char.character_id) end end end diff --git a/app/workers/wow_class_detail_worker.rb b/app/workers/wow_class_detail_worker.rb index d89ff55..7cf072d 100644 --- a/app/workers/wow_class_detail_worker.rb +++ b/app/workers/wow_class_detail_worker.rb @@ -2,7 +2,7 @@ class WowClassDetailWorker < WowSidekiqWorker def perform(class_id) - return unless (wow_class = WowClass.where(class_id: class_id).first) + return unless (wow_class = WowClass.find_by(class_id: class_id)) RBattlenet.set_options(locale: 'all') result = RBattlenet::Wow::PlayableClass.find(class_id) diff --git a/app/workers/wow_mount_detail_worker.rb b/app/workers/wow_mount_detail_worker.rb index c52551c..d59d129 100644 --- a/app/workers/wow_mount_detail_worker.rb +++ b/app/workers/wow_mount_detail_worker.rb @@ -2,7 +2,7 @@ class WowMountDetailWorker < WowSidekiqWorker def perform(mount_id) - return unless (mount = WowMount.where(mount_id: mount_id).first) + return unless (mount = WowMount.find_by(mount_id: mount_id)) RBattlenet.set_options(locale: 'all') result = RBattlenet::Wow::Mount.find(mount_id) diff --git a/app/workers/wow_reputation_detail_worker.rb b/app/workers/wow_reputation_detail_worker.rb new file mode 100644 index 0000000..52aff23 --- /dev/null +++ b/app/workers/wow_reputation_detail_worker.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +class WowReputationDetailWorker < WowSidekiqWorker + def perform(reputation_id) + return unless (wow_reputation = WowReputation.find_by(reputation_id: reputation_id)) + + RBattlenet.set_options(locale: 'all') + result = RBattlenet::Wow::ReputationFaction.find(reputation_id) + + return unless result.status_code == 200 + + # Global data + wow_reputation.wow_reputation_tier = WowReputationTier.find_by(reputation_tier_id: result.reputation_tiers.id) + wow_reputation.faction = result.player_faction.type if result.player_faction + + # If it's a meta faction + result.factions&.each do |faction| + wow_reputation.sub_wow_reputations << WowReputation.find_by(reputation_id: faction.id) + end + + # Localisation data + locales.each do |locale| + Mobility.with_locale(locale[0]) do + wow_reputation.description = result.description[locale[1]] if result.description + wow_reputation.translated_faction = result.player_faction.name[locale[1]] if result.player_faction + end + end + + wow_reputation.save + end +end diff --git a/app/workers/wow_reputation_tier_detail_worker.rb b/app/workers/wow_reputation_tier_detail_worker.rb new file mode 100644 index 0000000..ad0af89 --- /dev/null +++ b/app/workers/wow_reputation_tier_detail_worker.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +class WowReputationTierDetailWorker < WowSidekiqWorker + def perform(reputation_tier_id) + return unless (wow_reputation_tier = WowReputationTier.find_by(reputation_tier_id: reputation_tier_id)) + + RBattlenet.set_options(locale: 'all') + result = RBattlenet::Wow::ReputationTiers.find(reputation_tier_id) + + return unless result.status_code == 200 + + result.tiers.each do |tier| + reputation_tier_level = WowReputationTierLevel.where(wow_reputation_tier: wow_reputation_tier, order: tier.id).first_or_initialize + + reputation_tier_level.wow_reputation_tier = wow_reputation_tier + reputation_tier_level.order = tier.id + reputation_tier_level.min_value = tier.min_value + reputation_tier_level.max_value = tier.max_value + + # Localisation data + locales.each do |locale| + Mobility.with_locale(locale[0]) do + reputation_tier_level.name = tier.name[locale[1]] + end + end + + reputation_tier_level.save + end + + wow_reputation_tier.save + end +end diff --git a/app/workers/wow_reputation_tiers_worker.rb b/app/workers/wow_reputation_tiers_worker.rb new file mode 100644 index 0000000..7e1a99b --- /dev/null +++ b/app/workers/wow_reputation_tiers_worker.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class WowReputationTiersWorker < WowSidekiqWorker + def perform + RBattlenet.authenticate(client_id: ENV['BLIZZARD_API_CLIENT_ID'], client_secret: ENV['BLIZZARD_API_CLIENT_SECRET']) + RBattlenet.set_options(locale: 'en_US') + result = RBattlenet::Wow::ReputationTiers.all + + return unless result.status_code == 200 + + result.reputation_tiers.each do |reputation_tier| + wow_reputation_tier = WowReputationTier.find_or_initialize_by(reputation_tier_id: reputation_tier.id) + + # Global data + wow_reputation_tier.href = reputation_tier.key.href + + wow_reputation_tier.save + + WowReputationTierDetailWorker.perform_async(wow_reputation_tier.reputation_tier_id) if wow_reputation_tier.persisted? + end + end +end diff --git a/app/workers/wow_reputations_worker.rb b/app/workers/wow_reputations_worker.rb new file mode 100644 index 0000000..3e1aa7e --- /dev/null +++ b/app/workers/wow_reputations_worker.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +class WowReputationsWorker < WowSidekiqWorker + def perform + RBattlenet.authenticate(client_id: ENV['BLIZZARD_API_CLIENT_ID'], client_secret: ENV['BLIZZARD_API_CLIENT_SECRET']) + RBattlenet.set_options(locale: 'all') + result = RBattlenet::Wow::ReputationFaction.all + + return unless result.status_code == 200 + + result.factions.each do |faction| + wow_reputation = WowReputation.find_or_initialize_by(reputation_id: faction.id) + + # Global data + wow_reputation.href = faction.key.href + + # Localisation data + locales.each do |locale| + Mobility.with_locale(locale[0]) do + wow_reputation.name = faction.name[locale[1]] + end + end + + wow_reputation.save + + WowReputationDetailWorker.perform_async(wow_reputation.reputation_id) if wow_reputation.persisted? + end + end +end diff --git a/app/workers/wow_standing_worker.rb b/app/workers/wow_standing_worker.rb new file mode 100644 index 0000000..44cea35 --- /dev/null +++ b/app/workers/wow_standing_worker.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class WowStandingWorker < WowSidekiqWorker + def perform(wow_character_id) + return unless (wow_character = WowCharacter.find_by(character_id: wow_character_id)) + + RBattlenet.set_options(locale: 'en_US') + params = { realm: wow_character.wow_realm.slug, name: wow_character.name.downcase } + result = RBattlenet::Wow::Character::Reputations.find(params) + + return unless result.status_code == 200 + + result.reputations&.each do |reputation| + next unless (wow_reputation = WowReputation.find_by(reputation_id: reputation.faction.id)) + + wow_standing = WowStanding.where(wow_reputation: wow_reputation, wow_character: wow_character).first_or_initialize + + wow_standing.wow_reputation = wow_reputation + wow_standing.wow_character = wow_character + wow_standing.raw = reputation.standing.raw + wow_standing.value = reputation.standing.value + wow_standing.max = reputation.standing.max + wow_standing.tier = reputation.standing.tier + + wow_standing.save + end + end +end diff --git a/db/migrate/20210430163754_create_wow_reputation_tiers.rb b/db/migrate/20210430163754_create_wow_reputation_tiers.rb new file mode 100644 index 0000000..b2b8fa8 --- /dev/null +++ b/db/migrate/20210430163754_create_wow_reputation_tiers.rb @@ -0,0 +1,12 @@ +class CreateWowReputationTiers < ActiveRecord::Migration[6.1] + def change + create_table :wow_reputation_tiers do |t| + t.string :href + t.integer :reputation_tier_id, null: false + + t.timestamps + end + + add_index :wow_reputation_tiers, :reputation_tier_id, unique: true + end +end diff --git a/db/migrate/20210430164124_create_wow_reputation_tier_levels.rb b/db/migrate/20210430164124_create_wow_reputation_tier_levels.rb new file mode 100644 index 0000000..a9c2559 --- /dev/null +++ b/db/migrate/20210430164124_create_wow_reputation_tier_levels.rb @@ -0,0 +1,13 @@ +class CreateWowReputationTierLevels < ActiveRecord::Migration[6.1] + def change + create_table :wow_reputation_tier_levels do |t| + t.jsonb :name + t.integer :min_value + t.integer :max_value + t.integer :order + t.belongs_to :wow_reputation_tier + + t.timestamps + end + end +end diff --git a/db/migrate/20210430164825_create_wow_reputations.rb b/db/migrate/20210430164825_create_wow_reputations.rb new file mode 100644 index 0000000..7d5db4a --- /dev/null +++ b/db/migrate/20210430164825_create_wow_reputations.rb @@ -0,0 +1,17 @@ +class CreateWowReputations < ActiveRecord::Migration[6.1] + def change + create_table :wow_reputations do |t| + t.jsonb :name + t.string :href + t.string :faction + t.jsonb :translated_faction + t.jsonb :description + t.integer :reputation_id, null: false + t.belongs_to :wow_reputation_tier + + t.timestamps + end + + add_index :wow_reputations, :reputation_id, unique: true + end +end diff --git a/db/migrate/20210430165156_create_wow_standings.rb b/db/migrate/20210430165156_create_wow_standings.rb new file mode 100644 index 0000000..5d3c739 --- /dev/null +++ b/db/migrate/20210430165156_create_wow_standings.rb @@ -0,0 +1,15 @@ +class CreateWowStandings < ActiveRecord::Migration[6.1] + def change + create_table :wow_standings do |t| + t.belongs_to :wow_character + t.belongs_to :wow_reputation + t.integer :raw + t.integer :value + t.integer :max + t.integer :tier + t.jsonb :name + + t.timestamps + end + end +end diff --git a/db/migrate/20210430202802_add_index_to_wow_reputation_tier_order.rb b/db/migrate/20210430202802_add_index_to_wow_reputation_tier_order.rb new file mode 100644 index 0000000..a403ad0 --- /dev/null +++ b/db/migrate/20210430202802_add_index_to_wow_reputation_tier_order.rb @@ -0,0 +1,5 @@ +class AddIndexToWowReputationTierOrder < ActiveRecord::Migration[6.1] + def change + add_index :wow_reputation_tier_levels, :order + end +end diff --git a/db/migrate/20210430213202_add_wow_reputation_self_join.rb b/db/migrate/20210430213202_add_wow_reputation_self_join.rb new file mode 100644 index 0000000..d1995f4 --- /dev/null +++ b/db/migrate/20210430213202_add_wow_reputation_self_join.rb @@ -0,0 +1,5 @@ +class AddWowReputationSelfJoin < ActiveRecord::Migration[6.1] + def change + add_reference :wow_reputations, :meta_wow_reputation, foreign_key: { to_table: :wow_reputations } + end +end diff --git a/db/schema.rb b/db/schema.rb index 6815401..21112fb 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_29_161239) do +ActiveRecord::Schema.define(version: 2021_04_30_213202) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -263,6 +263,57 @@ ActiveRecord::Schema.define(version: 2021_04_29_161239) do t.index ["realm_id"], name: "index_wow_realms_on_realm_id", unique: true end + create_table "wow_reputation_tier_levels", force: :cascade do |t| + t.jsonb "name" + t.integer "min_value" + t.integer "max_value" + t.integer "order" + t.bigint "wow_reputation_tier_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["order"], name: "index_wow_reputation_tier_levels_on_order" + t.index ["wow_reputation_tier_id"], name: "index_wow_reputation_tier_levels_on_wow_reputation_tier_id" + end + + create_table "wow_reputation_tiers", force: :cascade do |t| + t.string "href" + t.integer "reputation_tier_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["reputation_tier_id"], name: "index_wow_reputation_tiers_on_reputation_tier_id", unique: true + end + + create_table "wow_reputations", force: :cascade do |t| + t.jsonb "name" + t.string "href" + t.string "faction" + t.jsonb "translated_faction" + t.jsonb "description" + t.integer "reputation_id", null: false + t.bigint "wow_reputation_tier_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.bigint "meta_wow_reputation_id" + t.index ["meta_wow_reputation_id"], name: "index_wow_reputations_on_meta_wow_reputation_id" + t.index ["reputation_id"], name: "index_wow_reputations_on_reputation_id", unique: true + t.index ["wow_reputation_tier_id"], name: "index_wow_reputations_on_wow_reputation_tier_id" + end + + create_table "wow_standings", force: :cascade do |t| + t.bigint "wow_character_id" + t.bigint "wow_reputation_id" + t.integer "raw" + t.integer "value" + t.integer "max" + t.integer "tier" + t.jsonb "name" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["wow_character_id"], name: "index_wow_standings_on_wow_character_id" + t.index ["wow_reputation_id"], name: "index_wow_standings_on_wow_reputation_id" + end + add_foreign_key "wow_characters", "wow_geo_positions", column: "bind_position_id" add_foreign_key "wow_characters", "wow_geo_positions", column: "last_position_id" + add_foreign_key "wow_reputations", "wow_reputations", column: "meta_wow_reputation_id" end diff --git a/spec/models/wow_reputation_spec.rb b/spec/models/wow_reputation_spec.rb new file mode 100644 index 0000000..fcb7f95 --- /dev/null +++ b/spec/models/wow_reputation_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe WowReputation, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/wow_reputation_tier_level_spec.rb b/spec/models/wow_reputation_tier_level_spec.rb new file mode 100644 index 0000000..5be04f3 --- /dev/null +++ b/spec/models/wow_reputation_tier_level_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe WowReputationTierLevel, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/wow_reputation_tier_spec.rb b/spec/models/wow_reputation_tier_spec.rb new file mode 100644 index 0000000..3a7b8b4 --- /dev/null +++ b/spec/models/wow_reputation_tier_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe WowReputationTier, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/wow_standing_spec.rb b/spec/models/wow_standing_spec.rb new file mode 100644 index 0000000..59dead0 --- /dev/null +++ b/spec/models/wow_standing_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe WowStanding, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end