From 7e4b9ea0c23eb660ab36a6114a7a3046d8f1c5f6 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 13 Jan 2018 16:01:51 -0500 Subject: Moved species from Pokémon to revision MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The migration will set all of the revisions of each Pokémon to have the species that that Pokémon was set to. If reversed, the migration sets the Pokémon's species to the first revision's species, which mimics the behavior of the engine from before this change, but do note that running the migration backwards like this can lose data. This change slightly affects the loading time of the front page. See #2. refs #3 --- app/controllers/pokeviewer/pokemon_controller.rb | 8 +-- app/jobs/pokeviewer/extract_save_data_job.rb | 4 +- app/models/pokeviewer/pokemon.rb | 66 +-------------------- app/models/pokeviewer/revision.rb | 68 +++++++++++++++++++++- app/models/pokeviewer/species.rb | 2 +- app/models/pokeviewer/trainer.rb | 4 +- app/views/pokeviewer/pokemon/index.html.haml | 12 ++-- app/views/pokeviewer/pokemon/show.html.haml | 54 ++++++++--------- .../20180113200119_move_species_to_revision.rb | 39 +++++++++++++ test/dummy/db/schema.rb | 6 +- 10 files changed, 152 insertions(+), 111 deletions(-) create mode 100644 db/migrate/20180113200119_move_species_to_revision.rb diff --git a/app/controllers/pokeviewer/pokemon_controller.rb b/app/controllers/pokeviewer/pokemon_controller.rb index 49a743c..7a1b9f9 100644 --- a/app/controllers/pokeviewer/pokemon_controller.rb +++ b/app/controllers/pokeviewer/pokemon_controller.rb @@ -10,9 +10,7 @@ module Pokeviewer order(slot: :asc). order("pokeviewer_revisions.sequential_id DESC"). group("pokeviewer_pokemon.uuid"). - select(:box, :slot, :uuid, :trainer_id, :species_id). - select(:ot_gender, :ot_name, :unown_letter). - select("pokeviewer_revisions.nickname AS nickname"). + includes(:revisions). chunk do |p| if p.trainer_id.nil? -1 @@ -71,8 +69,8 @@ module Pokeviewer def show @pokemon = Pokemon.includes( - :trainer, :species, :location, - revisions: [:item, :move_1, :move_2, :move_3, :move_4] + :trainer, :location, + revisions: [:species, :item, :move_1, :move_2, :move_3, :move_4] ).find_by_uuid! params[:id] end end diff --git a/app/jobs/pokeviewer/extract_save_data_job.rb b/app/jobs/pokeviewer/extract_save_data_job.rb index e9e0231..d1968af 100644 --- a/app/jobs/pokeviewer/extract_save_data_job.rb +++ b/app/jobs/pokeviewer/extract_save_data_job.rb @@ -71,7 +71,6 @@ module Pokeviewer args["pokemon"].each do |param| pk = Pokemon.find_or_create_by!(key: param["key"]) do |r| - r.species_id = param["species"] r.ot_name = param["otName"] r.ot_number = param["otId"] r.ot_gender = param["otGender"] @@ -99,7 +98,7 @@ module Pokeviewer r.pokeball = Pokemon.pokeball.values[param["pokeball"] - 1] # Handle Unown form - if r.species_id == 201 + if param["species"] == 201 r.unown_letter = Pokemon.unown_letter.values[param["unownLetter"]] end end @@ -117,6 +116,7 @@ module Pokeviewer pk.save! rev = Revision.new(pokemon: pk) + rev.species_id = param["species"] rev.nickname = param["nickname"] rev.experience = param["experience"] rev.level = param["level"] diff --git a/app/models/pokeviewer/pokemon.rb b/app/models/pokeviewer/pokemon.rb index 36ed8a8..db3da5a 100644 --- a/app/models/pokeviewer/pokemon.rb +++ b/app/models/pokeviewer/pokemon.rb @@ -3,8 +3,6 @@ module Pokeviewer extend Enumerize extend ActiveModel::Naming - belongs_to :species - has_many :revisions, -> { order "sequential_id ASC" }, dependent: :destroy belongs_to :trainer, optional: true @@ -78,60 +76,8 @@ module Pokeviewer uuid end - def icon_path - form = "" - if species_id == 201 - # Handle Unown form - form = "-#{unown_letter}" - elsif species_id == 386 - # Handle Deoxys forms - if trainer.firered? - form = "-attack" - elsif trainer.leafgreen? - form = "-defense" - elsif trainer.emerald? - form = "-speed" - end - end - - "pokeviewer/icons/#{species_id}#{form}.png" - end - - def sprite_path - shininess = "normal" - if shiny - shininess = "shiny" - end - - game = "ruby-sapphire" - unless trainer.nil? - if (trainer.firered? or trainer.leafgreen?) and (species_id <= 156 or species_id == 216 or species_id == 386) - game = "firered-leafgreen" - elsif trainer.emerald? - game = "emerald" - end - end - - form = "" - if species_id == 201 - # Handle Unown forms - form = "-#{unown_letter}" - elsif species_id == 386 - # Handle Deoxys forms - if trainer.firered? - form = "-attack" - elsif trainer.leafgreen? - form = "-defense" - elsif trainer.emerald? - form = "-speed" - end - end - - if game == "emerald" - "pokeviewer/sprites/emerald/#{shininess}/#{species_id}#{form}.gif" - else - "pokeviewer/sprites/#{game}/#{shininess}/#{species_id}#{form}.png" - end + def current + revisions.last end def outsider? @@ -178,14 +124,6 @@ module Pokeviewer "pokeviewer/items/#{Pokemon.pokeball.values.find_index(pokeball) + 1}.png" end - def ability - if second_ability - species.ability_2 - else - species.ability_1 - end - end - def gift_ribbon_description(ribbon) if trainer.nil? "" diff --git a/app/models/pokeviewer/revision.rb b/app/models/pokeviewer/revision.rb index e33ac4f..64b0e99 100644 --- a/app/models/pokeviewer/revision.rb +++ b/app/models/pokeviewer/revision.rb @@ -4,7 +4,7 @@ module Pokeviewer class Revision < ApplicationRecord include ActiveRecord::Diff - diff :nickname, :level, :hp, :attack, :defense, + diff :species_id, :nickname, :level, :hp, :attack, :defense, :special_attack, :special_defense, :speed, :coolness, :beauty, :cuteness, :smartness, :toughness, :sheen, :item_id, :move_1_id, :move_2_id, :move_3_id, :move_4_id, :move_1_pp_bonuses, :move_2_pp_bonuses, @@ -17,6 +17,8 @@ module Pokeviewer belongs_to :pokemon acts_as_sequenced scope: :pokemon_id + belongs_to :species + validates :nickname, presence: true validates :experience, presence: true, @@ -140,6 +142,70 @@ module Pokeviewer less_than_or_equal_to: 4, only_integer: true} + def icon_path + form = "" + if species_id == 201 + # Handle Unown form + form = "-#{pokemon.unown_letter}" + elsif species_id == 386 + # Handle Deoxys forms + if pokemon.trainer.firered? + form = "-attack" + elsif pokemon.trainer.leafgreen? + form = "-defense" + elsif pokemon.trainer.emerald? + form = "-speed" + end + end + + "pokeviewer/icons/#{species_id}#{form}.png" + end + + def sprite_path + shininess = "normal" + if pokemon.shiny + shininess = "shiny" + end + + game = "ruby-sapphire" + unless pokemon.trainer.nil? + if (pokemon.trainer.firered? or pokemon.trainer.leafgreen?) and (species_id <= 156 or species_id == 216 or species_id == 386) + game = "firered-leafgreen" + elsif pokemon.trainer.emerald? + game = "emerald" + end + end + + form = "" + if species_id == 201 + # Handle Unown forms + form = "-#{pokemon.unown_letter}" + elsif species_id == 386 + # Handle Deoxys forms + if pokemon.trainer.firered? + form = "-attack" + elsif pokemon.trainer.leafgreen? + form = "-defense" + elsif pokemon.trainer.emerald? + form = "-speed" + end + end + + if game == "emerald" + "pokeviewer/sprites/emerald/#{shininess}/#{species_id}#{form}.gif" + else + "pokeviewer/sprites/#{game}/#{shininess}/#{species_id}#{form}.png" + end + end + + def ability + if pokemon.second_ability + species.ability_2 + else + species.ability_1 + end + end + def move_1_pp move_1.pp * (5 + move_1_pp_bonuses) / 5 end diff --git a/app/models/pokeviewer/species.rb b/app/models/pokeviewer/species.rb index 429e762..aae66cc 100644 --- a/app/models/pokeviewer/species.rb +++ b/app/models/pokeviewer/species.rb @@ -2,7 +2,7 @@ module Pokeviewer class Species < ApplicationRecord extend Enumerize - has_many :pokemon, dependent: :restrict_with_exception + has_many :revisions, dependent: :restrict_with_exception validates :name, presence: true, uniqueness: true diff --git a/app/models/pokeviewer/trainer.rb b/app/models/pokeviewer/trainer.rb index b79547d..281e0aa 100644 --- a/app/models/pokeviewer/trainer.rb +++ b/app/models/pokeviewer/trainer.rb @@ -39,11 +39,11 @@ module Pokeviewer validates :box_14_name, presence: true def party - pokemon.party.includes(:species, :revisions) + pokemon.party.includes(revisions: [:species]) end def box(n) - pokemon.box(n).includes(:species, :revisions) + pokemon.box(n).includes(revisions: [:species]) end def box_name(n) diff --git a/app/views/pokeviewer/pokemon/index.html.haml b/app/views/pokeviewer/pokemon/index.html.haml index e1793dc..67ea2cf 100644 --- a/app/views/pokeviewer/pokemon/index.html.haml +++ b/app/views/pokeviewer/pokemon/index.html.haml @@ -8,8 +8,8 @@ %h3 Party - party.each do |p| %li - %span.party-icon= image_tag p.icon_path - %span.party-name= link_to p.nickname, p + %span.party-icon= image_tag p.current.icon_path + %span.party-name= link_to p.current.nickname, p - boxes.each do |box| .pc-box %h3= box[:name] @@ -21,9 +21,9 @@ - if p.nil? .spacer - else - = link_to image_tag(p.icon_path), p + = link_to image_tag(p.current.icon_path), p .pc-data.pkv-hover - .pc-data-name= p.nickname + .pc-data-name= p.current.nickname .pc-data-ot OT/ %span{ class: p.ot_gender }>= p.ot_name @@ -34,9 +34,9 @@ %ul.pokemon-list - @unaccounted.each do |p| %li.pc-pokemon.pkv-has-hover - = link_to image_tag(p.icon_path), p + = link_to image_tag(p.current.icon_path), p .pc-data.pkv-hover - .pc-data-name= p.nickname + .pc-data-name= p.current.nickname .pc-data-ot OT/ %span{ class: p.ot_gender }>= p.ot_name diff --git a/app/views/pokeviewer/pokemon/show.html.haml b/app/views/pokeviewer/pokemon/show.html.haml index 0daabb0..3f2750c 100644 --- a/app/views/pokeviewer/pokemon/show.html.haml +++ b/app/views/pokeviewer/pokemon/show.html.haml @@ -2,44 +2,44 @@ .pokemon-basics .pokemon-nameline = image_tag(@pokemon.pokeball_icon_path, class: "pokemon-ball") - %span.pokemon-name= @pokemon.revisions.last.nickname + %span.pokemon-name= @pokemon.current.nickname %span.pokemon-gender{ class: @pokemon.gender }= @pokemon.gender_symbol - .pokemon-level= "Lv. #{@pokemon.revisions.last.level}" + .pokemon-level= "Lv. #{@pokemon.current.level}" .pokemon-image .pokemon-image-wrap - = image_tag @pokemon.sprite_path, class: "pokemon-sprite" + = image_tag @pokemon.current.sprite_path, class: "pokemon-sprite" - if @pokemon.shiny? = image_tag "pokeviewer/ShinyIVStar.png", class: "pkv-shiny-star" - - if @pokemon.revisions.last.item.nil? + - if @pokemon.current.item.nil? .pokemon-item-label Item .pokemon-item None - else .pokemon-item-label.with-item Item .pokemon-item.pkv-has-hover - = image_tag(@pokemon.revisions.last.item.icon_path) - = @pokemon.revisions.last.item.name + = image_tag(@pokemon.current.item.icon_path) + = @pokemon.current.item.name .pkv-hover - .pc-data-name= @pokemon.revisions.last.item.name - - if @pokemon.revisions.last.item.tm? - .pc-move-name= @pokemon.revisions.last.item.move.name - = @pokemon.revisions.last.item.description(@pokemon.trainer.game) + .pc-data-name= @pokemon.current.item.name + - if @pokemon.current.item.tm? + .pc-move-name= @pokemon.current.item.move.name + = @pokemon.current.item.description(@pokemon.trainer.game) .pokemon-tab.pokemon-details %table %tr %th Pokédex No. %td - .table-bubble.tb-top= @pokemon.species_id + .table-bubble.tb-top= @pokemon.current.species_id %tr %th Name %td - .table-bubble= @pokemon.species.name + .table-bubble= @pokemon.current.species.name %tr %th Type %td .table-bubble - = image_for_type @pokemon.species.type_1 - - if @pokemon.species.type_2 - = image_for_type @pokemon.species.type_2 + = image_for_type @pokemon.current.species.type_1 + - if @pokemon.current.species.type_2 + = image_for_type @pokemon.current.species.type_2 %tr %th OT %td.ot-gender{ class: @pokemon.ot_gender } @@ -58,7 +58,7 @@ %tr %th HP %td - .table-bubble.tb-top= @pokemon.revisions.last.hp + .table-bubble.tb-top= @pokemon.current.hp %tr %th Attack @@ -67,7 +67,7 @@ - if @pokemon.nature_hinders?(:attack) %span.nature-hinder - %td - .table-bubble= @pokemon.revisions.last.attack + .table-bubble= @pokemon.current.attack %tr %th Defense @@ -76,7 +76,7 @@ - if @pokemon.nature_hinders?(:defense) %span.nature-hinder - %td - .table-bubble= @pokemon.revisions.last.defense + .table-bubble= @pokemon.current.defense %tr %th Sp. Atk @@ -85,7 +85,7 @@ - if @pokemon.nature_hinders?(:special_attack) %span.nature-hinder - %td - .table-bubble= @pokemon.revisions.last.special_attack + .table-bubble= @pokemon.current.special_attack %tr %th Sp. Def @@ -94,7 +94,7 @@ - if @pokemon.nature_hinders?(:special_defense) %span.nature-hinder - %td - .table-bubble= @pokemon.revisions.last.special_defense + .table-bubble= @pokemon.current.special_defense %tr %th Speed @@ -103,7 +103,7 @@ - if @pokemon.nature_hinders?(:speed) %span.nature-hinder - %td - .table-bubble.tb-bottom= @pokemon.revisions.last.speed + .table-bubble.tb-bottom= @pokemon.current.speed %tr.pokemon-nature-label %th{ colspan: 2 } Nature %tr @@ -116,18 +116,18 @@ %th %td .tb-only.pkv-has-hover - = @pokemon.ability.name + = @pokemon.current.ability.name .pkv-hover - .pc-data-name= @pokemon.ability.name - = @pokemon.ability.description + .pc-data-name= @pokemon.current.ability.name + = @pokemon.current.ability.description .pokemon-tab.pokemon-moves %table - (1..4).each do |i| - = move_details @pokemon.revisions.last, i - .pokemon-tab.pokemon-contest= condition_diagram @pokemon.revisions.last + = move_details @pokemon.current, i + .pokemon-tab.pokemon-contest= condition_diagram @pokemon.current .pokemon-tab.pokemon-ribbons %ul - - @pokemon.revisions.last.ribbons.each do |ribbon| + - @pokemon.current.ribbons.each do |ribbon| %li.pkv-has-hover = image_tag("pokeviewer/ribbons/#{ribbon[:filename]}") .pkv-hover diff --git a/db/migrate/20180113200119_move_species_to_revision.rb b/db/migrate/20180113200119_move_species_to_revision.rb new file mode 100644 index 0000000..8b94d48 --- /dev/null +++ b/db/migrate/20180113200119_move_species_to_revision.rb @@ -0,0 +1,39 @@ +class MoveSpeciesToRevision < ActiveRecord::Migration[5.1] + def up + change_table :pokeviewer_revisions do |t| + t.references :species, null: true + end + + Pokeviewer::Revision.all.each do |r| + r.species_id = r.pokemon.species_id + r.save! + end + + remove_column :pokeviewer_pokemon, :species_id + + change_column_null :pokeviewer_revisions, :species_id, false + + add_foreign_key :pokeviewer_revisions, :pokeviewer_species, + column: :species_id + end + + def down + def up + change_table :pokeviewer_pokemon do |t| + t.references :species, null: true + end + + Pokeviewer::Pokemon.all.each do |p| + p.species_id = p.revisions.first.species_id + p.save! + end + + remove_column :pokeviewer_revisions, :species_id + + change_column_null :pokeviewer_pokemon, :species_id, false + + add_foreign_key :pokeviewer_pokemon, :pokeviewer_species, + column: :species_id + end + end +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index a527d06..9752892 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/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: 20171011015648) do +ActiveRecord::Schema.define(version: 20180113200119) do create_table "pokeviewer_abilities", force: :cascade do |t| t.string "name", limit: 191, null: false @@ -58,7 +58,6 @@ ActiveRecord::Schema.define(version: 20171011015648) do create_table "pokeviewer_pokemon", force: :cascade do |t| t.string "uuid", limit: 191, null: false - t.integer "species_id", null: false t.integer "trainer_id" t.string "key", limit: 191 t.string "ot_name", null: false @@ -78,7 +77,6 @@ ActiveRecord::Schema.define(version: 20171011015648) do t.integer "location_id" t.string "pokeball", null: false t.index ["key"], name: "index_pokeviewer_pokemon_on_key", unique: true - t.index ["species_id"], name: "index_pokeviewer_pokemon_on_species_id" t.index ["trainer_id"], name: "index_pokeviewer_pokemon_on_trainer_id" t.index ["uuid"], name: "index_pokeviewer_pokemon_on_uuid", unique: true end @@ -129,12 +127,14 @@ ActiveRecord::Schema.define(version: 20171011015648) do t.boolean "national_ribbon", default: false t.boolean "earth_ribbon", default: false t.boolean "world_ribbon", default: false + t.integer "species_id", null: false t.index ["move_1_id"], name: "index_pokeviewer_revisions_on_move_1_id" t.index ["move_2_id"], name: "index_pokeviewer_revisions_on_move_2_id" t.index ["move_3_id"], name: "index_pokeviewer_revisions_on_move_3_id" t.index ["move_4_id"], name: "index_pokeviewer_revisions_on_move_4_id" t.index ["pokemon_id", "sequential_id"], name: "index_pokeviewer_revisions_on_pokemon_id_and_sequential_id", unique: true t.index ["pokemon_id"], name: "index_pokeviewer_revisions_on_pokemon_id" + t.index ["species_id"], name: "index_pokeviewer_revisions_on_species_id" end create_table "pokeviewer_species", force: :cascade do |t| -- cgit 1.4.1