Class: GameMachine::GameSystems::ObjectDbProxy

Inherits:
Actor::Base
  • Object
show all
Includes:
Commands
Defined in:
lib/game_machine/game_systems/objectdb_proxy.rb

Constant Summary

Constants inherited from Actor::Base

Actor::Base::ON_RECEIVE_HOOKS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Commands

#commands

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

Class Method Details

.save_entity(entity) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/game_machine/game_systems/objectdb_proxy.rb', line 12

def self.save_entity(entity)
  scoped_id = scope_entity_id(entity.id,entity.player.id)
  entity.set_id(scoped_id)
  entity.set_save(false)
  ref = ObjectDb.find_distributed(entity.get_id)
  ref.tell(MessageLib::ObjectdbPut.new.set_entity(entity))
  entity
end

.scope_entity_id(entity_id, player_id) ⇒ Object



21
22
23
# File 'lib/game_machine/game_systems/objectdb_proxy.rb', line 21

def self.scope_entity_id(entity_id,player_id)
  "#{player_id}_#{entity_id}"
end

.unscope_entity_id(entity_id, player_id) ⇒ Object



25
26
27
# File 'lib/game_machine/game_systems/objectdb_proxy.rb', line 25

def self.unscope_entity_id(entity_id,player_id)
  unscoped_id = entity_id.sub("#{player_id}_",'')
end

Instance Method Details

#on_receive(message) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/game_machine/game_systems/objectdb_proxy.rb', line 29

def on_receive(message)
  if message.is_a?(MessageLib::Entity)
    if message.has_objectdb_get
      objectdb_get = message.get_objectdb_get
      entity_id = objectdb_get.get_entity_id
      player_id = objectdb_get.get_player_id

      response = MessageLib::ObjectdbGetResponse.new

      # Make request with scoped id
      scoped_id = self.class.scope_entity_id(entity_id,player_id)
      if entity = commands.datastore.get(scoped_id)

        # Unscope the entity id
        unscoped_id = self.class.unscope_entity_id(entity.id,player_id)
        entity.set_id(unscoped_id)

        response.set_entity_found(true)
      else
        entity = MessageLib::Entity.new.set_id(entity_id)
        response.set_entity_found(false)
      end


      entity.set_objectdb_get_response(response)
      commands.player.send_message(entity,player_id)
    end
  end
end