Class: GameOverseer::ClientManager

Inherits:
Object
  • Object
show all
Defined in:
lib/gameoverseer/clients/client_manager.rb

Overview

Stores client data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClientManager

Returns a new instance of ClientManager.



7
8
9
10
# File 'lib/gameoverseer/clients/client_manager.rb', line 7

def initialize
  ClientManager.instance = self
  @clients = []
end

Instance Attribute Details

#clientsObject

Returns the value of attribute clients.



5
6
7
# File 'lib/gameoverseer/clients/client_manager.rb', line 5

def clients
  @clients
end

Class Method Details

.instanceClientManager

Returns:



59
60
61
# File 'lib/gameoverseer/clients/client_manager.rb', line 59

def self.instance
  @instance
end

.instance=(_instance) ⇒ Object

Parameters:



64
65
66
# File 'lib/gameoverseer/clients/client_manager.rb', line 64

def self.instance=_instance
  @instance = _instance
end

Instance Method Details

#add(client_id, ip_address) ⇒ Object

Add client to clients list

Parameters:

  • client_id (Integer)
  • ip_address (String)


15
16
17
18
19
# File 'lib/gameoverseer/clients/client_manager.rb', line 15

def add(client_id, ip_address)
  @clients << {client_id: client_id, ip_address: ip_address}
  GameOverseer::Services.client_connected(client_id, ip_address)
  GameOverseer::Console.log("ClientManager> client with id '#{client_id}' connected")
end

#get(client_id) ⇒ Hash

Gets client data

Parameters:

  • client_id (Integer)

Returns:

  • (Hash)

    hash containing client data



36
37
38
39
40
41
42
43
44
# File 'lib/gameoverseer/clients/client_manager.rb', line 36

def get(client_id)
  _hash = @clients.detect do |hash|
    if hash[:client_id] == client_id
      true
    end
  end

  return _hash
end

#remove(client_id) ⇒ Object

Removes client data and disconnects client

Parameters:

  • client_id (Integer)

    ID of client



48
49
50
51
52
53
54
55
56
# File 'lib/gameoverseer/clients/client_manager.rb', line 48

def remove(client_id)
  @clients.each do |hash|
    if hash[:client_id] == client_id
      @clients.delete(hash)
      GameOverseer::Services.client_disconnected(client_id)
      GameOverseer::Console.log("ClientManager> client with id '#{client_id}' disconnected")
    end
  end
end

#update(client_id, key, value) ⇒ Object

Store client specific data in a Hash

Parameters:

  • client_id (Integer)

    ID of client

  • key (String|Symbol)
  • value

    What the key should equal



25
26
27
28
29
30
31
# File 'lib/gameoverseer/clients/client_manager.rb', line 25

def update(client_id, key, value)
  @clients.each do |hash|
    if hash[:client_id] == client_id
      hash[key] = value
    end
  end
end