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) ⇒ 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

.executorProc (readonly)

The proc to call when the action is executed

Returns:

  • (Proc)


77
78
79
# File 'lib/gamefic/action.rb', line 77

def executor
  @executor
end

.verbObject

Returns the value of attribute verb.



72
73
74
# File 'lib/gamefic/action.rb', line 72

def verb
  @verb
end

Instance Attribute Details

#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:



7
8
9
# File 'lib/gamefic/action.rb', line 7

def arguments
  @arguments
end

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.

Parameters:

Returns:

  • (self, nil)


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.

Returns:

  • (Boolean)


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

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

.meta?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/gamefic/action.rb', line 79

def meta?
  @meta ||= false
end

.on_execute(&block) ⇒ Object



92
93
94
# File 'lib/gamefic/action.rb', line 92

def on_execute &block
  @executor = block
end

.queriesObject



88
89
90
# File 'lib/gamefic/action.rb', line 88

def queries
  @queries ||= []
end

.rankInteger

Returns:

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

.signatureObject



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>

Parameters:

Returns:



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.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)


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

#executeObject

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.

Returns:

  • (Boolean)


26
27
28
# File 'lib/gamefic/action.rb', line 26

def executed?
  @executed
end

#meta?Boolean

True if the action is flagged as meta.

Returns:

  • (Boolean)


48
49
50
# File 'lib/gamefic/action.rb', line 48

def meta?
  self.class.meta?
end

#rankObject



41
42
43
# File 'lib/gamefic/action.rb', line 41

def rank
  self.class.rank
end

#signatureObject



37
38
39
# File 'lib/gamefic/action.rb', line 37

def signature
  self.class.signature
end

#verbSymbol

The verb associated with this action.

Returns:

  • (Symbol)

    The symbol representing the verb



33
34
35
# File 'lib/gamefic/action.rb', line 33

def verb
  self.class.verb
end