oakdex-battle 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb6705359f3c133ea6fe2ed3402b0afa848b008c3d546cc2f5ccb641c3967415
4
- data.tar.gz: 94556920e80e6b1d273ded0be30fcc0058ebbf2ccd0d663049ba06b941e96c15
3
+ metadata.gz: 5dc5066190a2d5b69fbdd414ec99dbd63f6c9239153406d2ca4ab7ebcb055b22
4
+ data.tar.gz: 99b756dc78622714fbc2824e839a5f2ea68d8a51bd3e9b512b0c6f496787e8f6
5
5
  SHA512:
6
- metadata.gz: c456d5bdccf1f8fca681f8438137c4814c1ee4aa0a023c80feab4178e458110a2df0a84895db08d5e44ed11177f4b65afb556776385f9b8c40ea114f6b628883
7
- data.tar.gz: 6240ad289a2b5d6ac010d620bcf10535610118f94dbe7af8aed8d89134780043c7291f9dc976c31fd4d08fbb736cdfbc5479bc2b7917381c72b8dd47fecf4251
6
+ metadata.gz: 0abf78e77d33d9f64d8eb13e5ca06f4c3f17a21f70988db1c546a98a2777fcf66bd12e13391b830ce3a4aae4d35a377736efaabfcfd30a285f7337767550ac32
7
+ data.tar.gz: ce718361d1313ab36cf382d778d750f09bdc98604ffa4456445a27b35d7c1f7ee77b964b02cb17fd19f619a61a3604e260c8519aa408744db067943a101d9e51
data/README.md CHANGED
@@ -50,11 +50,11 @@ battle.log.size # => 1
50
50
  battle.log.last # => [['sends_to_battle', 'Ash', 'Pikachu'], ['sends_to_battle', 'Misty', 'Bulbasaur']]
51
51
  battle.arena # => Snapshot of current state as Hash
52
52
  battle.finished? # => false
53
- battle.valid_actions_for(trainer1) # => [{ action: 'move', pokemon: pok1, move: <Move>, target: pok2 }, ...]
53
+ valid_actions = battle.valid_actions_for(trainer1) # => [{"action"=>"move", "pokemon"=>"eb55b984-f0f0-48e5-9268-5bbf791a0793", "move"=>"Nuzzle", "target"=>["Misty", 0]}, {"action"=>"move", "pokemon"=>"eb55b984-f0f0-48e5-9268-5bbf791a0793", "move"=>"Hypnosis", "target"=>["Misty", 0]}, {"action"=>"recall", "pokemon"=>0, "target"=>"ceea31e4-adfc-4138-a148-424fa08c8f79"}, ...]
54
54
 
55
- battle.add_action(trainer1, { action: 'move', pokemon: pok1, move: <Move>, target: pok2 }) # => true
55
+ battle.add_action(trainer1, valid_actions.first) # => true
56
56
 
57
- battle.add_action(trainer1, { action: 'move', pokemon: pok1, move: <Move>, target: pok2 }) # => false
57
+ battle.add_action(trainer1, invalid_action) # => false
58
58
 
59
59
  battle.valid_actions_for(trainer1) # => []
60
60
  battle.continue # => false
data/lib/oakdex/battle.rb CHANGED
@@ -25,16 +25,15 @@ module Oakdex
25
25
  @current_log = []
26
26
  @actions = []
27
27
  @turns = []
28
+ @sides = [@team1, @team2].map do |team|
29
+ Side.new(self, team)
30
+ end
28
31
  end
29
32
 
30
33
  def pokemon_per_side
31
34
  @options[:pokemon_per_side] || @team1.size
32
35
  end
33
36
 
34
- def arena
35
- { sides: sides }
36
- end
37
-
38
37
  def valid_actions_for(trainer)
39
38
  valid_action_service.valid_actions_for(trainer)
40
39
  end
@@ -52,7 +51,7 @@ module Oakdex
52
51
  end
53
52
 
54
53
  def continue
55
- return start if sides.empty?
54
+ return start if @log.empty?
56
55
  return false unless trainers.all? { |t| valid_actions_for(t).empty? }
57
56
  execute_actions
58
57
  true
