Class: Gamefic::Action

Inherits:
Object
  • Object
show all
Includes:
Scriptable::Queries
Defined in:
lib/gamefic/action.rb

Overview

The handler for executing a command response.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Scriptable::Queries

#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.

Parameters:



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

#actorActor (readonly)

Returns:



10
11
12
# File 'lib/gamefic/action.rb', line 10

def actor
  @actor
end

#inputString? (readonly)

Returns:



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

def input
  @input
end

#matchesArray<Match> (readonly)

Returns:



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

def matches
  @matches
end

#responseResponse (readonly)

Returns:



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

def response
  @response
end

Class Method Details

.sort(actions) ⇒ Array<Action>

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.

Parameters:

Returns:



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

#argumentsObject



44
45
46
# File 'lib/gamefic/action.rb', line 44

def arguments
  matches.map(&:argument)
end

#commandObject



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

def command
  @command ||= Command.new(response.verb, matches.map(&:argument), response.meta?, input)
end

#executeObject



48
49
50
51
# File 'lib/gamefic/action.rb', line 48

def execute
  response.execute(actor, *arguments)
  self
end

#invalid?Boolean

Returns:

  • (Boolean)


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

def invalid?
  !valid?
end

#meta?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/gamefic/action.rb', line 86

def meta?
  response.meta?
end

#precisionInteger

The precision of the response.

Returns:

  • (Integer)


74
75
76
# File 'lib/gamefic/action.rb', line 74

def precision
  response.precision
end

#queriesObject



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

def queries
  response.queries
end

#strictnessInteger

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.

Returns:

  • (Integer)


67
68
69
# File 'lib/gamefic/action.rb', line 67

def strictness
  matches.sum(0, &:strictness)
end

#substantialityObject

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

Returns:

  • (Boolean)


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

def valid?
  response.accept?(actor, command)
end

#verbObject



32
33
34
# File 'lib/gamefic/action.rb', line 32

def verb
  response.verb
end