diff options
author | David Gay <david@davidgay.org> | 2021-04-06 21:56:44 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-04-06 21:56:44 -0400 |
commit | 4ed2a36d48b6677d7ddb137e073a985d6391b8b4 (patch) | |
tree | 5e94cc8de10e747f3157142a6fab73e99b047dcf /app | |
parent | b85c671ca482e9d36bd2ee94231f365e7e0d65a5 (diff) |
Super-bare-bones pokemon adding and viewing
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/games/ptu/pokemon_controller.rb | 22 | ||||
-rw-r--r-- | app/javascript/stylesheets/core.css | 35 | ||||
-rw-r--r-- | app/models/ptu_pokemon.rb | 13 | ||||
-rw-r--r-- | app/views/application/_flash_messages.html.erb | 9 | ||||
-rw-r--r-- | app/views/games/ptu/pokemon/new.html.erb | 30 | ||||
-rw-r--r-- | app/views/games/ptu/pokemon/show.html.erb | 32 | ||||
-rw-r--r-- | app/views/layouts/application.html.erb | 1 |
7 files changed, 142 insertions, 0 deletions
diff --git a/app/controllers/games/ptu/pokemon_controller.rb b/app/controllers/games/ptu/pokemon_controller.rb index 8290a73..485f03d 100644 --- a/app/controllers/games/ptu/pokemon_controller.rb +++ b/app/controllers/games/ptu/pokemon_controller.rb @@ -1,4 +1,26 @@ class Games::Ptu::PokemonController < ApplicationController def show + @pokemon = PtuPokemon.find(params[:id]) end + + def new + @pokemon = PtuPokemon.new + end + + def create + @pokemon = PtuPokemon.new(pokemon_params) + if @pokemon.save + flash[:success] = "Created Pokémon #{@pokemon.name}." + redirect_to games_ptu_pokemon_path(@pokemon) + else + flash[:error] = "Failed to create Pokémon." + redirect_to new_games_ptu_pokemon_path + end + end + + private + def pokemon_params + params.require(:ptu_pokemon).permit(:name, :base_hp, :base_atk, :base_def, :base_spatk, :base_spdef, + :base_speed, :male_chance, :notes) + end end diff --git a/app/javascript/stylesheets/core.css b/app/javascript/stylesheets/core.css index a174cfb..15b234b 100644 --- a/app/javascript/stylesheets/core.css +++ b/app/javascript/stylesheets/core.css @@ -8,3 +8,38 @@ a:hover { p { margin-top: 1rem; } + +label { + @apply mb-2; +} + +input { + @apply border border-black p-1; +} +input:focus { + @apply outline-none border; +} + +button, [type="button"], [type="reset"], [type="submit"] { + @apply py-1 px-2 cursor-pointer; +} +button:hover, [type="button"]:hover, [type="reset"]:hover, [type="submit"]:hover { + @apply bg-gray-300; +} + +button:focus, [type="button"]:focus, [type="reset"]:focus, [type="submit"]:focus { +} + +select { + @apply border border-black p-1; +} +select:focus { + @apply border-gray-700; +} + +textarea { + @apply border border-black p-1; +} +textarea:focus { + @apply border-gray-700; +} diff --git a/app/models/ptu_pokemon.rb b/app/models/ptu_pokemon.rb index 1c57500..c34d32a 100644 --- a/app/models/ptu_pokemon.rb +++ b/app/models/ptu_pokemon.rb @@ -1,4 +1,17 @@ class PtuPokemon < ApplicationRecord + validates :name, uniqueness: true validates :name, :base_hp, :base_atk, :base_def, :base_spatk, :base_spdef, :base_speed, presence: true validates :male_chance, inclusion: 0..100, allow_nil: true + + def female_chance + (100.0 - self.male_chance) if self.male_chance + end + + def gender_ratio_string + if self.male_chance + "Male #{self.male_chance}% / Female #{self.female_chance}%" + else + "No gender" + end + end end diff --git a/app/views/application/_flash_messages.html.erb b/app/views/application/_flash_messages.html.erb new file mode 100644 index 0000000..7db1762 --- /dev/null +++ b/app/views/application/_flash_messages.html.erb @@ -0,0 +1,9 @@ +<% if flash[:error] %> + <div class="block rounded my-2 p-2 bg-red-200 text-red-800"> + <%= flash[:error] %> + </div> +<% elsif flash[:success] %> + <div class="block rounded my-2 p-2 bg-green-200 text-green-800"> + <%= flash[:success] %> + </div> +<% end %> diff --git a/app/views/games/ptu/pokemon/new.html.erb b/app/views/games/ptu/pokemon/new.html.erb new file mode 100644 index 0000000..7ba0426 --- /dev/null +++ b/app/views/games/ptu/pokemon/new.html.erb @@ -0,0 +1,30 @@ +<h1>New Pokemon</h1> + +<%= form_with model: @pokemon, url: games_ptu_pokemon_index_path do |f| %> + <%= f.label :name %> + <%= f.text_field :name, required: true %> + + <%= f.label :base_hp, "Base HP" %> + <%= f.number_field :base_hp, min: 1, required: true %> + + <%= f.label :base_atk, "Base ATK" %> + <%= f.number_field :base_atk, min: 1, required: true %> + + <%= f.label :base_def, "Base DEF" %> + <%= f.number_field :base_def, min: 1, required: true %> + + <%= f.label :base_spatk, "Base SPATK" %> + <%= f.number_field :base_spatk, min: 1, required: true %> + + <%= f.label :base_spdef, "Base SPDEF" %> + <%= f.number_field :base_spdef, min: 1, required: true %> + + <%= f.label :base_speed, "Base SPEED" %> + <%= f.number_field :base_speed, min: 1, required: true %> + + <%= f.label :male_chance, "Male Chance" %> + <%= f.number_field :male_chance, min: 0, step: 0.01 %> % + + <%= f.submit "Save" %> + +<% end %> diff --git a/app/views/games/ptu/pokemon/show.html.erb b/app/views/games/ptu/pokemon/show.html.erb new file mode 100644 index 0000000..e7b80ce --- /dev/null +++ b/app/views/games/ptu/pokemon/show.html.erb @@ -0,0 +1,32 @@ +<h1><%= @pokemon.name %></h1> + +<table> + <tr> + <td>Base HP</td> + <td><%= @pokemon.base_hp %></td> + </tr> + <tr> + <td>Base ATK</td> + <td><%= @pokemon.base_atk %></td> + </tr> + <tr> + <td>Base DEF</td> + <td><%= @pokemon.base_def %></td> + </tr> + <tr> + <td>Base SPATK</td> + <td><%= @pokemon.base_spatk %></td> + </tr> + <tr> + <td>Base SPDEF</td> + <td><%= @pokemon.base_spdef %></td> + </tr> + <tr> + <td>Base SPEED</td> + <td><%= @pokemon.base_speed %></td> + </tr> + <tr> + <td>Gender Ratio</td> + <td><%= @pokemon.gender_ratio_string %></td> + </tr> +</table> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ac331aa..2be4dc3 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,6 +14,7 @@ <div class="flex flex-col min-h-screen"> <%= render "header" %> <div class="container mx-auto flex-grow"> + <%= render "flash_messages" %> <%= yield %> </div> <%= render "footer" %> |