Class: PfrpgCore::EffectProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/pfrpg_core/effect_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(character, effects) ⇒ EffectProcessor

Returns a new instance of EffectProcessor.



4
5
6
7
# File 'lib/pfrpg_core/effect_processor.rb', line 4

def initialize(character, effects)
  @character = character
  @effects = effects
end

Instance Attribute Details

#characterObject (readonly)

Returns the value of attribute character.



3
4
5
# File 'lib/pfrpg_core/effect_processor.rb', line 3

def character
  @character
end

#effectsObject (readonly)

Returns the value of attribute effects.



3
4
5
# File 'lib/pfrpg_core/effect_processor.rb', line 3

def effects
  @effects
end

Instance Method Details

#effects_hash(effects) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/pfrpg_core/effect_processor.rb', line 22

def effects_hash(effects)
  hash = {}
  effects.each do |effect|
    if hash[effect.key] == nil
      hash[effect.key] = []
    end
    hash[effect.key] << effect
  end
  hash
end

#find_max(effects) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/pfrpg_core/effect_processor.rb', line 33

def find_max(effects)
  max = nil
  effects.each do |e|
    if max == nil || is_larger(e.value, max.value)
      max = e
    end
  end
  return max
end

#is_larger(a, b) ⇒ Object



43
44
45
46
47
# File 'lib/pfrpg_core/effect_processor.rb', line 43

def is_larger(a, b)
  int_a = MathHelper.is_number(a) ? a.to_i : -999
  int_b = MathHelper.is_number(b) ? b.to_i : -999
  int_a > int_b
end

#process_effectsObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/pfrpg_core/effect_processor.rb', line 9

def process_effects
  final = []
  no_stacking = effects_hash(@effects)
  no_stacking.keys.each do |key|
    if PfrpgTables::Tables::Bonus.stackable?(key) || PfrpgTables::Tables::Bonus.special?(key)
      final << no_stacking[key]
    else
      final << find_max(no_stacking[key])
    end
  end
  return final.flatten
end