class Oakdex::Pokemon

Represents detailed pokemon instance

Constants

BATTLE_STATS
VERSION

Attributes

trainer[RW]

Public Class Methods

create(species_name, options = {}) click to toggle source
# File lib/oakdex/pokemon.rb, line 29
def self.create(species_name, options = {})
  species = Oakdex::Pokedex::Pokemon.find!(species_name)
  Factory.create(species, options)
end
from_json(json) click to toggle source
# File lib/oakdex/pokemon.rb, line 270
def self.from_json(json)
  Import.new(json).import!
end
new(species_id, attributes = {}) click to toggle source
# File lib/oakdex/pokemon.rb, line 34
def initialize(species_id, attributes = {})
  @species_id = species_id
  @attributes = attributes
  @battle_mode = false
  @attributes[:growth_events] ||= []
  species
end
root() click to toggle source
# File lib/oakdex/pokemon.rb, line 25
def self.root
  File.expand_path '../../../', __FILE__
end

Public Instance Methods

add_ev(stat, ev_to_add) click to toggle source
# File lib/oakdex/pokemon.rb, line 154
def add_ev(stat, ev_to_add)
  @attributes[:ev] = @attributes[:ev].map do |k, v|
    [k, k.to_s == stat ? [v + ev_to_add, 255].min : v]
  end.to_h
end
add_exp(exp_to_add) click to toggle source
# File lib/oakdex/pokemon.rb, line 150
def add_exp(exp_to_add)
  @attributes[:exp] += exp_to_add
end
add_growth_event(klass, options = {}) click to toggle source
# File lib/oakdex/pokemon.rb, line 232
def add_growth_event(klass, options = {})
  evt = klass.new(self, options.select { |k, _| k != :after })
  if options[:after]
    index = @attributes[:growth_events].index(options[:after])
    if index.nil?
      @attributes[:growth_events] << evt
    else
      @attributes[:growth_events].insert(index + 1, evt)
    end
  else
    @attributes[:growth_events] << evt
  end

  evt
end
amie() click to toggle source
# File lib/oakdex/pokemon.rb, line 98
def amie
  {
    affection: 0,
    fullness: 0,
    enjoyment: 0
  }.merge(@attributes[:amie] || {})
end
amie_level(amie_stat) click to toggle source
# File lib/oakdex/pokemon.rb, line 106
def amie_level(amie_stat)
  5 - [255, 150, 100, 50, 1, 0].find_index do |treshold|
    amie[amie_stat] >= treshold
  end
end
change_hp_by(hp_change) click to toggle source
# File lib/oakdex/pokemon.rb, line 118
def change_hp_by(hp_change)
  @attributes[:hp] = if hp_change < 0
                       [@attributes[:hp] + hp_change, 0].max
                     else
                       [@attributes[:hp] + hp_change, hp].min
                     end
end
change_pp_by(move_name, pp_change) click to toggle source
# File lib/oakdex/pokemon.rb, line 126
def change_pp_by(move_name, pp_change)
  move = moves.find { |m| m.name == move_name }
  return unless move
  move.pp = if pp_change < 0
              [move.pp + pp_change, 0].max
            else
              [move.pp + pp_change, move.max_pp].min
            end
end
current_hp() click to toggle source
# File lib/oakdex/pokemon.rb, line 74
def current_hp
  @attributes[:hp]
end
disable_battle_mode() click to toggle source
# File lib/oakdex/pokemon.rb, line 212
def disable_battle_mode
  @battle_mode = false
end
dup() click to toggle source
Calls superclass method
# File lib/oakdex/pokemon.rb, line 256
def dup
  duplicated = super
  new_attributes = @attributes.dup
  new_attributes[:moves] = new_attributes[:moves].map(&:dup)
  new_attributes[:growth_events] = new_attributes[:growth_events].map(&:dup)
  duplicated.instance_variable_set(:@attributes, new_attributes)

  duplicated
end
enable_battle_mode() click to toggle source
# File lib/oakdex/pokemon.rb, line 208
def enable_battle_mode
  @battle_mode = true
