class Gamefic::Action
Attributes
The proc to call when the action is executed
@return [Proc]
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>]
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
# File lib/gamefic/action.rb, line 83 def add_query q @specificity = nil queries.push q end
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
# File lib/gamefic/action.rb, line 79 def meta? @meta ||= false end
# File lib/gamefic/action.rb, line 10 def initialize actor, arguments @actor = actor @arguments = arguments @executed = false end
# File lib/gamefic/action.rb, line 92 def on_execute &block @executor = block end
# File lib/gamefic/action.rb, line 88 def queries @queries ||= [] end
@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
# File lib/gamefic/action.rb, line 96 def signature # @todo This is clearly unfinished "#{verb} #{queries.map {|m| m.signature}.join(', ')}" end
@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
# 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
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
True if the execute
method has been called for this action.
@return [Boolean]
# File lib/gamefic/action.rb, line 26 def executed? @executed end
True if the action is flagged as meta.
@return [Boolean]
# File lib/gamefic/action.rb, line 48 def meta? self.class.meta? end
# File lib/gamefic/action.rb, line 41 def rank self.class.rank end
# File lib/gamefic/action.rb, line 37 def signature self.class.signature end
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