Class: GameMachine::ObjectDb

Inherits:
Actor::Base
  • Object
show all
Defined in:
server/lib/game_machine/object_db.rb

Constant Summary

Constants inherited from Actor::Base

Actor::Base::ON_RECEIVE_HOOKS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Actor::Base

aspect, aspects, find, find_by_address, find_distributed, find_distributed_local, find_remote, hashring, local_path, logger, model_filter, #onReceive, player_controller, #receive_message, #schedule_message, #schedule_message_once, #sender, set_player_controller

Instance Attribute Details

#cacheObject

Returns the value of attribute cache


14
15
16
# File 'server/lib/game_machine/object_db.rb', line 14

def cache
  @cache
end

#classmapObject

Returns the value of attribute classmap


14
15
16
# File 'server/lib/game_machine/object_db.rb', line 14

def classmap
  @classmap
end

#storeObject

Returns the value of attribute store


14
15
16
# File 'server/lib/game_machine/object_db.rb', line 14

def store
  @store
end

Class Method Details

.dbprocsObject


5
6
7
8
9
10
11
# File 'server/lib/game_machine/object_db.rb', line 5

def dbprocs
  if @dbprocs
    @dbprocs
  else
    @dbprocs = java.util.concurrent.ConcurrentHashMap.new
  end
end

Instance Method Details

#class_cache(classname) ⇒ Object


21
22
23
24
25
26
27
28
29
30
# File 'server/lib/game_machine/object_db.rb', line 21

def class_cache(classname)
  return MessageLib::Entity if classname.nil?

  if cached = classmap.fetch(classname,nil)
    return cached
  else
    classmap[classname] = "GameMachine::MessageLib::#{classname}".constantize
    classmap[classname]
  end
end

#get_entity(entity_id, klass) ⇒ Object


37
38
39
40
41
42
43
# File 'server/lib/game_machine/object_db.rb', line 37

def get_entity(entity_id,klass)
  if bytes = cache.get(entity_id)
    class_cache(klass).parse_from(bytes)
  else
    store.get(entity_id,klass)
  end
end

#on_receive(message) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'server/lib/game_machine/object_db.rb', line 45

def on_receive(message)
  if message.is_a?(MessageLib::ObjectdbUpdate)
    procname = message.get_update_method.to_sym
    current_entity_id = message.get_current_entity_id
    update_entity = message.get_update_entity
    current_entity = get_entity(current_entity_id,'Entity')
    if self.class.dbprocs.has_key?(procname)
      dbproc = self.class.dbprocs[procname]
      returned_entity = dbproc.call(
        current_entity_id,current_entity,update_entity
      )
      set_entity(returned_entity)
      sender.tell(returned_entity || false)
    else
      GameMachine.logger.warn("Unable to find dbproc #{procname}")
    end
  else
    unhandled(message)
  end
end

#post_init(*args) ⇒ Object


15
16
17
18
19
# File 'server/lib/game_machine/object_db.rb', line 15

def post_init(*args)
  @store = DbLib::Store.get_instance
  @cache = DbLib::MemoryMap.get_instance.get_map
  @classmap = {}
end

#set_entity(entity) ⇒ Object


32
33
34
35
# File 'server/lib/game_machine/object_db.rb', line 32

def set_entity(entity)
  cache.put(entity.id,entity.to_byte_array)
  store.set(entity.id,entity)
end