diff options
Diffstat (limited to 'app/controllers/pokemon_controller.rb')
| -rw-r--r-- | app/controllers/pokemon_controller.rb | 99 |
1 files changed, 99 insertions, 0 deletions
| 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 | ||
