Class: GameMachine::ClientManager

Inherits:
Actor::Base
  • Object
show all
Defined in:
server/lib/game_machine/client_manager.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

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

#channelObject (readonly)

Returns the value of attribute channel


6
7
8
# File 'server/lib/game_machine/client_manager.rb', line 6

def channel
  @channel
end

#client_to_playerObject (readonly)

Returns the value of attribute client_to_player


6
7
8
# File 'server/lib/game_machine/client_manager.rb', line 6

def client_to_player
  @client_to_player
end

#local_actorsObject (readonly)

Returns the value of attribute local_actors


6
7
8
# File 'server/lib/game_machine/client_manager.rb', line 6

def local_actors
  @local_actors
end

#local_clientsObject (readonly)

Returns the value of attribute local_clients


6
7
8
# File 'server/lib/game_machine/client_manager.rb', line 6

def local_clients
  @local_clients
end

#playersObject (readonly)

Returns the value of attribute players


6
7
8
# File 'server/lib/game_machine/client_manager.rb', line 6

def players
  @players
end

#remote_clientsObject (readonly)

Returns the value of attribute remote_clients


6
7
8
# File 'server/lib/game_machine/client_manager.rb', line 6

def remote_clients
  @remote_clients
end

Class Method Details

.cluster_connectionsObject

Cluster only connections. We should not be handling things like entity tracking for this type of connection. This is for stuff that is not latency sensitive such as chat, groups, etc..


34
35
36
# File 'server/lib/game_machine/client_manager.rb', line 34

def self.cluster_connections
  local_players.select {|player_id,type| type == 'cluster'}
end

.get_map(name) ⇒ Object


16
17
18
19
20
21
22
23
24
# File 'server/lib/game_machine/client_manager.rb', line 16

def self.get_map(name)
  unless @map
    @map = java.util.concurrent.ConcurrentHashMap.new
  end
  unless @map[name]
    @map[name] = java.util.concurrent.ConcurrentHashMap.new
  end
  @map[name]
end

.local_connectionsObject

Includes region + combined.


27
28
29
# File 'server/lib/game_machine/client_manager.rb', line 27

def self.local_connections
  local_players.select {|player_id,type| type != 'cluster'}
end

.local_playersObject


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

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

.send_to_player(message) ⇒ Object


38
39
40
41
42
43
44
45
# File 'server/lib/game_machine/client_manager.rb', line 38

def self.send_to_player(message)
  logger.debug("Sending #{message} to player #{message.player.id}")
  if local_players.has_key?(message.player.id)
    Actor::Base.find(message.player.id).tell(message)
  else
    find.tell(message)
  end
end

Instance Method Details

#cluster_connection?(client_connection) ⇒ Boolean

Returns:

  • (Boolean)

127
128
129
130
131
132
133
134
135
136
# File 'server/lib/game_machine/client_manager.rb', line 127

def cluster_connection?(client_connection)
  case client_connection.type
  when 'cluster'
    true
  when 'combined'
    true
  else
    false
  end
end

#create_client_event(client_id, player_id, event) ⇒ Object


194
195
196
197
198
# File 'server/lib/game_machine/client_manager.rb', line 194

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

#create_client_manager_event(client_id, player_id, event) ⇒ Object


189
190
191
192
# File 'server/lib/game_machine/client_manager.rb', line 189

def create_client_manager_event(client_id,player_id,event)
  client_manager_event = MessageLib::ClientManagerEvent.new.
    set_client_id(client_id).set_player_id(player_id).set_event(event)
end

#on_receive(message) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'server/lib/game_machine/client_manager.rb', line 58

