summaryrefslogtreecommitdiff
path: root/app/models/ptu_pokemon.rb
blob: a3517011a1e0972d9e22eecb90b192e9f37869bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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

  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