Class: Gamefic::Response

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

Methods included from Scriptable::Queries

#available, #children, #descendants, #extended, #global, #integer, #myself, #parent, #plaintext, #siblings

Constructor Details

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

Returns a new instance of Response.

Parameters:

  • verb (Symbol)
  • queries (Array<Object>)
  • meta (Boolean) (defaults to: false)


24
25
26
27
28
29
# 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

Instance Attribute Details

#blockProc (readonly)

Returns:

  • (Proc)


19
20
21
# File 'lib/gamefic/response.rb', line 19

def block
  @block
end

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



16
17
18
# File 'lib/gamefic/response.rb', line 16

def queries
  @queries
end

#verbSymbol (readonly)

Returns:

  • (Symbol)


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

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)


48
49
50
51
52
# 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

#bind(narrative) ⇒ Object



76
77
78
# File 'lib/gamefic/response.rb', line 76

def bind(narrative)
  clone.inject_binding narrative
end

#bound?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/gamefic/response.rb', line 72

def bound?
  !!gamefic_binding.narrative
end

#execute(*args) ⇒ Object



54
55
56
57
# File 'lib/gamefic/response.rb', line 54

def execute *args
  Gamefic.logger.warn "Executing unbound response #{inspect}" unless bound?
  gamefic_binding.call(*args)
end

#inspectObject



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

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)


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

def meta?
  @meta
end

#precisionInteger

Note:

Precision is decreased if the response has a nil verb.

The total precision of all the response’s queries.

Returns:

  • (Integer)


64
65
66
# File 'lib/gamefic/response.rb', line 64

def precision
  @precision ||= calculate_precision
end

#syntaxObject



40
41
42
# File 'lib/gamefic/response.rb', line 40

def syntax
  @syntax ||= generate_default_syntax
end