@@ -75,6 +74,24 @@ module Oakdex
75
74
  sides.each(&:remove_fainted)
76
75
  end
77
76
 
77
+ def side_by_id(id)
78
+ sides.find { |s| s.id == id }
79
+ end
80
+
81
+ def trainers
82
+ sides.flat_map(&:trainers)
83
+ end
84
+
85
+ def pokemon_by_id(id)
86
+ trainers.each do |trainer|
87
+ trainer.team.each do |p|
88
+ return p if p.id == id
89
+ end
90
+ end
91
+
92
+ nil
93
+ end
94
+
78
95
  private
79
96
 
80
97
  def valid_action_service
@@ -86,9 +103,7 @@ module Oakdex
86
103
  end
87
104
 
88
105
  def start
89
- @sides = [@team1, @team2].map do |team|
90
- Side.new(self, team).tap(&:send_to_battle)
91
- end
106
+ sides.each(&:send_to_battle)
92
107
  finish_turn
93
108
  true
94
109
  end
@@ -98,10 +113,6 @@ module Oakdex
98
113
  finish_turn
99
114
  end
100
115
 
101
- def trainers
102
- sides.flat_map(&:trainers)
103
- end
104
-
105
116
  def finish_turn
106
117
  @log << @current_log
107
118
  @current_log = []
@@ -11,7 +11,8 @@ module Oakdex
11
11
 
12
12
  def_delegators :@turn, :battle
13
13
 
14
- attr_reader :trainer, :damage, :turn
14
+ attr_reader :trainer, :damage
15
+ attr_accessor :turn
15
16
 
16
17
  def initialize(trainer, attributes)
17
18
  @trainer = trainer
@@ -24,23 +25,33 @@ module Oakdex
24
25
 
25
26
  def pokemon
26
27
  return pokemon_by_team_position if item?
27
- recall? ? pokemon_by_position : @attributes[:pokemon]
28
+ recall? ? pokemon_by_position : battle.pokemon_by_id(pokemon_id)
29
+ end
30
+
31
+ def pokemon_id
32
+ move? ? @attributes['pokemon'] : nil
28
33
  end
29
34
 
30
35
  def pokemon_position
31
- recall? ? @attributes[:pokemon] : nil
36
+ recall? ? @attributes['pokemon'] : nil
32
37
  end
33
38
 
34
39
  def target
35
- recall? ? @attributes[:target] : targets
40
+ recall? ? battle.pokemon_by_id(@attributes['target']) : targets
41
+ end
42
+
43
+ def target_id
44
+ recall? ? @attributes['target'] : nil
36
45
  end
37
46
 
38
47
  def type
39
- @attributes[:action]
48
+ @attributes['action']
40
49
  end
41
50
 
42
51
  def move
43
- @attributes[:move]
52
+ return unless @attributes['move']
53
+ @move ||= pokemon.moves.find { |m| m.name == @attributes['move'] }
54
+ @move ||= Oakdex::Pokemon::Move.create(@attributes['move'])
44
55
  end
45
56
 
46
57
  def hitting_probability
@@ -52,8 +63,7 @@ module Oakdex
52
63
  @hitting == 1
53
64
  end
54
65
 
55
- def execute(turn)
56
- @turn = turn
66
+ def execute
57
67
  return execute_growth if growth?
58
68
  return execute_recall if recall?
59
69
  return execute_use_item if item?
@@ -61,19 +71,20 @@ module Oakdex
61
71
  end
62
72
 
63
73
  def item_id
64
- @attributes[:item_id]
74
+ @attributes['item_id']
65
75
  end
66
76
 
67
77
  private
68
78
 
69
79
  def targets
70
80
  target_list.map do |target|
71
- target_by_position(target[0], target[1])
81
+ side = battle.side_by_id(target[0])
82
+ target_by_position(side, target[1])
72
83
  end.compact
73
84
  end
74
85
 
75
86
  def target_list
76
- list = @attributes[:target]
87
+ list = @attributes['target']
77
88
  return [] if (list || []).empty?
78
89
  list = [list] unless list[0].is_a?(Array)
79
90
  list
@@ -83,6 +94,10 @@ module Oakdex
83
94
  type == 'recall'
