class Gamefic::Action
The handler for executing a command response.
Attributes
@return [Actor]
@return [String, nil]
@return [Array<Match>]
@return [Response]
Public Class Methods
Source
# File lib/gamefic/action.rb, line 25 def initialize(actor, response, matches, input = nil) @actor = actor @response = response @matches = matches @input = input end
@param actor [Actor] @param response [Response] @param matches [Array<Match>] @param input [String, nil]
Source
# 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
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.
@param actions [Array<Action>] @return [Array<Action>]
Public Instance Methods
Source
# File lib/gamefic/action.rb, line 36 def command @command ||= Command.new(response.verb, matches.map(&:argument), response.meta?, input) end
Source
# File lib/gamefic/action.rb, line 48 def execute response.execute(actor, *arguments) self end
Source
# File lib/gamefic/action.rb, line 74 def precision response.precision end
The precision of the response.
@return [Integer]
Source
# File lib/gamefic/action.rb, line 67 def strictness matches.sum(0, &:strictness) end
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.
@return [Integer]
Source
# File lib/gamefic/action.rb, line 56 def substantiality arguments.that_are(Entity).length + (verb ? 1 : 0) end
The total substantiality of the action, based on how many of the arguments are concrete entities and whether the action has a verb.
Source
# File lib/gamefic/action.rb, line 78 def valid? response.accept?(actor, command) end