Class: GameMachine::ObjectDb

Inherits:
Actor::Base
  • Object
show all
Defined in:
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, model_filter, #onReceive, player_controller, #receive_message, #schedule_message, #sender, set_player_controller

Instance Attribute Details

#entitiesObject

Returns the value of attribute entities.



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

def entities
  @entities
end

#storeObject

Returns the value of attribute store.



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

def store
  @store
end

Class Method Details

.dbprocsObject



5
6
7
8
9
10
11
# File '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

#delete_allObject



25
26
27
28
# File 'lib/game_machine/object_db.rb', line 25

def delete_all
  entities = {}
  store.delete_all
end

#delete_entity(entity_id) ⇒ Object



20
21
22
23
# File 'lib/game_machine/object_db.rb', line 20

def delete_entity(entity_id)
  entities.delete(entity_id)
  @store.delete(entity_id)
end

#get_entity(entity_id, klass) ⇒ Object



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

def get_entity(entity_id,klass)
  entity = entities.fetch(entity_id,nil)
  if entity.nil?
    entity = store.get(entity_id,klass)
  else
    entity.clone
  end
end

#on_receive(message) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/game_machine/object_db.rb', line 44

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

  elsif message.is_a?(MessageLib::ObjectdbPut)
    set_entity(message.get_entity)
    get_sender.tell(true,nil)
  elsif message.is_a?(MessageLib::ObjectdbGet)
    if entity = get_entity(message.get_entity_id,message.get_klass)
      get_sender.tell(entity,nil)
    end
  elsif message.is_a?(MessageLib::ObjectdbDel)
    delete_entity(message.get_entity_id)
  elsif message.respond_to?(:get_id)
    set_entity(message)
    get_sender.tell(true,nil)
  else
    unhandled(message)
  end
end

#post_init(*args) ⇒ Object



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

def post_init(*args)
  @entities = {}
  @store = DataStore.instance
end

#set_entity(entity) ⇒ Object



30
31
32
33
# File 'lib/game_machine/object_db.rb', line 30

def set_entity(entity)
  entities[entity.id] = entity
  WriteBehindCache.find_distributed(entity.id).tell(entity)
end