Implement ServerStatus with rapidjson

This commit is contained in:
DankParrot 2020-07-31 17:56:35 -07:00
parent 480d060360
commit a53140890d
1 changed files with 50 additions and 13 deletions

View File

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