Class: Caldera::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/caldera/client.rb

Constant Summary collapse

LOGGER =
Logging.logger[self]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_shards:, user_id:, connect: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • num_shards (Integer, String)
  • user_id (Integer, String)
  • connect (Proc<String, String>) (defaults to: nil)


26
27
28
29
30
31
32
33
34
# File 'lib/caldera/client.rb', line 26

def initialize(num_shards:, user_id:, connect: nil)
  @num_shards = num_shards.to_s
  @user_id = user_id.to_s
  @players = {}
  @connect_proc = connect
  @nodes = []
  @voice_state_mutex = Mutex.new
  @voice_states = Hash.new { |h, guild_id| h[guild_id] = {} }
end

Instance Attribute Details

#nodesArray<Node> (readonly)

Returns:



21
22
23
# File 'lib/caldera/client.rb', line 21

def nodes
  @nodes
end

#num_shardsString (readonly)

Returns:

  • (String)


12
13
14
# File 'lib/caldera/client.rb', line 12

def num_shards
  @num_shards
end

#playersArray<Player> (readonly)

Returns:



18
19
20
# File 'lib/caldera/client.rb', line 18

def players
  @players
end

#user_idString (readonly)

Returns:

  • (String)


15
16
17
# File 'lib/caldera/client.rb', line 15

def user_id
  @user_id
end

Instance Method Details

#add_node(authorization:, uri: nil, rest_uri: nil, ws_uri: nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/caldera/client.rb', line 71

def add_node(authorization:, uri: nil, rest_uri: nil, ws_uri: nil)
  uri = URI(uri) unless uri.is_a?(URI::Generic)
  rest_uri ||= uri.clone.tap { |u| u.scheme = 'http' }
  ws_uri ||= uri.clone.tap { |u| u.scheme = 'ws' }

  new_node = Node.new(rest_uri, ws_uri, authorization, self)
  new_node.start

  @nodes << new_node
end

#best_nodeObject



87
88
89
# File 'lib/caldera/client.rb', line 87

def best_node
  @nodes.min { |n| n.stats['cpu']['systemLoad'] }
end

#connect(guild_id, channel_id, timeout: nil) ⇒ Caldera::Player

Parameters:

  • guild_id (Integer, String)
  • channel_id (Integer, String)
  • timeout (Number) (defaults to: nil)

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/caldera/client.rb', line 40

def connect(guild_id, channel_id, timeout: nil)
  return Timeout.timeout(timeout) { connect(guild_id, channel_id) } if timeout

  gid = guild_id.to_s

  return @players[gid] if @players[gid]

  @connect_proc.call(gid, channel_id.to_s)
  sleep 0.05 until @players[gid]

  @players[gid]
end

#get_player(guild_id) ⇒ Object



91
92
93
# File 'lib/caldera/client.rb', line 91

def get_player(guild_id)
  @players[guild_id.to_s]
end

#remove_node(node) ⇒ Object



82
83
84
85
# File 'lib/caldera/client.rb', line 82

def remove_node(node)
  @nodes.delete(node)
  node.stop
end

#update_voice_state(guild_id, session_id: nil, event: nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/caldera/client.rb', line 53

def update_voice_state(guild_id, session_id: nil, event: nil)
  guild_id = guild_id.to_s
  @voice_state_mutex.synchronize do
    @voice_states[guild_id][:session_id] = session_id if session_id
    @voice_states[guild_id][:event] = event if event
  end

  state = @voice_states[guild_id]

  if state[:session_id] && state[:event]
    LOGGER.info { "Creating player for #{guild_id}" }
    best_node.create_player(guild_id, state[:session_id], state[:event])
    @voice_states.delete(guild_id)
  else
    LOGGER.debug { "Recieved partial info for creating player for #{guild_id}: #{state}" }
  end
end