continue to backport old code

This commit is contained in:
2021-04-24 00:24:05 +02:00
parent 5d7217f355
commit 04434760c5
72 changed files with 795 additions and 78 deletions

View File

@@ -0,0 +1,3 @@
// Place all the styles related to the wow_mounts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/

View File

@@ -0,0 +1,3 @@
// Place all the styles related to the wow_pets controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/

View File

@@ -2,7 +2,7 @@ class ApplicationController < ActionController::Base
around_action :switch_locale
def switch_locale(&action)
locale = I18n.default_locale
locale = I18n.locale_available?(request.headers['Locale']) ? request.headers['Locale'] : I18n.default_locale
I18n.with_locale(locale, &action)
end

View File

@@ -1,3 +1,3 @@
class ProtectedController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_user!
end

View File

@@ -11,6 +11,9 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
if @user.persisted?
sign_in_and_redirect @user, event: :authentication
WowCharactersWorker.perform_async(@user.id)
WowMountsCollectionWorker.perform_async(@user.id)
WowPetsCollectionWorker.perform_async(@user.id)
set_flash_message(:notice, :success, kind: 'Bnet') if is_navigational_format?
else
session['devise.bnet_data'] = request.env['omniauth.auth'].except(:extra)

View File

@@ -1,5 +1,9 @@
class WowCharactersController < ProtectedController
def index
@characters = current_user.wow_characters.all
@wow_characters = current_user.wow_characters.all
end
def show
@wow_character = current_user.wow_characters.find(params[:id])
end
end

View File

@@ -0,0 +1,9 @@
class WowMountsController < ProtectedController
def index
@wow_mounts = WowMount.all
end
def show
@wow_mount = WowMount.find(params[:id])
end
end

View File

@@ -0,0 +1,9 @@
class WowPetsController < ProtectedController
def index
@wow_pets = WowPet.all
end
def show
@wow_pet = WowPet.find(params[:id])
end
end

View File

@@ -0,0 +1,2 @@
module WowMountsHelper
end

View File

@@ -0,0 +1,2 @@
module WowPetsHelper
end

View File

@@ -0,0 +1,4 @@
class LearnedWowPetAbility < ApplicationRecord
belongs_to :wow_pet
belongs_to :wow_pet_ability
end

View File

@@ -8,6 +8,10 @@ class User < ApplicationRecord
devise :rememberable, :omniauthable, omniauth_providers: [:bnet]
has_many :wow_characters, dependent: :destroy
has_many :user_obtain_wow_mounts, dependent: :destroy
has_many :wow_mounts, through: :user_obtain_wow_mounts
has_many :user_obtain_wow_pets, dependent: :destroy
has_many :wow_pets, through: :user_obtain_wow_pets
validates :battletag, presence: true, uniqueness: true
@@ -28,8 +32,8 @@ class User < ApplicationRecord
def self.new_with_session(params, session)
super.tap do |user|
if (data = session['devise.bnet_data']) && session['devise.bnet_data']['extra']['raw_info']
user.email = data['email'] if user.email.blank?
if (data = session['devise.bnet_data']) && session['devise.bnet_data']['extra']['raw_info'] && user.email.blank?
user.email = data['email']
end
end
end

View File

@@ -0,0 +1,4 @@
class UserObtainWowMount < ApplicationRecord
belongs_to :user
belongs_to :wow_mount
end

View File

@@ -0,0 +1,4 @@
class UserObtainWowPet < ApplicationRecord
belongs_to :user
belongs_to :wow_pet
end

View File

@@ -11,4 +11,35 @@ class WowCharacter < ApplicationRecord
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

10
app/models/wow_mount.rb Normal file
View File

@@ -0,0 +1,10 @@
class WowMount < ApplicationRecord
extend Mobility
translates :name, :description, :translated_faction, :translated_source
has_many :user_obtain_mounts, dependent: :destroy
has_many :users, through: :user_obtain_mounts
validates :name, presence: true
validates :mount_id, presence: true, uniqueness: true
end

10
app/models/wow_pet.rb Normal file
View File

@@ -0,0 +1,10 @@
class WowPet < ApplicationRecord
extend Mobility
translates :name, :translated_battle_pet_type, :description, :translated_source_type
has_many :learned_wow_pet_abilities, dependent: :destroy
has_many :wow_pet_abilities, through: :learned_wow_pet_abilities
validates :name, presence: true
validates :pet_id, presence: true, uniqueness: true
end

View File

