Module: Omega::Client::Trackable::ClassMethods

Defined in:
lib/omega/client/mixins/trackable.rb

Overview

Methods that are defined on the class including the Trackable module

Instance Method Summary collapse

Instance Method Details

#entity_event(events = {}) ⇒ Object

Define an event on this entity type which clients can register handlers for specific entity instances.

Examples:

class MiningShip
  include Trackable
  entity_type Manufactured::Ship
  get_method "manufactured::get_entity"

  entity_event :resource_collected =>
    { :subscribe    => "manufactured::subscribe_to",
      :notification => "manufactured::event_occurred",
      :match => proc { |e,*args| args[1] == e.id } }

Options Hash (events):

  • :setup (Callable)

    method to be invoked w/ entity to begin listening for events

  • :subscribe (String)

    server method to invoke to being listening for events

  • :notification (String)

    local rjr method invoked by server to notify client event occurred.

  • :match (Callable)

    optional callback to validate if notification matches local entity

  • :update (Callable)

    optional callback to update entity before handling notification


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/omega/client/mixins/trackable.rb', line 201

def entity_event(events = {})
  events.keys.each { |e|
    event_setup = []

    # TODO skip subscription/notification handling
    # if already listening for this entity ?

    if events[e].has_key?(:setup)
      event_setup << events[e][:setup]
    end

    if events[e].has_key?(:subscribe)
      event_setup << lambda { |*args|
        self.node.invoke(events[e][:subscribe], self.entity.id, e)
      }
    end

    if events[e].has_key?(:notification)
      event_setup << lambda { |*args|
        @event_serializer = Mutex.new
        @handled ||= []
        unless @handled.include?(e)
          notification = events[e][:notification]
          self.node.handle(notification) { |*args|
            if events[e][:match].nil? || events[e][:match].call(self, *args)
              @event_serializer.synchronize {
                events[e][:update].call(self, *args) if events[e][:update]
                self.raise_event e, *args
              }
            end
          }
          @handled << e
        end
      }
    end

    @event_setup ||= {}
    @event_setup[e] = event_setup
  }
end

#entity_init(&bl) ⇒ Object

Register a method to be invoked after instance of this class is created by local mechanisms.

For this reason, when using Trackable, instances of the class including it should only be created through the instantiation methods defined here.


153
154
155
156
157
158
159
# File 'lib/omega/client/mixins/trackable.rb', line 153

def entity_init(&bl)
  @entity_init ||= []
  @entity_init << bl unless bl.nil?
  @entity_init +
    (self.superclass.respond_to?(:entity_init) ?
     self.superclass.entity_init : [])
end

#entity_type(type = nil) ⇒ Object

Define server side entity type to track


120
121
122
123
124
125
126
# File 'lib/omega/client/mixins/trackable.rb', line 120

def entity_type(type=nil)
  @entity_type = type unless type.nil?
  @entity_type.nil? ?
    (self.superclass.respond_to?(:entity_type) ?
     self.superclass.entity_type : nil) :
     @entity_type
end

#entity_validation(&bl) ⇒ Object

Register a method to be invoked after serverside entit(y|ies) are retrieved to perform additional client side verification of the entity type.

Examples:

class MiningShip
  include Trackable
  entity_type Manufactured::Ship
  get_method "manufactured::get_entity"
  entity_validation { |e| e.type == :miner }
end

139
140
141
142
143
144
145
# File 'lib/omega/client/mixins/trackable.rb', line 139

def entity_validation(&bl)
  @entity_validation ||= []
  @entity_validation << bl unless bl.nil?
  @entity_validation +
    (self.superclass.respond_to?(:entity_validation) ?
     self.superclass.entity_validation : [])
end

#event_setup(callbacks = nil) ⇒ Object

Manually get/set the event setup callbacks

See Also:


173
174
175
176
177
178
179
# File 'lib/omega/client/mixins/trackable.rb', line 173

def event_setup(callbacks=nil)
  @event_setup ||= {}
  @event_setup = callbacks unless callbacks.nil?

  (self.superclass.respond_to?(:event_setup) ?
   self.superclass.event_setup : {}).merge(@event_setup)
end

#get(id) ⇒ nil, Trackable

Return entity tracked by this class corresponding to id, or nil if not found

Examples:

class Ship
  include Trackable
  entity_type Manufactured::Ship
  get_method "manufactured::get_entity"
end

Ship.get('ship1')
# => <Omega::Client::Ship#...>

277
278
279
280
281
282
# File 'lib/omega/client/mixins/trackable.rb', line 277

def get(id)
  e = track_entity node.invoke(self.get_method,
                               'with_id', id)
  return nil unless validate_entity(e)
  e
end

#get_allArray<Trackable>

Return array of all server side entities tracked by this class

Examples:

class Ship
  include Trackable
  entity_type Manufactured::Ship
  get_method "manufactured::get_entity"
end

Ship.get_all
# => [<Omega::Client::Ship#...>,<Omega::Client::Ship#...>,...]

256
257
258
259
260
261
# File 'lib/omega/client/mixins/trackable.rb', line 256

def get_all
  node.invoke(self.get_method,
              'of_type', self.entity_type).
       select  { |e| validate_entity(e) }.
       collect { |e| track_entity(e) }
end

#get_method(method_name = nil) ⇒ Object

Get/set the method used to retrieve serverside entities.


162
163
164
165
166
167
168
# File 'lib/omega/client/mixins/trackable.rb', line 162

def get_method(method_name=nil)
  @get_method = method_name unless method_name.nil?
  @get_method.nil? ?
    (self.superclass.respond_to?(:get_method) ?
     self.superclass.get_method : nil) :
     @get_method
end

#nodeObject

Class wrapper around Trackable.node


115
116
117
# File 'lib/omega/client/mixins/trackable.rb', line 115

def node
  Trackable.node
end

#owned_by(user_id) ⇒ Array<Trackable>

Return array of all server side entities tracked by this class owned by the specified user

TODO move this into its own module

Examples:

class Ship
  include Trackable
  entity_type Manufactured::Ship
  get_method "manufactured::get_entity"
end

Ship.owned_by('Anubis')
# => [<Omega::Client::Ship#...>,<Omega::Client::Ship#...>,...]

300
301
302
303
304
305
306
# File 'lib/omega/client/mixins/trackable.rb', line 300

def owned_by(user_id)
  node.invoke(self.get_method,
              'of_type', self.entity_type,
              'owned_by', user_id).
       select  { |e| validate_entity(e) }.
       collect { |e| track_entity(e) }
end