Class: ESM::Command::Server::Server

Inherits:
ApplicationCommand show all
Defined in:
lib/esm/command/server/server.rb

Instance Method Summary collapse

Instance Method Details

#add_connection_info(e) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/esm/command/server/server.rb', line 76

def add_connection_info(e)
  e.add_field(name: I18n.t(:server_id), value: "```#{target_server.server_id}```")
  e.add_field(name: I18n.t(:ip), value: "```#{target_server.server_ip}```", inline: true)
  e.add_field(name: I18n.t(:port), value: "```#{target_server.server_port}```")
  return unless target_server.connected?

  e.add_field(name: I18n.t("commands.server.online_for"), value: "```#{target_server.uptime}```")
  e.add_field(name: I18n.t("commands.server.restart_in"), value: "```#{target_server.time_left_before_restart}```")
end

#add_server_info(e) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/esm/command/server/server.rb', line 86

def add_server_info(e)
  query_response = query_server
  return if query_response.nil?

  e.add_field(name: I18n.t(:map), value: query_response.map_name, inline: true)
  e.add_field(name: I18n.t(:players), value: "#{query_response.number_of_players}/#{query_response.max_players}", inline: true)
  e.add_field(name: I18n.t(:game_version), value: query_response.game_version, inline: true)
end

#add_server_mods(e) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/esm/command/server/server.rb', line 95

def add_server_mods(e)
  return if target_server.server_mods.blank?

  grouped_mods = target_server.server_mods.group_by { |mod| mod.mod_required? ? I18n.t(:required_mods) : I18n.t(:optional_mods) }
  grouped_mods.each do |header, mods|
    mod_field = {name: header, value: [], inline: true}
    process_mods(e, mod_field, mods)
  end
end

#on_executeObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/esm/command/server/server.rb', line 29

def on_execute
  embed =
    ESM::Embed.build do |e|
      e.title = target_server.server_name
      e.color = target_server.connected? ? :green : :red

      if !target_server.connected?
        e.description =
          if target_server.disconnected_at.nil?
            I18n.t("commands.servers.offline")
          else
            I18n.t("commands.servers.offline_for", time: target_server.time_since_last_connection)
          end
      end

      # Server ID, IP, port, status
      add_connection_info(e)

      # Map, players, game version
      add_server_info(e)

      # Mods
      add_server_mods(e)
    end

  reply(embed)
end

#process_mods(e, mod_field, mods) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/esm/command/server/server.rb', line 105

def process_mods(e, mod_field, mods)
  mods.each do |mod|
    # If we have a mod_link, convert that to be a hyperlink
    mod_line =
      if mod.mod_link.blank?
        "#{mod.mod_name} #{mod.mod_version}"
      else
        "[#{mod.mod_name} #{mod.mod_version}](#{mod.mod_link})"
      end

    # If the owner added more mods than our field can hold, send it and create a new field
    if (mod_field[:value].total_size + mod_line.size) > ESM::Embed::Limit::FIELD_VALUE_LENGTH_MAX
      e.add_field(**mod_field)
      mod_field = {name: "#{mod_field[:name]} #{I18n.t(:continued)}", value: [], inline: true}
    end

    mod_field[:value] << mod_line
  end

  e.add_field(**mod_field)
end

#query_serverObject

Command Methods



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/esm/command/server/server.rb', line 60

def query_server
  # I store the connection port. Query port is always +1
  server = SourceServer.new(IPAddr.new(target_server.server_ip), target_server.server_port.to_i + 1)

  # Connect to the server
  server.init

  # This contains:
  #     :protocol_version, :server_name, :map_name, :game_directory, :game_description, :app_id, :number_of_players,
  #     :max_players, :number_of_bots, :dedicated, :operating_system, :password_needed, :secure, :game_version, :server_port,
  #     :server_id, :server_tags, :game_id
  server.server_info.to_istruct
rescue
  nil
end