class Gamefic::Command

A concrete representation of an input as a verb and an array of arguments.

Attributes

arguments[R]

@return [Array<Array<Entity>, Entity, String>]

precision[R]

@return [Integer]

strictness[R]

@return [Integer]

verb[R]

@return [Symbol]

Public Class Methods

compose(actor, input) click to toggle source

Compose a command from input.

@param actor [Actor] @param input [String] @return [Command]

# File lib/gamefic/command.rb, line 47
def compose actor, input
  expressions = Syntax.tokenize(input, actor.epic.syntaxes)
  expressions.flat_map { |expression| expression_to_commands(actor, expression) }
             .first || Command.new(nil, [])
end
new(verb, arguments, strictness = 0, precision = 0) click to toggle source

@param verb [Symbol] @param arguments [Array<Array<Entity>, Entity, String>] @param strictness [Integer] @param precision [Integer]

@todo Consider making strictness and precision required or providing

another generator
# File lib/gamefic/command.rb, line 26
def initialize verb, arguments, strictness = 0, precision = 0
  @verb = verb
  @arguments = arguments
  @strictness = strictness
  @precision = precision
end

Private Class Methods

expression_to_commands(actor, expression) click to toggle source

@param actor [Actor] @param expression [Expression] @return [Array<Command>]

# File lib/gamefic/command.rb, line 58
def expression_to_commands actor, expression
  Gamefic.logger.info "Evaluating #{expression.inspect}"
  actor.epic
       .responses_for(expression.verb)
       .map { |response| response.to_command(actor, expression) }
       .compact
       .sort_by.with_index { |result, idx| [-result.substantiality, -result.strictness, -result.precision, idx] }
end

Public Instance Methods

inspect() click to toggle source
# File lib/gamefic/command.rb, line 37
def inspect
  "#<#{self.class} #{([verb] + arguments).map(&:inspect).join(', ')}>"
end
substantiality() click to toggle source
# File lib/gamefic/command.rb, line 33
def substantiality
  @substantiality ||= arguments.that_are(Entity).length + (verb ? 1 : 0)
end