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.



10
11
12
# File 'lib/game_machine/object_db.rb', line 10

def entities
  @entities
end

#storeObject

Returns the value of attribute store.



10
11
12
# File 'lib/game_machine/object_db.rb', line 10

def store
  @store
end

Class Method Details

.dbprocsObject



5
6
7
# File 'lib/game_machine/object_db.rb', line 5

def dbprocs
  @dbprocs ||= java.util.concurrent.ConcurrentHashMap.new
end

Instance Method Details

#delete_allObject



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

def delete_all
  entities = {}
  store.delete_all
end

#delete_entity(entity_id) ⇒ Object



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

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

#get_entity(entity_id) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/game_machine/object_db.rb', line 31

def get_entity(entity_id)
  entity = entities.fetch(entity_id,nil)
  if entity.nil?
    if bytes = store.get(entity_id)
      entity = MessageLib::Entity.parse_from(bytes)
    end
  end
  entity
end

#on_receive(message) ⇒ Object



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

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
    unless current_entity = get_entity(current_entity_id)
      current_entity = MessageLib::Entity.new.set_id(current_entity_id)
    end
    returned_entity = self.class.dbprocs[procname].call(
      current_entity,update_entity
    )
    set_entity(returned_entity)
    sender.tell(returned_entity || false)
  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



11
12
13
14
# File 'lib/game_machine/object_db.rb', line 11

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

#set_entity(entity) ⇒ Object



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

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