@@ -0,0 +1,10 @@
class WowPetAbility < ApplicationRecord
extend Mobility
translates :name, :translated_battle_pet_type
has_many :learned_pet_abilities, dependent: :destroy
has_many :wow_pets, through: :learned_pet_abilities
validates :name, presence: true
validates :ability_id, presence: true, uniqueness: true
end

View File

@@ -1,2 +1,5 @@
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<% if user_signed_in? %>
<h1>Welcome <%= current_user.battletag %></h1>
<% else %>
<h1>Welcome guest</h1>
<% end %>

View File

@@ -1,24 +1,33 @@
<nav class="navbar fixed-top navbar-dark navbar-expand-lg bg-dark">
<nav class="navbar navbar-dark navbar-expand-lg bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">Northwhale 2</a>
<a class="navbar-brand" href="<%= root_path %>">Northwhale 2</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="nav navbar-nav flex-row ml-md-auto d-none d-md-flex">
<li class="nav-item">
<% if user_signed_in? %>
<% if user_signed_in? %>
<ul class="navbar-nav mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="<%= wow_characters_path %>">Characters</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="<%= wow_mounts_path %>">Mounts</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="<%= wow_pets_path %>">Pets</a>
</li>
</ul>
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Menu
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Menu
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li>Bnet: <%= current_user.battletag %></li>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<li class="dropdown-item disabled" >Bnet:<%= current_user.battletag %></li>
<li><hr class="dropdown-divider"></li>
<li><%= link_to '<i class="fas fa-sign-out-alt"></i>'.html_safe, destroy_user_session_path, method: :delete %></li>
<li><%= link_to '<i class="fas fa-sign-out-alt"></i>'.html_safe, destroy_user_session_path, method: :delete, class: "dropdown-item" %></li>
</ul>
</li>
<% else %>
<%= link_to 'Sign in with <i class="fab fa-battle-net"></i>'.html_safe, user_bnet_omniauth_authorize_path, method: :post, class: "btn btn-primary" %>
<%= link_to 'Sign in with <i class="fab fa-battle-net"></i>'.html_safe, user_bnet_omniauth_authorize_path, method: :post, class: "btn btn-primary nav-item" %>
<% end %>
</li>
</ul>
</div>
</div>

View File

@@ -15,6 +15,8 @@
<%= render 'layouts/navbar' %>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<div class="container-fluid">
<%= yield %>
</div>
</body>
</html>

View File

@@ -4,26 +4,26 @@
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Name</th>
<th scope="col">Gender</th>
<th scope="col">Realm</th>
<th scope="col">Race</th>
<th scope="col">Class</th>
<th scope="col">Faction</th>
<th scope="col">Level</th>
<th scope="col"><%= t('character_list.name') %></th>
<th scope="col"><%= t('character_list.gender') %></th>
<th scope="col"><%= t('character_list.realm') %></th>
<th scope="col"><%= t('character_list.race') %></th>
<th scope="col"><%= t('character_list.class') %></th>
<th scope="col"><%= t('character_list.faction') %></th>
<th scope="col"><%= t('character_list.level') %></th>
</tr>
</thead>
<tbody>
<% @characters.each do |character| %>
<% @wow_characters.each do |character| %>
<tr>
<td><% if character.wow_character_medium %>
<img class="rounded-circle border border-white" src=<%= character.wow_character_medium.avatar %> alt="avatar">
<% end %></td>
<td><%= character.name %></td>
<td><%= link_to character.name, character %></td>
<td><%= character.translated_gender %></td>
<td><%= character.wow_realm.name %></td>
<td><%= %></td>
<td><%= %></td>
<td><%= character.gender_race_name %></td>
<td><%= character.gender_class_name %></td>
<td><%= character.translated_faction %></td>
<td><%= character.level %></td>
</tr>

View File

@@ -0,0 +1,19 @@
<div class="row">
<div class="col-5">
<% if @wow_character.wow_character_medium %>
<img src=<%= @wow_character.wow_character_medium.main_raw %> class="card-img-top" alt="character-main-image">
<% 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>
<p class="card-text"><%= @wow_character.last_login_timestamp %></p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>
<div class="col-7">
Post du perso
</div>
</div>

View File

@@ -0,0 +1,20 @@
<h2>Mount list</h2>
<table class="table table-hover table-dark table-image">
<thead>
<tr>
<th scope="col"><%= t('mount_list.name') %></th>
<th scope="col"><%= t('mount_list.description') %></th>
<th scope="col"><%= t('mount_list.owned') %></th>
</tr>
</thead>
<tbody>
<% @wow_mounts.each do |mount| %>
<tr>
<td><%= link_to mount.name, mount %></td>
<td><%= mount.description %></td>
<td></td>
</tr>
<% end %>
</tbody>
</table>

