<commit_before>require 'turntabler/boot'
require 'turntabler/message'
require 'turntabler/snag'
require 'turntabler/song'

module Turntabler
  # Provides access to all of the events that get triggered by incoming messages
  # from the Turntable API
  # @api private
  class Event
    class << self
      # Maps Turntable command => event name
      # @return [Hash<String, String>]
      attr_reader :commands

      # Defines a new event that maps to the given Turntable command.  The
      # block defines how to typecast the data that is received from Turntable.
      # 
      # @param [String] name The name of the event exposed to the rest of the library
      # @param [String] command The Turntable command that this event name maps to
      # @yield [data] Gives the data to typecast to the block
      # @yieldparam [Hash] data The data received from Turntable
      # @yieldreturn The typecasted data that should be passed into any handlers bound to the event
      # @return [nil]
      def handle(name, command = name, &block)
        block ||= lambda {}
        commands[command] = name

        define_method("typecast_#{command}_event", &block)
        protected :"typecast_#{command}_event"
      end
      
      # Determines whether the given command is handled
      # 
      # @param [String] command The command to check for the existence of
      # @return [Boolean] +true+ if the command exists, otherwise +false+
      def command?(command)
        command && commands.include?(command.to_sym)
      end
    end

    @commands = {}

    # An authenticated session is missing
    handle :session_missing, :no_session

    # The client is being asked to disconnect
    handle :session_end_requested, :killdashnine do
      room_id = data['roomid']
      if !room_id || room && room.id == room_id
        client.close(true)
        data['msg'] || 'Unknown reason'
      end
    end

    # The client's connection has closed
    handle :session_ended

    # A heartbeat was received from Turntable to ensure the client's connection
    # is still valid
    handle :heartbeat

    # A response was receivied from a prior command sent to Turntable
    handle :response_received do
      data
    end

    # Information about the room was updated
    handle :room_updated, :update_room do
      room.attributes = data
      room
    end

    # One or more users have entered the room
    handle :user_entered, :registered do
      data['user'].map do |attrs|
        user = room.build_user(attrs)
        room.listeners << user
        user
      end
    end

    # One or more users have left the room
    handle :user_left, :deregistered do
      data['user'].map do |attrs|
        user = room.build_user(attrs)
        room.listeners.delete(user)
        user
      end
    end

    # A user has been booted from the room
    handle :user_booted, :booted_user do
      boot = Boot.new(client, data)
      client.room = nil if boot.user == client.user
      boot
    end

    # A user's name / profile has been updated
    handle :user_updated, :update_user do
      fans_change = data.delete('fans')
      user = room.build_user(data)
      user.attributes = {'fans' => user.fans_count + fans_change}
      user
    end

    # A user's stickers have been updated
    handle :user_updated, :update_sticker_placements do
      room.build_user(data)
    end

    # A user spoke in the chat room
    handle :user_spoke, :speak do
      Message.new(client, data)
    end

    # A new dj was added to the room
    handle :dj_added, :add_dj do
      new_djs = []
      data['user'].each_with_index do |attrs, index|
        new_djs << user = room.build_user(attrs.merge('placements' => data['placements'][index]))
        room.listeners << user
        room.djs << user
      end
      new_djs
    end

    # A dj was removed from the room<commit_msg>Fix user_updated event assuming there was a change in the user's fan count<commit_after>require 'turntabler/boot'
require 'turntabler/message'
require 'turntabler/snag'
require 'turntabler/song'

module Turntabler
  # Provides access to all of the events that get triggered by incoming messages
  # from the Turntable API
  # @api private
  class Event
    class << self
      # Maps Turntable command => event name
      # @return [Hash<String, String>]
      attr_reader :commands

      # Defines a new event that maps to the given Turntable command.  The
      # block defines how to typecast the data that is received from Turntable.
      # 
      # @param [String] name The name of the event exposed to the rest of the library
      # @param [String] command The Turntable command that this event name maps to
      # @yield [data] Gives the data to typecast to the block
      # @yieldparam [Hash] data The data received from Turntable
      # @yieldreturn The typecasted data that should be passed into any handlers bound to the event
      # @return [nil]
      def handle(name, command = name, &block)
        block ||= lambda {}
        commands[command] = name

        define_method("typecast_#{command}_event", &block)
        protected :"typecast_#{command}_event"
      end
      
      # Determines whether the given command is handled
      # 
      # @param [String] command The command to check for the existence of
      # @return [Boolean] +true+ if the command exists, otherwise +false+
      def command?(command)
        command && commands.include?(command.to_sym)
      end
    end

    @commands = {}

    # An authenticated session is missing
    handle :session_missing, :no_session

    # The client is being asked to disconnect
    handle :session_end_requested, :killdashnine do
      room_id = data['roomid']
      if !room_id || room && room.id == room_id
        client.close(true)
        data['msg'] || 'Unknown reason'
      end
    end

    # The client's connection has closed
    handle :session_ended

    # A heartbeat was received from Turntable to ensure the client's connection
    # is still valid
    handle :heartbeat

    # A response was receivied from a prior command sent to Turntable
    handle :response_received do
      data
    end

    # Information about the room was updated
    handle :room_updated, :update_room do
      room.attributes = data
      room
    end

    # One or more users have entered the room
    handle :user_entered, :registered do
      data['user'].map do |attrs|
        user = room.build_user(attrs)
        room.listeners << user
        user
      end
    end

    # One or more users have left the room
    handle :user_left, :deregistered do
      data['user'].map do |attrs|
        user = room.build_user(attrs)
        room.listeners.delete(user)
        user
      end
    end

    # A user has been booted from the room
    handle :user_booted, :booted_user do
      boot = Boot.new(client, data)
      client.room = nil if boot.user == client.user
      boot
    end

    # A user's name / profile has been updated
    handle :user_updated, :update_user do
      fans_change = data.delete('fans')
      user = room.build_user(data)
      user.attributes = {'fans' => user.fans_count + fans_change} if fans_change
      user
    end

    # A user's stickers have been updated
    handle :user_updated, :update_sticker_placements do
      room.build_user(data)
    end

    # A user spoke in the chat room
    handle :user_spoke, :speak do
      Message.new(client, data)
    end

    # A new dj was added to the room
    handle :dj_added, :add_dj do
      new_djs = []
      data['user'].each_with_index do |attrs, index|
        new_djs << user = room.build_user(attrs.merge('placements' => data['placements'][index]))
        room.listeners << user
        room.djs << user
      end
      new_djs
    end

    # A dj was removed from the room
{'fraction_non_alphanumeric': 0.07671618852459017, 'fraction_numerical': 0.0, 'mean_word_length': 2.5414965986394558, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5774174', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2209, 'n_words': 831}