Class: Oakdex::Battle

Inherits:
Object
  • Object
show all
Defined in:
lib/oakdex/battle.rb,
lib/oakdex/battle/side.rb,
lib/oakdex/battle/turn.rb,
lib/oakdex/battle/action.rb,
lib/oakdex/battle/damage.rb,
lib/oakdex/battle/trainer.rb,
lib/oakdex/battle/version.rb,
lib/oakdex/battle/move_execution.rb,
lib/oakdex/battle/in_battle_pokemon.rb,
lib/oakdex/battle/status_conditions.rb,
lib/oakdex/battle/valid_action_service.rb,
lib/oakdex/battle/status_conditions/base.rb,
lib/oakdex/battle/status_conditions/burn.rb,
lib/oakdex/battle/status_conditions/sleep.rb,
lib/oakdex/battle/active_in_battle_pokemon.rb,
lib/oakdex/battle/status_conditions/freeze.rb,
lib/oakdex/battle/status_conditions/poison.rb,
lib/oakdex/battle/status_conditions/paralysis.rb,
lib/oakdex/battle/status_conditions/non_volatile.rb,
lib/oakdex/battle/status_conditions/badly_poisoned.rb

Overview

Represents battle, with has n turns and m sides

Defined Under Namespace

Modules: StatusConditions Classes: Action, ActiveInBattlePokemon, Damage, InBattlePokemon, MoveExecution, Side, Trainer, Turn, ValidActionService

Constant Summary collapse

VERSION =
'0.2.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(team1, team2, options = {}) ⇒ Battle

Returns a new instance of Battle.



19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



16
17
18
# File 'lib/oakdex/battle.rb', line 16

def actions
  @actions
end

#current_logObject (readonly)

Returns the value of attribute current_log.



16
17
18
# File 'lib/oakdex/battle.rb', line 16

def current_log
  @current_log
end

#logObject (readonly)

Returns the value of attribute log.



16
17
18
# File 'lib/oakdex/battle.rb', line 16

def log
  @log
end

#sidesObject (readonly)

Returns the value of attribute sides.



16
17
18
# File 'lib/oakdex/battle.rb', line 16

def sides
  @sides
end

#team1Object (readonly)

Returns the value of attribute team1.



16
17
18
# File 'lib/oakdex/battle.rb', line 16

def team1
  @team1
end

#team2Object (readonly)

Returns the value of attribute team2.



16
17
18
# File 'lib/oakdex/battle.rb', line 16

def team2
  @team2
end

Instance Method Details

#add_action(trainer, action) ⇒ Object



41
42
43
44
45
# 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) ⇒ Object



69
70
71
# File 'lib/oakdex/battle.rb', line 69

def add_to_log(*args)
  @current_log << args.to_a
end

#continueObject



53
54
55
56
57
58
# 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?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/oakdex/battle.rb', line 60

def finished?
  !fainted_sides.empty?
end

#pokemon_by_id(id) ⇒ Object



85
86
87
88
89
90
91
92
93
# 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_sideObject



33
34
35
# File 'lib/oakdex/battle.rb', line 33

def pokemon_per_side
  @options[:pokemon_per_side] || @team1.size
end

#remove_faintedObject



73
74
75
# File 'lib/oakdex/battle.rb', line 73

def remove_fainted
  sides.each(&:remove_fainted)
end

#side_by_id(id) ⇒ Object



77
78
79
# File 'lib/oakdex/battle.rb', line 77

def side_by_id(id)
  sides.find { |s| s.id == id }
end

#simulate_action(trainer) ⇒ Object



47
48
49
50
51
# 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_hObject



95
96
97
98
99
100
101
# File 'lib/oakdex/battle.rb', line 95

def to_h
  {
    finished: finished?,
    pokemon_per_side: pokemon_per_side,
    sides: sides.map(&:to_h)
  }
end

#trainersObject



81
82
83
# File 'lib/oakdex/battle.rb', line 81

def trainers
  sides.flat_map(&:trainers)
end

#valid_actions_for(trainer) ⇒ Object



37
38
39
# File 'lib/oakdex/battle.rb', line 37

def valid_actions_for(trainer)
  valid_action_service.valid_actions_for(trainer)
end

#winnerObject



64
65
66
67
# File 'lib/oakdex/battle.rb', line 64

def winner
  return if fainted_sides.empty?
  (sides - fainted_sides).flat_map(&:trainers)
end