Class: Gamefic::Action
Class Attribute Summary collapse
-
.executor ⇒ Proc
readonly
The proc to call when the action is executed.
-
.verb ⇒ Object
readonly
Returns the value of attribute verb.
Instance Attribute Summary collapse
-
#arguments ⇒ Array<Object>
(also: #parameters)
readonly
An array of objects on which the action will operate, e.g., an entity that is a direct object of a command.
Class Method Summary collapse
- .add_query(q) ⇒ Object
-
.attempt(actor, tokens) ⇒ self?
Return an instance of this Action if the actor can execute it with the provided tokens, or nil if the tokens are invalid.
-
.hidden? ⇒ Boolean
True if this action is not intended to be performed directly by a character.
- .meta? ⇒ Boolean
- .on_execute(&block) ⇒ Object
- .queries ⇒ Object
- .rank ⇒ Integer
- .signature ⇒ Object
- .subclass(verb, *queries, meta: false, &block) ⇒ Class<Action>
- .valid?(actor, objects) ⇒ Boolean
Instance Method Summary collapse
-
#execute ⇒ Object
Perform the action.
-
#executed? ⇒ Boolean
True if the #execute method has been called for this action.
-
#initialize(actor, arguments) ⇒ Action
constructor
A new instance of Action.
-
#meta? ⇒ Boolean
True if the action is flagged as meta.
- #rank ⇒ Object
- #signature ⇒ Object
-
#verb ⇒ Symbol
The verb associated with this action.
Constructor Details
#initialize(actor, arguments) ⇒ Action
Returns a new instance of Action.
10 11 12 13 14 |
# File 'lib/gamefic/action.rb', line 10 def initialize actor, arguments @actor = actor @arguments = arguments @executed = false end |
Class Attribute Details
.executor ⇒ Proc (readonly)
The proc to call when the action is executed
77 78 79 |
# File 'lib/gamefic/action.rb', line 77 def executor @executor end |
.verb ⇒ Object
Returns the value of attribute verb.
72 73 74 |
# File 'lib/gamefic/action.rb', line 72 def verb @verb end |
Instance Attribute Details
Class Method Details
.add_query(q) ⇒ Object
83 84 85 86 |
# File 'lib/gamefic/action.rb', line 83 def add_query q @specificity = nil queries.push q end |
.attempt(actor, tokens) ⇒ self?
Return an instance of this Action if the actor can execute it with the provided tokens, or nil if the tokens are invalid.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/gamefic/action.rb', line 140 def attempt actor, tokens result = [] matches = Gamefic::Query::Matches.new([], '', '') queries.each_with_index do |p, i| return nil if tokens[i].nil? && matches.remaining == '' matches = p.resolve(actor, "#{matches.remaining} #{tokens[i]}".strip, continued: (i < queries.length - 1)) return nil if matches.objects.empty? accepted = matches.objects.select { |o| p.accept?(o) } return nil if accepted.empty? if p.ambiguous? result.push accepted else return nil if accepted.length != 1 result.push accepted.first end end new(actor, result) end |
.hidden? ⇒ Boolean
True if this action is not intended to be performed directly by a character. If the action is hidden, users should not be able to perform it with a direct command. By default, any action whose verb starts with an underscore is hidden.
108 109 110 |
# File 'lib/gamefic/action.rb', line 108 def hidden? verb.to_s.start_with?('_') end |
.meta? ⇒ Boolean
79 80 81 |
# File 'lib/gamefic/action.rb', line 79 def ||= false end |
.on_execute(&block) ⇒ Object
92 93 94 |
# File 'lib/gamefic/action.rb', line 92 def on_execute &block @executor = block end |
.queries ⇒ Object
88 89 90 |
# File 'lib/gamefic/action.rb', line 88 def queries @queries ||= [] end |
.rank ⇒ Integer
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/gamefic/action.rb', line 113 def rank if @rank.nil? @rank = 0 queries.each do |q| @rank += (q.rank + 1) end @rank -= 1000 if verb.nil? end @rank end |
.signature ⇒ Object
96 97 98 99 |
# File 'lib/gamefic/action.rb', line 96 def signature # @todo This is clearly unfinished "#{verb} #{queries.map {|m| m.signature}.join(', ')}" end |
.subclass(verb, *queries, meta: false, &block) ⇒ Class<Action>
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/gamefic/action.rb', line 56 def self.subclass verb, *queries, meta: false, &block act = Class.new(self) do self.verb = verb self. = queries.each do |q| add_query q end on_execute &block end if !block.nil? && act.queries.length + 1 != block.arity && block.arity > 0 raise ArgumentError.new("Number of parameters is not compatible with proc arguments") end act end |
.valid?(actor, objects) ⇒ Boolean
124 125 126 127 128 129 130 131 132 |
# File 'lib/gamefic/action.rb', line 124 def valid? actor, objects return false if objects.length != queries.length i = 0 queries.each do |p| return false unless p.include?(actor, objects[i]) i += 1 end true end |
Instance Method Details
#execute ⇒ Object
Perform the action.
18 19 20 21 |
# File 'lib/gamefic/action.rb', line 18 def execute @executed = true self.class.executor.call(@actor, *arguments) unless self.class.executor.nil? end |
#executed? ⇒ Boolean
True if the #execute method has been called for this action.
26 27 28 |
# File 'lib/gamefic/action.rb', line 26 def executed? @executed end |
#meta? ⇒ Boolean
True if the action is flagged as meta.
48 49 50 |
# File 'lib/gamefic/action.rb', line 48 def self.class. end |
#rank ⇒ Object
41 42 43 |
# File 'lib/gamefic/action.rb', line 41 def rank self.class.rank end |
#signature ⇒ Object
37 38 39 |
# File 'lib/gamefic/action.rb', line 37 def signature self.class.signature end |
#verb ⇒ Symbol
The verb associated with this action.
33 34 35 |
# File 'lib/gamefic/action.rb', line 33 def verb self.class.verb end |