Sha256: 98723cbb233b67b7885b699c31fb06b26a26032feb816712ed088ecaabbb75d3

Contents?: true

Size: 1.89 KB

Versions: 24

Compression:

Stored size: 1.89 KB

Contents

class QstatRequest
  attr_accessor :result

  def initialize(endpoint)
    @endpoint = endpoint
  end

  def result
    @result ||= execute
  end

  def to_embed
    return nil if is_empty?

    embed = Discordrb::Webhooks::Embed.new

    teams.each do |team|
      embed.add_field(team.to_embed_field)
    end

    embed
  end

  def to_message
    return server_summary if is_empty?

    [server_summary, player_table].join("\n")
  end

  def server_summary
    return "#{@endpoint} isn't responding" unless game_map
    return "#{name} | #{@endpoint} | #{game_map} | #{numplayers}/#{maxplayers}" unless has_spectators?

    "#{name} | #{@endpoint} | #{game_map} | #{numplayers}/#{maxplayers} players | #{numspectators}/#{maxspectators} spectators"
  end

  def is_empty?
    !has_players? && !has_spectators?
  end

  def player_names
    players.map(&:name)
  end

  def has_players?
    numplayers && numplayers > 0
  end

  private

  def has_spectators?
    numspectators && numspectators > 0
  end

  def teams
    @teams ||= build_roster
  end

  def data
    @data ||= JSON.parse(result).first
  end

  def execute
    `qstat -json -P -qws #{@endpoint}`
  end

  def player_table
    players.sort_by { |player| player.team.number }.map(&:to_row).join("\n")
  end

  def name
    data['name']
  end

  def address
    data['address']
  end

  def game_map
    data['map']
  end

  def numplayers
    data['numplayers']
  end

  def maxplayers
    data['maxplayers']
  end

  def numspectators
    data['numspectators']
  end

  def maxspectators
    data['maxspectators']
  end

  def build_roster
    return nil if is_empty?

    roster = Roster.new

    data['players'].map do |player_data|
      player = Player.new(player_data)
      roster.enroll(player)
    end

    roster.teams.sort_by { |team| team.number }
  end

  def players
    data['players'].map do |player_data|
      Player.new(player_data)
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
qwtf_discord_bot-5.4.13 lib/qstat_request.rb
qwtf_discord_bot-5.4.12 lib/qstat_request.rb
qwtf_discord_bot-5.4.11 lib/qstat_request.rb
qwtf_discord_bot-5.4.10 lib/qstat_request.rb
qwtf_discord_bot-5.4.9 lib/qstat_request.rb
qwtf_discord_bot-5.4.8 lib/qstat_request.rb
qwtf_discord_bot-5.4.7 lib/qstat_request.rb
qwtf_discord_bot-5.4.6 lib/qstat_request.rb
qwtf_discord_bot-5.4.5 lib/qstat_request.rb
qwtf_discord_bot-5.4.4 lib/qstat_request.rb
qwtf_discord_bot-5.4.3 lib/qstat_request.rb
qwtf_discord_bot-5.4.2 lib/qstat_request.rb
qwtf_discord_bot-5.4.1 lib/qstat_request.rb
qwtf_discord_bot-5.4.0 lib/qstat_request.rb
qwtf_discord_bot-5.3.7 lib/qstat_request.rb
qwtf_discord_bot-5.3.6 lib/qstat_request.rb
qwtf_discord_bot-5.3.5 lib/qstat_request.rb
qwtf_discord_bot-5.3.4 lib/qstat_request.rb
qwtf_discord_bot-5.3.3 lib/qstat_request.rb
qwtf_discord_bot-5.3.2 lib/qstat_request.rb