oakdex-pokemon 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d56143befcd254ccac067dfdb99833ebdd08e865
4
- data.tar.gz: c001f5809aa83482bfc53faaac4f5a2058b12825
3
+ metadata.gz: ece893d782898db992873cd83ba8528c78e0bd21
4
+ data.tar.gz: a100e34a8228c83bd14bc49f593f8ac35af53d80
5
5
  SHA512:
6
- metadata.gz: 91c81951efe0bc65b3c238a50c855ce221fa5914ac33be1f85a6702745f9eb3a31d8d2bcec9d59c2bc9320900ab36dbcdc1b416daea359dcfdc2d86269af58b3
7
- data.tar.gz: 8d712bf555aba6ffcf69d08386348c8137a5d8e5803766fa22bcdaa4fa6562da636b12360f180010d7d7bb3c643036ee2ff8eda49ab7c9137fe15c80bce885a7
6
+ metadata.gz: 643a162e94ddaf55ef4e289892bcaae39f4a128348162ac85b1a62ae53cfaf7b3f5b0f4370f06c1f629923c720d8ed8ec4d3de2403320d6420e631524ced7392
7
+ data.tar.gz: 8fb593746fcee99c0a98c68939624e5c55382a80f745abece8903a90715e4333ca70e62148727e7c1622ef016341914229ba85412cb07f2c3eac1a7047a6f5f2
data/README.md CHANGED
@@ -15,9 +15,8 @@ pikachu = Oakdex::Pokemon.create('Pikachu', level: 12)
15
15
  bulbasaur = Oakdex::Pokemon.create('Bulbasaur', { # many options available
16
16
  exp: 120,
17
17
  gender: 'female',
18
- ability: 'Soundproof',
19
- nature: 'Bashful',
20
- item: 'Earth Plate',
18
+ ability_id: 'Soundproof',
19
+ nature_id: 'Bashful',
21
20
  hp: 2,
22
21
  iv: {
23
22
  hp: 8,
@@ -57,22 +56,88 @@ pikachu.change_hp_by(38)
57
56
  pikachu.current_hp # => 0
58
57
  pikachu.change_pp_by('Thunder Shock', -1)
59
58
  pikachu.moves.map { |p| "#{p.name} #{p.pp}" } # => ["Quick Attack 30", "Tail Whip 30", "Growl 40", "Thunder Shock 29"]
60
- ```
61
-
62
59
 
63
- ### Exp Gain Calculation
64
-
65
- ```ruby
66
- fainted = Oakdex::Pokemon.create('Pikachu', level: 10)
67
- winner = Oakdex::Pokemon.create('Bulbasaur', level: 12)
68
-
69
- Oakdex::Pokemon::ExperienceGainCalculator.calculate(fainted, winner) # => 269
70
- Oakdex::Pokemon::ExperienceGainCalculator.calculate(fainted, winner, flat: true) # => 225
71
- Oakdex::Pokemon::ExperienceGainCalculator.calculate(fainted, winner, winner_using_exp_share: true) # => 135
60
+ pikachu.exp # => 1728
61
+ pikachu.increment_level
62
+ pikachu.level # => 12
63
+ pikachu.exp # => 1728
64
+
65
+ # Pikachu learns Electro Ball in Level 13
66
+ while pikachu.growth_event? do
67
+ e = pikachu.growth_event
68
+ if e.read_only?
69
+ puts e.message
70
+ e.execute
71
+ else
72
+ puts e.message
73
+ puts e.possible_actions # => ['Forget Quick Attack', ..., 'Do not learn Electro Ball']
74
+ e.execute(e.possible_actions.sample)
75
+ end
76
+ end
77
+ pikachu.level # => 13
78
+ pikachu.exp # => 2197
79
+
80
+ # Calculate exp from won battles
81
+ fainted_opponent = bulbasaur
82
+ pikachu.gain_exp_from_battle(fainted_opponent, using_exp_share: false, flat: false)
83
+
84
+ # Evolution by level
85
+ charmander = Oakdex::Pokemon.create('Charmander', level: 15)
86
+ charmander.increment_level
87
+ # Charmander envolves to Charmeleon
88
+ while charmander.growth_event? do
89
+ e = charmander.growth_event
90
+ if e.read_only?
91
+ puts e.message
92
+ e.execute
93
+ else
94
+ puts e.message
95
+ puts e.possible_actions # => ['Continue', 'Skip']
96
+ e.execute(e.possible_actions.first)
97
+ end
98
+ end
99
+ charmander.level # => 16
100
+ charmander.name # => Charmeleon
101
+
102
+ # Evolution by trade
103
+ feebas = Oakdex::Pokemon.create('Feebas', level: 12, item_id: 'Prism Scale')
104
+ trainer = OpenStruct.new(name: 'My Awesome Trainer')
105
+ feebas.trade_to(trainer)
106
+ # Feebas envolves to Milotic
107
+ while feebas.growth_event? do
108
+ e = feebas.growth_event
109
+ if e.read_only?
110
+ puts e.message
111
+ e.execute
112
+ else
113
+ puts e.message
114
+ puts e.possible_actions # => ['Continue', 'Skip']
115
+ e.execute(e.possible_actions.first)
116
+ end
117
+ end
118
+ feebas.name # => Milotic
119
+ feebas.trainer # => trainer
120
+
121
+ # Evolution by item
122
+ exeggcute = Oakdex::Pokemon.create('Exeggcute', level: 12)
123
+ exeggcute.usable_item?('Leaf Stone') # => true
124
+ exeggcute.use_item('Leaf Stone')
125
+ # Exeggcute envolves to Exeggutor
126
+ while exeggcute.growth_event? do
127
+ e = exeggcute.growth_event
128
+ if e.read_only?
129
+ puts e.message
130
+ e.execute
131
+ else
132
+ puts e.message
133
+ puts e.possible_actions # => ['Continue', 'Skip']
134
+ e.execute(e.possible_actions.first)
135
+ end
136
+ end
137
+ exeggcute.name # => Exeggutor
72
138
  ```
73
139
 
74
140
 
75
-
76
141
  ## Contributing
77
142
 
78
143
  I would be happy if you want to add your contribution to the project. In order to contribute, you just have to fork this repository.
@@ -5,6 +5,8 @@ require 'oakdex/pokemon/stat'
5
5
  require 'oakdex/pokemon/move'
6
6
  require 'oakdex/pokemon/factory'
7
7
  require 'oakdex/pokemon/experience_gain_calculator'
8
+ require 'oakdex/pokemon/evolution_matcher'
9
+ require 'oakdex/pokemon/growth_events'
8
10
 
9
11
  module Oakdex
10
12
  # Represents detailed pokemon instance
@@ -13,19 +15,27 @@ module Oakdex
13
15
 
14
16
  BATTLE_STATS = %i[hp atk def sp_atk sp_def speed]
15
17
 
16
- def_delegators :@species, :types
18
+ def_delegators :species, :types
17
19
 
18
20
  attr_accessor :trainer
19
- attr_reader :species
20
21
 
21
22
  def self.create(species_name, options = {})
22
23
  species = Oakdex::Pokedex::Pokemon.find!(species_name)
23
24
  Factory.create(species, options)
24
25
  end
25
26
 
26
- def initialize(species, attributes = {})
27
- @species = species
27
+ def initialize(species_id, attributes = {})
28
+ @species_id = species_id
28
29
  @attributes = attributes
30
+ @attributes[:growth_events] ||= []
31
+ end
32
+
33
+ def species
34
+ @species ||= Oakdex::Pokedex::Pokemon.find!(@species_id)
35
+ end
36
+
37
+ def inspect
38
+ "#<#{self.class.name}:#{object_id} #{@attributes.inspect}>"
29
39
  end
30
40
 
31
41
  def primary_status_condition
@@ -37,13 +47,17 @@ module Oakdex
37
47
  end
38
48
 
39
49
  def name
40
- @species.names['en']
50
+ species.names['en']
41
51
  end
42
52
 
43
53
  def gender
44
54
  @attributes[:gender]
45
55
  end
46
56
 
57
+ def friendship
58
+ @attributes[:friendship]
59
+ end
60
+
47
61
  def moves
48
62
  @attributes[:moves]
49
63
  end
@@ -110,8 +124,12 @@ module Oakdex
110
124
  end
111
125
  end
112
126
 
127
+ def exp
128
+ @attributes[:exp]
129
+ end
130
+
113
131
  def level
114
- Stat.level_by_exp(@species.leveling_rate, @attributes[:exp])
132
+ Stat.level_by_exp(species.leveling_rate, @attributes[:exp])
115
133
  end
116
134
 
117
135
  BATTLE_STATS.each do |stat|
@@ -120,16 +138,111 @@ module Oakdex
120
138
  end
121
139
  end
122
140
 
141
+ def add_exp(exp_to_add)
142
+ @attributes[:exp] += exp_to_add
143
+ end
144
+
145
+ def learn_new_move(move_id, replaced_move_id = nil)
146
+ new_move = Move.create(move_id)
147
+ if replaced_move_id.nil?
148
+ @attributes[:moves] << new_move
149
+ else
150
+ index = @attributes[:moves]
151
+ .find_index { |m| m.name == replaced_move_id }
152
+ @attributes[:moves][index] = new_move if index
153
+ end
154
+ end
155
+
156
+ def gain_exp(gained_exp)
157
+ add_growth_event(GrowthEvents::GainedExp, gained_exp: gained_exp)
158
+ end
159
+
160
+ def trade_to(trainer)
161
+ self.trainer = trainer
162
+ available_evolution = EvolutionMatcher.new(self, 'trade').evolution
163
+
164
+ add_growth_event(GrowthEvents::Evolution,
165
+ evolution: available_evolution) if available_evolution
166
+ end
167
+
168
+ def usable_item?(item_id)
169
+ !evolution_by_item(item_id).nil?
170
+ end
171
+
172
+ def use_item(item_id)
173
+ return unless usable_item?(item_id)
174
+ add_growth_event(GrowthEvents::Evolution,
175
+ evolution: evolution_by_item(item_id))
176
+ end
177
+
178
+ def increment_level
179
+ gained_exp = exp_next_level - @attributes[:exp]
180
+ gain_exp(gained_exp)
181
+ end
182
+
183
+ def gain_exp_from_battle(fainted, options = {})
184
+ exp = ExperienceGainCalculator.calculate(fainted, self, options)
185
+ gain_exp(exp)
186
+ end
187
+
188
+ def growth_event?
189
+ !@attributes[:growth_events].empty?
190
+ end
191
+
192
+ def growth_event
193
+ @attributes[:growth_events].first
194
+ end
195
+
196
+ def remove_growth_event
197
+ @attributes[:growth_events].shift
198
+ end
199
+
200
+ def add_growth_event(klass, options = {})
201
+ evt = klass.new(self, options.select { |k, _| k != :after })
202
+ if options[:after]
203
+ index = @attributes[:growth_events].index(options[:after])
204
+ if index.nil?
205
+ @attributes[:growth_events] << evt
206
+ else
207
+ @attributes[:growth_events].insert(index + 1, evt)
208
+ end
209
+ else
210
+ @attributes[:growth_events] << evt
211
+ end
212
+
213
+ evt
214
+ end
215
+
216
+ def envolve_to(species_id)
217
+ old_max_hp = hp
218
+ @species = nil
219
+ @species_id = species_id
220
+ change_hp_by(hp - old_max_hp) unless fainted?
221
+ species
222
+ end
223
+
123
224
  private
124
225
 
226
+ def evolution_by_item(item_id)
227
+ EvolutionMatcher.new(self, 'item', item_id: item_id).evolution
228
+ end
229
+
125
230
  def initial_stat(stat)
126
231
  Stat.initial_stat(stat,
127
232
  level: level,
128
- nature: @attributes[:nature],
233
+ nature: nature,
129
234
  iv: @attributes[:iv],
130
235
  ev: @attributes[:ev],
131
- base_stats: @species.base_stats
236
+ base_stats: species.base_stats
132
237
  )
133
238
  end
239
+
240
+ def nature
241
+ @nature ||= Oakdex::Pokedex::Nature.find!(@attributes[:nature_id])
242
+ end
243
+
244
+ def exp_next_level
245
+ Stat.exp_by_level(species.leveling_rate, level + 1)
246
+ end
134
247
  end
135
248
  end
@@ -0,0 +1,110 @@
1
+ module Oakdex
2
+ class Pokemon
3
+ # Calculates if Pokemon can envolve and to which
4
+ # item, move_learned, trade, level_up are possible triggers
5
+ class EvolutionMatcher
6
+ TRIGGERS = %w[item trade level level_up move_learned happiness]
7
+
8
+ def initialize(pokemon, trigger, options = {})
9
+ @pokemon = pokemon
10
+ @trigger = trigger
11
+ @options = options
12
+ end
13
+
14
+ def evolution
15
+ evolutions.sample
16
+ end
17
+
18
+ private
19
+
20
+ def evolutions
21
+ available_evolutions.map do |e|
22
+ e['to'] if trigger_for(e) == @trigger && valid_evolution?(e)
23
+ end.compact
24
+ end
25
+
26
+ def available_evolutions
27
+ @pokemon.species.evolutions
28
+ end
29
+
30
+ def trigger_for(evolution)
31
+ original_trigger = TRIGGERS.find { |t| evolution[t] }
32
+ return 'level_up' if %w[level happiness].include?(original_trigger)
33
+ original_trigger
34
+ end
35
+
36
+ def valid_evolution?(e)
37
+ hold_item_match?(e) && item_match?(e) && happiness_match?(e) &&
38
+ level_match?(e) && move_learned_match?(e) && conditions_match?(e)
39
+ end
40
+
41
+ def hold_item_match?(e)
42
+ !e['hold_item'] || e['hold_item'] == @pokemon.item_id
43
+ end
44
+
45
+ def item_match?(e)
46
+ !e['item'] || e['item'] == @options[:item_id]
47
+ end
48
+
49
+ def happiness_match?(e)
50
+ !e['happiness'] || @pokemon.friendship >= 220
51
+ end
52
+
53
+ def level_match?(e)
54
+ !e['level'] || @pokemon.level >= e['level']
55
+ end
56
+
57
+ def move_learned_match?(e)
58
+ !e['move_learned'] || currently_learned_move?(e) ||
59
+ pokemon_learned_move_already?(e)
60
+ end
61
+
62
+ def currently_learned_move?(e)
63
+ e['move_learned'] && e['move_learned'] == @options[:move_id]
64
+ end
65
+
66
+ def pokemon_learned_move_already?(e)
67
+ @trigger != 'move_learned' &&
68
+ @pokemon.moves.map(&:name).include?(e['move_learned'])
69
+ end
70
+
71
+ def conditions_match?(e)
72
+ (e['conditions'] || []).all? do |condition|
73
+ method_name = condition.downcase.tr(' é', '_e')
74
+ .gsub('>', 'bigger_than')
75
+ .gsub('=', 'equal')
76
+ .gsub('<', 'lower_than')
77
+ if respond_to?("#{method_name}?", true)
78
+ send("#{method_name}?")
79
+ else
80
+ false
81
+ end
82
+ end
83
+ end
84
+
85
+ def female?
86
+ @pokemon.gender == 'female'
87
+ end
88
+
89
+ def male?
90
+ @pokemon.gender == 'male'
91
+ end
92
+
93
+ def attack_bigger_than_defense?
94
+ @pokemon.atk > @pokemon.def
95
+ end
96
+
97
+ def attack_equal_defense?
98
+ @pokemon.atk == @pokemon.def
99
+ end
100
+
101
+ def defense_bigger_than_defense?
102
+ @pokemon.def > @pokemon.atk
103
+ end
104
+
105
+ def random?
106
+ true
107
+ end
108
+ end
109
+ end
110
+ end
@@ -2,13 +2,14 @@ module Oakdex
2
2
  class Pokemon
3
3
  # Creates Pokemon instance and prefills attributes
4
4
  class Factory
5
- REQUIRED_ATTRIBUTES = %i[exp gender ability nature hp iv ev moves]
5
+ REQUIRED_ATTRIBUTES = %i[exp gender ability_id nature_id hp iv ev moves]
6
6
  OPTIONAL_ATTRIBUTES = %i[
7
7
  original_trainer
8
8
  primary_status_condition
9
9
  wild
10
10
  item_id
11
11
  amie
12
+ friendship
12
13
  ]
13
14
 
14
15
  class << self
@@ -18,7 +19,7 @@ module Oakdex
18
19
  OPTIONAL_ATTRIBUTES).map do |attr|
19
20
  [attr, factory.send(attr)]
20
21
  end]
21
- Pokemon.new(species, attributes)
22
+ Pokemon.new(species.names['en'], attributes)
22
23
  end
23
24
  end
24
25
 
@@ -41,6 +42,10 @@ module Oakdex
41
42
  @options[:wild]
42
43
  end
43
44
 
45
+ def friendship
46
+ @options[:friendship] || @species.base_friendship
47
+ end
48
+
44
49
  def item_id
45
50
  @options[:item_id]
46
51
  end
@@ -84,11 +89,11 @@ module Oakdex
84
89
  end.compact
85
90
  end
86
91
 
87
- def ability
88
- if @options['ability']
89
- Oakdex::Pokedex::Ability.find!(@options['ability'])
92
+ def ability_id
93
+ if @options[:ability_id]
94
+ @options[:ability_id]
90
95
  else
91
- Oakdex::Pokedex::Ability.find!(abilities.sample['name'])
96
+ abilities.sample['name']
92
97
  end
93
98
  end
94
99
 
@@ -146,12 +151,16 @@ module Oakdex
146
151
  end
147
152
  end
148
153
 
149
- def nature(options = {})
150
- @nature ||= if options[:nature]
151
- Oakdex::Pokedex::Nature.find!(options[:nature])
152
- else
153
- Oakdex::Pokedex::Nature.all.values.sample
154
- end
154
+ def nature_id
155
+ @nature_id ||= if @options[:nature_id]
156
+ @options[:nature_id]
157
+ else
158
+ Oakdex::Pokedex::Nature.all.values.sample.names['en']
159
+ end
160
+ end
161
+
162
+ def nature
163
+ @nature ||= Oakdex::Pokedex::Nature.find!(nature_id)
155
164
  end
156
165
  end
157
166
  end
@@ -0,0 +1,17 @@
1
+ module Oakdex
2
+ class Pokemon
3
+ # Represents Growth events namespace (Moves, Evolution etc.)
4
+ module GrowthEvents
5
+ end
6
+ end
7
+ end
8
+
9
+ require 'oakdex/pokemon/growth_events/base'
10
+ require 'oakdex/pokemon/growth_events/gained_exp'
11
+ require 'oakdex/pokemon/growth_events/learn_move'
12
+ require 'oakdex/pokemon/growth_events/level_up'
13
+ require 'oakdex/pokemon/growth_events/forgot_and_learned_move'
14
+ require 'oakdex/pokemon/growth_events/did_not_learn_move'
15
+ require 'oakdex/pokemon/growth_events/did_evolution'
16
+ require 'oakdex/pokemon/growth_events/skipped_evolution'
17
+ require 'oakdex/pokemon/growth_events/evolution'
@@ -0,0 +1,35 @@
1
+ require 'forwardable'
2
+
3
+ class Oakdex::Pokemon
4
+ module GrowthEvents
5
+ # Represents Base GrowthEvent
6
+ class Base
7
+ def initialize(pokemon, options = {})
8
+ @pokemon = pokemon
9
+ @options = options
10
+ end
11
+
12
+ def read_only?
13
+ possible_actions.empty?
14
+ end
15
+
16
+ def possible_actions
17
+ []
18
+ end
19
+
20
+ def message
21
+ raise 'implement me'
22
+ end
23
+
24
+ def execute(_action = nil)
25
+ remove_event
26
+ end
27
+
28
+ private
29
+
30
+ def remove_event
31
+ @pokemon.remove_growth_event
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ require 'forwardable'
2
+
3
+ class Oakdex::Pokemon
4
+ module GrowthEvents
5
+ # When pokemon envolved successfully
6
+ class DidEvolution < Base
7
+ def message
8
+ "#{@options[:original]} envolved into #{@pokemon.name}."
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'forwardable'
2
+
3
+ class Oakdex::Pokemon
4
+ module GrowthEvents
5
+ # When move was not learned
6
+ class DidNotLearnMove < Base
7
+ def message
8
+ "#{@pokemon.name} did not learn #{@options[:move_id]}."
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,31 @@
1
+ require 'forwardable'
2
+
3
+ class Oakdex::Pokemon
4
+ module GrowthEvents
5
+ # When pokemon could learn move
6
+ class Evolution < Base
7
+ def message
8
+ "#{@pokemon.name} wants to envolve to #{@options[:evolution]}."
9
+ end
10
+
11
+ def possible_actions
12
+ %w[Continue Skip]
13
+ end
14
+
15
+ def execute(action)
16
+ if action == 'Skip'
17
+ @pokemon.add_growth_event(GrowthEvents::SkippedEvolution,
18
+ after: self)
19
+ else
20
+ original = @pokemon.name
21
+ @pokemon.envolve_to(@options[:evolution])
22
+ @pokemon.add_growth_event(GrowthEvents::DidEvolution,
23
+ original: original,
24
+ after: self)
25
+ end
26
+
27
+ remove_event
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+ require 'forwardable'
2
+
3
+ class Oakdex::Pokemon
4
+ module GrowthEvents
5
+ # When move was learnt and other move was forgotten
6
+ class ForgotAndLearnedMove < Base
7
+ def message
8
+ "#{@pokemon.name} learned #{@options[:move_id]} and forgot #{@options[:forgot_move_id]}."
9
+ end
10
+
11
+ def execute
12
+ if available_evolution
13
+ @pokemon.add_growth_event(GrowthEvents::Evolution,
14
+ evolution: available_evolution,
15
+ after: self)
16
+ end
17
+ remove_event
18
+ end
19
+
20
+ private
21
+
22
+ def available_evolution
23
+ @available_evolution ||= Oakdex::Pokemon::EvolutionMatcher
24
+ .new(@pokemon, 'move_learned', move_id: @options[:move_id]).evolution
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ require 'forwardable'
2
+
3
+ class Oakdex::Pokemon
4
+ module GrowthEvents
5
+ # When pokemon gains exp
6
+ class GainedExp < Base
7
+ def message
8
+ "#{@pokemon.name} gained #{@options[:gained_exp]} EXP."
9
+ end
10
+
11
+ def execute
12
+ level_before = @pokemon.level
13
+ @pokemon.add_exp(@options[:gained_exp])
14
+ last_evt = self
15
+ ((level_before + 1)...(@pokemon.level + 1)).to_a.each do |new_level|
16
+ last_evt = @pokemon.add_growth_event(GrowthEvents::LevelUp,
17
+ new_level: new_level,
18
+ after: last_evt)
19
+ end
20
+ remove_event
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,66 @@
1
+ require 'forwardable'
2
+
3
+ class Oakdex::Pokemon
4
+ module GrowthEvents
5
+ # When pokemon could learn move
6
+ class LearnMove < Base
7
+ def message
8
+ if move_slot_left?
9
+ "#{@pokemon.name} learned #{@options[:move_id]}."
10
+ else
11
+ "#{@pokemon.name} wants to learn #{@options[:move_id]} but has already 4 moves."
12
+ end
13
+ end
14
+
15
+ def possible_actions
16
+ return [] if move_slot_left?
17
+ [not_learn_action] + unlearn_moves.keys
18
+ end
19
+
20
+ def execute(action = nil)
21
+ if move_slot_left?
22
+ @pokemon.learn_new_move(@options[:move_id])
23
+ if available_evolution
24
+ @pokemon.add_growth_event(GrowthEvents::Evolution,
25
+ evolution: available_evolution,
26
+ after: self)
27
+ end
28
+ else
29
+ if action == not_learn_action
30
+ @pokemon.add_growth_event(GrowthEvents::DidNotLearnMove,
31
+ move_id: @options[:move_id], after: self)
32
+ else
33
+ move_id = unlearn_moves[action]
34
+ @pokemon.learn_new_move(@options[:move_id], move_id)
35
+ @pokemon.add_growth_event(GrowthEvents::ForgotAndLearnedMove,
36
+ move_id: @options[:move_id],
37
+ forgot_move_id: move_id,
38
+ after: self)
39
+ end
40
+ end
41
+ remove_event
42
+ end
43
+
44
+ private
45
+
46
+ def available_evolution
47
+ @available_evolution ||= Oakdex::Pokemon::EvolutionMatcher
48
+ .new(@pokemon, 'move_learned', move_id: @options[:move_id]).evolution
49
+ end
50
+
51
+ def not_learn_action
52
+ "Do not learn #{@options[:move_id]}"
53
+ end
54
+
55
+ def unlearn_moves
56
+ @pokemon.moves.map do |move|
57
+ ["Forget #{move.name}", move.name]
58
+ end.to_h
59
+ end
60
+
61
+ def move_slot_left?
62
+ @pokemon.moves.size < 4
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,40 @@
1
+ require 'forwardable'
2
+
3
+ class Oakdex::Pokemon
4
+ module GrowthEvents
5
+ # When pokemon reached the next level
6
+ class LevelUp < Base
7
+ def message
8
+ "#{@pokemon.name} reached Level #{@options[:new_level]}."
9
+ end
10
+
11
+ def execute
12
+ last_evt = self
13
+ available_moves.each do |move_id|
14
+ last_evt = @pokemon.add_growth_event(GrowthEvents::LearnMove,
15
+ move_id: move_id,
16
+ after: last_evt)
17
+ end
18
+ if available_evolution
19
+ @pokemon.add_growth_event(GrowthEvents::Evolution,
20
+ evolution: available_evolution,
21
+ after: last_evt)
22
+ end
23
+ remove_event
24
+ end
25
+
26
+ private
27
+
28
+ def available_moves
29
+ @pokemon.species.learnset.map do |m|
30
+ m['move'] if m['level'] && m['level'] == @options[:new_level]
31
+ end.compact
32
+ end
33
+
34
+ def available_evolution
35
+ @available_evolution ||= Oakdex::Pokemon::EvolutionMatcher
36
+ .new(@pokemon, 'level_up').evolution
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,12 @@
1
+ require 'forwardable'
2
+
3
+ class Oakdex::Pokemon
4
+ module GrowthEvents
5
+ # When pokemon did not evolve
6
+ class SkippedEvolution < Base
7
+ def message
8
+ "#{@pokemon.name} did not envolve."
9
+ end
10
+ end
11
+ end
12
+ end
@@ -24,6 +24,17 @@ module Oakdex
24
24
  @max_pp = max_pp
25
25
  end
26
26
 
27
+ def inspect
28
+ fields = instance_variables.map do |name|
29
+ if name == :@move_type
30
+ "#{name}=#<Oakdex::Pokedex::Move #{@move_type.name}>"
31
+ else
32
+ "#{name}=#{instance_variable_get(name)}"
33
+ end
34
+ end
35
+ "#<#{self.class.name}:#{object_id} #{fields.join(', ')}>"
36
+ end
37
+
27
38
  def name
28
39
  @move_type.names['en']
29
40
  end
@@ -1,5 +1,5 @@
1
1
  module Oakdex
2
2
  class Pokemon
3
- VERSION = '0.0.4'
3
+ VERSION = '0.0.5'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oakdex-pokemon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jalyna Schroeder
@@ -33,8 +33,19 @@ files:
33
33
  - README.md
34
34
  - lib/oakdex.rb
35
35
  - lib/oakdex/pokemon.rb
36
+ - lib/oakdex/pokemon/evolution_matcher.rb
36
37
  - lib/oakdex/pokemon/experience_gain_calculator.rb
37
38
  - lib/oakdex/pokemon/factory.rb
39
+ - lib/oakdex/pokemon/growth_events.rb
40
+ - lib/oakdex/pokemon/growth_events/base.rb
41
+ - lib/oakdex/pokemon/growth_events/did_evolution.rb
42
+ - lib/oakdex/pokemon/growth_events/did_not_learn_move.rb
43
+ - lib/oakdex/pokemon/growth_events/evolution.rb
44
+ - lib/oakdex/pokemon/growth_events/forgot_and_learned_move.rb
45
+ - lib/oakdex/pokemon/growth_events/gained_exp.rb
46
+ - lib/oakdex/pokemon/growth_events/learn_move.rb
47
+ - lib/oakdex/pokemon/growth_events/level_up.rb
48
+ - lib/oakdex/pokemon/growth_events/skipped_evolution.rb
38
49
  - lib/oakdex/pokemon/move.rb
39
50
  - lib/oakdex/pokemon/stat.rb
40
51
  - lib/oakdex/pokemon/version.rb