Class: Gamefic::Action
Overview
The handler for executing a command response.
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#available, #children, #descendants, #extended, #global, #integer, #myself, #parent, #plaintext, #siblings
Constructor Details
#initialize(actor, response, matches, input = nil) ⇒ Action
Returns a new instance of Action.
25
26
27
28
29
30
|
# File 'lib/gamefic/action.rb', line 25
def initialize(actor, response, matches, input = nil)
@actor = actor
@response = response
@matches = matches
@input = input
end
|
Instance Attribute Details
10
11
12
|
# File 'lib/gamefic/action.rb', line 10
def actor
@actor
end
|
19
20
21
|
# File 'lib/gamefic/action.rb', line 19
def input
@input
end
|
16
17
18
|
# File 'lib/gamefic/action.rb', line 16
def matches
@matches
end
|
13
14
15
|
# File 'lib/gamefic/action.rb', line 13
def response
@response
end
|
Class Method Details
Sort an array of actions in the order in which a Dispatcher should attempt to execute them.
Order is determined by the actions’ substantiality, strictness, and precision. In the event of a tie, the most recently defined action has higher priority.
99
100
101
102
103
|
# File 'lib/gamefic/action.rb', line 99
def self.sort(actions)
actions.sort_by.with_index do |action, idx|
[-action.substantiality, -action.strictness, -action.precision, idx]
end
end
|
Instance Method Details
#arguments ⇒ Object
44
45
46
|
# File 'lib/gamefic/action.rb', line 44
def arguments
matches.map(&:argument)
end
|
#command ⇒ Object
36
37
38
|
# File 'lib/gamefic/action.rb', line 36
def command
@command ||= Command.new(response.verb, matches.map(&:argument), response.meta?, input)
end
|
#execute ⇒ Object
48
49
50
51
|
# File 'lib/gamefic/action.rb', line 48
def execute
response.execute(actor, *arguments)
self
end
|
#invalid? ⇒ Boolean
82
83
84
|
# File 'lib/gamefic/action.rb', line 82
def invalid?
!valid?
end
|
86
87
88
|
# File 'lib/gamefic/action.rb', line 86
def meta?
response.meta?
end
|
#precision ⇒ Integer
The precision of the response.
74
75
76
|
# File 'lib/gamefic/action.rb', line 74
def precision
response.precision
end
|
#queries ⇒ Object
40
41
42
|
# File 'lib/gamefic/action.rb', line 40
def queries
response.queries
end
|
#strictness ⇒ Integer
The total strictness of all the matches.
The higher the strictness, the more precisely the tokens from the user input match the arguments. For example, if the user is interacting with a pencil, the command TAKE PENCIL is stricter than TAKE PEN.
67
68
69
|
# File 'lib/gamefic/action.rb', line 67
def strictness
matches.sum(0, &:strictness)
end
|
#substantiality ⇒ Object
The total substantiality of the action, based on how many of the arguments are concrete entities and whether the action has a verb.
56
57
58
|
# File 'lib/gamefic/action.rb', line 56
def substantiality
arguments.that_are(Entity).length + (verb ? 1 : 0)
end
|
#valid? ⇒ Boolean
78
79
80
|
# File 'lib/gamefic/action.rb', line 78
def valid?
response.accept?(actor, command)
end
|
#verb ⇒ Object
32
33
34
|
# File 'lib/gamefic/action.rb', line 32
def verb
response.verb
end
|