From 5ade37d852bd1e96f9451ab98619359a5a048cee Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 29 Jan 2018 21:13:35 -0500 Subject: Added Pokédex viewing page MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently a work in progress. The queries used to display the Pokémon for each species are very inefficient. The text at the top of the page is also very specific to the author. --- app/assets/stylesheets/pokeviewer/pokedex.scss | 85 ++++++++++++++++++++++ app/controllers/pokeviewer/pokedex_controller.rb | 14 ++++ app/helpers/pokeviewer/pokedex_helper.rb | 51 +++++++++++++ app/jobs/pokeviewer/extract_save_data_job.rb | 10 +++ app/models/pokeviewer/pokedex_entry.rb | 6 ++ app/models/pokeviewer/species.rb | 9 +++ app/models/pokeviewer/trainer.rb | 2 + app/views/pokeviewer/pokedex/index.html.haml | 4 + config/routes.rb | 2 + ...0129213822_create_pokeviewer_pokedex_entries.rb | 20 +++++ test/dummy/db/schema.rb | 13 +++- 11 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/pokeviewer/pokedex.scss create mode 100644 app/controllers/pokeviewer/pokedex_controller.rb create mode 100644 app/helpers/pokeviewer/pokedex_helper.rb create mode 100644 app/models/pokeviewer/pokedex_entry.rb create mode 100644 app/views/pokeviewer/pokedex/index.html.haml create mode 100644 db/migrate/20180129213822_create_pokeviewer_pokedex_entries.rb diff --git a/app/assets/stylesheets/pokeviewer/pokedex.scss b/app/assets/stylesheets/pokeviewer/pokedex.scss new file mode 100644 index 0000000..5cecb28 --- /dev/null +++ b/app/assets/stylesheets/pokeviewer/pokedex.scss @@ -0,0 +1,85 @@ +// Place all the styles related to the Pokedex controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ + +.pkvd-table { + border-spacing: 5px; + + th { + text-align: right; + font-family: 'Power Green'; + font-weight: normal; + padding-right: 0.5em; + + img { + image-rendering: pixelated; + } + } + + td { + padding: 0 0.5em; + text-align: center; + font-family: 'Power Green'; + + &.pkvd-unseen { + border: 1px dashed gray; + color: gray; + } + + &.pkvd-seen { + border-width: 3px; + border-style: solid; + font-weight: bold; + + &.ruby { + border-color: #D67873; + color: #D67873; + } + + &.sapphire { + border-color: #445E9C; + color: #445E9C; + } + + &.firered { + border-color: #F5AC78; + color: #F5AC78; + } + + &.leafgreen { + border-color: #A7DB8D; + color: #A7DB8D; + } + + &.emerald { + border-color: #4E8234; + color: #4E8234; + } + } + + &.pkvd-caught { + color: black; + font-weight: bold; + + &.ruby { + background-color: #D67873; + } + + &.sapphire { + background-color: #445E9C; + } + + &.firered { + background-color: #F5AC78; + } + + &.leafgreen { + background-color: #A7DB8D; + } + + &.emerald { + background-color: #4E8234; + } + } + } +} diff --git a/app/controllers/pokeviewer/pokedex_controller.rb b/app/controllers/pokeviewer/pokedex_controller.rb new file mode 100644 index 0000000..8286977 --- /dev/null +++ b/app/controllers/pokeviewer/pokedex_controller.rb @@ -0,0 +1,14 @@ +require_dependency "pokeviewer/application_controller" + +module Pokeviewer + class PokedexController < ApplicationController + def index + @species = Species. + order(id: :asc). + includes(:pokedex_entries). + order("pokeviewer_pokedex_entries.trainer_id ASC") + + @trainers = Trainer.order(id: :asc) + end + end +end diff --git a/app/helpers/pokeviewer/pokedex_helper.rb b/app/helpers/pokeviewer/pokedex_helper.rb new file mode 100644 index 0000000..fe3e575 --- /dev/null +++ b/app/helpers/pokeviewer/pokedex_helper.rb @@ -0,0 +1,51 @@ +module Pokeviewer + module PokedexHelper + + def pokedex_table(all_species, trainers) + final_result = "".html_safe + + all_species.each do |species| + noted_trainers = species.pokedex_entries.to_a + + if noted_trainers.empty? + poke_name = "???" + poke_image = image_tag "pokeviewer/icons/0.png" + else + poke_name = species.name + poke_image = image_tag "pokeviewer/icons/#{species.id}.png" + end + + result = "".html_safe + + result << tag.th("\##{species.id}") + result << tag.th(poke_name) + result << tag.th(poke_image) + + trainers.each do |trainer| + if !noted_trainers.empty? and noted_trainers.first.trainer_id == trainer.id + nt = noted_trainers.shift + + if nt.caught + result << tag.td(trainer.display_number, + class: ["pkvd-caught", trainer.game]) + else + result << tag.td(trainer.display_number, + class: ["pkvd-seen", trainer.game]) + end + else + result << tag.td(trainer.display_number, class: "pkvd-unseen") + end + end + + result << tag.td( + species.current_revisions.map { + |p| link_to p.nickname, p.pokemon }.join(", ").html_safe) + + final_result << tag.tr(result) + end + + tag.table(final_result, class: "pkvd-table") + end + + end +end diff --git a/app/jobs/pokeviewer/extract_save_data_job.rb b/app/jobs/pokeviewer/extract_save_data_job.rb index b0999ef..bb18d68 100644 --- a/app/jobs/pokeviewer/extract_save_data_job.rb +++ b/app/jobs/pokeviewer/extract_save_data_job.rb @@ -173,6 +173,16 @@ module Pokeviewer rev.save! end end + + game.pokedex_entries.clear + + args["seen"].each do |param| + game.pokedex_entries.create(species_id: param, caught: false) + end + + args["caught"].each do |param| + game.pokedex_entries.create(species_id: param, caught: true) + end end end end diff --git a/app/models/pokeviewer/pokedex_entry.rb b/app/models/pokeviewer/pokedex_entry.rb new file mode 100644 index 0000000..9be6a6a --- /dev/null +++ b/app/models/pokeviewer/pokedex_entry.rb @@ -0,0 +1,6 @@ +module Pokeviewer + class PokedexEntry < ApplicationRecord + belongs_to :trainer + belongs_to :species + end +end diff --git a/app/models/pokeviewer/species.rb b/app/models/pokeviewer/species.rb index aae66cc..400d679 100644 --- a/app/models/pokeviewer/species.rb +++ b/app/models/pokeviewer/species.rb @@ -4,6 +4,8 @@ module Pokeviewer has_many :revisions, dependent: :restrict_with_exception + has_many :pokedex_entries, dependent: :destroy + validates :name, presence: true, uniqueness: true validates :type_1, presence: true @@ -13,5 +15,12 @@ module Pokeviewer belongs_to :ability_1, class_name: "Ability" belongs_to :ability_2, class_name: "Ability", optional: true + + def current_revisions + revisions. + where("pokeviewer_pokemon.current_id = pokeviewer_revisions.id"). + includes(:pokemon). + references(:pokemon) + end end end diff --git a/app/models/pokeviewer/trainer.rb b/app/models/pokeviewer/trainer.rb index 0ea12c8..950dac0 100644 --- a/app/models/pokeviewer/trainer.rb +++ b/app/models/pokeviewer/trainer.rb @@ -4,6 +4,8 @@ module Pokeviewer has_many :pokemon, dependent: :nullify + has_many :pokedex_entries, dependent: :destroy + validates :number, presence: true, numericality: { greater_than_or_equal_to: 0, only_integer: true } diff --git a/app/views/pokeviewer/pokedex/index.html.haml b/app/views/pokeviewer/pokedex/index.html.haml new file mode 100644 index 0000000..676cd9f --- /dev/null +++ b/app/views/pokeviewer/pokedex/index.html.haml @@ -0,0 +1,4 @@ +%h3 Fef's Pokédex +%p The following table shows my progress toward completing the Pokémon National Pokédex in generation III. Because I am playing multiple games, the progress is shown for each specific game. The columns are colored based on what game they are for: dark red for Ruby, dark blue for Sapphire, dark green for Emerald, light red for FireRed, and light green for LeafGreen. The cells also contain the trainer ID for that game. +%p If a cell is grayed out, that means that game has not encountered that Pokémon species yet. If a cell has a colored border but is not filled in, that means that game has seen the given species but not caught it. A filled in cell means that the game has recorded data on that species. If a species has not been seen in any of the save files, the name and image of the species is not shown. += pokedex_table @species, @trainers diff --git a/config/routes.rb b/config/routes.rb index 438ac39..2d59eea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,4 +4,6 @@ Pokeviewer::Engine.routes.draw do resources :pokemon, only: [:show] + resources :pokedex, only: [:index] + end diff --git a/db/migrate/20180129213822_create_pokeviewer_pokedex_entries.rb b/db/migrate/20180129213822_create_pokeviewer_pokedex_entries.rb new file mode 100644 index 0000000..99ac4f6 --- /dev/null +++ b/db/migrate/20180129213822_create_pokeviewer_pokedex_entries.rb @@ -0,0 +1,20 @@ +class CreatePokeviewerPokedexEntries < ActiveRecord::Migration[5.1] + def change + create_table :pokeviewer_pokedex_entries do |t| + t.references :trainer, null: true + t.references :species, null: true + t.boolean :caught, null: true, default: false + + t.timestamps + end + + add_foreign_key :pokeviewer_pokedex_entries, :pokeviewer_trainer, + column: :trainer_id + + add_foreign_key :pokeviewer_pokedex_entries, :pokeviewer_species, + column: :species_id + + add_index :pokeviewer_pokedex_entries, [:trainer_id, :species_id], + unique: true + end +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 5f8c1c0..996df86 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: 20180114170238) do +ActiveRecord::Schema.define(version: 20180129213822) do create_table "pokeviewer_abilities", force: :cascade do |t| t.string "name", limit: 191, null: false @@ -56,6 +56,17 @@ ActiveRecord::Schema.define(version: 20180114170238) do t.index ["name"], name: "index_pokeviewer_moves_on_name", unique: true end + create_table "pokeviewer_pokedex_entries", force: :cascade do |t| + t.integer "trainer_id" + t.integer "species_id" + t.boolean "caught", default: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["species_id"], name: "index_pokeviewer_pokedex_entries_on_species_id" + t.index ["trainer_id", "species_id"], name: "index_pokeviewer_pokedex_entries_on_trainer_id_and_species_id", unique: true + t.index ["trainer_id"], name: "index_pokeviewer_pokedex_entries_on_trainer_id" + end + create_table "pokeviewer_pokemon", force: :cascade do |t| t.string "uuid", limit: 191, null: false t.integer "trainer_id" -- cgit 1.4.1