Class: QstatRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/qstat_request.rb

Constant Summary collapse

MSG_SNIPPET_DELIMITER =
' · '

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ QstatRequest

Returns a new instance of QstatRequest.



6
7
8
# File 'lib/qstat_request.rb', line 6

def initialize(endpoint)
  @endpoint = endpoint
end

Instance Attribute Details

#resultObject

Returns the value of attribute result.



4
5
6
# File 'lib/qstat_request.rb', line 4

def result
  @result
end

Instance Method Details

#embed_summaryObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/qstat_request.rb', line 66

def embed_summary
  info = [@endpoint, game_map]

  info += if !has_spectators?
            ["#{numplayers}/#{maxplayers}"]
          else
            [
              "#{numplayers}/#{maxplayers} players",
              "#{numspectators}/#{maxspectators} spectators"
            ]
          end

  info << join_link

  info.join(MSG_SNIPPET_DELIMITER)
end

#has_players?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/qstat_request.rb', line 91

def has_players?
  numplayers && numplayers > 0
end

#is_empty?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/qstat_request.rb', line 83

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


62
63
64
# File 'lib/qstat_request.rb', line 62

def join_link
  "[Join](http://phobos.baseq.fr:9999/join?url=#{@endpoint})"
end

#player_namesObject



87
88
89
# File 'lib/qstat_request.rb', line 87

def player_names
  players.map(&:name)
end

#server_summaryObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/qstat_request.rb', line 45

def server_summary
  return "#{@endpoint} isn't responding" unless game_map

  info = [name, @endpoint, game_map]

  info += if !has_spectators?
            ["#{numplayers}/#{maxplayers}"]
          else
            [
              "#{numplayers}/#{maxplayers} players",
              "#{numspectators}/#{maxspectators} spectators"
            ]
          end

  info.join(MSG_SNIPPET_DELIMITER)
end

#to_embedObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/qstat_request.rb', line 14

def to_embed
  return nil if is_empty?

  embed = Discordrb::Webhooks::Embed.new

  teams.each do |team|
    embed << team.to_embed_field
  end

  embed
end

#to_full_embedObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/qstat_request.rb', line 26

def to_full_embed
  Discordrb::Webhooks::Embed.new.tap do |embed|
    embed.add_field(
      name: name,
      value: embed_summary,
    )

    teams.each do |team|
      embed << team.to_embed_field
    end
  end
end

#to_messageObject



39
40
41
42
43
# File 'lib/qstat_request.rb', line 39

def to_message
  return server_summary if is_empty?

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