class Gamefic::Action

Attributes

executor[R]

The proc to call when the action is executed

@return [Proc]

meta[W]
verb[RW]
arguments[R]

An array of objects on which the action will operate, e.g., an entity that is a direct object of a command.

@return [Array<Object>]

parameters[R]

An array of objects on which the action will operate, e.g., an entity that is a direct object of a command.

@return [Array<Object>]

Public Class Methods

add_query(q) click to toggle source
# File lib/gamefic/action.rb, line 83
def add_query q
  @specificity = nil
  queries.push q
end
attempt(actor, command) click to toggle source

Return an instance of this Action if the actor can execute it with the provided tokens, or nil if the tokens are invalid.

@param action [Gamefic::Entity] @param command [Command] @return [self, nil]

# File lib/gamefic/action.rb, line 138
def attempt actor, command
  return nil if command.verb != verb
  tokens = command.arguments
  result = []
  matches = Gamefic::Query::Matches.new([], '', '')
  queries.each_with_index do |p, i|
    return nil if tokens[i].nil? && matches.remaining == ''
    matches = p.resolve(actor, "#{matches.remaining} #{tokens[i]}".strip, continued: (i < queries.length - 1))
    return nil if matches.objects.empty?
    accepted = matches.objects.select { |o| p.accept?(o) }
    return nil if accepted.empty?
    if p.ambiguous?
      result.push accepted
    else
      return nil if accepted.length != 1
      result.push accepted.first
    end
  end
  new(actor, result)
end
hidden?() click to toggle source

True if this action is not intended to be performed directly by a character. If the action is hidden, users should not be able to perform it with a direct command. By default, any action whose verb starts with an underscore is hidden.

@return [Boolean]

# File lib/gamefic/action.rb, line 108
def hidden?
  verb.to_s.start_with?('_')
end
meta?() click to toggle source
# File lib/gamefic/action.rb, line 79
def meta?
  @meta ||= false
end
new(actor, arguments) click to toggle source
# File lib/gamefic/action.rb, line 10
def initialize actor, arguments
  @actor = actor
  @arguments = arguments
  @executed = false
end
on_execute(&block) click to toggle source
# File lib/gamefic/action.rb, line 92
def on_execute &block
  @executor = block
end
queries() click to toggle source
# File lib/gamefic/action.rb, line 88
def queries
  @queries ||= []
end
rank() click to toggle source

@return [Integer]

# File lib/gamefic/action.rb, line 113
def rank
  if @rank.nil?
    @rank = 0
    queries.each do |q|
      @rank += (q.rank + 1)
    end
    @rank -= 1000 if verb.nil?
  end
  @rank
end
signature() click to toggle source
# File lib/gamefic/action.rb, line 96
def signature
  # @todo This is clearly unfinished
  "#{verb} #{queries.map {|m| m.signature}.join(', ')}"
end
subclass(verb, *queries, meta: false, &block) click to toggle source

@param verb [Symbol] @param queries [Array<Gamefic::Query::Base>] @param meta [Boolean] @return [Class<Action>]

# File lib/gamefic/action.rb, line 56
def self.subclass verb, *queries, meta: false, &block
  act = Class.new(self) do
    self.verb = verb
    self.meta = meta
    queries.each do |q|
      add_query q
    end
    on_execute &block
  end
  if !block.nil? && act.queries.length + 1 != block.arity && block.arity > 0
    raise ArgumentError.new("Number of parameters is not compatible with proc arguments")
  end
  act
end
valid?(actor, objects) click to toggle source
# File lib/gamefic/action.rb, line 124
def valid? actor, objects
  return false if objects.length != queries.length
  queries.each_with_index do |p, i|
    return false unless p.include?(actor, objects[i])
  end
  true
end

Public Instance Methods

execute() click to toggle source

Perform the action.

# File lib/gamefic/action.rb, line 18
def execute
  @executed = true
  self.class.executor.call(@actor, *arguments) unless self.class.executor.nil?
end
executed?() click to toggle source

True if the execute method has been called for this action.

@return [Boolean]

# File lib/gamefic/action.rb, line 26
def executed?
  @executed
end
meta?() click to toggle source

True if the action is flagged as meta.

@return [Boolean]

# File lib/gamefic/action.rb, line 48
def meta?
  self.class.meta?
end
rank() click to toggle source
# File lib/gamefic/action.rb, line 41
def rank
  self.class.rank
end
signature() click to toggle source
# File lib/gamefic/action.rb, line 37
def signature
  self.class.signature
end
verb() click to toggle source

The verb associated with this action.

@return [Symbol] The symbol representing the verb

# File lib/gamefic/action.rb, line 33
def verb
  self.class.verb
end