From c58d76edc7926ea03b193e9a40183184a564bf0a Mon Sep 17 00:00:00 2001 From: David Gay Date: Fri, 9 Apr 2021 18:22:35 -0400 Subject: Improve hacked-together pokemon generator, plus BSR --- app/controllers/games/ptu/encounters_controller.rb | 2 +- app/controllers/games/ptu/pokemon_controller.rb | 2 +- app/javascript/stylesheets/core.css | 4 +++ app/models/ptu_pokemon.rb | 41 +++++++++++++++++----- app/views/games/ptu/encounters/index.html.erb | 16 +++++---- app/views/games/ptu/index.html.erb | 6 ++++ app/views/games/ptu/pokemon/index.html.erb | 2 ++ config/routes.rb | 1 + 8 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 app/views/games/ptu/index.html.erb diff --git a/app/controllers/games/ptu/encounters_controller.rb b/app/controllers/games/ptu/encounters_controller.rb index a7da852..07d6c7f 100644 --- a/app/controllers/games/ptu/encounters_controller.rb +++ b/app/controllers/games/ptu/encounters_controller.rb @@ -1,6 +1,6 @@ class Games::Ptu::EncountersController < ApplicationController def index - @pokemons = PtuPokemon.all + @pokemons = PtuPokemon.all.order("name ASC") if params[:commit] @results = [] pokemon = PtuPokemon.find(params[:pokemon]) diff --git a/app/controllers/games/ptu/pokemon_controller.rb b/app/controllers/games/ptu/pokemon_controller.rb index e5329a7..1b88063 100644 --- a/app/controllers/games/ptu/pokemon_controller.rb +++ b/app/controllers/games/ptu/pokemon_controller.rb @@ -1,6 +1,6 @@ class Games::Ptu::PokemonController < ApplicationController def index - @pokemons = PtuPokemon.all + @pokemons = PtuPokemon.all.order("name ASC") end def show diff --git a/app/javascript/stylesheets/core.css b/app/javascript/stylesheets/core.css index ec06839..613e961 100644 --- a/app/javascript/stylesheets/core.css +++ b/app/javascript/stylesheets/core.css @@ -44,3 +44,7 @@ textarea { textarea:focus { @apply border-gray-700; } + +td { + @apply p-2 border border-gray-600; +} diff --git a/app/models/ptu_pokemon.rb b/app/models/ptu_pokemon.rb index a351701..6074fcd 100644 --- a/app/models/ptu_pokemon.rb +++ b/app/models/ptu_pokemon.rb @@ -17,7 +17,6 @@ class PtuPokemon < ApplicationRecord def random_stat_hash(level: 1) nature = PtuNature.find(PtuNature.pluck(:id).sample) - points_to_assign = 10 + level if self.male_chance gender = 1 + rand(100) <= self.male_chance ? "Male" : "Female" else @@ -28,22 +27,48 @@ class PtuPokemon < ApplicationRecord level: level, gender: gender, nature: nature.name, - stats: { + base_stats: { hp: self.base_hp, atk: self.base_atk, def: self.base_def, spatk: self.base_spatk, spdef: self.base_spdef, speed: self.base_speed, - } + }, + stats: {}, } + + # Adjust base stats for nature + hash[:base_stats][nature.raises.to_sym] += nature.raises.to_sym == :hp ? 1 : 2 + hash[:base_stats][nature.lowers.to_sym] -= nature.lowers.to_sym == :hp ? 1 : 2 + hash[:base_stats][nature.lowers.to_sym] = [1, hash[:base_stats][nature.lowers.to_sym]].max + + # e.g. [[:speed, 9], [:atk, 8], [:def, 8], [:spatk, 6], [:spdef, 6], [:hp, 5]] + sorted_base_stats = hash[:base_stats].sort_by { |_, v| -v } + + rank = 1 + # e.g. [1, 2, 2, 4, 4, 6] + rankings_array = sorted_base_stats.each_with_index.map { |a, i| sorted_base_stats[i - 1][1] == a[1] ? rank : (rank = i + 1) } + + # e.g. {:speed=>1, :atk=>2, :def=>2, :spatk=>4, :spdef=>4, :hp=>6} + base_stat_rankings = Hash[sorted_base_stats.map { |s| s[0] }.zip(rankings_array)] + + hash[:stats] = hash[:base_stats].deep_dup + points_to_assign = 10 + level points_to_assign.times do - stat = hash[:stats].keys.sample - hash[:stats][stat] += stat == :hp ? 1 : 2 + loop do + stat = hash[:stats].keys.sample + higher_base_stats = base_stat_rankings.select { |_, v| v < base_stat_rankings[stat] }.keys + if higher_base_stats.select { |s| hash[:stats][s] <= (hash[:stats][stat] + 1) }.any? + next + else + hash[:stats][stat] += 1 and break + end + end end - hash[:stats][nature.raises.to_sym] += nature.raises.to_sym == :hp ? 1 : 2 - hash[:stats][nature.lowers.to_sym] -= nature.lowers.to_sym == :hp ? 1 : 2 - hash[:stats][nature.lowers.to_sym] = [1, hash[:stats][nature.lowers.to_sym]].max + + hash[:hit_points] = 10 + level + (hash[:stats][:hp] * 3) + hash end end diff --git a/app/views/games/ptu/encounters/index.html.erb b/app/views/games/ptu/encounters/index.html.erb index 4712a3c..3da5214 100644 --- a/app/views/games/ptu/encounters/index.html.erb +++ b/app/views/games/ptu/encounters/index.html.erb @@ -1,3 +1,5 @@ +<%= link_to "Back", games_ptu_path %> +

