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

add_hashring, aspect, aspects, find, find_by_address, find_distributed, find_distributed_local, find_remote, hashring, hashrings, local_path, model_filter, #onReceive, player_controller, #receive_message, reset_hashrings, #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) ⇒ Object



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

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

#on_receive(message) ⇒ Object



43
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
# File 'lib/game_machine/object_db.rb', line 43

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)
    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)
    sender.tell(true)
  elsif message.is_a?(MessageLib::ObjectdbGet)
    sender.tell(get_entity(message.get_entity_id) || false)
  elsif message.is_a?(MessageLib::ObjectdbDel)
    delete_entity(message.get_entity_id)
  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