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

Returns a new instance of Response.

Parameters:



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

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

Instance Attribute Details

#narrativeNarrative (readonly)

Returns:



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

def narrative
  @narrative
end

#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

#accept?(actor, command) ⇒ Boolean

True if the Response can be executed for the given actor and command.

Parameters:

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
# File 'lib/gamefic/response.rb', line 62

def accept? actor, command
  return false if command.verb != verb || command.arguments.length != queries.length

  queries.each_with_index do |query, idx|
    return false unless query.accept?(actor, command.arguments[idx])
  end

  true
end

#attempt(actor, command) ⇒ Action?

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

Parameters:

Returns:



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

def attempt actor, command
  return nil unless accept?(actor, command)

  Action.new(actor, command.arguments, self)
end

#execute(*args) ⇒ Object



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

def execute *args
  Stage.run(narrative, *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



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

def precision
  @precision ||= calculate_precision
end

#syntaxObject



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

def syntax
  @syntax ||= generate_default_syntax
end