1
0
cuberite-2a/src/WorldStorage/StatSerializer.cpp

147 lines
2.4 KiB
C++
Raw Normal View History

2014-05-11 07:57:06 -04:00
// StatSerializer.cpp
#include "Globals.h"
#include "StatSerializer.h"
#include "../Statistics.h"
2014-05-13 07:53:15 -04:00
cStatSerializer::cStatSerializer(const AString & a_WorldName, const AString & a_PlayerName, cStatManager * a_Manager)
2014-05-11 07:57:06 -04:00
: m_Manager(a_Manager)
{
2014-05-13 07:53:15 -04:00
// Even though stats are shared between worlds, they are (usually) saved
// inside the folder of the default world.
2014-05-11 07:57:06 -04:00
AString StatsPath;
Printf(StatsPath, "%s%cstats", a_WorldName.c_str(), cFile::PathSeparator);
2014-05-11 07:57:06 -04:00
2014-05-11 13:30:54 -04:00
m_Path = StatsPath + "/" + a_PlayerName + ".json";
2014-05-11 07:57:06 -04:00
2014-05-13 07:53:15 -04:00
// Ensure that the directory exists.
2014-05-11 07:57:06 -04:00
cFile::CreateFolder(FILE_IO_PREFIX + StatsPath);
}
bool cStatSerializer::Load(void)
{
AString Data = cFile::ReadWholeFile(FILE_IO_PREFIX + m_Path);
if (Data.empty())
{
return false;
}
Json::Value Root;
Json::Reader Reader;
if (Reader.parse(Data, Root, false))
{
return LoadStatFromJSON(Root);
}
return false;
}
bool cStatSerializer::Save(void)
{
Json::Value Root;
SaveStatToJSON(Root);
cFile File;
if (!File.Open(FILE_IO_PREFIX + m_Path, cFile::fmWrite))
{
return false;
}
Json::StyledWriter Writer;
AString JsonData = Writer.write(Root);
File.Write(JsonData.data(), JsonData.size());
File.Close();
return true;
}
void cStatSerializer::SaveStatToJSON(Json::Value & a_Out)
{
2015-05-24 07:56:56 -04:00
for (unsigned int i = 0; i < static_cast<unsigned int>(statCount); ++i)
2014-05-11 07:57:06 -04:00
{
2015-05-24 07:56:56 -04:00
StatValue Value = m_Manager->GetValue(static_cast<eStatistic>(i));
2014-05-11 07:57:06 -04:00
if (Value != 0)
{
2015-05-24 07:56:56 -04:00
const AString & StatName = cStatInfo::GetName(static_cast<eStatistic>(i));
2014-05-11 07:57:06 -04:00
a_Out[StatName] = Value;
}
2014-05-11 13:30:54 -04:00
// TODO 2014-05-11 xdot: Save "progress"
2014-05-11 07:57:06 -04:00
}
}
bool cStatSerializer::LoadStatFromJSON(const Json::Value & a_In)
{
m_Manager->Reset();
2015-10-19 10:03:55 -04:00
for (Json::Value::const_iterator it = a_In.begin() ; it != a_In.end() ; ++it)
2014-05-11 07:57:06 -04:00
{
AString StatName = it.key().asString();
eStatistic StatType = cStatInfo::GetType(StatName);
if (StatType == statInvalid)
{
2014-05-11 13:30:54 -04:00
LOGWARNING("Invalid statistic type \"%s\"", StatName.c_str());
2014-05-11 07:57:06 -04:00
continue;
}
2015-10-19 10:03:55 -04:00
const Json::Value & Node = *it;
2014-05-11 13:30:54 -04:00
if (Node.isInt())
{
m_Manager->SetValue(StatType, Node.asInt());
}
else if (Node.isObject())
{
StatValue Value = Node.get("value", 0).asInt();
// TODO 2014-05-11 xdot: Load "progress"
m_Manager->SetValue(StatType, Value);
}
else
{
LOGWARNING("Invalid statistic value for type \"%s\"", StatName.c_str());
}
2014-05-11 07:57:06 -04:00
}
return true;
}