Class: Gamefic::Action

Inherits:
Object show all
Defined in:
lib/gamefic/action.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actor, arguments, with_callbacks = false) ⇒ Action

Returns a new instance of Action.

Parameters:



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

.executorProc (readonly)

The proc to call when the action is executed

Returns:

  • (Proc)


113
114
115
# File 'lib/gamefic/action.rb', line 113

def executor
  @executor
end

.verbObject

Returns the value of attribute verb.



108
109
110
# File 'lib/gamefic/action.rb', line 108

def verb
  @verb
end

Instance Attribute Details

#actorGamefic::Actor (readonly)

Returns:



4
5
6
# File 'lib/gamefic/action.rb', line 4

def actor
  @actor
end

#argumentsArray<Object> (readonly) Also known as: parameters

An array of objects on which the action will operate, e.g., an entity that is a direct object of a command.

Returns:



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

def arguments
  @arguments
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.

Parameters:

Returns:

  • (self, nil)


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.

Returns:

  • (Boolean)


144
145
146
# File 'lib/gamefic/action.rb', line 144

def hidden?
  verb.to_s.start_with?('_')
end

.meta?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/gamefic/action.rb', line 115

def meta?
  @meta ||= false
end

.on_execute(&block) ⇒ Object



128
129
130
# File 'lib/gamefic/action.rb', line 128

def on_execute &block
  @executor = block
end

.queriesObject



124
125
126
# File 'lib/gamefic/action.rb', line 124

def queries
  @queries ||= []
end

.rankInteger

Returns:

  • (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

.signatureObject



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>

Parameters:

Returns:



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.meta = meta
    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

Returns:

  • (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

#cancelObject

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

#executeObject

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.

Returns:

  • (Boolean)


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

def executed?
  @executed
end

#meta?Boolean

True if the action is flagged as meta.

Returns:

  • (Boolean)


65
66
67
# File 'lib/gamefic/action.rb', line 65

def meta?
  self.class.meta?
end

#rankObject



58
59
60
# File 'lib/gamefic/action.rb', line 58

def rank
  self.class.rank
end

#signatureObject



54
55
56
# File 'lib/gamefic/action.rb', line 54

def signature
  self.class.signature
end

#verbSymbol

The verb associated with this action.

Returns:

  • (Symbol)

    The symbol representing the verb



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

def verb
  self.class.verb
end