Encounters

Use this page to generate random Pokemon for an encounter.

@@ -27,6 +29,7 @@ Level Gender Nature + Hit Points HP ATK DEF @@ -42,12 +45,13 @@ <%= result[:level] %> <%= result[:gender] %> <%= result[:nature] %> - <%= result[:stats][:hp] %> - <%= result[:stats][:atk] %> - <%= result[:stats][:def] %> - <%= result[:stats][:spatk] %> - <%= result[:stats][:spdef] %> - <%= result[:stats][:speed] %> + <%= result[:hit_points] %> + <%= result[:stats][:hp] %> (<%= result[:base_stats][:hp] %>) + <%= result[:stats][:atk] %> (<%= result[:base_stats][:atk] %>) + <%= result[:stats][:def] %> (<%= result[:base_stats][:def] %>) + <%= result[:stats][:spatk] %> (<%= result[:base_stats][:spatk] %>) + <%= result[:stats][:spdef] %> (<%= result[:base_stats][:spdef] %>) + <%= result[:stats][:speed] %> (<%= result[:base_stats][:speed] %>) <% end %> diff --git a/app/views/games/ptu/index.html.erb b/app/views/games/ptu/index.html.erb new file mode 100644 index 0000000..d9b83e7 --- /dev/null +++ b/app/views/games/ptu/index.html.erb @@ -0,0 +1,6 @@ +

PTU

+ + diff --git a/app/views/games/ptu/pokemon/index.html.erb b/app/views/games/ptu/pokemon/index.html.erb index a1d7443..46d356e 100644 --- a/app/views/games/ptu/pokemon/index.html.erb +++ b/app/views/games/ptu/pokemon/index.html.erb @@ -1,3 +1,5 @@ +<%= link_to "Back", games_ptu_path %> +

Pokemon

<%= link_to "New", new_games_ptu_pokemon_path %>
diff --git a/config/routes.rb b/config/routes.rb index d577816..9228d5d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ Rails.application.routes.draw do root "home#index", as: :home namespace :games do + get "/ptu", to: "ptu#index" namespace :ptu do resources :pokemon resources :encounters, only: [:index] -- cgit v1.2.3