class Oakdex::Battle

Represents battle, with has n turns and m sides

Constants

VERSION

Attributes

actions[R]
current_log[R]
log[R]
sides[R]
team1[R]
team2[R]

Public Class Methods

new(team1, team2, options = {}) click to toggle source
# File lib/oakdex/battle.rb, line 19
def initialize(team1, team2, options = {})
  @team1 = team1.is_a?(Array) ? team1 : [team1]
  @team2 = team2.is_a?(Array) ? team2 : [team2]
  @options = options
  @sides = []
  @log = []
  @current_log = []
  @actions = []
  @turns = []
  @sides = [@team1, @team2].map do |team|
    Side.new(self, team)
  end
end

Public Instance Methods

add_action(trainer, action) click to toggle source
# File lib/oakdex/battle.rb, line 41
def add_action(trainer, action)
  return false unless valid_actions_for(trainer).include?(action)
  @actions << Action.new(trainer, action)
  true
end
add_to_log(*args) click to toggle source
# File lib/oakdex/battle.rb, line 69
def add_to_log(*args)
  @current_log << args.to_a
end
continue() click to toggle source
# File lib/oakdex/battle.rb, line 53
def continue
  return start if @log.empty?
  return false unless trainers.all? { |t| valid_actions_for(t).empty? }
  execute_actions
  true
end
finished?() click to toggle source
# File lib/oakdex/battle.rb, line 60
def finished?
  !fainted_sides.empty?
end
pokemon_by_id(id) click to toggle source
# File lib/oakdex/battle.rb, line 85
def pokemon_by_id(id)
  trainers.each do |trainer|
    trainer.team.each do |p|
      return p if p.id == id
    end
  end

  nil
end
pokemon_per_side() click to toggle source
# File lib/oakdex/battle.rb, line 33
def pokemon_per_side
  @options[:pokemon_per_side] || @team1.size
end
remove_fainted() click to toggle source
# File lib/oakdex/battle.rb, line 73
def remove_fainted
  sides.each(&:remove_fainted)
end
side_by_id(id) click to toggle source
# File lib/oakdex/battle.rb, line 77
def side_by_id(id)
  sides.find { |s| s.id == id }
end
simulate_action(trainer) click to toggle source
# File lib/oakdex/battle.rb, line 47
def simulate_action(trainer)
  valid_actions = valid_actions_for(trainer)
  return false if valid_actions.empty?
  add_action(trainer, valid_actions.sample)
end
to_h() click to toggle source
# File lib/oakdex/battle.rb, line 95
def to_h
  {
    finished: finished?,
    pokemon_per_side: pokemon_per_side,
    sides: sides.map(&:to_h)
  }
end
trainers() click to toggle source
# File lib/oakdex/battle.rb, line 81
def trainers
  sides.flat_map(&:trainers)
end
valid_actions_for(trainer) click to toggle source
# File lib/oakdex/battle.rb, line 37
def valid_actions_for(trainer)
  valid_action_service.valid_actions_for(trainer)
end
winner() click to toggle source
# File lib/oakdex/battle.rb, line 64
def winner
  return if fainted_sides.empty?
  (sides - fainted_sides).flat_map(&:trainers)
end

Private Instance Methods

execute_actions() click to toggle source
# File lib/oakdex/battle.rb, line 119
def execute_actions
  @turns << Turn.new(self, @actions).tap(&:execute)
  finish_turn
end
fainted_sides() click to toggle source
# File lib/oakdex/battle.rb, line 109
def fainted_sides
  sides.select(&:fainted?)
end
finish_turn() click to toggle source
# File lib/oakdex/battle.rb, line 124
def finish_turn
  @log << @current_log
  @current_log = []
  @actions = []
end
start() click to toggle source
# File lib/oakdex/battle.rb, line 113
def start
  sides.each(&:send_to_battle)
  finish_turn
  true
end
valid_action_service() click to toggle source
# File lib/oakdex/battle.rb, line 105
def valid_action_service
  @valid_action_service ||= ValidActionService.new(self)
end