Module: Natural20::EntityStateEvaluator

Included in:
Entity
Defined in:
lib/natural_20/concerns/evaluator/entity_state_evaluator.rb

Instance Method Summary collapse

Instance Method Details

#apply_effect(expression, context = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/natural_20/concerns/evaluator/entity_state_evaluator.rb', line 47

def apply_effect(expression, context = {})
  action, value = expression.split(':')
  case action
  when 'status'
    {
      source: self,
      type: value.to_sym,
      battle: context[:battle]
    }
  end
end

#eval_if(conditions, context = {}) ⇒ Boolean

Safely evaluates a DSL to return a boolean expression

Parameters:

  • conditions (String)
  • context (Hash) (defaults to: {})

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/natural_20/concerns/evaluator/entity_state_evaluator.rb', line 7

def eval_if(conditions, context = {})
  or_groups = conditions.split('|')
  !!or_groups.detect do |g|
    and_groups = g.split('&')
    and_groups.detect do |and_g|
      cmd, test_expression = and_g.strip.split(':')
      invert = test_expression[0] == '!'
      test_expression = test_expression[1..test_expression.size - 1] if test_expression[0] == '!'
      result = case cmd.to_sym
               when :inventory
                 item_count(test_expression).positive?
               when :equipped
                 equipped_items.map(&:name).include?(test_expression.to_sym)
               when :object_type
                 context[:item_type].to_s.downcase == test_expression.to_s.downcase
               when :target
                 (test_expression == 'object' && context[:target].object?)
               when :entity
                 (test_expression == 'pc' && pc?) ||
                 (test_expression == 'npc' && npc?)
               when :state
                 case test_expression
                 when 'unconscious'
                   unconscious?
                 when 'stable'
                   stable?
                 when 'dead'
                   dead?
                 when 'conscious'
                   conscious?
                 end
               else
                 raise "Invalid expression #{cmd} #{test_expression}"
               end

      invert ? result : !result
    end.nil?
  end
end