Class: Gamefic::Action

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#parametersObject (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

.executorObject



97
98
99
# File 'lib/gamefic/action.rb', line 97

def executor
  @executor
end

.hidden?Boolean

Returns:

  • (Boolean)


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

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

.meta?Boolean

Returns:

  • (Boolean)


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

def meta?
  @meta ||= false
end

.on_execute(&block) ⇒ Object



84
85
86
# File 'lib/gamefic/action.rb', line 84

def on_execute &block
  @executor = block
end

.order_keyObject



101
102
103
# File 'lib/gamefic/action.rb', line 101

def order_key
  @order_key ||= 0
end

.queriesObject



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

def queries
  @queries ||= []
end

.rankObject



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

.signatureObject



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

Returns:

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

.verbObject



67
68
69
# File 'lib/gamefic/action.rb', line 67

def verb
  @verb
end

Instance Method Details

#argumentsObject



17
18
19
# File 'lib/gamefic/action.rb', line 17

def arguments
  parameters
end

#executeObject



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

Returns:

  • (Boolean)


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

def executed?
  @executed
end

#meta?Boolean

Returns:

  • (Boolean)


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

def meta?
  self.class.meta?
end

#order_keyObject



46
47
48
# File 'lib/gamefic/action.rb', line 46

def order_key
  self.class.order_key
end

#rankObject



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

def rank
  self.class.rank
end

#signatureObject



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

def signature
  self.class.signature
end

#verbObject



30
31
32
# File 'lib/gamefic/action.rb', line 30

def verb
  self.class.verb
end