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
- #actor ⇒ Gamefic::Actor readonly
-
#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, command, with_callbacks = false) ⇒ 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
-
#cancel ⇒ Object
Cancel an action.
-
#execute ⇒ Object
Perform the action.
-
#executed? ⇒ Boolean
True if the #execute method has been called for this action.
-
#initialize(actor, arguments, with_callbacks = false) ⇒ 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, with_callbacks = false) ⇒ Action
Returns a new instance of Action.
15 16 17 18 19 20 |
# File 'lib/gamefic/action.rb', line 15 def initialize actor, arguments, with_callbacks = false @actor = actor @arguments = arguments @executed = false @with_callbacks = with_callbacks end |
Class Attribute Details
.executor ⇒ Proc (readonly)
The proc to call when the action is executed
113 114 115 |
# File 'lib/gamefic/action.rb', line 113 def executor @executor end |
.verb ⇒ Object
Returns the value of attribute verb.
108 109 110 |
# File 'lib/gamefic/action.rb', line 108 def verb @verb end |
Instance Attribute Details
#actor ⇒ Gamefic::Actor (readonly)
4 5 6 |
# File 'lib/gamefic/action.rb', line 4 def actor @actor end |
Class Method Details
.add_query(q) ⇒ Object
119 120 121 122 |
# File 'lib/gamefic/action.rb', line 119 def add_query q @specificity = nil queries.push q end |
.attempt(actor, command, with_callbacks = false) ⇒ self?
Return an instance of this Action if the actor can execute it with the provided tokens, or nil if the tokens are invalid.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/gamefic/action.rb', line 174 def attempt actor, command, with_callbacks = false return nil if command.verb != verb tokens = command.arguments 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, with_callbacks) 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.
144 145 146 |
# File 'lib/gamefic/action.rb', line 144 def hidden? verb.to_s.start_with?('_') end |
.meta? ⇒ Boolean
115 116 117 |
# File 'lib/gamefic/action.rb', line 115 def @meta ||= false end |
.on_execute(&block) ⇒ Object
128 129 130 |
# File 'lib/gamefic/action.rb', line 128 def on_execute &block @executor = block end |
.queries ⇒ Object
124 125 126 |
# File 'lib/gamefic/action.rb', line 124 def queries @queries ||= [] end |
.rank ⇒ Integer
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/gamefic/action.rb', line 149 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
132 133 134 135 |
# File 'lib/gamefic/action.rb', line 132 def signature # @todo This is clearly unfinished "#{verb} #{queries.map {|m| m.signature}.join(', ')}" end |
.subclass(verb, *queries, meta: false, &block) ⇒ Class<Action>
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/gamefic/action.rb', line 73 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
160 161 162 163 164 165 166 |
# File 'lib/gamefic/action.rb', line 160 def valid? actor, objects return false if objects.length != queries.length queries.each_with_index do |p, i| return false unless p.include?(actor, objects[i]) end true end |
Instance Method Details
#cancel ⇒ Object
Cancel an action. This method can be called in a before_action hook to prevent subsequent hooks and the action itself from being executed.
36 37 38 |
# File 'lib/gamefic/action.rb', line 36 def cancel @cancelled = true end |
#execute ⇒ Object
Perform the action.
24 25 26 27 28 29 30 31 |
# File 'lib/gamefic/action.rb', line 24 def execute return if @cancelled run_before_actions return if @cancelled self.class.executor.call(@actor, *arguments) unless self.class.executor.nil? @executed = true run_after_actions end |
#executed? ⇒ Boolean
True if the #execute method has been called for this action.
43 44 45 |
# File 'lib/gamefic/action.rb', line 43 def executed? @executed end |
#meta? ⇒ Boolean
True if the action is flagged as meta.
65 66 67 |
# File 'lib/gamefic/action.rb', line 65 def self.class. end |
#rank ⇒ Object
58 59 60 |
# File 'lib/gamefic/action.rb', line 58 def rank self.class.rank end |
#signature ⇒ Object
54 55 56 |
# File 'lib/gamefic/action.rb', line 54 def signature self.class.signature end |
#verb ⇒ Symbol
The verb associated with this action.
50 51 52 |
# File 'lib/gamefic/action.rb', line 50 def verb self.class.verb end |