lib/gamefic/response.rb in gamefic-3.0.0 vs lib/gamefic/response.rb in gamefic-3.1.0
- old
+ new
@@ -1,6 +1,6 @@
-# frozen_literal_string: true
+# frozen_string_literal: true
module Gamefic
# A proc to be executed in response to a command that matches its verb and
# queries.
#
@@ -9,21 +9,21 @@
attr_reader :verb
# @return [Array<Query::Base>]
attr_reader :queries
- # @return [Proc]
- # attr_reader :block
+ # @return [Narrative]
+ attr_reader :narrative
# @param verb [Symbol]
- # @param stage [Object]
+ # @param narrative [Narrative]
# @param queries [Array<Query::Base>]
# @param meta [Boolean]
# @param block [Proc]
- def initialize verb, stage, *queries, meta: false, &block
+ def initialize verb, narrative, *queries, meta: false, &block
@verb = verb
- @stage = stage
+ @narrative = narrative
@queries = map_queryable_objects(queries)
@meta = meta
@block = block
end
@@ -46,38 +46,32 @@
# Return an Action if the Response can accept the actor's command.
#
# @param actor [Entity]
# @param command [Command]
- # @param with_hooks [Boolean]
# @return [Action, nil]
def attempt actor, command
- return nil if command.verb != verb
+ return nil unless accept?(actor, command)
- tokens = command.arguments.clone
- result = []
- remainder = ''
+ Action.new(actor, command.arguments, self)
+ end
- queries.each do |qd|
- token = tokens.shift
- txt = "#{remainder} #{token}".strip
- return nil if txt.empty?
+ # True if the Response can be executed for the given actor and command.
+ #
+ # @param actor [Active]
+ # @param command [Command]
+ def accept? actor, command
+ return false if command.verb != verb || command.arguments.length != queries.length
- response = qd.query(actor, txt)
- return nil if response.match.nil?
-
- result.push response.match
-
- remainder = response.remainder
+ queries.each_with_index do |query, idx|
+ return false unless query.accept?(actor, command.arguments[idx])
end
- return nil unless tokens.empty? && remainder.empty?
-
- Action.new(actor, result, self)
+ true
end
def execute *args
- Stage.run(@stage, *args, &@block)
+ Stage.run(narrative, *args, &@block)
end
def precision
@precision ||= calculate_precision
end