<commit_before># frozen_string_literal: true

class ActiveReservationCheckerWorker
  include Sidekiq::Worker

  sidekiq_options retry: 1

  def perform(reservation_id)
    @reservation = Reservation.find(reservation_id)
    @server = @reservation.server

    return unless @server

    fetch_server_stats

    if @reservation.server.occupied?
      handle_occupied_server
    else
      handle_empty_server
    end
  end

  private

  def fetch_server_stats
    Rails.cache.delete "server_info_#{@reservation.server_id}"
    @server_info = @server.server_info

    begin
      @server_info.status
      @server_info.fetch_stats
      @server_info.fetch_rcon_status
      save_sdr_info(@server_info) if sdr_info_missing?
      ServerMetric.new(@server_info)
      @server.rcon_exec "sv_logsecret #{@reservation.logsecret}"
    rescue SteamCondenser::Error, Errno::ECONNREFUSED
      Rails.logger.warn "Couldn't update #{@reservation.server.name}"
    end
  end

  def handle_occupied_server
    @reservation.update_column(:last_number_of_players, @server_info.number_of_players)
    @reservation.update_column(:inactive_minute_counter, 0)
    @reservation.warn_nearly_over if @reservation.nearly_over?
    @reservation.apply_api_keys if @reservation.enable_demos_tf?
  end

  def handle_empty_server
    previous_number_of_players = @reservation.last_number_of_players.to_i
    @reservation.update_column(:last_number_of_players, 0)
    @reservation.increment!(:inactive_minute_counter)
    if @reservation.inactive_too_long? && !@reservation.lobby?
      @reservation.user.increment!(:expired_reservations)
      @reservation.update_attribute(:end_instantly, true)
      @reservation.end_reservation
    elsif previous_number_of_players.positive? && (@reservation.starts_at < 30.minutes.ago) && @reservation.auto_end?
      Rails.logger.warn "Automatically ending #{@reservation} because it went from occupied to empty"
      @reservation.end_reservation
    end
  end

  def sdr_info_missing?
    @server.sdr? && @reservation.sdr_ip.nil?
  end

  def save_sdr_info(server_info)
    return if server_info.ip.nil?

    @reservation.update_columns(
      sdr_ip: server_info.ip,
      sdr_port: server_info.port,
      sdr_tv_port: server_info.port + 1
    )
    @reservation.server.update_columns(
      last_sdr_ip: server_info.ip,
      last_sdr_port: server_info.port,
      last_sdr_tv_port: server_info.port + 1
    )
  end
end<commit_msg>Add a status message when a server became available through SDR<commit_after># frozen_string_literal: true

class ActiveReservationCheckerWorker
  include Sidekiq::Worker

  sidekiq_options retry: 1

  def perform(reservation_id)
    @reservation = Reservation.find(reservation_id)
    @server = @reservation.server

    return unless @server

    fetch_server_stats

    if @reservation.server.occupied?
      handle_occupied_server
    else
      handle_empty_server
    end
  end

  private

  def fetch_server_stats
    Rails.cache.delete "server_info_#{@reservation.server_id}"
    @server_info = @server.server_info

    begin
      @server_info.status
      @server_info.fetch_stats
      @server_info.fetch_rcon_status
      save_sdr_info(@server_info) if sdr_info_missing?
      ServerMetric.new(@server_info)
      @server.rcon_exec "sv_logsecret #{@reservation.logsecret}"
    rescue SteamCondenser::Error, Errno::ECONNREFUSED
      Rails.logger.warn "Couldn't update #{@reservation.server.name}"
    end
  end

  def handle_occupied_server
    @reservation.update_column(:last_number_of_players, @server_info.number_of_players)
    @reservation.update_column(:inactive_minute_counter, 0)
    @reservation.warn_nearly_over if @reservation.nearly_over?
    @reservation.apply_api_keys if @reservation.enable_demos_tf?
  end

  def handle_empty_server
    previous_number_of_players = @reservation.last_number_of_players.to_i
    @reservation.update_column(:last_number_of_players, 0)
    @reservation.increment!(:inactive_minute_counter)
    if @reservation.inactive_too_long? && !@reservation.lobby?
      @reservation.user.increment!(:expired_reservations)
      @reservation.update_attribute(:end_instantly, true)
      @reservation.end_reservation
    elsif previous_number_of_players.positive? && (@reservation.starts_at < 30.minutes.ago) && @reservation.auto_end?
      Rails.logger.warn "Automatically ending #{@reservation} because it went from occupied to empty"
      @reservation.end_reservation
    end
  end

  def sdr_info_missing?
    @server.sdr? && @reservation.sdr_ip.nil?
  end

  def save_sdr_info(server_info)
    return if server_info.ip.nil?

    @reservation.update_columns(
      sdr_ip: server_info.ip,
      sdr_port: server_info.port,
      sdr_tv_port: server_info.port + 1
    )
    @reservation.server.update_columns(
      last_sdr_ip: server_info.ip,
      last_sdr_port: server_info.port,
      last_sdr_tv_port: server_info.port + 1
    )
    @reservation.status_update("SDR ready, server available at #{server_info.ip}:#{server_info.port}")
  end
end
{'fraction_non_alphanumeric': 0.08573129589204208, 'fraction_numerical': 0.0027783290335384003, 'mean_word_length': 4.195876288659794, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1554835', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1682, 'n_words': 271}