FeatherMC/src/ServerStatus.h

76 lines
2.0 KiB
C
Raw Normal View History

2020-07-25 04:44:00 +01:00
#pragma once
2020-08-01 01:56:35 +01:00
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
2020-07-25 04:44:00 +01:00
2020-08-01 01:56:35 +01:00
#include <string>
2020-07-25 04:44:00 +01:00
namespace Feather
2020-07-25 04:44:00 +01:00
{
class ServerStatus
{
2020-08-01 01:56:35 +01:00
using string = std::string;
string cachedJSON;
2020-07-25 04:44:00 +01:00
public:
// Version
2020-08-03 07:31:33 +01:00
string versionName = "1.15.2";//"1.16.1";
int protocol = 578;//736;
2020-07-25 04:44:00 +01:00
// Players
int maxPlayers;
int numPlayers;
// Description
string descriptionText = "";
2020-08-01 01:56:35 +01:00
const string &GetServerStatusJSON(bool forceUpdate = false)
2020-07-25 04:44:00 +01:00
{
2020-08-01 01:56:35 +01:00
using namespace rapidjson;
if (cachedJSON.length() > 0 && !forceUpdate) return cachedJSON;
Document doc;
doc.SetObject();
auto &alloc = doc.GetAllocator();
Value version(kObjectType);
{
Value name;
name.SetString(versionName.c_str(), versionName.length());
version.AddMember("name", name, alloc);
version.AddMember("protocol", protocol, alloc);
}
doc.AddMember("version", version, alloc);
Value players(kObjectType);
{
players.AddMember("max", maxPlayers, alloc);
players.AddMember("online", numPlayers, alloc);
Value sample(kArrayType);
players.AddMember("sample", sample, alloc);
}
doc.AddMember("players", players, alloc);
// TODO: use chat object
Value description(kObjectType);
{
Value text;
text.SetString(descriptionText.c_str(), descriptionText.length());
description.AddMember("text", text, alloc);
}
doc.AddMember("description", description, alloc);
StringBuffer buf;
Writer<StringBuffer> writer(buf);
doc.Accept(writer);
cachedJSON.assign(buf.GetString(), buf.GetLength());
return cachedJSON;
2020-07-25 04:44:00 +01:00
}
};
2020-08-01 00:57:33 +01:00
}