diff options
| author | Star Rauchenberger <fefferburbia@gmail.com> | 2024-12-07 11:49:49 -0500 |
|---|---|---|
| committer | Star Rauchenberger <fefferburbia@gmail.com> | 2024-12-07 11:49:49 -0500 |
| commit | 56f5841d4b9c12296cdfcaeff174b2627d59afc8 (patch) | |
| tree | 4f7da4ebbe5ee15a1594b26466ed78e2cf10de35 /app/controllers | |
| parent | c1b0443ba2aebdbd39291ddab0c189f3f4831320 (diff) | |
| download | pokeviewer-56f5841d4b9c12296cdfcaeff174b2627d59afc8.tar.gz pokeviewer-56f5841d4b9c12296cdfcaeff174b2627d59afc8.tar.bz2 pokeviewer-56f5841d4b9c12296cdfcaeff174b2627d59afc8.zip | |
Migrate to full rails app
Diffstat (limited to 'app/controllers')
| -rw-r--r-- | app/controllers/application_controller.rb | 3 | ||||
| -rw-r--r-- | app/controllers/concerns/.keep | 0 | ||||
| -rw-r--r-- | app/controllers/pokedex_controller.rb | 10 | ||||
| -rw-r--r-- | app/controllers/pokemon_controller.rb | 99 | ||||
| -rw-r--r-- | app/controllers/pokeviewer/application_controller.rb | 5 | ||||
| -rw-r--r-- | app/controllers/pokeviewer/pokedex_controller.rb | 14 | ||||
| -rw-r--r-- | app/controllers/pokeviewer/pokemon_controller.rb | 103 | ||||
| -rw-r--r-- | app/controllers/pokeviewer/uploader_controller.rb | 26 | ||||
| -rw-r--r-- | app/controllers/uploader_controller.rb | 23 |
9 files changed, 135 insertions, 148 deletions
| diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb new file mode 100644 index 0000000..1c07694 --- /dev/null +++ b/app/controllers/application_controller.rb | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | class ApplicationController < ActionController::Base | ||
| 2 | protect_from_forgery with: :exception | ||
| 3 | end | ||
| diff --git a/app/controllers/concerns/.keep b/app/controllers/concerns/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/controllers/concerns/.keep | |||
| diff --git a/app/controllers/pokedex_controller.rb b/app/controllers/pokedex_controller.rb new file mode 100644 index 0000000..81c00e8 --- /dev/null +++ b/app/controllers/pokedex_controller.rb | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | class PokedexController < ApplicationController | ||
| 2 | def index | ||
| 3 | @species = Species. | ||
| 4 | order(id: :asc). | ||
| 5 | includes(:pokedex_entries). | ||
| 6 | order("pokedex_entries.trainer_id ASC") | ||
| 7 | |||
| 8 | @trainers = Trainer.order(id: :asc) | ||
| 9 | end | ||
| 10 | end | ||
| diff --git a/app/controllers/pokemon_controller.rb b/app/controllers/pokemon_controller.rb new file mode 100644 index 0000000..3f1ecde --- /dev/null +++ b/app/controllers/pokemon_controller.rb | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | class PokemonController < ApplicationController | ||
| 2 | before_action :load_pokemon, only: [:show, :embed] | ||
| 3 | |||
| 4 | def index | ||
| 5 | pokemon = Pokemon.order(Arel.sql("trainer_id IS NULL DESC")). | ||
| 6 | order(trainer_id: :asc). | ||
| 7 | order(box: :asc). | ||
| 8 | order(slot: :asc). | ||
| 9 | joins(:current). | ||
| 10 | includes(:current). | ||
| 11 | chunk do |p| | ||
| 12 | if p.trainer_id.nil? | ||
| 13 | -1 | ||
| 14 | else | ||
| 15 | p.trainer_id | ||
| 16 | end | ||
| 17 | end | ||
| 18 | |||
| 19 | @unaccounted = [] | ||
| 20 | |||
| 21 | begin | ||
| 22 | if pokemon.peek.first == -1 | ||
| 23 | @unaccounted = pokemon.next.second | ||
| 24 | end | ||
| 25 | rescue StopIteration | ||
| 26 | # There are no Pokémon, but that's fine. | ||
| 27 | end | ||
| 28 | |||
| 29 | @trainers = Trainer.order(id: :asc).all.map do |trainer| | ||
| 30 | if trainer.id == pokemon.peek.first | ||
| 31 | party = [] | ||
| 32 | |||
| 33 | boxes = (1..14).map do |i| | ||
| 34 | { | ||
| 35 | name: trainer.box_name(i), | ||
| 36 | pokemon: [nil] * 30 | ||
| 37 | } | ||
| 38 | end | ||
| 39 | |||
| 40 | pokemon.next.second.chunk do |p| | ||
| 41 | if p.box.nil? | ||
| 42 | -1 | ||
| 43 | else | ||
| 44 | p.box | ||
| 45 | end | ||
| 46 | end.each do |box, pokes| | ||
| 47 | if box == -1 | ||
| 48 | party = pokes | ||
| 49 | else | ||
| 50 | boxes[box-1][:pokemon] = (0..29).map do |i| | ||
| 51 | if not pokes.empty? and (pokes.first.slot == i) | ||
| 52 | pokes.shift | ||
| 53 | else | ||
| 54 | nil | ||
| 55 | end | ||
| 56 | end | ||
| 57 | end | ||
| 58 | end | ||
| 59 | |||
| 60 | [trainer, party, boxes] | ||
| 61 | else | ||
| 62 | nil | ||
| 63 | end | ||
| 64 | end.compact | ||
| 65 | end | ||
| 66 | |||
| 67 | def show | ||
| 68 | end | ||
| 69 | |||
| 70 | def show_revision | ||
| 71 | @revision = Revision. | ||
| 72 | where( | ||
| 73 | sequential_id: params[:revision_id], | ||
| 74 | pokemon: { uuid: params[:id] } | ||
| 75 | ).includes( | ||
| 76 | :species, :item, :move_1, :move_2, :move_3, :move_4, | ||
| 77 | pokemon: [:trainer, :location] | ||
| 78 | ).first | ||
| 79 | |||
| 80 | @pokemon = @revision.pokemon | ||
| 81 | |||
| 82 | render :show | ||
| 83 | end | ||
| 84 | |||
| 85 | def embed | ||
| 86 | render layout: false | ||
| 87 | end | ||
| 88 | |||
| 89 | protected | ||
| 90 | def load_pokemon | ||
| 91 | @pokemon = Pokemon.includes( | ||
| 92 | current: [ | ||
| 93 | :species, :item, :move_1, :move_2, :move_3, :move_4, | ||
| 94 | pokemon: [:trainer, :location]] | ||
| 95 | ).find_by_uuid! params[:id] | ||
| 96 | |||
| 97 | @revision = @pokemon.current | ||
| 98 | end | ||
| 99 | end | ||
| diff --git a/app/controllers/pokeviewer/application_controller.rb b/app/controllers/pokeviewer/application_controller.rb deleted file mode 100644 index e342b11..0000000 --- a/app/controllers/pokeviewer/application_controller.rb +++ /dev/null | |||
| @@ -1,5 +0,0 @@ | |||
| 1 | module Pokeviewer | ||
| 2 | class ApplicationController < ::ApplicationController | ||
| 3 | protect_from_forgery with: :exception | ||
| 4 | end | ||
| 5 | end | ||
| diff --git a/app/controllers/pokeviewer/pokedex_controller.rb b/app/controllers/pokeviewer/pokedex_controller.rb deleted file mode 100644 index 8286977..0000000 --- a/app/controllers/pokeviewer/pokedex_controller.rb +++ /dev/null | |||
| @@ -1,14 +0,0 @@ | |||
| 1 | require_dependency "pokeviewer/application_controller" | ||
| 2 | |||
| 3 | module Pokeviewer | ||
| 4 | class PokedexController < ApplicationController | ||
| 5 | def index | ||
| 6 | @species = Species. | ||
| 7 | order(id: :asc). | ||
| 8 | includes(:pokedex_entries). | ||
| 9 | order("pokeviewer_pokedex_entries.trainer_id ASC") | ||
| 10 | |||
| 11 | @trainers = Trainer.order(id: :asc) | ||
| 12 | end | ||
| 13 | end | ||
| 14 | end | ||
| diff --git a/app/controllers/pokeviewer/pokemon_controller.rb b/app/controllers/pokeviewer/pokemon_controller.rb deleted file mode 100644 index d0bf432..0000000 --- a/app/controllers/pokeviewer/pokemon_controller.rb +++ /dev/null | |||
| @@ -1,103 +0,0 @@ | |||
| 1 | require_dependency "pokeviewer/application_controller" | ||
| 2 | |||
| 3 | module Pokeviewer | ||
| 4 | class PokemonController < ApplicationController | ||
| 5 | before_action :load_pokemon, only: [:show, :embed] | ||
| 6 | |||
| 7 | def index | ||
| 8 | pokemon = Pokemon.order(Arel.sql("trainer_id IS NULL DESC")). | ||
| 9 | order(trainer_id: :asc). | ||
| 10 | order(box: :asc). | ||
| 11 | order(slot: :asc). | ||
| 12 | joins(:current). | ||
| 13 | includes(:current). | ||
| 14 | chunk do |p| | ||
| 15 | if p.trainer_id.nil? | ||
| 16 | -1 | ||
| 17 | else | ||
| 18 | p.trainer_id | ||
| 19 | end | ||
| 20 | end | ||
| 21 | |||
| 22 | @unaccounted = [] | ||
| 23 | |||
| 24 | begin | ||
| 25 | if pokemon.peek.first == -1 | ||
| 26 | @unaccounted = pokemon.next.second | ||
| 27 | end | ||
| 28 | rescue StopIteration | ||
| 29 | # There are no Pokémon, but that's fine. | ||
| 30 | end | ||
| 31 | |||
| 32 | @trainers = Trainer.order(id: :asc).all.map do |trainer| | ||
| 33 | if trainer.id == pokemon.peek.first | ||
| 34 | party = [] | ||
| 35 | |||
| 36 | boxes = (1..14).map do |i| | ||
| 37 | { | ||
| 38 | name: trainer.box_name(i), | ||
| 39 | pokemon: [nil] * 30 | ||
| 40 | } | ||
| 41 | end | ||
| 42 | |||
| 43 | pokemon.next.second.chunk do |p| | ||
| 44 | if p.box.nil? | ||
| 45 | -1 | ||
| 46 | else | ||
| 47 | p.box | ||
| 48 | end | ||
| 49 | end.each do |box, pokes| | ||
| 50 | if box == -1 | ||
| 51 | party = pokes | ||
| 52 | else | ||
| 53 | boxes[box-1][:pokemon] = (0..29).map do |i| | ||
| 54 | if not pokes.empty? and (pokes.first.slot == i) | ||
| 55 | pokes.shift | ||
| 56 | else | ||
| 57 | nil | ||
| 58 | end | ||
| 59 | end | ||
| 60 | end | ||
| 61 | end | ||
| 62 | |||
| 63 | [trainer, party, boxes] | ||
| 64 | else | ||
| 65 | nil | ||
| 66 | end | ||
| 67 | end.compact | ||
| 68 | end | ||
| 69 | |||
| 70 | def show | ||
| 71 | end | ||
| 72 | |||
| 73 | def show_revision | ||
| 74 | @revision = Revision. | ||
| 75 | where( | ||
| 76 | sequential_id: params[:revision_id], | ||
| 77 | pokeviewer_pokemon: { uuid: params[:id] } | ||
| 78 | ).includes( | ||
| 79 | :species, :item, :move_1, :move_2, :move_3, :move_4, | ||
| 80 | pokemon: [:trainer, :location] | ||
| 81 | ).first | ||
| 82 | |||
| 83 | @pokemon = @revision.pokemon | ||
| 84 | |||
| 85 | render :show | ||
| 86 | end | ||
| 87 | |||
| 88 | def embed | ||
| 89 | render layout: false | ||
| 90 | end | ||
| 91 | |||
| 92 | protected | ||
| 93 | def load_pokemon | ||
| 94 | @pokemon = Pokemon.includes( | ||
| 95 | current: [ | ||
| 96 | :species, :item, :move_1, :move_2, :move_3, :move_4, | ||
| 97 | pokemon: [:trainer, :location]] | ||
| 98 | ).find_by_uuid! params[:id] | ||
| 99 | |||
| 100 | @revision = @pokemon.current | ||
| 101 | end | ||
| 102 | end | ||
| 103 | end | ||
| diff --git a/app/controllers/pokeviewer/uploader_controller.rb b/app/controllers/pokeviewer/uploader_controller.rb deleted file mode 100644 index d72dd9a..0000000 --- a/app/controllers/pokeviewer/uploader_controller.rb +++ /dev/null | |||
| @@ -1,26 +0,0 @@ | |||
| 1 | require_dependency "pokeviewer/application_controller" | ||
| 2 | |||
| 3 | module Pokeviewer | ||
| 4 | class UploaderController < ApplicationController | ||
| 5 | skip_before_action :verify_authenticity_token | ||
| 6 | before_action :authenticate_user_from_token! | ||
| 7 | |||
| 8 | def submit | ||
| 9 | ExtractSaveDataJob.perform_later params[:game].as_json | ||
| 10 | |||
| 11 | render json: { message: "Data submitted for processing." } | ||
| 12 | end | ||
| 13 | |||
| 14 | private | ||
| 15 | |||
| 16 | def authenticate_user_from_token! | ||
| 17 | login = request.headers["X-User-Login"].presence | ||
| 18 | token = request.headers["X-User-Token"].presence | ||
| 19 | |||
| 20 | unless authenticate_pokeviewer(login, token) | ||
| 21 | head :unauthorized | ||
| 22 | end | ||
| 23 | end | ||
| 24 | |||
| 25 | end | ||
| 26 | end | ||
| diff --git a/app/controllers/uploader_controller.rb b/app/controllers/uploader_controller.rb new file mode 100644 index 0000000..72d4c2e --- /dev/null +++ b/app/controllers/uploader_controller.rb | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | class UploaderController < ApplicationController | ||
| 2 | skip_before_action :verify_authenticity_token | ||
| 3 | before_action :authenticate_user_from_token! | ||
| 4 | |||
| 5 | def submit | ||
| 6 | ExtractSaveDataJob.perform_later params[:game].as_json | ||
| 7 | |||
| 8 | render json: { message: "Data submitted for processing." } | ||
| 9 | end | ||
| 10 | |||
| 11 | private | ||
| 12 | |||
| 13 | def authenticate_user_from_token! | ||
| 14 | login = request.headers["X-User-Login"].presence | ||
| 15 | token = request.headers["X-User-Token"].presence | ||
| 16 | |||
| 17 | # TODO: Replace this. | ||
| 18 | unless authenticate_pokeviewer(login, token) | ||
| 19 | head :unauthorized | ||
| 20 | end | ||
| 21 | end | ||
| 22 | |||
| 23 | end | ||
