<commit_before>require "hlds_log_parser/version"
require "eventmachine"
require "rdoc"

module HldsLogParser

  class Client

    attr_accessor :s

    # Creates a new client which will receive HLDS logs (using UDP)
    #
    # ==== Attributes
    #
    # * +host+ - Hostname / IP Address the server will be running
    # * +port+ - Port to listen to
    def initialize(host, port)
      EM.run {
        @@s = EM::open_datagram_socket(host, port, Handler)
        puts "=> Client is now listening on #{host}:#{port} waiting for HLDS logs"
      }
     end

    public 

    # Stops the running client
    def stop
      @@s.close_connection
      puts "=> Client is now stopped."
    end
  end

  class Handler < EM::Connection
    # Get data from Client and parse using Regexp
    #
    # * match the end of round, with score and victory type
    # * match who killed who with what
    #
    # ==== Attributes
    #
    # * +data+ - Data received by Client from HLDS server (a line of log)
    def receive_data(data)
      if data.gsub(/(: Team ")/).count > 0
        # L 05/10/2000 - 12:34:56: Team "CT" triggered "CTs_Win" (CT "3") (T "0")
        winner, type, score_ct, score_t = data.match(/Team "([A-Z]+)" triggered "([A-Za-z_]+)" \(CT "(\d+)"\) \(T "(\d+)"\)/i).captures
        HldsDisplayer.new("[CT] #{score_ct} - #{score_t} [TE] => Victoire des #{get_winner(winner)} par #{get_type(type)}")
      elsif data.gsub(/(\>" killed ")/).count > 0
        # L 05/10/2000 - 12:34:56: "Killer | Player<66><STEAM_ID_LAN><TERRORIST>" killed "Killed | Player<60><STEAM_ID_LAN><CT>" with "ak47"
        killer, killer_id, killer_team, killed, killed_id, killed_team, weapon = data.match(/: "(.+)<(\d+)><STEAM_ID_LAN><([A-Z]+)>" killed "(.+)<(\d+)><STEAM_ID_LAN><([A-Z]+)>" with "([A-Za-z0-9_]+)/i).captures
        HldsDisplayer.new("[#{killer_team}] #{killer} killed [#{killed_team}] #{killed} with #{weapon}")
      end
    end

    # Format the winner team name
    #
    # ==== Attributes
    #
    # * +winner+ - Round winner (+CT+ or +T+) from logs
    def get_winner(winner)
      case winner
      when "T"
        return "Terroristes"
      else
        return "Anti-Terroristes"
      end
    end
<commit_msg>v0.0.3 => Added 'get_kill_team_name' method<commit_after>require "hlds_log_parser/version"
require "eventmachine"
require "rdoc"

module HldsLogParser

  class Client

    attr_accessor :s

    # Creates a new client which will receive HLDS logs (using UDP)
    #
    # ==== Attributes
    #
    # * +host+ - Hostname / IP Address the server will be running
    # * +port+ - Port to listen to
    def initialize(host, port)
      EM.run {
        @@s = EM::open_datagram_socket(host, port, Handler)
        puts "=> Client is now listening on #{host}:#{port} waiting for HLDS logs"
      }
     end

    public 

    # Stops the running client
    def stop
      @@s.close_connection
      puts "=> Client is now stopped."
    end
  end

  class Handler < EM::Connection
    # Get data from Client and parse using Regexp
    #
    # * match the end of round, with score and victory type
    # * match who killed who with what
    #
    # ==== Attributes
    #
    # * +data+ - Data received by Client from HLDS server (a line of log)
    def receive_data(data)
      if data.gsub(/(: Team ")/).count > 0
        # L 05/10/2000 - 12:34:56: Team "CT" triggered "CTs_Win" (CT "3") (T "0")
        winner, type, score_ct, score_t = data.match(/Team "([A-Z]+)" triggered "([A-Za-z_]+)" \(CT "(\d+)"\) \(T "(\d+)"\)/i).captures
        HldsDisplayer.new("[CT] #{score_ct} - #{score_t} [TE] => Victoire des #{get_winner(winner)} par #{get_type(type)}")
      elsif data.gsub(/(\>" killed ")/).count > 0
        # L 05/10/2000 - 12:34:56: "Killer | Player<66><STEAM_ID_LAN><TERRORIST>" killed "Killed | Player<60><STEAM_ID_LAN><CT>" with "ak47"
        killer, killer_id, killer_team, killed, killed_id, killed_team, weapon = data.match(/: "(.+)<(\d+)><STEAM_ID_LAN><([A-Z]+)>" killed "(.+)<(\d+)><STEAM_ID_LAN><([A-Z]+)>" with "([A-Za-z0-9_]+)/i).captures
        HldsDisplayer.new("[#{get_kill_team_name(killer_team)}] #{killer} killed [#{get_kill_team_name(killed_team)}] #{killed} with #{weapon}")
      end
    end

    # Format the winner team name
    #
    # ==== Attributes
    #
    # * +winner+ - Round winner (+CT+ or +T+) from logs
    def get_winner(winner)
      case winner
      when "T"
        return "Terroristes"
      else
        return "Anti-Terroristes"
      end
    end

    # Format team name on frag 
    #
    # ==== Attributes
    #
    # * +team+ - Round winner (+CT+ or +TERRORIST+) from logs
    def get_kill_team_name(team)
      case team
      when "TERRORIST"
        return "TE"
      else
        return "CT"
      end
    end

{'fraction_non_alphanumeric': 0.1571787358727501, 'fraction_numerical': 0.017371285056509, 'mean_word_length': 2.6177138531415594, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 40, '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': '6502008', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1689, 'n_words': 462}