end
envolve_to(species_id) click to toggle source
# File lib/oakdex/pokemon.rb, line 248
def envolve_to(species_id)
  old_max_hp = hp
  @species = nil
  @species_id = species_id
  change_hp_by(hp - old_max_hp) unless fainted?
  species
end
ev_max?(stat) click to toggle source
# File lib/oakdex/pokemon.rb, line 160
def ev_max?(stat)
  @attributes[:ev][stat].to_i >= 255
end
exp() click to toggle source
# File lib/oakdex/pokemon.rb, line 136
def exp
  @attributes[:exp]
end
fainted?() click to toggle source
# File lib/oakdex/pokemon.rb, line 78
def fainted?
  current_hp.zero?
end
for_game() click to toggle source
# File lib/oakdex/pokemon.rb, line 282
def for_game
  {
    name: name,
    species_id: species.name,
    primary_status_condition: primary_status_condition,
    type_ids: types,
    gender: gender,
    friendship: friendship,
    moves: moves.map { |m| m.to_h.merge({ type_id: m.type.name }) },
    current_hp: current_hp,
    fainted: fainted?,
    wild: wild?,
    original_trainer: original_trainer,
    item_id: item_id,
    traded: traded?,
    exp: exp,
    level: level,
    exp_next_level: exp_next_level,
    nature_id: @attributes[:nature_id],
    ability_id: @attributes[:ability_id]
  }.merge(BATTLE_STATS.map { |s| [s, public_send(s)] }.to_h)
end
friendship() click to toggle source
# File lib/oakdex/pokemon.rb, line 66
def friendship
  @attributes[:friendship]
end
gain_exp(gained_exp) click to toggle source
# File lib/oakdex/pokemon.rb, line 175
def gain_exp(gained_exp)
  add_growth_event(GrowthEvents::GainedExp, gained_exp: gained_exp)
end
gender() click to toggle source
# File lib/oakdex/pokemon.rb, line 62
def gender
  @attributes[:gender]
end
grow_from_battle(fainted, options = {}) click to toggle source
# File lib/oakdex/pokemon.rb, line 202
def grow_from_battle(fainted, options = {})
  exp = ExperienceGainCalculator.calculate(fainted, self, options)
  gain_exp(exp)
  gain_ev_from_battle(fainted) unless options[:using_exp_share]
end
growth_event() click to toggle source
# File lib/oakdex/pokemon.rb, line 220
def growth_event
  growth_events.first
end
growth_event?() click to toggle source
# File lib/oakdex/pokemon.rb, line 216
def growth_event?
  !growth_events.empty?
end
increment_level() click to toggle source
# File lib/oakdex/pokemon.rb, line 197
def increment_level
  gained_exp = exp_next_level - @attributes[:exp]
  gain_exp(gained_exp)
end
inspect() click to toggle source
# File lib/oakdex/pokemon.rb, line 46
def inspect
  "#<#{self.class.name}:#{object_id} #{@attributes.inspect}>"
end
item_id() click to toggle source
# File lib/oakdex/pokemon.rb, line 94
def item_id
  @attributes[:item_id]
end
learn_new_move(move_id, replaced_move_id = nil) click to toggle source
# File lib/oakdex/pokemon.rb, line 164
def learn_new_move(move_id, replaced_move_id = nil)
  new_move = Move.create(move_id)
  if replaced_move_id.nil?
    @attributes[:moves] << new_move
  else
    index = @attributes[:moves]
      .find_index { |m| m.name == replaced_move_id }
    @attributes[:moves][index] = new_move if index
  end
end
level() click to toggle source
# File lib/oakdex/pokemon.rb, line 140
def level
  Stat.level_by_exp(species.leveling_rate, @attributes[:exp])
end
moves() click to toggle source
# File lib/oakdex/pokemon.rb, line 70
def moves
  @attributes[:moves]
end
moves_with_pp() click to toggle source
# File lib/oakdex/pokemon.rb, line 82
def moves_with_pp
  moves.select { |m| m.pp > 0 }
