summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-04-07 22:20:14 -0400
committerDavid Gay <david@davidgay.org>2021-04-07 22:20:14 -0400
commitb1d335e65bfcd813c3565257f8c18f22cdac08dc (patch)
tree90746516cc5fda892345a38a60d66c8b4da19fe2
parent5fc450ebd4249efc15e41b1b152a214697f5f415 (diff)
Hacked together Pokemon generator
-rw-r--r--app/controllers/games/ptu/encounters_controller.rb13
-rw-r--r--app/models/ptu_pokemon.rb32
-rw-r--r--app/views/games/ptu/encounters/index.html.erb55
-rw-r--r--config/routes.rb1
4 files changed, 101 insertions, 0 deletions
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 @@
+<h1>Encounters</h1>
+
+<p>Use this page to generate random Pokemon for an encounter.</p>
+
+<%= 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 %>
+ <h2>Results</h2>
+ <table class="table-auto">
+ <thead>
+ <tr>
+ <th>Pokemon</th>
+ <th>Level</th>
+ <th>Gender</th>
+ <th>Nature</th>
+ <th>HP</th>
+ <th>ATK</th>
+ <th>DEF</th>
+ <th>SPATK</th>
+ <th>SPDEF</th>
+ <th>SPEED</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% @results.each do |result| %>
+ <tr>
+ <td><%= result[:name] %></td>
+ <td><%= result[:level] %></td>
+ <td><%= result[:gender] %></td>
+ <td><%= result[:nature] %></td>
+ <td><%= result[:stats][:hp] %></td>
+ <td><%= result[:stats][:atk] %></td>
+ <td><%= result[:stats][:def] %></td>
+ <td><%= result[:stats][:spatk] %></td>
+ <td><%= result[:stats][:spdef] %></td>
+ <td><%= result[:stats][:speed] %></td>
+ </tr>
+ <% end %>
+ </tbody>
+ </table>
+<% 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