Class: Caldera::Client
- Inherits:
-
Object
- Object
- Caldera::Client
- Defined in:
- lib/caldera/client.rb
Constant Summary collapse
- LOGGER =
Logging.logger[self]
Instance Attribute Summary collapse
- #nodes ⇒ Array<Node> readonly
- #num_shards ⇒ String readonly
- #players ⇒ Array<Player> readonly
- #user_id ⇒ String readonly
Instance Method Summary collapse
- #add_node(authorization:, uri: nil, rest_uri: nil, ws_uri: nil) ⇒ Object
- #best_node ⇒ Object
- #connect(guild_id, channel_id, timeout: nil) ⇒ Caldera::Player
- #get_player(guild_id) ⇒ Object
-
#initialize(num_shards:, user_id:, connect: nil) ⇒ Client
constructor
A new instance of Client.
- #remove_node(node) ⇒ Object
- #update_voice_state(guild_id, session_id: nil, event: nil) ⇒ Object
Constructor Details
#initialize(num_shards:, user_id:, connect: nil) ⇒ Client
Returns a new instance of Client.
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
#nodes ⇒ Array<Node> (readonly)
21 22 23 |
# File 'lib/caldera/client.rb', line 21 def nodes @nodes end |
#num_shards ⇒ String (readonly)
12 13 14 |
# File 'lib/caldera/client.rb', line 12 def num_shards @num_shards end |
#players ⇒ Array<Player> (readonly)
18 19 20 |
# File 'lib/caldera/client.rb', line 18 def players @players end |
#user_id ⇒ String (readonly)
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, , self) new_node.start @nodes << new_node end |
#best_node ⇒ Object
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
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 |