class Gamefic::Response
A proc to be executed in response to a command that matches its verb and queries.
Attributes
@return [Proc]
@return [Array<Query::Base, Query::Text
>]
@return [Symbol]
Public Class Methods
Source
# File lib/gamefic/response.rb, line 24 def initialize verb, *queries, meta: false, &block @verb = verb&.to_sym @meta = meta @block = block @queries = map_queries(queries) end
@param verb [Symbol] @param queries [Array<Object>] @param meta [Boolean]
Public Instance Methods
Source
# File lib/gamefic/response.rb, line 48 def accept?(actor, command) command.verb == verb && command.arguments.length == queries.length && queries.zip(command.arguments).all? { |query, argument| query.accept?(actor, argument) } end
True if the Response
can be executed for the given actor and command.
@param actor [Active] @param command [Command]
Source
# File lib/gamefic/response.rb, line 76 def bind(narrative) clone.inject_binding narrative end
Source
# File lib/gamefic/response.rb, line 54 def execute *args Gamefic.logger.warn "Executing unbound response #{inspect}" unless bound? gamefic_binding.call(*args) end
Source
# File lib/gamefic/response.rb, line 68 def inspect "#<#{self.class} #{([verb] + queries).map(&:inspect).join(', ')}>" end
Source
# File lib/gamefic/response.rb, line 36 def meta? @meta end
The ‘meta?` flag is just a way for authors to identify responses that serve a purpose other than performing in-game actions. Out-of-game responses can include features like displaying help documentation or listing credits.
Source
# File lib/gamefic/response.rb, line 64 def precision @precision ||= calculate_precision end
The total precision of all the response’s queries.
@note Precision is decreased if the response has a nil verb.
@return [Integer]
Source
# File lib/gamefic/response.rb, line 40 def syntax @syntax ||= generate_default_syntax end
Protected Instance Methods
Source
# File lib/gamefic/response.rb, line 82 def inject_binding(narrative) @queries = map_queries(narrative.unproxy(@queries)) @gamefic_binding = Binding.new(narrative, @block) self end
Private Instance Methods
Source
# File lib/gamefic/response.rb, line 101 def calculate_precision total = queries.sum(&:precision) total -= 1000 unless verb total end
@return [Integer]
Source
# File lib/gamefic/response.rb, line 90 def gamefic_binding @gamefic_binding ||= Binding.new(nil, @block) end
Source
# File lib/gamefic/response.rb, line 94 def generate_default_syntax args = queries.length.times.map { |num| num.zero? ? ':var' : ":var#{num + 1}" } tmpl = "#{verb} #{args.join(' ')}".strip Syntax.new(tmpl, tmpl) end
Source
# File lib/gamefic/response.rb, line 107 def map_queries(args) args.map { |arg| select_query(arg) } end
Source
# File lib/gamefic/response.rb, line 111 def select_query(arg) case arg when Entity, Class, Module, Proc, Proxy::Base available(arg) when String, Regexp plaintext(arg) when Query::Base arg else raise ArgumentError, "invalid argument in response: #{arg.inspect}" end end