# frozen_string_literal: true module Steam module Handler # Holds a collection of Handler objects # # @example Create a collection of Handler objects # collection = Handler::Collection.new # collection << MyHandler.new(client) class Collection include Enumerable # Creates an empty list of handlers def initialize @handlers = [] end # Handle a packet. If a handler is found that cares about this packet, # the packet object is passed to the handler. # # @param packet [Networking::Packet] the packet to handle def handle(packet) handler = find_handler(packet.msg_type) if handler.nil? Steam.logger.debug("No hander found for: #{EMsgUtil.new(packet.emsg).name}") return false end handler.handle(packet) end # Add a handler to the collection # # @param handlers [Array] the handler to add def add(*handlers) handlers.each do |handler| @handlers << handler Steam.logger.debug("Added handler #{handler.class.name}") end end # Iterate through each Handler. Allows the collection to act as an # Enumerable def each(&block) @handlers.each(&block) end private # @api private # :nocov: def find_handler(msg) message_handlers = @handlers.select do |handler| handler.handles?(msg) end message_handlers.first end end end end