76 lines
2.0 KiB
C++
76 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include "rapidjson/document.h"
|
|
#include "rapidjson/writer.h"
|
|
#include "rapidjson/stringbuffer.h"
|
|
|
|
#include <string>
|
|
|
|
namespace Feather
|
|
{
|
|
class ServerStatus
|
|
{
|
|
using string = std::string;
|
|
string cachedJSON;
|
|
|
|
public:
|
|
// Version
|
|
string versionName = "1.15.2";//"1.16.1";
|
|
int protocol = 578;//736;
|
|
|
|
// Players
|
|
int maxPlayers;
|
|
int numPlayers;
|
|
|
|
// Description
|
|
string descriptionText = "";
|
|
|
|
const string &GetServerStatusJSON(bool forceUpdate = false)
|
|
{
|
|
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;
|
|
}
|
|
};
|
|
}
|