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, stage, *queries, meta: false, &block) ⇒ Response

Returns a new instance of Response.

Parameters:

  • verb (Symbol)
  • stage (Object)
  • queries (Array<Query::Base>)
  • meta (Boolean) (defaults to: false)
  • block (Proc)


22
23
24
25
26
27
28
# File 'lib/gamefic/response.rb', line 22

def initialize verb, stage, *queries, meta: false, &block
  @verb = verb
  @stage = stage
  @queries = map_queryable_objects(queries)
  @meta = meta
  @block = block
end

Instance Attribute Details

#queriesArray<Query::Base> (readonly)

Returns:



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

#attempt(actor, command) ⇒ Action?

Return an Action if the Response can accept the actor’s command.

Parameters:

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gamefic/response.rb', line 53

def attempt actor, command
  return nil if command.verb != verb

  tokens = command.arguments.clone
  result = []
  remainder = ''

  queries.each do |qd|
    token = tokens.shift
    txt = "#{remainder} #{token}".strip
    return nil if txt.empty?

    response = qd.query(actor, txt)
    return nil if response.match.nil?

    result.push response.match

    remainder = response.remainder
  end

  return nil unless tokens.empty? && remainder.empty?

  Action.new(actor, result, self)
end

#execute(*args) ⇒ Object



78
79
80
# File 'lib/gamefic/response.rb', line 78

def execute *args
  Stage.run(@stage, *args, &@block)
end

#hidden?Boolean

Returns:

  • (Boolean)


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

def hidden?
  @hidden ||= verb.to_s.start_with?('_')
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)


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

def meta?
  @meta
end

#precisionObject



82
83
84
# File 'lib/gamefic/response.rb', line 82

def precision
  @precision ||= calculate_precision
end

#syntaxObject



43
44
45
# File 'lib/gamefic/response.rb', line 43

def syntax
  @syntax ||= generate_default_syntax
end