View File

@@ -0,0 +1,17 @@
<div class="row">
<div class="col-5">
<div class="card">
<% if @wow_mount.asset_zoom %>
<img src=<%= @wow_mount.asset_zoom %> class="card-img-top" alt="mount-zoom-image">
<% end %>
<div class="card-body">
<h5 class="card-title"><%= @wow_mount.name %></h5>
<p class="card-text">Description: <%= @wow_mount.description %></p>
<p class="card-text">Owned: </p>
</div>
</div>
</div>
<div class="col-7">
<h1>Suite de la page de la monture</h1>
</div>
</div>

View File

@@ -0,0 +1,20 @@
<h2>Pet list</h2>
<table class="table table-hover table-dark table-image">
<thead>
<tr>
<th scope="col"><%= t('pet_list.name') %></th>
<th scope="col"><%= t('pet_list.description') %></th>
<th scope="col"><%= t('pet_list.owned') %></th>
</tr>
</thead>
<tbody>
<% @wow_pets.each do |pet| %>
<tr>
<td><%= link_to pet.name, pet %></td>
<td><%= pet.description %></td>
<td></td>
</tr>
<% end %>
</tbody>
</table>

View File

@@ -0,0 +1,19 @@
<div class="row">
<div class="col-5">
<div class="card">
<% if @wow_pet.icon %>
<img src=<%= @wow_pet.icon %> class="card-img-top" alt="pet-icon-image">
<% end %>
<div class="card-body">
<h5 class="card-title"><%= @wow_pet.name %></h5>
<p class="card-text">Description: <%= @wow_pet.description %></p>
<% @wow_pet.wow_pet_abilities.each do |ability| %>
<p>Ability: <%= ability.name %></p>
<% end %>
</div>
</div>
</div>
<div class="col-7">
<h1>Suite de la page du pet</h1>
</div>
</div>

View File

@@ -0,0 +1,33 @@
class WowMountDetailWorker < WowSidekiqWorker
def locales
super
end
def perform(mount_id)
return unless (mount = WowMount.where(mount_id: mount_id).first)
RBattlenet.set_options(locale: 'all')
result = RBattlenet::Wow::Mount.find(mount_id)
return unless result.status_code == 200
mount.faction = result.faction.type if result.faction
# Localisation data
locales.each do |locale|
Mobility.with_locale(locale[0]) do
mount.translated_faction = result.faction.name[locale[1]] if result.faction
mount.description = result.description[locale[1]]
end
end
if result.creature_displays.first
mount.creature_display_id = result.creature_displays.first.id
media = RBattlenet::Wow::CreatureMedia.find(result.creature_displays.first.id)
mount.asset_zoom = media.assets.find { |asset| asset['key'] == 'zoom' }.value || nil
end
mount.save
end
end

View File

@@ -0,0 +1,19 @@
class WowMountsCollectionWorker
include Sidekiq::Worker
def perform(user_id)
# Update the WoW character list
return unless (user = User.find(user_id))
RBattlenet.set_options(locale: 'en_US')
result = RBattlenet::Wow::Profile::MountsCollection.find(user.token)
return unless result.status_code == 200
result.mounts.each do |mount|
next unless (local_mount = WowMount.where(mount_id: mount.mount.id).first)
UserObtainWowMount.where(user: user.id, wow_mount: local_mount.id).first_or_create
end
end
end

View File

@@ -0,0 +1,30 @@
class WowMountsWorker < WowSidekiqWorker
def locales
super
end
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::Mount.all
return unless result.status_code == 200
result.mounts.each do |mount|
wow_mount = WowMount.where(mount_id: mount.id).first_or_initialize
# Global data
wow_mount.mount_id = mount.id
wow_mount.href = mount.key.href
# Localisation data
locales.each do |locale|
Mobility.with_locale(locale[0]) { wow_mount.name = mount.name[locale[1]] }
end
wow_mount.save
WowMountDetailWorker.perform_async(wow_mount.mount_id) if wow_mount.persisted?
end
end
end

View File

