Class: Gamefic::Response

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(verb, narrative, *args, meta: false, &block) ⇒ Response

Returns a new instance of Response.

Parameters:

  • verb (Symbol)
  • narrative (Narrative)
  • args (Array<Object>)
  • meta (Boolean) (defaults to: false)
  • block (Proc)


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 = meta
  @callback = Callback.new(narrative, block)
end

Instance Attribute Details

#queriesArray<Query::Base, Query::Text> (readonly)



12
13
14
# File 'lib/gamefic/response.rb', line 12

def queries
  @queries
end

#verbSymbol (readonly)

Returns:

  • (Symbol)


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.

Parameters:

Returns:

  • (Boolean)


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.

Parameters:

Returns:



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

Returns:

  • (Boolean)


35
36
37
# File 'lib/gamefic/response.rb', line 35

def hidden?
  @hidden ||= verb.to_s.start_with?('_')
end

#inspectObject



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.

Returns:

  • (Boolean)


31
32
33
# File 'lib/gamefic/response.rb', line 31

def meta?
  @meta
end

#precisionObject



68
69
70
# File 'lib/gamefic/response.rb', line 68

def precision
  @precision ||= calculate_precision
end

#syntaxObject



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.

Parameters:

Returns:



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