class IVCalculator

Attributes

base_stats[RW]
ivs[RW]
level[RW]
nature[RW]
pokemon[RW]
stats[RW]

Public Class Methods

check(pokemon) click to toggle source
# File lib/iv_calculator.rb, line 23
def self.check(pokemon)
  @stats = { hp: pokemon.hp, atk: pokemon.atk, def: pokemon.def, spa: pokemon.spa, spd: pokemon.spd, spe: pokemon.spe }
  @base_stats = pokemon.base_stats
  @nature = pokemon.nature
  @level = pokemon.level
  calculate_ivs
end
new(pokemon, nature, level, hp, atk, defense, spa, spd, spe, base_stats) click to toggle source
# File lib/iv_calculator.rb, line 15
def initialize(pokemon, nature, level, hp, atk, defense, spa, spd, spe, base_stats)
  @pokemon = pokemon.downcase
  @nature = NATURES[nature.downcase.to_sym]
  @base_stats = base_stats || fetch_base_stats(pokemon)
  @level = level
  @stats = { hp: hp, atk: atk, def: defense, spa: spa, spd: spd, spe: spe }
end

Public Instance Methods

calculate_ivs() click to toggle source
# File lib/iv_calculator.rb, line 31
def calculate_ivs
  @ivs = stats.keys.map { |key| iv_range(key) }
end
iv_range(stat) click to toggle source
# File lib/iv_calculator.rb, line 35
def iv_range(stat)
  range = 32.times.filter { |iv| stats[stat] == calculate_stat(stat, iv) }
      
  return range.size > 1 ? "#{range.first}-#{range.last}" : "#{range.first || 'Undetermined'}"
end
print_ivs() click to toggle source

Private Instance Methods

calculate_stat(stat, iv) click to toggle source

floor rounds down

# File lib/iv_calculator.rb, line 63
def calculate_stat(stat, iv)
  if stat == :hp
    ((2.0 * base_stats[stat] + iv) * level / 100.0).floor + level + 10
  else
    ((((2.0 * base_stats[stat] + iv) * level / 100.0).floor + 5) * nature[stat]).floor
  end
end
fetch_base_stats(pokemon) click to toggle source
# File lib/iv_calculator.rb, line 47
def fetch_base_stats(pokemon)
  uri = URI("https://pokeapi.co/api/v2/pokemon/#{pokemon}")
  response = JSON.parse(Net::HTTP.get(uri), symbolize_names: true)
  base_stats = {
    hp:  response[:stats][0][:base_stat],
    atk: response[:stats][1][:base_stat],
    def: response[:stats][2][:base_stat],
    spa: response[:stats][3][:base_stat],
    spd: response[:stats][4][:base_stat],
    spe: response[:stats][5][:base_stat]
  }
rescue JSON::ParserError
  raise PokemonDoesNotExistError, "Could not find pokemon: #{pokemon}"
end