From 5b3da087070383ca1d808df653ad92a4c554b926 Mon Sep 17 00:00:00 2001 From: Etienne Ischer Date: Sun, 30 May 2021 17:49:54 +0200 Subject: [PATCH] add covenant --- Gemfile | 2 +- Gemfile.lock | 6 ++--- app/controllers/wow_characters_controller.rb | 2 +- app/models/wow_character.rb | 6 +++-- app/models/wow_covenant.rb | 10 ++++++++ app/models/wow_covenant_progress.rb | 4 ++++ app/views/wow_characters/index.html.erb | 2 ++ app/workers/wow_character_detail_worker.rb | 11 +++++++++ app/workers/wow_characters_worker.rb | 2 +- app/workers/wow_covenant_detail_worker.rb | 23 ++++++++++++++++++ app/workers/wow_covenants_worker.rb | 24 +++++++++++++++++++ config/locales/wow_characters/de-de.yml | 1 + config/locales/wow_characters/en-gb.yml | 1 + config/locales/wow_characters/en-us.yml | 1 + config/locales/wow_characters/es-es.yml | 1 + config/locales/wow_characters/es-mx.yml | 1 + config/locales/wow_characters/fr-fr.yml | 1 + config/locales/wow_characters/it.yml | 1 + config/locales/wow_characters/ko.yml | 1 + config/locales/wow_characters/pt-br.yml | 1 + config/locales/wow_characters/ru-ru.yml | 1 + config/locales/wow_characters/zh-cn.yml | 1 + config/locales/wow_characters/zh-tw.yml | 1 + .../20210530135118_create_wow_covenants.rb | 14 +++++++++++ ...30135651_create_wow_covenant_progresses.rb | 11 +++++++++ ...venant_progress_belong_to_wow_character.rb | 5 ++++ db/schema.rb | 22 ++++++++++++++++- spec/models/wow_covenant_progress_spec.rb | 5 ++++ spec/models/wow_covenant_spec.rb | 5 ++++ 29 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 app/models/wow_covenant.rb create mode 100644 app/models/wow_covenant_progress.rb create mode 100644 app/workers/wow_covenant_detail_worker.rb create mode 100644 app/workers/wow_covenants_worker.rb create mode 100644 db/migrate/20210530135118_create_wow_covenants.rb create mode 100644 db/migrate/20210530135651_create_wow_covenant_progresses.rb create mode 100644 db/migrate/20210530151735_rename_covenant_progress_belong_to_wow_character.rb create mode 100644 spec/models/wow_covenant_progress_spec.rb create mode 100644 spec/models/wow_covenant_spec.rb diff --git a/Gemfile b/Gemfile index 91bce0e..57714c1 100644 --- a/Gemfile +++ b/Gemfile @@ -35,7 +35,7 @@ gem 'omniauth-bnet', '~> 2.0.0' # This gem provides a mitigation against CVE-2015-9284 gem 'omniauth-rails_csrf_protection', '~> 0.1.2' # A Ruby wrapper around Blizzard's Game Data and Profile APIs -gem 'rbattlenet', '~> 2.2.6', git: 'https://github.com/Dainii/rbattlenet' +gem 'rbattlenet', '~> 2.2.7', git: 'https://github.com/Dainii/rbattlenet' # A gem that provides Rails integration for the Sentry error logger gem 'sentry-rails', '~> 4.4.0' gem 'sentry-ruby', '~> 4.4.1' diff --git a/Gemfile.lock b/Gemfile.lock index d6e248c..d5d8d0e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,8 +1,8 @@ GIT remote: https://github.com/Dainii/rbattlenet - revision: 52c159e1418350247c2f928f08930e0881bc6827 + revision: 6fc990a743b1cec56e95820aec9222b5ff2e843a specs: - rbattlenet (2.2.6) + rbattlenet (2.2.7) require_all typhoeus (~> 1.1) @@ -411,7 +411,7 @@ DEPENDENCIES rails (~> 6.1.3, >= 6.1.3.1) rails-erd rails-i18n (~> 6.0.0) - rbattlenet (~> 2.2.6)! + rbattlenet (~> 2.2.7)! redis (~> 4.2.5) rspec-rails rubocop diff --git a/app/controllers/wow_characters_controller.rb b/app/controllers/wow_characters_controller.rb index 8f62fc6..3701e63 100644 --- a/app/controllers/wow_characters_controller.rb +++ b/app/controllers/wow_characters_controller.rb @@ -2,7 +2,7 @@ class WowCharactersController < ProtectedController def index - @wow_characters = current_user.wow_characters.includes(:wow_realm, :wow_race, :wow_class, :wow_character_medium, :wow_guild) + @wow_characters = current_user.wow_characters.includes(:wow_realm, :wow_race, :wow_class, :wow_character_medium, :wow_guild, :wow_covenant) end def show diff --git a/app/models/wow_character.rb b/app/models/wow_character.rb index 6ea9b79..fce4562 100644 --- a/app/models/wow_character.rb +++ b/app/models/wow_character.rb @@ -10,10 +10,12 @@ class WowCharacter < ApplicationRecord belongs_to :wow_race belongs_to :wow_character_title, optional: true belongs_to :wow_guild, optional: true + has_one :wow_covenant_progress, dependent: :destroy + has_one :wow_covenant, through: :wow_covenant_progress 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_one :wow_character_medium, dependent: :destroy + has_many :wow_standings, dependent: :destroy has_many :wow_reputations, through: :wow_standings validates :name, presence: true diff --git a/app/models/wow_covenant.rb b/app/models/wow_covenant.rb new file mode 100644 index 0000000..2c4efb4 --- /dev/null +++ b/app/models/wow_covenant.rb @@ -0,0 +1,10 @@ +class WowCovenant < ApplicationRecord + extend Mobility + translates :name, :description + + has_many :wow_covenant_progresses, dependent: :nullify + has_many :wow_characters, through: :wow_covenant_progresses + + validates :name, presence: true + validates :covenant_id, presence: true, uniqueness: true +end diff --git a/app/models/wow_covenant_progress.rb b/app/models/wow_covenant_progress.rb new file mode 100644 index 0000000..9d6a649 --- /dev/null +++ b/app/models/wow_covenant_progress.rb @@ -0,0 +1,4 @@ +class WowCovenantProgress < ApplicationRecord + belongs_to :wow_character + belongs_to :wow_covenant +end diff --git a/app/views/wow_characters/index.html.erb b/app/views/wow_characters/index.html.erb index 4132edc..cec0307 100644 --- a/app/views/wow_characters/index.html.erb +++ b/app/views/wow_characters/index.html.erb @@ -10,6 +10,7 @@ <%= t('wow_characters.character_list.race') %> <%= t('wow_characters.character_list.class') %> <%= t('wow_characters.character_list.faction') %> + <%= t('wow_characters.character_list.covenant') %> <%= t('wow_characters.character_list.level') %> @@ -25,6 +26,7 @@ <%= gender_race_name(character.gender, character.wow_race) %> <%= gender_class_name(character.gender, character.wow_class) %> <%= character.translated_faction %> + <%= character.wow_covenant&.name %> <%= character.level %> <% end %> diff --git a/app/workers/wow_character_detail_worker.rb b/app/workers/wow_character_detail_worker.rb index eb53214..5b830ec 100644 --- a/app/workers/wow_character_detail_worker.rb +++ b/app/workers/wow_character_detail_worker.rb @@ -24,6 +24,8 @@ class WowCharacterDetailWorker < WowSidekiqWorker 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) @@ -36,4 +38,13 @@ class WowCharacterDetailWorker < WowSidekiqWorker 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 diff --git a/app/workers/wow_characters_worker.rb b/app/workers/wow_characters_worker.rb index cc7f886..78664a2 100644 --- a/app/workers/wow_characters_worker.rb +++ b/app/workers/wow_characters_worker.rb @@ -23,7 +23,7 @@ class WowCharactersWorker < WowSidekiqWorker wow_char.wow_class = WowClass.where(class_id: character.playable_class.id).first wow_char.wow_race = WowRace.where(race_id: character.playable_race.id).first wow_char.user = user - wow_char.account_id = account.map_id + wow_char.account_id = account.id locales.each do |locale| Mobility.with_locale(locale[0]) do diff --git a/app/workers/wow_covenant_detail_worker.rb b/app/workers/wow_covenant_detail_worker.rb new file mode 100644 index 0000000..ba38ea1 --- /dev/null +++ b/app/workers/wow_covenant_detail_worker.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class WowCovenantDetailWorker < WowSidekiqWorker + def perform(covenant_id) + return unless (wow_covenant = WowCovenant.find_by(covenant_id: covenant_id)) + + RBattlenet.set_options(locale: 'all') + result = RBattlenet::Wow::Covenant.find(covenant_id) + + return unless result.status_code == 200 + + wow_covenant.media_id = result.media.id + + # Localisation data + locales.each do |locale| + Mobility.with_locale(locale[0]) do + wow_covenant.description = result.description[locale[1]] + end + end + + wow_covenant.save + end +end diff --git a/app/workers/wow_covenants_worker.rb b/app/workers/wow_covenants_worker.rb new file mode 100644 index 0000000..de580d1 --- /dev/null +++ b/app/workers/wow_covenants_worker.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class WowCovenantsWorker < 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::Covenant.all + + return unless result.status_code == 200 + + result.covenants.each do |covenant| + wow_covenant = WowCovenant.find_or_initialize_by(covenant_id: covenant.id) + + # Localisation data + locales.each do |locale| + Mobility.with_locale(locale[0]) { wow_covenant.name = covenant.name[locale[1]] } + end + + wow_covenant.save + + WowCovenantDetailWorker.perform_async(wow_covenant.covenant_id) if wow_covenant.persisted? + end + end +end diff --git a/config/locales/wow_characters/de-de.yml b/config/locales/wow_characters/de-de.yml index 1ef8cfb..586313d 100644 --- a/config/locales/wow_characters/de-de.yml +++ b/config/locales/wow_characters/de-de.yml @@ -9,3 +9,4 @@ de-de: faction: "Fraktion" level: "Niveau" guild: "Gilde" + covenant: "Pakte" diff --git a/config/locales/wow_characters/en-gb.yml b/config/locales/wow_characters/en-gb.yml index eb6cd00..684b78b 100644 --- a/config/locales/wow_characters/en-gb.yml +++ b/config/locales/wow_characters/en-gb.yml @@ -9,3 +9,4 @@ en-gb: faction: "Faction" level: "Level" guild: "Guild" + covenant: "Covenant" diff --git a/config/locales/wow_characters/en-us.yml b/config/locales/wow_characters/en-us.yml index f74b54d..68e5eda 100644 --- a/config/locales/wow_characters/en-us.yml +++ b/config/locales/wow_characters/en-us.yml @@ -9,3 +9,4 @@ en-us: faction: "Faction" level: "Level" guild: "Guild" + covenant: "Covenant" diff --git a/config/locales/wow_characters/es-es.yml b/config/locales/wow_characters/es-es.yml index 284ffda..4afde89 100644 --- a/config/locales/wow_characters/es-es.yml +++ b/config/locales/wow_characters/es-es.yml @@ -9,3 +9,4 @@ es-es: faction: "Facción" level: "Nivel" guild: "Gremio" + covenant: "Curia" diff --git a/config/locales/wow_characters/es-mx.yml b/config/locales/wow_characters/es-mx.yml index 69bbbcf..ab6ed2d 100644 --- a/config/locales/wow_characters/es-mx.yml +++ b/config/locales/wow_characters/es-mx.yml @@ -9,3 +9,4 @@ es-mx: faction: "Facción" level: "Nivel" guild: "Gremio" + covenant: "Pacto" diff --git a/config/locales/wow_characters/fr-fr.yml b/config/locales/wow_characters/fr-fr.yml index 1ba8105..7e89ae6 100644 --- a/config/locales/wow_characters/fr-fr.yml +++ b/config/locales/wow_characters/fr-fr.yml @@ -9,3 +9,4 @@ fr-fr: faction: "Faction" level: "Niveau" guild: "Guilde" + covenant: "Congrégation" diff --git a/config/locales/wow_characters/it.yml b/config/locales/wow_characters/it.yml index 2f64726..7a5e5f6 100644 --- a/config/locales/wow_characters/it.yml +++ b/config/locales/wow_characters/it.yml @@ -9,3 +9,4 @@ it: faction: "Fazione" level: "Livello" guild: "Gilda" + covenant: "Congregazione" diff --git a/config/locales/wow_characters/ko.yml b/config/locales/wow_characters/ko.yml index 9dca245..dbdd3be 100644 --- a/config/locales/wow_characters/ko.yml +++ b/config/locales/wow_characters/ko.yml @@ -9,3 +9,4 @@ ko: faction: "파벌" level: "수평" guild: "동업 조합" + covenant: "계약" diff --git a/config/locales/wow_characters/pt-br.yml b/config/locales/wow_characters/pt-br.yml index 513c112..11041bd 100644 --- a/config/locales/wow_characters/pt-br.yml +++ b/config/locales/wow_characters/pt-br.yml @@ -9,3 +9,4 @@ pt-br: faction: "Facção" level: "Nível" guild: "Guilda" + covenant: "Pacto" diff --git a/config/locales/wow_characters/ru-ru.yml b/config/locales/wow_characters/ru-ru.yml index 1250a84..d8aec42 100644 --- a/config/locales/wow_characters/ru-ru.yml +++ b/config/locales/wow_characters/ru-ru.yml @@ -9,3 +9,4 @@ ru-ru: faction: "Фракция" level: "Уровень" guild: "Гильдия" + covenant: "Ковенанты" diff --git a/config/locales/wow_characters/zh-cn.yml b/config/locales/wow_characters/zh-cn.yml index 95b0244..18f2ad6 100644 --- a/config/locales/wow_characters/zh-cn.yml +++ b/config/locales/wow_characters/zh-cn.yml @@ -9,3 +9,4 @@ zh-cn: faction: "派" level: "水平" guild: "公会" + covenant: "盟约" diff --git a/config/locales/wow_characters/zh-tw.yml b/config/locales/wow_characters/zh-tw.yml index 7a45127..e7e618a 100644 --- a/config/locales/wow_characters/zh-tw.yml +++ b/config/locales/wow_characters/zh-tw.yml @@ -9,3 +9,4 @@ zh-tw: faction: "派" level: "水平" guild: "公会" + covenant: "盟约" diff --git a/db/migrate/20210530135118_create_wow_covenants.rb b/db/migrate/20210530135118_create_wow_covenants.rb new file mode 100644 index 0000000..94f17a7 --- /dev/null +++ b/db/migrate/20210530135118_create_wow_covenants.rb @@ -0,0 +1,14 @@ +class CreateWowCovenants < ActiveRecord::Migration[6.1] + def change + create_table :wow_covenants do |t| + t.integer :covenant_id + t.jsonb :name + t.jsonb :description + t.integer :media_id + + t.timestamps + end + + add_index :wow_covenants, :covenant_id, unique: true + end +end diff --git a/db/migrate/20210530135651_create_wow_covenant_progresses.rb b/db/migrate/20210530135651_create_wow_covenant_progresses.rb new file mode 100644 index 0000000..0bc9405 --- /dev/null +++ b/db/migrate/20210530135651_create_wow_covenant_progresses.rb @@ -0,0 +1,11 @@ +class CreateWowCovenantProgresses < ActiveRecord::Migration[6.1] + def change + create_table :wow_covenant_progresses do |t| + t.integer :renown_level + t.belongs_to :wow_characters + t.belongs_to :wow_covenant + + t.timestamps + end + end +end diff --git a/db/migrate/20210530151735_rename_covenant_progress_belong_to_wow_character.rb b/db/migrate/20210530151735_rename_covenant_progress_belong_to_wow_character.rb new file mode 100644 index 0000000..79e82a0 --- /dev/null +++ b/db/migrate/20210530151735_rename_covenant_progress_belong_to_wow_character.rb @@ -0,0 +1,5 @@ +class RenameCovenantProgressBelongToWowCharacter < ActiveRecord::Migration[6.1] + def change + rename_column :wow_covenant_progresses, :wow_characters_id, :wow_character_id + end +end diff --git a/db/schema.rb b/db/schema.rb index ff5705d..d605549 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_05_30_121125) do +ActiveRecord::Schema.define(version: 2021_05_30_151735) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -151,6 +151,26 @@ ActiveRecord::Schema.define(version: 2021_05_30_121125) do t.index ["class_id"], name: "index_wow_classes_on_class_id", unique: true end + create_table "wow_covenant_progresses", force: :cascade do |t| + t.integer "renown_level" + t.bigint "wow_character_id" + t.bigint "wow_covenant_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["wow_character_id"], name: "index_wow_covenant_progresses_on_wow_character_id" + t.index ["wow_covenant_id"], name: "index_wow_covenant_progresses_on_wow_covenant_id" + end + + create_table "wow_covenants", force: :cascade do |t| + t.integer "covenant_id" + t.jsonb "name" + t.jsonb "description" + t.integer "media_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["covenant_id"], name: "index_wow_covenants_on_covenant_id", unique: true + end + create_table "wow_geo_maps", force: :cascade do |t| t.integer "map_id" t.jsonb "name" diff --git a/spec/models/wow_covenant_progress_spec.rb b/spec/models/wow_covenant_progress_spec.rb new file mode 100644 index 0000000..0d3a880 --- /dev/null +++ b/spec/models/wow_covenant_progress_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe WowCovenantProgress, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/wow_covenant_spec.rb b/spec/models/wow_covenant_spec.rb new file mode 100644 index 0000000..06d484b --- /dev/null +++ b/spec/models/wow_covenant_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe WowCovenant, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end