Class: Gamefic::Action
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) ⇒ Object
-
.executor ⇒ Proc
The proc to call when the action is executed.
-
.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 ⇒ Object
- .signature ⇒ Object
- .subclass(verb, *q, meta: false, &block) ⇒ Object
- .valid?(actor, objects) ⇒ Boolean
- .verb ⇒ Object
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
15 16 17 18 19 |
# File 'lib/gamefic/action.rb', line 15 def initialize actor, arguments @actor = actor @arguments = arguments @executed = false end |
Instance Attribute Details
Class Method Details
.add_query(q) ⇒ Object
81 82 83 84 |
# File 'lib/gamefic/action.rb', line 81 def add_query q @specificity = nil queries.push q end |
.attempt(actor, tokens) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/gamefic/action.rb', line 138 def attempt actor, tokens i = 0 result = [] matches = Gamefic::Query::Matches.new([], '', '') queries.each { |p| return nil if tokens[i].nil? and 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 i += 1 } self.new(actor, result) end |
.executor ⇒ Proc
The proc to call when the action is executed
113 114 115 |
# File 'lib/gamefic/action.rb', line 113 def executor @executor 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.
106 107 108 |
# File 'lib/gamefic/action.rb', line 106 def hidden? verb.to_s.start_with?('_') end |
.meta? ⇒ Boolean
77 78 79 |
# File 'lib/gamefic/action.rb', line 77 def ||= false end |
.on_execute(&block) ⇒ Object
90 91 92 |
# File 'lib/gamefic/action.rb', line 90 def on_execute &block @executor = block end |
.queries ⇒ Object
86 87 88 |
# File 'lib/gamefic/action.rb', line 86 def queries @queries ||= [] end |
.rank ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/gamefic/action.rb', line 117 def rank if @rank.nil? @rank = 0 queries.each { |q| @rank += (q.rank + 1) } @rank -= 1000 if verb.nil? end @rank end |
.signature ⇒ Object
94 95 96 97 |
# File 'lib/gamefic/action.rb', line 94 def signature # @todo This is clearly unfinished "#{verb} #{queries.map{|m| m.signature}.join(', ')}" end |
.subclass(verb, *q, meta: false, &block) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/gamefic/action.rb', line 57 def self.subclass verb, *q, meta: false, &block act = Class.new(self) do self.verb = verb self. = q.each { |q| add_query q } on_execute &block end if !block.nil? and act.queries.length + 1 != block.arity and block.arity > 0 raise ActionArgumentError.new("Number of parameters is not compatible with proc arguments") end act end |
.valid?(actor, objects) ⇒ Boolean
128 129 130 131 132 133 134 135 136 |
# File 'lib/gamefic/action.rb', line 128 def valid? actor, objects return false if objects.length != queries.length i = 0 queries.each { |p| return false unless p.include?(actor, objects[i]) i += 1 } true end |
.verb ⇒ Object
73 74 75 |
# File 'lib/gamefic/action.rb', line 73 def verb @verb end |
Instance Method Details
#execute ⇒ Object
Perform the action.
23 24 25 26 |
# File 'lib/gamefic/action.rb', line 23 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.
31 32 33 |
# File 'lib/gamefic/action.rb', line 31 def executed? @executed end |
#meta? ⇒ Boolean
True if the action is flagged as meta.
53 54 55 |
# File 'lib/gamefic/action.rb', line 53 def self.class. end |
#rank ⇒ Object
46 47 48 |
# File 'lib/gamefic/action.rb', line 46 def rank self.class.rank end |
#signature ⇒ Object
42 43 44 |
# File 'lib/gamefic/action.rb', line 42 def signature self.class.signature end |
#verb ⇒ Symbol
The verb associated with this action.
38 39 40 |
# File 'lib/gamefic/action.rb', line 38 def verb self.class.verb end |