lib/tamashii/manager/channel.rb in tamashii-manager-0.1.7 vs lib/tamashii/manager/channel.rb in tamashii-manager-0.2.0

- old
+ new

@@ -1,9 +1,10 @@ -require 'tamashii/manager/channel_pool' +# frozen_string_literal: true module Tamashii module Manager + # :nodoc: class Channel < Set SERVER_ID = 0 class << self def pool @@ -21,11 +22,11 @@ def select_channel(client) case client.type when :checkin servers else - return pool.get_idle || pool.create! if pool[client.tag].nil? + return pool.idle || pool.create! if pool[client.tag].nil? pool[client.tag] end end def subscribe(client) @@ -33,50 +34,62 @@ channel.add(client) client.tag = channel.id pool.ready(channel) - Manager.logger.info("Client #{client.id} subscribe to Channel ##{channel.id}") + Manager.logger.info( + "Client #{client.id} subscribe to Channel ##{channel.id}" + ) channel end def unsubscribe(client) channel = select_channel(client) channel.delete(client) - Manager.logger.info("Client #{client.id} unsubscribe to Channel ##{channel.id}") + Manager.logger.info( + "Client #{client.id} unsubscribe to Channel ##{channel.id}" + ) - if channel.empty? && channel.id != SERVER_ID - pool.idle(channel.id) - Manager.logger.debug("Channel Pool add - ##{channel.id}, available channels: #{pool.idle.size}") - end + idle_channel(channel) if channel.empty? && channel.id != SERVER_ID end + + def idle_channel(channel) + pool.idle(channel.id) + # rubocop:disable Metrics/LineLength + Manager.logger.debug("Channel Pool add - ##{channel.id}, available channels: #{pool.idle.size}") + # rubocop:enable Metrics/LineLength + end end attr_reader :id def initialize(id, *args) super(*args) @id = id end def send_to(channel_id, packet) - return unless channel = Channel.pool[channel_id] + channel = Channel.pool[channel_id] + return unless channel.nil? channel.broadcast(packet) end def broadcast(packet, exclude_server = false) Manager.logger.info("Broadcast \"#{packet}\" to Channel ##{@id}") each do |client| client.send(packet) end + + # rubocop:disable Metrics/LineLength Channel.servers.broadcast(packet) unless id == SERVER_ID || exclude_server + # rubocop:enable Metrics/LineLength end def broadcast_all(packet) - Channel.pool.each do |id, channel| - channel.broadcast(packet, true) unless channel.nil? + Channel.pool.each do |_id, channel| + channel&.broadcast(packet, true) end Channel.servers.broadcast(packet) unless id == SERVER_ID end end end