add everything to manage reputations

This commit is contained in:
2021-05-01 00:22:15 +02:00
parent ca80fd55fa
commit d227cdc87c
30 changed files with 366 additions and 43 deletions

View File

@@ -1,4 +0,0 @@
# frozen_string_literal: true
module CharactersHelper
end

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,7 @@
class WowStanding < ApplicationRecord
extend Mobility
translates :name
belongs_to :wow_character
belongs_to :wow_reputation
end

View File

@@ -22,8 +22,8 @@
<td><%= link_to character.name, character %></td>
<td><%= character.translated_gender %></td>
<td><%= character.wow_realm.name %></td>
<td><%= character.gender_race_name %></td>
<td><%= character.gender_class_name %></td>
<td><%= gender_race_name(character.gender, character.wow_race) %></td>
<td><%= gender_class_name(character.gender, character.wow_class) %></td>
<td><%= character.translated_faction %></td>
<td><%= character.level %></td>
</tr>

View File

@@ -5,9 +5,9 @@
<% end %>
<div class="card">
<div class="card-body">
<h5 class="card-title"><%= @wow_character.title_name %></h5>
<p class="card-text"><%= @wow_character.gender_race_name %></p>
<p class="card-text"><%= @wow_character.gender_class_name %></p>
<h5 class="card-title"><%= wow_character_title_name(@wow_character) %></h5>
<p class="card-text"><%= gender_race_name(@wow_character.gender, @wow_character.wow_race) %></p>
<p class="card-text"><%= gender_class_name(@wow_character.gender, @wow_character.wow_class) %></p>
<p class="card-text">Last connection: <%= @wow_character.last_login_timestamp %></p>
<p class="card-text">Last position: Map: <%= @wow_character.last_position.wow_geo_map.name %> Zone: <%= @wow_character.last_position.wow_geo_zone.name %></p>
<p class="card-text">Bind position: Map: <%= @wow_character.bind_position.wow_geo_map.name %> Zone: <%= @wow_character.bind_position.wow_geo_zone.name %></p>

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,5 @@
class AddIndexToWowReputationTierOrder < ActiveRecord::Migration[6.1]
def change
add_index :wow_reputation_tier_levels, :order
end
end

View File

@@ -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

53
db/schema.rb generated
View File

@@ -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

View File

@@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe WowReputation, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe WowReputationTierLevel, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe WowReputationTier, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe WowStanding, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end