Class: GameMachine::GameSystems::RegionManager

Inherits:
Actor::Base
  • Object
show all
Includes:
Commands, Models
Defined in:
server/lib/game_machine/game_systems/region_manager.rb

Constant Summary

Constants inherited from Actor::Base

Actor::Base::ON_RECEIVE_HOOKS

Instance Attribute 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

Returns the value of attribute regions


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

def regions
  @regions
end

#serversObject

Returns the value of attribute servers


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

def servers
  @servers
end

Instance Method Details

#assign_serversObject


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'server/lib/game_machine/game_systems/region_manager.rb', line 61

def assign_servers
  regions.each do |name,region|
    if region.server.nil?
      ClusterMonitor.cluster_members.keys.each do |address|
        unless servers.has_key?(address)
          region.server = address
          servers[address] = name
          region.save!
          self.class.logger.info "Region #{region.name} assigned to #{region.server}"
          break
        end
      end
    end
  end
end

#check_regionsObject


15
16
17
18
19
# File 'server/lib/game_machine/game_systems/region_manager.rb', line 15

def check_regions
  unassign_down_servers
  assign_servers
  notify_managers
end

#load_from_configObject


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'server/lib/game_machine/game_systems/region_manager.rb', line 29

def load_from_config
  Application.config.regions.each do |entry|
    name = entry[0]
    manager = entry[1]
    unless region = Region.find!(name)
      region = Region.new(
        :id => name,
        :name => name,
        :manager => manager
      )
      region.save!
    end
    regions[name] = region
    if region.server
      servers[region.server] = name
    end
  end
end

#notify_managersObject


21
22
23
24
25
26
27
# File 'server/lib/game_machine/game_systems/region_manager.rb', line 21

def notify_managers
  regions.each do |name,region|
    if region.manager && region.server
      region.manager.constantize.find_by_address(region.server).tell(region)
    end
  end
end

#on_receive(message) ⇒ Object


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

def on_receive(message)
  if message.is_a?(String)
    if message == 'check_regions'
      check_regions
    else
      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)
      #GameMachine.logger.info "#{self.class.name} sent regions to #{message.player.id}"
    end
  end
end

#post_init(*args) ⇒ Object


8
9
10
11
12
13
# File 'server/lib/game_machine/game_systems/region_manager.rb', line 8

def post_init(*args)
  @regions = {}
  @servers = {}
  load_from_config
  schedule_message('check_regions',2,:seconds)
end

#unassign_down_serversObject


48
49
50
51
52
53
54
55
56
57
58
59
# File 'server/lib/game_machine/game_systems/region_manager.rb', line 48

def unassign_down_servers
  regions.each do |name,region|
    if region.server
      unless ClusterMonitor.cluster_members.has_key?(region.server)
        self.class.logger.warn "Node #{region.server} no longer in cluster, region #{region.name} dissasociated"
        servers.delete(region.server)
        region.server = nil
        region.save!
      end
    end
  end
end