Class: ESM::Server

Inherits:
ApplicationRecord show all
Defined in:
lib/esm/model/server.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.correct_id(server_id) ⇒ Object

Checks to see if there are any corrections and provides them for the server id



12
13
14
15
# File 'lib/esm/model/server.rb', line 12

def self.correct_id(server_id)
  checker = DidYouMean::SpellChecker.new(dictionary: server_ids)
  checker.correct(server_id)
end

.server_idsObject



5
6
7
8
9
# File 'lib/esm/model/server.rb', line 5

def self.server_ids
  ESM.cache.fetch("server_ids", expires_in: ESM.config.cache.server_ids) do
    ESM::Database.with_connection { pluck(:server_id) }
  end
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/esm/model/server.rb', line 21

def connected?
  return ESM::Websocket.connected?(server_id) unless v2? # V1

  !connection.nil?
end

#connectionObject



17
18
19
# File 'lib/esm/model/server.rb', line 17

def connection
  ESM::Connection::Server.client(public_id)
end

#execute_sqf!(code, execute_on: "server", player: nil, target: nil) ⇒ Any

Sends the provided SQF code to the linked connection.

@note: The result is ran through a JSON parser. The type may not be what you expect, but it will be consistent. For example, an empty hash map will always be represented by an empty array []

Parameters:

  • code (String)

    Valid and error free SQF code as a string

  • execute_on (String) (defaults to: "server")

    Valid options: "server", "player", "all"

  • player (ESM::User, nil) (defaults to: nil)

    The player who initiated the request Note: This technically can be nil but errors triggered by this function may look weird

  • target (ESM::User, ESM::User::Ephemeral, nil) (defaults to: nil)

    The user to execute the code on if execute_on is "player"

Returns:

  • (Any)

    The result of the SQF code.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/esm/model/server.rb', line 58

def execute_sqf!(code, execute_on: "server", player: nil, target: nil)
  message = ESM::Message.new.set_type(:call)
    .set_data(
      function_name: "ESMs_command_sqf",
      execute_on: "server",
      code: code
    )
    .(player:, target:)

  response = send_message(message).data.result

  # Check if it's JSON like
  result = ESM::JSON.parse(response.to_s)
  return response if result.nil?

  # Check to see if its a hashmap
  possible_hashmap = ESM::Arma::HashMap.from(result)
  return result if possible_hashmap.nil?

  result
end

#log_error(log_message) ⇒ Object

Sends a message to the client with a unique ID then logs the ID to the community's logging channel



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/esm/model/server.rb', line 30

def log_error(log_message)
  uuid = SecureRandom.uuid
  send_error("[#{uuid}] #{log_message}")

  return if community.logging_channel_id.blank?

  ESM.bot.deliver(
    I18n.t("exceptions.extension_error", server_id: server_id, id: uuid),
    to: community.logging_channel_id
  )
end

#status_embed(status, reason: "") ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/esm/model/server.rb', line 80

def status_embed(status, reason: "")
  ESM::Embed.build do |e|
    e.color = (status == :connected) ? :green : :red
    e.title = "#{server_name} (`#{server_id}`)"

    if status == :connected
      e.description = I18n.t("server_connected")
    else
      description = I18n.t("server_disconnect.base")
      description += "\n#{reason}" if reason.present?

      e.description = description

      e.footer = I18n.t("server_disconnect.footer")
    end

    e.add_field(name: I18n.t("uptime"), value: uptime, inline: true)
  end
end