Class: GameMachine::ClientManagerUpdater

Inherits:
Actor::Base
  • Object
show all
Defined in:
server/lib/game_machine/client_manager_updater.rb

Constant Summary

Constants inherited from Actor::Base

Actor::Base::ON_RECEIVE_HOOKS

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#local_playersObject (readonly)

Returns the value of attribute local_players


8
9
10
# File 'server/lib/game_machine/client_manager_updater.rb', line 8

def local_players
  @local_players
end

#playersObject (readonly)

Returns the value of attribute players


8
9
10
# File 'server/lib/game_machine/client_manager_updater.rb', line 8

def players
  @players
end

Instance Method Details

#cluster_event?(message) ⇒ Boolean

Returns:

  • (Boolean)

17
18
19
20
# File 'server/lib/game_machine/client_manager_updater.rb', line 17

def cluster_event?(message)
  message.is_a?(JavaLib::ClusterEvent::MemberUp) ||
   message.is_a?(JavaLib::ClusterEvent::ReachableMember)
end

#create_client_event(client_id, player_id, event) ⇒ Object


22
23
24
25
26
# File 'server/lib/game_machine/client_manager_updater.rb', line 22

def create_client_event(client_id,player_id,event)
  client_event = MessageLib::ClientEvent.new.set_client_id(client_id).
    set_event(event).set_player_id(player_id)
  client_event.set_sender_id(Akka.instance.address + '|' + self.class.name)
end

#on_receive(message) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
# File 'server/lib/game_machine/client_manager_updater.rb', line 55

def on_receive(message)
  if message.is_a?(String)
    if message == 'update_remote_members'
      update_remote_members('update',true)

    end
  elsif cluster_event?(message)
    update_remote_member(message.member,'update')
  else
    unhandled(message)
  end
end

#post_init(*args) ⇒ Object


9
10
11
12
13
14
15
# File 'server/lib/game_machine/client_manager_updater.rb', line 9

def post_init(*args)
  @local_players = ClientManager.local_players
  @players = ClientManager.get_map('players')
  @cluster = JavaLib::Cluster.get(getContext.system)
  @cluster.subscribe(getSelf, JavaLib::ClusterEvent::MemberEvent.java_class)
  schedule_message('update_remote_members',10 * 1000)
end

#update_remote_member(member, event_type) ⇒ Object


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

def update_remote_member(member,event_type)
  self.class.logger.debug "Updating remote #{member.address.to_string}"
  remote_ref = Actor::Base.find_remote(member.address.to_string,self.class.name)
  
  players.each do |player_id,client_id|
    #send_client_event(client_id,player_id,event_type)
    client_event = create_client_event(client_id,player_id,event_type)
    entity = MessageLib::Entity.new.set_id('0').set_client_event(client_event)
    remote_ref.tell(entity,get_self)
    self.class.logger.debug "Sent #{event_type} for #{player_id}"
  end
end

#update_remote_members(event_type, sample) ⇒ Object

Ensures player info gets spread around the cluster even in the event of network issues/lost messages


30
31
32
33
34
35
36
37
38
39
40
# File 'server/lib/game_machine/client_manager_updater.rb', line 30

def update_remote_members(event_type,sample)
  begin
    members = ClusterMonitor.remote_members.values.to_a
    if sample
      members = members.sample(50)
    end
    members.each {|member| update_remote_member(member,event_type) }
  rescue Exception => e
    self.class.logger.error "Error updating remote members #{e.message}"
  end
end