2024-06-27 15:58:40 +00:00
|
|
|
module shared_utils.server_status;
|
|
|
|
|
|
|
|
import std.json;
|
2024-08-03 18:15:41 +00:00
|
|
|
import std.algorithm;
|
|
|
|
import std.array;
|
2024-06-27 15:58:40 +00:00
|
|
|
|
2024-08-03 18:15:41 +00:00
|
|
|
/**
|
|
|
|
* A struct containing basic information about a single Minecraft server.
|
|
|
|
*/
|
2024-06-27 15:58:40 +00:00
|
|
|
struct ServerStatus {
|
2024-08-03 18:15:41 +00:00
|
|
|
/// A unique identifier for the server, defined by the agent's "servers.json" file.
|
|
|
|
const string identifier = null;
|
|
|
|
/// The human-readable name of the server.
|
|
|
|
const string name = null;
|
|
|
|
/// A description for the server.
|
|
|
|
const string description = null;
|
|
|
|
/// Whether the server is online.
|
|
|
|
const bool online = false;
|
|
|
|
/// The number of players online.
|
|
|
|
const int playersOnline = 0;
|
|
|
|
/// The maximum number of players that the server allows.
|
|
|
|
const int maxPlayers = 0;
|
|
|
|
/// A list of names of all players that are online.
|
|
|
|
const string[] playerNames = [];
|
2024-06-27 15:58:40 +00:00
|
|
|
|
2024-08-03 18:15:41 +00:00
|
|
|
/**
|
|
|
|
* Converts this status to a JSON object.
|
|
|
|
* Returns: The JSON object.
|
|
|
|
*/
|
|
|
|
JSONValue toJsonObject() const {
|
2024-06-27 15:58:40 +00:00
|
|
|
JSONValue obj = JSONValue.emptyObject;
|
|
|
|
obj.object["identifier"] = JSONValue(identifier);
|
|
|
|
obj.object["name"] = JSONValue(name);
|
2024-08-03 18:15:41 +00:00
|
|
|
obj.object["description"] = JSONValue(description);
|
2024-06-27 15:58:40 +00:00
|
|
|
obj.object["online"] = JSONValue(online);
|
|
|
|
obj.object["playersOnline"] = JSONValue(playersOnline);
|
|
|
|
obj.object["maxPlayers"] = JSONValue(maxPlayers);
|
|
|
|
obj.object["playerNames"] = JSONValue.emptyArray;
|
|
|
|
foreach (playerName; playerNames) {
|
|
|
|
obj.object["playerNames"].array ~= JSONValue(playerName);
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2024-08-03 18:15:41 +00:00
|
|
|
/**
|
|
|
|
* Converts this status to a JSON string.
|
|
|
|
* Returns: The JSON string.
|
|
|
|
*/
|
|
|
|
string toJsonString() const {
|
|
|
|
JSONValue obj = toJsonObject();
|
|
|
|
return obj.toJSON();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a status from a JSON object. Throws a JSONException in case of
|
|
|
|
* errors in the provided data.
|
|
|
|
* Params:
|
|
|
|
* obj = The JSON object.
|
|
|
|
* Returns: The server status.
|
|
|
|
*/
|
2024-06-27 15:58:40 +00:00
|
|
|
static ServerStatus fromJsonObject(JSONValue obj) {
|
|
|
|
if (obj.type != JSONType.OBJECT) throw new JSONException("JSON value is not an object.");
|
2024-08-03 18:15:41 +00:00
|
|
|
return ServerStatus(
|
|
|
|
obj.object["identifier"].str,
|
|
|
|
obj.object["name"].str,
|
|
|
|
obj.object["description"].str,
|
|
|
|
obj.object["online"].boolean,
|
|
|
|
cast(int) obj.object["playersOnline"].integer,
|
|
|
|
cast(int) obj.object["maxPlayers"].integer,
|
|
|
|
obj.object["playerNames"].array().map!(node => node.str).array
|
|
|
|
);
|
2024-06-27 15:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 18:15:41 +00:00
|
|
|
/**
|
|
|
|
* Serializes a list of server statuses into a single JSON array.
|
|
|
|
* Params:
|
|
|
|
* statuses = The statuses to serialize.
|
|
|
|
* Returns: The JSON array.
|
|
|
|
*/
|
|
|
|
JSONValue serializeServerStatuses(in ServerStatus[] statuses) {
|
2024-06-27 15:58:40 +00:00
|
|
|
JSONValue arr = JSONValue.emptyArray;
|
|
|
|
foreach (s; statuses) {
|
|
|
|
arr.array ~= s.toJsonObject();
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2024-08-03 18:15:41 +00:00
|
|
|
/**
|
|
|
|
* Deserializes a list of server statuses from a JSON array.
|
|
|
|
* Params:
|
|
|
|
* arr = The JSON array.
|
|
|
|
* Returns: The list of server statuses.
|
|
|
|
*/
|
|
|
|
ServerStatus[] deserializeServerStatuses(in JSONValue arr) {
|
2024-06-27 15:58:40 +00:00
|
|
|
if (arr.type != JSONType.ARRAY) throw new JSONException("JSON value is not an array.");
|
2024-08-03 18:15:41 +00:00
|
|
|
return arr.array().map!(obj => ServerStatus.fromJsonObject(obj)).array;
|
2024-06-27 15:58:40 +00:00
|
|
|
}
|