84
95
  end
85
96
 
97
+ def move?
98
+ type == 'move'
99
+ end
100
+
86
101
  def item?
87
102
  type == 'use_item_on_pokemon'
88
103
  end
@@ -93,11 +108,11 @@ module Oakdex
93
108
 
94
109
  def pokemon_by_position
95
110
  trainer.active_in_battle_pokemon
96
- .find { |ibp| ibp.position == @attributes[:pokemon] }&.pokemon
111
+ .find { |ibp| ibp.position == @attributes['pokemon'] }&.pokemon
97
112
  end
98
113
 
99
114
  def pokemon_by_team_position
100
- trainer.team[@attributes[:pokemon_team_pos]]
115
+ trainer.team[@attributes['pokemon_team_pos']]
101
116
  end
102
117
 
103
118
  def target_by_position(side, position)
@@ -120,11 +135,11 @@ module Oakdex
120
135
  end
121
136
 
122
137
  def item_actions
123
- @attributes[:item_actions]
138
+ @attributes['item_actions']
124
139
  end
125
140
 
126
141
  def execute_growth
127
- trainer.growth_event.execute(@attributes[:option])
142
+ trainer.growth_event.execute(@attributes['option'])
128
143
  while trainer.growth_event? && trainer.growth_event.read_only?
129
144
  e = trainer.growth_event
130
145
  add_log e.message
@@ -6,7 +6,7 @@ module Oakdex
6
6
  class ActiveInBattlePokemon
7
7
  extend Forwardable
8
8
 
9
- def_delegators :@pokemon, :moves_with_pp, :fainted?
9
+ def_delegators :@pokemon, :moves_with_pp, :fainted?, :id
10
10
  def_delegators :@side, :battle
11
11
 
12
12
  attr_reader :pokemon, :position, :side
@@ -18,7 +18,7 @@ module Oakdex
18
18
  end
19
19
 
20
20
  def action_added?
21
- actions.any? { |a| a.pokemon == pokemon }
21
+ actions.any? { |a| a.pokemon_id == id }
22
22
  end
23
23
 
24
24
  def valid_move_actions
@@ -28,10 +28,10 @@ module Oakdex
28
28
  moves.flat_map do |move|
29
29
  targets_in_battle(move).map do |target|
30
30
  {
31
- action: 'move',
32
- pokemon: pokemon,
33
- move: move,
34
- target: target
31
+ 'action' => 'move',
32
+ 'pokemon' => pokemon.id,
33
+ 'move' => move.name,
34
+ 'target' => target
35
35
  }
36
36
  end
37
37
  end
@@ -50,12 +50,16 @@ module Oakdex
50
50
  end
51
51
 
52
52
  def targets_in_battle?(targets)
53
- targets.any? { |target| target[0].pokemon_in_battle?(target[1]) }
53
+ targets.any? do |target|
54
+ side = battle.side_by_id(target[0])
55
+ side.pokemon_in_battle?(target[1])
56
+ end
54
57
  end
55
58
 
56
59
  def target_in_battle?(target)
57
- target[0].pokemon_in_battle?(target[1]) ||
58
- (!target[0].pokemon_left? && target[1] == 0)
60
+ side = battle.side_by_id(target[0])
61
+ side.pokemon_in_battle?(target[1]) ||
62
+ (!side.pokemon_left? && target[1] == 0)
59
63
  end
60
64
 
61
65
  def struggle_move
@@ -106,30 +110,30 @@ module Oakdex
106
110
  end
107
111
 
108
112
  def self_target
109
- [@side, position]
113
+ [@side.id, position]
110
114
  end
111
115
 
112
116
  def adjacent_foes
113
117
  [
114
- [other_side, position - 1],
115
- [other_side, position],
116
- [other_side, position + 1]
118
+ [other_side.id, position - 1],
119
+ [other_side.id, position],
120
+ [other_side.id, position + 1]
117
121
  ].select { |t| t[1] >= 0 && t[1] < pokemon_per_side }
118
122
  end
119
123
 
120
124
  def adjacent_users
