Implement ServerStatus with rapidjson
This commit is contained in:
parent
480d060360
commit
a53140890d
|
@ -1,15 +1,18 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include "rapidjson/document.h"
|
||||||
#include <sstream>
|
#include "rapidjson/writer.h"
|
||||||
|
#include "rapidjson/stringbuffer.h"
|
||||||
|
|
||||||
using string = std::string;
|
#include <string>
|
||||||
using stringstream = std::stringstream;
|
|
||||||
|
|
||||||
namespace Feather
|
namespace Feather
|
||||||
{
|
{
|
||||||
class ServerStatus
|
class ServerStatus
|
||||||
{
|
{
|
||||||
|
using string = std::string;
|
||||||
|
string cachedJSON;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Version
|
// Version
|
||||||
string versionName = "1.16.1";
|
string versionName = "1.16.1";
|
||||||
|
@ -22,17 +25,51 @@ namespace Feather
|
||||||
// Description
|
// Description
|
||||||
string descriptionText = "";
|
string descriptionText = "";
|
||||||
|
|
||||||
string GetServerStatusJSON()
|
const string &GetServerStatusJSON(bool forceUpdate = false)
|
||||||
{
|
{
|
||||||
// TODO: PLACEHOLDER for real JSON
|
using namespace rapidjson;
|
||||||
stringstream j;
|
|
||||||
j << "{";
|
|
||||||
j << "\"version\":{\"name\":\"" << versionName << "\",\"protocol\":" << protocol << "},";
|
|
||||||
j << "\"players\":{\"max\":" << maxPlayers << ",\"online\":" << numPlayers << ",\"sample\":[]},";
|
|
||||||
j << "\"description\": {\"text\":\"" << descriptionText << "\"}";
|
|
||||||
j << "}";
|
|
||||||
|
|
||||||
return j.str();
|
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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue