<commit_before>desc 'Get status of QLRace servers and save to cache'
task get_server_info: :environment do
  require 'steam-condenser'

  servers = []
  de_ip = Rails.env.production? ? 'localhost' : 'de.qlrace.com'
  ips = [de_ip, 'il.qlrace.com', 'au.qlrace.com']
  ports = [27_960, 27_961, 27_962, 27_963, 27_970, 27_971, 27_972, 27_973]
  ips.each do |ip|
    ports.each { |port| servers << get_server_info(ip, port) }
  end

  ports = [27_960, 27_961, 27_962, 27_963, 27_964, 27_970, 27_971]
  ports.each { |port| servers << get_server_info('kr.qlrace.com', port) }

  data = { time: Time.now.utc.strftime('%H:%M:%S'), servers: servers.compact }
  Rails.cache.write('servers', data)
end

def get_server_info(ip, port)
  server = SourceServer.new(ip, port)
  info = server.server_info

  players = []
  server.players.each do |n, player|
    name = n.gsub(/\^[0-9]/, '') # remove colour codes from names
    time = player.score <= 0 ? 2_147_483_647 : player.score
    players << { name: name, time: time }
  end

  players.sort_by! { |k| k[:time] }
  num_players = "#{info[:number_of_players]}/#{info[:max_players]}"
  { name: info[:server_name], address: "#{ip}:#{port}",
    map: info[:map_name].downcase, num_players: num_players, players: players }
rescue SocketError, Errno::ECONNREFUSED, SteamCondenser::TimeoutError
  return<commit_msg>set ip of server data to de.qlrace.com if localhost<commit_after>desc 'Get status of QLRace servers and save to cache'
task get_server_info: :environment do
  require 'steam-condenser'

  servers = []
  de_ip = Rails.env.production? ? 'localhost' : 'de.qlrace.com'
  ips = [de_ip, 'il.qlrace.com', 'au.qlrace.com']
  ports = [27_960, 27_961, 27_962, 27_963, 27_970, 27_971, 27_972, 27_973]
  ips.each do |ip|
    ports.each { |port| servers << get_server_info(ip, port) }
  end

  ports = [27_960, 27_961, 27_962, 27_963, 27_964, 27_970, 27_971]
  ports.each { |port| servers << get_server_info('kr.qlrace.com', port) }

  data = { time: Time.now.utc.strftime('%H:%M:%S'), servers: servers.compact }
  Rails.cache.write('servers', data)
end

def get_server_info(ip, port)
  server = SourceServer.new(ip, port)
  info = server.server_info

  players = []
  server.players.each do |n, player|
    name = n.gsub(/\^[0-9]/, '') # remove colour codes from names
    time = player.score <= 0 ? 2_147_483_647 : player.score
    players << { name: name, time: time }
  end

  players.sort_by! { |k| k[:time] }
  num_players = "#{info[:number_of_players]}/#{info[:max_players]}"
  ip = 'de.qlrace.com' if ip == 'localhost'
  { name: info[:server_name], address: "#{ip}:#{port}",
    map: info[:map_name].downcase, num_players: num_players, players: players }
rescue SocketError, Errno::ECONNREFUSED, SteamCondenser::TimeoutError
  return
{'fraction_non_alphanumeric': 0.15897620764239365, 'fraction_numerical': 0.06344628695025234, 'mean_word_length': 4.316091954022989, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 3, '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': '219964', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1077, 'n_words': 242}