Class: Gamefic::Response
- Inherits:
-
Object
- Object
- Gamefic::Response
- Defined in:
- lib/gamefic/response.rb
Overview
A proc to be executed in response to a command that matches its verb and queries.
Instance Attribute Summary collapse
- #queries ⇒ Array<Query::Base, Query::Text> readonly
- #verb ⇒ Symbol readonly
Instance Method Summary collapse
-
#accept?(actor, command) ⇒ Boolean
True if the Response can be executed for the given actor and command.
-
#attempt(actor, command) ⇒ Action?
Return an Action if the Response can accept the actor’s command.
- #execute(*args) ⇒ Object
- #hidden? ⇒ Boolean
-
#initialize(verb, narrative, *args, meta: false, &block) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ Object
-
#meta? ⇒ Boolean
The ‘meta?` flag is just a way for authors to identify responses that serve a purpose other than performing in-game actions.
- #precision ⇒ Object
- #syntax ⇒ Object
-
#to_command(actor, expression) ⇒ Command?
Turn an actor and an expression into a command by matching the expression’s tokens to queries.
Constructor Details
#initialize(verb, narrative, *args, meta: false, &block) ⇒ Response
Returns a new instance of Response.
19 20 21 22 23 24 |
# File 'lib/gamefic/response.rb', line 19 def initialize verb, narrative, *args, meta: false, &block @verb = verb @queries = map_queries(args, narrative) @meta = @callback = Callback.new(narrative, block) end |
Instance Attribute Details
#queries ⇒ Array<Query::Base, Query::Text> (readonly)
12 13 14 |
# File 'lib/gamefic/response.rb', line 12 def queries @queries end |
#verb ⇒ Symbol (readonly)
9 10 11 |
# File 'lib/gamefic/response.rb', line 9 def verb @verb end |
Instance Method Details
#accept?(actor, command) ⇒ Boolean
True if the Response can be executed for the given actor and command.
58 59 60 61 62 |
# File 'lib/gamefic/response.rb', line 58 def accept? actor, command command.verb == verb && command.arguments.length == queries.length && queries.zip(command.arguments).all? { |query, argument| query.accept?(actor, argument) } end |
#attempt(actor, command) ⇒ Action?
Return an Action if the Response can accept the actor’s command.
48 49 50 51 52 |
# File 'lib/gamefic/response.rb', line 48 def attempt actor, command return nil unless accept?(actor, command) Action.new(actor, command.arguments, self) end |
#execute(*args) ⇒ Object
64 65 66 |
# File 'lib/gamefic/response.rb', line 64 def execute *args @callback.run(*args) end |
#hidden? ⇒ Boolean
35 36 37 |
# File 'lib/gamefic/response.rb', line 35 def hidden? @hidden ||= verb.to_s.start_with?('_') end |
#inspect ⇒ Object
94 95 96 |
# File 'lib/gamefic/response.rb', line 94 def inspect "#<#{self.class} #{([verb] + queries).map(&:inspect).join(', ')}>" end |
#meta? ⇒ Boolean
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.
31 32 33 |
# File 'lib/gamefic/response.rb', line 31 def @meta end |
#precision ⇒ Object
68 69 70 |
# File 'lib/gamefic/response.rb', line 68 def precision @precision ||= calculate_precision end |
#syntax ⇒ Object
39 40 41 |
# File 'lib/gamefic/response.rb', line 39 def syntax @syntax ||= generate_default_syntax end |
#to_command(actor, expression) ⇒ Command?
Turn an actor and an expression into a command by matching the expression’s tokens to queries. Return nil if the expression could not be matched.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/gamefic/response.rb', line 79 def to_command actor, expression return log_and_discard unless expression.verb == verb && expression.tokens.length <= queries.length results = filter(actor, expression) return log_and_discard unless results Gamefic.logger.info "Accepted #{inspect}" Command.new( verb, results.map(&:match), results.sum(&:strictness), precision ) end |