@@ -0,0 +1,30 @@
class WowPetAbilitiesWorker < WowSidekiqWorker
def locales
super
end
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::PetAbility.all
return unless result.status_code == 200
result.abilities.each do |ability|
wow_pet_ability = WowPetAbility.where(ability_id: ability.id).first_or_initialize
# Global data
wow_pet_ability.ability_id = ability.id
wow_pet_ability.href = ability.key.href
# Localisation data
locales.each do |locale|
Mobility.with_locale(locale[0]) { wow_pet_ability.name = ability.name[locale[1]] }
end
wow_pet_ability.save
WowPetAbilityDetailWorker.perform_async(wow_pet_ability.ability_id) if wow_pet_ability.persisted?
end
end
end

View File

@@ -0,0 +1,33 @@
class WowPetAbilityDetailWorker < WowSidekiqWorker
def locales
super
end
def perform(ability_id)
return unless (ability = WowPetAbility.where(ability_id: ability_id).first)
RBattlenet.set_options(locale: 'all')
result = RBattlenet::Wow::PetAbility.find(ability_id)
return unless result.status_code == 200
ability.battle_pet_type = result.battle_pet_type.type
ability.battle_pet_type_id = result.battle_pet_type.id
ability.rounds = result.rounds if result.rounds
# Localisation data
locales.each do |locale|
Mobility.with_locale(locale[0]) do
ability.translated_battle_pet_type = result.battle_pet_type.name[locale[1]]
end
end
if result.media
media = RBattlenet::Wow::PetAbilityMedia.find(result.media.id)
ability.media = media.assets.find { |asset| asset['key'] == 'icon' }.value || nil
end
ability.save
end
end

View File

@@ -0,0 +1,44 @@
class WowPetDetailWorker < WowSidekiqWorker
def locales
super
end
def perform(pet_id)
return unless (pet = WowPet.where(pet_id: pet_id).first)
RBattlenet.set_options(locale: 'all')
result = RBattlenet::Wow::Pet.find(pet_id)
return unless result.status_code == 200
pet.is_capturable = result.is_capturable
pet.is_battlepet = result.is_battlepet
pet.is_alliance_only = result.is_alliance_only
pet.is_horde_only = result.is_horde_only
pet.is_capturable = result.is_capturable
pet.is_random_creature_display = result.is_random_creature_display
pet.icon = result.icon
pet.creature_id = result.creature.id
pet.battle_pet_type = result.battle_pet_type.type
pet.battle_pet_type_id = result.battle_pet_type.id
# Localisation data
locales.each do |locale|
Mobility.with_locale(locale[0]) do
pet.translated_battle_pet_type = result.battle_pet_type.name[locale[1]]
pet.description = result.description[locale[1]]
end
end
if result.abilities
result.abilities.each do |ability|
next unless (local_ability = WowPetAbility.where(ability_id: ability.ability.id).first)
LearnedWowPetAbility.where(wow_pet: pet.id, wow_pet_ability: local_ability.id).first_or_create
end
end
pet.save
end
end

View File

@@ -0,0 +1,18 @@
class WowPetsCollectionWorker
include Sidekiq::Worker
def perform(user_id)
return unless (user = User.find(user_id))
RBattlenet.set_options(locale: 'en_US')
result = RBattlenet::Wow::Profile::PetsCollection.find(user.token)
return unless result.status_code == 200
result.pets.each do |pet|
next unless (local_pet = WowPet.where(pet_id: pet.species.id).first)
UserObtainWowPet.where(user: user.id, wow_pet: local_pet.id).first_or_create
end
end
end

View File

@@ -0,0 +1,30 @@
class WowPetsWorker < WowSidekiqWorker
def locales
super
end
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::Pet.all
return unless result.status_code == 200
result.pets.each do |pet|
wow_pet = WowPet.where(pet_id: pet.id).first_or_initialize
# Global data
wow_pet.pet_id = pet.id
wow_pet.href = pet.key.href
# Localisation data
locales.each do |locale|
Mobility.with_locale(locale[0]) { wow_pet.name = pet.name[locale[1]] }
end
wow_pet.save
WowPetDetailWorker.perform_async(wow_pet.pet_id) if wow_pet.persisted?
end
end
end

View File

@@ -0,0 +1,20 @@
class WowSidekiqWorker
include Sidekiq::Worker
def locales
[
['en-us', 'en_US'],
['es-mx', 'es_MX'],
['pt-br', 'pt_BR'],
['de-de', 'de_DE'],
['en-gb', 'en_GB'],
['es-es', 'es_ES'],
['fr-fr', 'fr_FR'],
['it', 'it_IT'],
['ru-ru', 'ru_RU'],
['ko', 'ko_KR'],
['zh-tw', 'zh_TW'],
['zh-cn', 'zh_CN']
]
end
end