Module: Gamefic::Director::Parser

Defined in:
lib/gamefic/director/parser.rb

Class Method Summary collapse

Class Method Details

.from_string(actor, command) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gamefic/director/parser.rb', line 36

def self.from_string(actor, command)
  options = []
  if command.to_s == ''
    return options
  end
  matches = Syntax.tokenize(command, actor.playbook.syntaxes)
  matches.each { |match|
    actions = actor.playbook.actions_for(match.verb)
    actions.each { |action|
      options.concat bind_contexts_in_result(actor, match.arguments, action)
    }
  }
  options.sort{ |a,b| b.action.specificity <=> a.action.specificity }
end

.from_tokens(actor, tokens) ⇒ Object



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
# File 'lib/gamefic/director/parser.rb', line 7

def self.from_tokens(actor, tokens)
  options = []
  command = tokens.shift
  actions = actor.playbook.actions_for(command.to_sym)
  actions.each { |action|
    if action.queries.length == tokens.length
      valid = true
      index = 0
      action.queries.each { |query|
        if query.validate(actor, tokens[index]) == false
          valid = false
          break
        end
        index += 1
      }
      if valid
        arguments = []
        tokens.each { |token|
            arguments.push [token]
        }
        options.push Order.new(actor, action, arguments)
      end
    end
  }
  if options.length == 0
    tokens.unshift command
  end
  options.sort{ |a,b| b.action.specificity <=> a.action.specificity }
end