def on_receive(message)
  if message.is_a?(MessageLib::Entity)

    # Outgoing message to player
    if message.send_to_player
      if self.class.local_players.has_key?(message.player.id)
        Actor::Base.find(message.player.id).tell(message)
      else
        send_to_remote_player(message)
      end

    # client events come from other client managers
    elsif message.client_event != nil
      if message.client_event.sender_id.match(/#{@server}/)
        return
      end
      process_client_event(message.client_event)
    
    # Unregister requests come from clients only
    elsif message.client_manager_unregister != nil
      unregister_sender(message)

    # Register requests come from actors and clients
    elsif message.client_manager_register != nil
      register_sender(message)
    else
      unhandled(message)
    end
  else
    unhandled(message)
  end
end

#post_init(*args) ⇒ Object


47
48
49
50
51
52
53
54
55
56
# File 'server/lib/game_machine/client_manager.rb', line 47

def post_init(*args)
  @server = Akka.instance.address
  @channel = 'client_events'
  @local_actors = self.class.get_map('local_actors')
  @remote_clients = self.class.get_map('remote_clients')
  @local_clients = self.class.get_map('local_clients')
  @players = self.class.get_map('players')
  @client_to_player = self.class.get_map('client_to_player')
  subscribe(channel)
end

#process_client_event(client_event) ⇒ Object


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'server/lib/game_machine/client_manager.rb', line 106

def process_client_event(client_event)
  if client_event.event == 'disconnected'
    remote_clients.delete(client_event.client_id)
    client_to_player.delete(client_event.client_id)
    players.delete(client_event.player_id)
    self.class.logger.debug("client #{client_event.client_id} disconnected")
  elsif client_event.event == 'connected'
    remote_ref = sender_id_to_actor_ref(client_event.sender_id)
    remote_clients[client_event.client_id] = remote_ref
    players[client_event.player_id] = client_event.client_id
    client_to_player[client_event.client_id] = client_event.player_id
    self.class.logger.debug("client #{client_event.client_id} connected")
  elsif client_event.event == 'update'
    remote_ref = sender_id_to_actor_ref(client_event.sender_id)
    remote_clients[client_event.client_id] = remote_ref
    players[client_event.player_id] = client_event.client_id
    client_to_player[client_event.client_id] = client_event.player_id
    self.class.logger.debug("client #{client_event.client_id} update")
  end
end

#register_sender(message) ⇒ Object


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'server/lib/game_machine/client_manager.rb', line 157

def register_sender(message)
  register_msg = message.client_manager_register
  register_type = register_msg.register_type
  name = register_msg.name
  events = register_msg.events

  # Client register
  if register_type == 'client'
    if cluster_connection?(message.client_connection)
      send_client_event(name,message.player.id,'connected')
    end
    local_clients[name] = message.client_connection
    players[message.player.id] = name
    self.class.local_players[message.player.id] = message.client_connection.type
    get_sender.tell(message,get_self)
    self.class.logger.debug("client #{name} registered")
  # Actor register
  elsif register_type == 'actor'
    self.class.logger.debug "Actor #{name} registered"
    local_actors[name] = events
  end
end

#send_client_event(client_id, player_id, event) ⇒ Object


200
201
202
203
204
205
206
# File 'server/lib/game_machine/client_manager.rb', line 200

def send_client_event(client_id,player_id,event)
  client_event = create_client_event(client_id,player_id,event)
  entity = MessageLib::Entity.new.set_id('0').set_client_event(client_event)
  publish = MessageLib::Publish.new
  publish.set_topic(channel).set_message(entity).set_game_id('system')
  MessageQueue.find.tell(publish,get_self)
end

#send_client_manager_event(client_id, player_id, event) ⇒ Object


181
182
183
184
185
186
187
# File 'server/lib/game_machine/client_manager.rb', line 181

def send_client_manager_event(client_id,player_id,event)
  local_actors.each do |name,events|
    client_manager_event = create_client_manager_event(client_id,player_id,event)
    Actor::Base.find(name).tell(client_manager_event)
    self.class.logger.debug "Send #{event} to #{name}"
  end
end

#send_to_remote_player(message) ⇒ Object


91
92
93
94
95
96
97
98
99
# File 'server/lib/game_machine/client_manager.rb', line 91

def send_to_remote_player(message)
  self.class.logger.debug("Send to remote player #{message.player.id}")
  if client_id = players.fetch(message.player.id,nil)
    if remote_ref = remote_clients.fetch(client_id,nil)
      self.class.logger.debug("Found remote client for #{message.player.id}")
      remote_ref.tell(message)
    end
  end
end

#sender_id_to_actor_ref(sender_id) ⇒ Object


101
102
103
104
# File 'server/lib/game_machine/client_manager.rb', line 101

def sender_id_to_actor_ref(sender_id)
  server,name = sender_id.split('|')
  Actor::Base.find_remote(server,name)
end

#subscribe(topic) ⇒ Object


208
209
210
211
# File 'server/lib/game_machine/client_manager.rb', line 208

def subscribe(topic)
  message = MessageLib::Subscribe.new.set_topic(topic).set_game_id('system')
  MessageQueue.find.tell(message,get_self)
end

#unregister_sender(message) ⇒ Object


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'server/lib/game_machine/client_manager.rb', line 138

def unregister_sender(message)
  unregister_msg = message.client_manager_unregister
  register_type = unregister_msg.register_type
  name = unregister_msg.name
  if register_type == 'client'
    if cluster_connection?(message.client_connection)
      send_client_event(name,message.player.id,'disconnected')
    end

    # Notify registered actors using a client manager event
    send_client_manager_event(name,message.player.id,'disconnected')

    local_clients.delete(name)
    players.delete(message.player.id)
    self.class.local_players.delete(message.player.id)
    self.class.logger.debug("client #{name} unregistered")
  end
end