Class: GameMachine::GameSystems::RegionService

Inherits:
Actor::Base
  • Object
show all
Includes:
Commands, Models
Defined in:
server/lib/game_machine/game_systems/region_service.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 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, logger, model_filter, #onReceive, player_controller, #receive_message, #schedule_message, #schedule_message_once, #sender, set_player_controller

Instance Attribute Details

#regionsObject (readonly)

Returns the value of attribute regions


7
8
9
# File 'server/lib/game_machine/game_systems/region_service.rb', line 7

def regions
  @regions
end

Class Method Details

.addressObject


18
19
20
# File 'server/lib/game_machine/game_systems/region_service.rb', line 18

def self.address
  @address
end

.address=(address) ⇒ Object


22
23
24
# File 'server/lib/game_machine/game_systems/region_service.rb', line 22

def self.address=(address)
  @address = address
end

.regionObject


26
27
28
# File 'server/lib/game_machine/game_systems/region_service.rb', line 26

def self.region
  @region
end

.region=(region) ⇒ Object


30
31
32
# File 'server/lib/game_machine/game_systems/region_service.rb', line 30

def self.region=(region)
  @region = region
end

.regionsObject


10
11
12
13
14
15
16
# File 'server/lib/game_machine/game_systems/region_service.rb', line 10

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

Instance Method Details

#load_from_configObject


53
54
55
56
57
58
59
60
61
62
63
64
# File 'server/lib/game_machine/game_systems/region_service.rb', line 53

def load_from_config
  Application.config.regions.each do |entry|
    name = entry[0]
    # Make sure we set some value here, it will get updated with a valid
    # region in update_regions
    if region = Region.find!(name)
      self.class.regions[name] = region
    else
      self.class.regions[name] = false
    end
  end
end

#on_receive(message) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'server/lib/game_machine/game_systems/region_service.rb', line 76

def on_receive(message)
  if message.is_a?(String)
    if message == 'update_regions'
      update_regions
    end
  elsif message.is_a?(MessageLib::Entity)
    # TODO fix this, leaving for now until we figure out how complex this needs
    # to be
    if message.id == 'regions'
      regions_msg = MessageLib::Regions.new.set_regions(regions_string)
      entity = MessageLib::Entity.new.set_id(message.player.id).set_regions(regions_msg)
      commands.player.send_message(entity,message.player.id)
    end
  end

end

#post_init(*args) ⇒ Object


34
35
36
37
38
39
40
# File 'server/lib/game_machine/game_systems/region_service.rb', line 34

def post_init(*args)
  cluster = JavaLib::Cluster.get(getContext.system)
  self.class.address = cluster.self_address.to_string
  load_from_config
  update_regions
  schedule_message('update_regions',10,:seconds)
end

#regions_stringObject


70
71
72
73
74
# File 'server/lib/game_machine/game_systems/region_service.rb', line 70

def regions_string
  self.class.regions.select{|name,region| region && !region.server.nil?}.map do |name,region|
    "#{region.name}=#{server_hostname(region.server)}"
  end.join('|')
end

#server_hostname(server) ⇒ Object


66
67
68
# File 'server/lib/game_machine/game_systems/region_service.rb', line 66

def server_hostname(server)
  server.sub('akka.tcp://[email protected]','').split(':').first
end

#update_regionsObject


42
43
44
45
46
47
48
49
50
51
# File 'server/lib/game_machine/game_systems/region_service.rb', line 42

def update_regions
  self.class.regions.keys.each do |name|
    if region = Region.find!(name)
      self.class.regions[name] = region
      if region.server == self.class.address
        self.class.region = name
      end
    end
  end
end