121
125
  [
122
- [@side, position - 1],
123
- [@side, position + 1]
126
+ [@side.id, position - 1],
127
+ [@side.id, position + 1]
124
128
  ].select { |t| t[1] >= 0 && t[1] < pokemon_per_side }
125
129
  end
126
130
 
127
131
  def all_users
128
- pokemon_per_side.times.map { |i| [@side, i] }
132
+ pokemon_per_side.times.map { |i| [@side.id, i] }
129
133
  end
130
134
 
131
135
  def all_foes
132
- pokemon_per_side.times.map { |i| [other_side, i] }
136
+ pokemon_per_side.times.map { |i| [other_side.id, i] }
133
137
  end
134
138
 
135
139
  def pokemon_per_side
@@ -1,5 +1,6 @@
1
1
  require 'forwardable'
2
2
  require 'oakdex/battle/status_conditions'
3
+ require 'securerandom'
3
4
 
4
5
  module Oakdex
5
6
  class Battle
@@ -115,6 +116,10 @@ module Oakdex
115
116
  stage(:critical_hit)
116
117
  end
117
118
 
119
+ def id
120
+ @id ||= SecureRandom.uuid
121
+ end
122
+
118
123
  Oakdex::Pokemon::BATTLE_STATS.each do |stat|
119
124
  define_method stat do
120
125
  (@pokemon.public_send(stat) * stage(stat) *
@@ -55,6 +55,10 @@ module Oakdex
55
55
  @trainers.all?(&:fainted?)
56
56
  end
57
57
 
58
+ def id
59
+ @id ||= trainers.map(&:name).join(',')
60
+ end
61
+
58
62
  private
59
63
 
60
64
  def pokemon_per_trainer
@@ -17,10 +17,11 @@ module Oakdex
17
17
  def execute
18
18
  execute_status_conditions(:before_turn)
19
19
 
20
+ @actions.each { |a| a.turn = self }
20
21
  ordered_actions.each do |action|
21
22
  next unless valid_target?(action)
22
23
  next if action.pokemon && action.pokemon.fainted?
23
- action.execute(self)
24
+ action.execute
24
25
  end
25
26
 
26
27
  execute_status_conditions(:after_turn)
@@ -50,8 +50,8 @@ module Oakdex
50
50
  if trainer.growth_event?
51
51
  trainer.growth_event.possible_actions.map do |option|
52
52
  {
53
- action: 'growth_event',
54
- option: option
53
+ 'action' => 'growth_event',
54
+ 'option' => option
55
55
  }
56
56
  end
57
57
  else
@@ -78,10 +78,10 @@ module Oakdex
78
78
  next unless pokemon.usable_item?(item_id, in_battle: true)
79
79
  possible_item_actions(pokemon, item_id).map do |item_actions|
80
80
  {
81
- action: 'use_item_on_pokemon',
82
- pokemon_team_pos: i,
83
- item_id: item_id,
84
- item_actions: item_actions
81
+ 'action' => 'use_item_on_pokemon',
82
+ 'pokemon_team_pos' => i,
83
+ 'item_id' => item_id,
84
+ 'item_actions' => item_actions
85
85
  }
86
86
  end
87
87
  end.compact
@@ -118,9 +118,9 @@ module Oakdex
118
118
  return if !recall_action_valid?(trainer, active_ibp, target) ||
119
119
  recall_action_for?(target)
120
120
  {
121
- action: 'recall',
122
- pokemon: active_ibp&.position || side(trainer).next_position,
123
- target: target
121
+ 'action' => 'recall',
122
+ 'pokemon' => active_ibp&.position || side(trainer).next_position,
123
+ 'target' => target.id
124
124
  }
125
125
  end
126
126
 
@@ -139,7 +139,7 @@ module Oakdex
139
139
 
140
140
  def recall_action_for?(target)
141
141
  actions.any? do |action|
142
- action.type == 'recall' && action.target == target
142
+ action.type == 'recall' && action.target_id == target.id
143
143
  end
144
144
  end
145
145
 
@@ -1,5 +1,5 @@
1
1
  module Oakdex
2
2
  class Battle
3
- VERSION = '0.1.1'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oakdex-battle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jalyna Schroeder
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-24 00:00:00.000000000 Z
11
+ date: 2019-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oakdex-pokemon