end
name() click to toggle source
# File lib/oakdex/pokemon.rb, line 58
def name
  species.names['en']
end
original_trainer() click to toggle source
# File lib/oakdex/pokemon.rb, line 90
def original_trainer
  @attributes[:original_trainer]
end
primary_status_condition() click to toggle source
# File lib/oakdex/pokemon.rb, line 50
def primary_status_condition
  @attributes[:primary_status_condition]
end
primary_status_condition=(value) click to toggle source
# File lib/oakdex/pokemon.rb, line 54
def primary_status_condition=(value)
  @attributes[:primary_status_condition] = value
end
remove_growth_event() click to toggle source
# File lib/oakdex/pokemon.rb, line 224
def remove_growth_event
  remove_event = growth_event
  return unless remove_event
  @attributes[:growth_events] = @attributes[:growth_events].select do |e|
    e != remove_event
  end
end
species() click to toggle source
# File lib/oakdex/pokemon.rb, line 42
def species
  @species ||= Oakdex::Pokedex::Pokemon.find!(@species_id)
end
to_h() click to toggle source
# File lib/oakdex/pokemon.rb, line 274
def to_h
  @attributes.dup.tap do |attributes|
    attributes[:species_id] = species.name
    attributes[:moves] = attributes[:moves].map(&:to_h)
    attributes[:growth_events] = attributes[:growth_events].map(&:to_h)
  end
end
to_json() click to toggle source
# File lib/oakdex/pokemon.rb, line 266
def to_json
  JSON.dump(to_h)
end
trade_to(trainer) click to toggle source
# File lib/oakdex/pokemon.rb, line 179
def trade_to(trainer)
  self.trainer = trainer
  available_evolution = EvolutionMatcher.new(self, 'trade').evolution

  add_growth_event(GrowthEvents::Evolution,
                   evolution: available_evolution) if available_evolution
end
traded?() click to toggle source
# File lib/oakdex/pokemon.rb, line 112
def traded?
  return false if trainer.nil? || original_trainer.nil?
  return false unless trainer.respond_to?(:name)
  trainer.name != original_trainer
end
usable_item?(item_id, options = {}) click to toggle source
# File lib/oakdex/pokemon.rb, line 187
def usable_item?(item_id, options = {})
  service = UseItemService.new(self, item_id, options)
  service.usable?
end
use_item(item_id, options = {}) click to toggle source
# File lib/oakdex/pokemon.rb, line 192
def use_item(item_id, options = {})
  service = UseItemService.new(self, item_id, options)
  service.use
end
wild?() click to toggle source
# File lib/oakdex/pokemon.rb, line 86
def wild?
  @attributes[:wild]
end

Private Instance Methods

exp_next_level() click to toggle source
# File lib/oakdex/pokemon.rb, line 338
def exp_next_level
  Stat.exp_by_level(species.leveling_rate, level + 1)
end
gain_ev_from_battle(fainted) click to toggle source
# File lib/oakdex/pokemon.rb, line 317
def gain_ev_from_battle(fainted)
  fainted.species.ev_yield.each do |stat, value|
    next if value.zero?
    add_growth_event(GrowthEvents::GainedEv, stat: stat, value: value)
  end
end
growth_events() click to toggle source
# File lib/oakdex/pokemon.rb, line 307
def growth_events
  if @battle_mode
    @attributes[:growth_events].select do |e|
      !e.instance_of?(Oakdex::Pokemon::GrowthEvents::Evolution)
    end
  else
    @attributes[:growth_events]
  end
end
initial_stat(stat) click to toggle source
# File lib/oakdex/pokemon.rb, line 324
def initial_stat(stat)
  Stat.initial_stat(stat,
                    level:      level,
                    nature:     nature,
                    iv:         @attributes[:iv],
                    ev:         @attributes[:ev],
                    base_stats: species.base_stats
                   )
end
nature() click to toggle source
# File lib/oakdex/pokemon.rb, line 334
def nature
  @nature ||= Oakdex::Pokedex::Nature.find!(@attributes[:nature_id])
end