From 012b083cbd74ab687d5529258ff045319265e594 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 24 Sep 2017 12:39:17 -0400 Subject: Added storage location information to Pokémon MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/pokeviewer/extract_save_data_job.rb | 20 +++++++++++++++----- app/models/pokeviewer/pokemon.rb | 4 ++++ .../20170924160524_add_storage_info_to_pokemon.rb | 6 ++++++ test/dummy/db/schema.rb | 4 +++- 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20170924160524_add_storage_info_to_pokemon.rb diff --git a/app/jobs/pokeviewer/extract_save_data_job.rb b/app/jobs/pokeviewer/extract_save_data_job.rb index 0a9b790..404716a 100644 --- a/app/jobs/pokeviewer/extract_save_data_job.rb +++ b/app/jobs/pokeviewer/extract_save_data_job.rb @@ -22,7 +22,9 @@ module Pokeviewer end end - pokemons = args["pokemon"].map do |param| + game.pokemon.clear + + 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"] @@ -47,6 +49,18 @@ module Pokeviewer end end + pk.trainer = game + + if param["storage"] == "party" + pk.box = nil + elsif param["storage"] == "box" + pk.box = param["box"] + end + + pk.slot = param["slot"] + + pk.save! + rev = Revision.new(pokemon: pk) rev.nickname = param["nickname"] rev.experience = param["experience"] @@ -85,11 +99,7 @@ module Pokeviewer if pk.revisions.empty? or rev.diff?(pk.revisions.last) rev.save! end - - pk end - - game.pokemon = pokemons end end end diff --git a/app/models/pokeviewer/pokemon.rb b/app/models/pokeviewer/pokemon.rb index 1e3bfc8..3975550 100644 --- a/app/models/pokeviewer/pokemon.rb +++ b/app/models/pokeviewer/pokemon.rb @@ -36,6 +36,10 @@ module Pokeviewer enumerize :unown_letter, in: [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o, :p, :q, :r, :s, :t, :u, :v, :w, :x, :y, :z, :question, :exclamation] + + validates :slot, presence: true, + uniqueness: { scope: [:trainer_id, :box] }, + unless: Proc.new { |a| a.trainer.nil? } def to_param uuid diff --git a/db/migrate/20170924160524_add_storage_info_to_pokemon.rb b/db/migrate/20170924160524_add_storage_info_to_pokemon.rb new file mode 100644 index 0000000..b519b1b --- /dev/null +++ b/db/migrate/20170924160524_add_storage_info_to_pokemon.rb @@ -0,0 +1,6 @@ +class AddStorageInfoToPokemon < ActiveRecord::Migration[5.1] + def change + add_column :pokeviewer_pokemon, :box, :integer, null: true + add_column :pokeviewer_pokemon, :slot, :integer, null: true + end +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 9d6d773..9a13685 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: 20170924152817) do +ActiveRecord::Schema.define(version: 20170924160524) do create_table "pokeviewer_moves", force: :cascade do |t| t.string "name", limit: 191, null: false @@ -38,6 +38,8 @@ ActiveRecord::Schema.define(version: 20170924152817) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "ot_gender", default: "", null: false + t.integer "box" + t.integer "slot" 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" -- cgit 1.4.1