From b1d335e65bfcd813c3565257f8c18f22cdac08dc Mon Sep 17 00:00:00 2001 From: David Gay Date: Wed, 7 Apr 2021 22:20:14 -0400 Subject: Hacked together Pokemon generator --- app/controllers/games/ptu/encounters_controller.rb | 13 +++++ app/models/ptu_pokemon.rb | 32 +++++++++++++ app/views/games/ptu/encounters/index.html.erb | 55 ++++++++++++++++++++++ config/routes.rb | 1 + 4 files changed, 101 insertions(+) create mode 100644 app/controllers/games/ptu/encounters_controller.rb create mode 100644 app/views/games/ptu/encounters/index.html.erb diff --git a/app/controllers/games/ptu/encounters_controller.rb b/app/controllers/games/ptu/encounters_controller.rb new file mode 100644 index 0000000..a7da852 --- /dev/null +++ b/app/controllers/games/ptu/encounters_controller.rb @@ -0,0 +1,13 @@ +class Games::Ptu::EncountersController < ApplicationController + def index + @pokemons = PtuPokemon.all + if params[:commit] + @results = [] + pokemon = PtuPokemon.find(params[:pokemon]) + params[:count].to_i.times do + level = rand(params[:min_level].to_i..params[:max_level].to_i) + @results.push(pokemon.random_stat_hash(level: level)) + end + end + end +end diff --git a/app/models/ptu_pokemon.rb b/app/models/ptu_pokemon.rb index c34d32a..a351701 100644 --- a/app/models/ptu_pokemon.rb +++ b/app/models/ptu_pokemon.rb @@ -14,4 +14,36 @@ class PtuPokemon < ApplicationRecord "No gender" end end + + 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 + gender = "No gender" + end + hash = { + name: self.name, + level: level, + gender: gender, + nature: nature.name, + 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, + } + } + points_to_assign.times do + stat = hash[:stats].keys.sample + hash[:stats][stat] += stat == :hp ? 1 : 2 + 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 + end end diff --git a/app/views/games/ptu/encounters/index.html.erb b/app/views/games/ptu/encounters/index.html.erb new file mode 100644 index 0000000..4712a3c --- /dev/null +++ b/app/views/games/ptu/encounters/index.html.erb @@ -0,0 +1,55 @@ +

Encounters

+ +

Use this page to generate random Pokemon for an encounter.

+ +<%= form_with url: games_ptu_encounters_path, method: :get do |f| %> + <%= f.label :pokemon, "Pokemon" %> + <%= f.select :pokemon, @pokemons.map { |p| [p.name, p.id] } %> + + <%= f.label :count, "Count" %> + <%= f.number_field :count, required: true, min: 1, max: 100 %> + + <%= f.label :min_level, "Min Level" %> + <%= f.number_field :min_level, required: true, min: 1, max: 100 %> + + <%= f.label :max_level, "Max Level" %> + <%= f.number_field :max_level, required: true, min: 1, max: 100 %> + + <%= f.submit "Generate" %> +<% end %> + +<% if @results %> +

Results

+ + + + + + + + + + + + + + + + + <% @results.each do |result| %> + + + + + + + + + + + + + <% end %> + +
PokemonLevelGenderNatureHPATKDEFSPATKSPDEFSPEED
<%= result[:name] %><%= result[:level] %><%= result[:gender] %><%= result[:nature] %><%= result[:stats][:hp] %><%= result[:stats][:atk] %><%= result[:stats][:def] %><%= result[:stats][:spatk] %><%= result[:stats][:spdef] %><%= result[:stats][:speed] %>
+<% end %> diff --git a/config/routes.rb b/config/routes.rb index e54ecf2..d577816 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ Rails.application.routes.draw do namespace :games do namespace :ptu do resources :pokemon + resources :encounters, only: [:index] end end end -- cgit v1.2.3