class Oakdex::Pokemon::EvolutionMatcher

Calculates if Pokemon can envolve and to which item, move_learned, trade, level_up are possible triggers

Constants

TRIGGERS

Public Class Methods

new(pokemon, trigger, options = {}) click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 8
def initialize(pokemon, trigger, options = {})
  @pokemon = pokemon
  @trigger = trigger
  @options = options
end

Public Instance Methods

evolution() click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 14
def evolution
  evolutions.sample
end

Private Instance Methods

attack_bigger_than_defense?() click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 93
def attack_bigger_than_defense?
  @pokemon.atk > @pokemon.def
end
attack_equal_defense?() click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 97
def attack_equal_defense?
  @pokemon.atk == @pokemon.def
end
available_evolutions() click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 26
def available_evolutions
  @pokemon.species.evolutions
end
conditions_match?(e) click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 71
def conditions_match?(e)
  (e['conditions'] || []).all? do |condition|
    method_name = condition.downcase.tr(' é', '_e')
      .gsub('>', 'bigger_than')
      .gsub('=', 'equal')
      .gsub('<', 'lower_than')
    if respond_to?("#{method_name}?", true)
      send("#{method_name}?")
    else
      false
    end
  end
end
currently_learned_move?(e) click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 62
def currently_learned_move?(e)
  e['move_learned'] && e['move_learned'] == @options[:move_id]
end
defense_bigger_than_defense?() click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 101
def defense_bigger_than_defense?
  @pokemon.def > @pokemon.atk
end
evolutions() click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 20
def evolutions
  available_evolutions.map do |e|
    e['to'] if trigger_for(e) == @trigger && valid_evolution?(e)
  end.compact
end
female?() click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 85
def female?
  @pokemon.gender == 'female'
end
happiness_match?(e) click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 49
def happiness_match?(e)
  !e['happiness'] || @pokemon.friendship >= 220
end
hold_item_match?(e) click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 41
def hold_item_match?(e)
  !e['hold_item'] || e['hold_item'] == @pokemon.item_id
end
item_match?(e) click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 45
def item_match?(e)
  !e['item'] || e['item'] == @options[:item_id]
end
level_match?(e) click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 53
def level_match?(e)
  !e['level'] || @pokemon.level >= e['level']
end
male?() click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 89
def male?
  @pokemon.gender == 'male'
end
move_learned_match?(e) click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 57
def move_learned_match?(e)
  !e['move_learned'] || currently_learned_move?(e) ||
    pokemon_learned_move_already?(e)
end
pokemon_learned_move_already?(e) click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 66
def pokemon_learned_move_already?(e)
  @trigger != 'move_learned' &&
    @pokemon.moves.map(&:name).include?(e['move_learned'])
end
random?() click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 105
def random?
  true
end
trigger_for(evolution) click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 30
def trigger_for(evolution)
  original_trigger = TRIGGERS.find { |t| evolution[t] }
  return 'level_up' if %w[level happiness].include?(original_trigger)
  original_trigger
end
valid_evolution?(e) click to toggle source
# File lib/oakdex/pokemon/evolution_matcher.rb, line 36
def valid_evolution?(e)
  hold_item_match?(e) && item_match?(e) && happiness_match?(e) &&
    level_match?(e) && move_learned_match?(e) && conditions_match?(e)
end