Class: Gamefic::Action
- Inherits:
-
Object
- Object
- Gamefic::Action
- Defined in:
- lib/gamefic/action.rb
Instance Attribute Summary collapse
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Class Method Summary collapse
- .add_query(q) ⇒ Object
- .attempt(actor, tokens) ⇒ Object
- .executor ⇒ Object
- .hidden? ⇒ Boolean
- .meta? ⇒ Boolean
- .on_execute(&block) ⇒ Object
- .order_key ⇒ Object
- .queries ⇒ Object
- .rank ⇒ Object
- .signature ⇒ Object
- .subclass(verb, *q, meta: false, order_key: 0, &block) ⇒ Object
- .valid?(actor, objects) ⇒ Boolean
- .verb ⇒ Object
Instance Method Summary collapse
- #arguments ⇒ Object
- #execute ⇒ Object
- #executed? ⇒ Boolean
-
#initialize(actor, parameters) ⇒ Action
constructor
A new instance of Action.
- #meta? ⇒ Boolean
- #order_key ⇒ Object
- #rank ⇒ Object
- #signature ⇒ Object
- #verb ⇒ Object
Constructor Details
#initialize(actor, parameters) ⇒ Action
Returns a new instance of Action.
10 11 12 13 14 |
# File 'lib/gamefic/action.rb', line 10 def initialize actor, parameters @actor = actor @parameters = parameters @executed = false end |
Instance Attribute Details
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
8 9 10 |
# File 'lib/gamefic/action.rb', line 8 def parameters @parameters end |
Class Method Details
.add_query(q) ⇒ Object
75 76 77 78 |
# File 'lib/gamefic/action.rb', line 75 def add_query q @specificity = nil queries.push q end |
.attempt(actor, tokens) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/gamefic/action.rb', line 126 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? if p.ambiguous? result.push matches.objects else return nil if matches.objects.length > 1 result.push matches.objects[0] end i += 1 } self.new(actor, result) end |
.executor ⇒ Object
97 98 99 |
# File 'lib/gamefic/action.rb', line 97 def executor @executor end |
.hidden? ⇒ Boolean
93 94 95 |
# File 'lib/gamefic/action.rb', line 93 def hidden? verb.to_s.start_with?('_') end |
.meta? ⇒ Boolean
71 72 73 |
# File 'lib/gamefic/action.rb', line 71 def ||= false end |
.on_execute(&block) ⇒ Object
84 85 86 |
# File 'lib/gamefic/action.rb', line 84 def on_execute &block @executor = block end |
.order_key ⇒ Object
101 102 103 |
# File 'lib/gamefic/action.rb', line 101 def order_key @order_key ||= 0 end |
.queries ⇒ Object
80 81 82 |
# File 'lib/gamefic/action.rb', line 80 def queries @queries ||= [] end |
.rank ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/gamefic/action.rb', line 105 def rank if @rank.nil? @rank = 0 queries.each { |q| @rank += (q.rank + 1) } @rank -= 1000 if verb.nil? end @rank end |
.signature ⇒ Object
88 89 90 91 |
# File 'lib/gamefic/action.rb', line 88 def signature # @todo This is clearly unfinished "#{verb} #{queries.map{|m| m.signature}.join(',')}" end |
.subclass(verb, *q, meta: false, order_key: 0, &block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gamefic/action.rb', line 50 def self.subclass verb, *q, meta: false, order_key: 0, &block act = Class.new(self) do self.verb = verb self. = self.order_key = order_key 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
116 117 118 119 120 121 122 123 124 |
# File 'lib/gamefic/action.rb', line 116 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
67 68 69 |
# File 'lib/gamefic/action.rb', line 67 def verb @verb end |
Instance Method Details
#arguments ⇒ Object
17 18 19 |
# File 'lib/gamefic/action.rb', line 17 def arguments parameters end |
#execute ⇒ Object
21 22 23 24 |
# File 'lib/gamefic/action.rb', line 21 def execute @executed = true self.class.executor.call(@actor, *@parameters) unless self.class.executor.nil? end |
#executed? ⇒ Boolean
26 27 28 |
# File 'lib/gamefic/action.rb', line 26 def executed? @executed end |
#meta? ⇒ Boolean
42 43 44 |
# File 'lib/gamefic/action.rb', line 42 def self.class. end |
#order_key ⇒ Object
46 47 48 |
# File 'lib/gamefic/action.rb', line 46 def order_key self.class.order_key end |
#rank ⇒ Object
38 39 40 |
# File 'lib/gamefic/action.rb', line 38 def rank self.class.rank end |
#signature ⇒ Object
34 35 36 |
# File 'lib/gamefic/action.rb', line 34 def signature self.class.signature end |
#verb ⇒ Object
30 31 32 |
# File 'lib/gamefic/action.rb', line 30 def verb self.class.verb end |