diff options
author | Mattes D <github@xoft.cz> | 2014-05-18 22:30:34 +0200 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2014-05-18 22:30:34 +0200 |
commit | ac005681839134018f0162cfb8cb1453e11cbb46 (patch) | |
tree | 41cc966732262c0a8f5441932b758344856d6259 /src/WorldStorage/StatSerializer.cpp | |
parent | Merge pull request #1005 from SphinxC0re/LoadPluginfolder (diff) | |
parent | Fixes (diff) | |
download | cuberite-ac005681839134018f0162cfb8cb1453e11cbb46.tar cuberite-ac005681839134018f0162cfb8cb1453e11cbb46.tar.gz cuberite-ac005681839134018f0162cfb8cb1453e11cbb46.tar.bz2 cuberite-ac005681839134018f0162cfb8cb1453e11cbb46.tar.lz cuberite-ac005681839134018f0162cfb8cb1453e11cbb46.tar.xz cuberite-ac005681839134018f0162cfb8cb1453e11cbb46.tar.zst cuberite-ac005681839134018f0162cfb8cb1453e11cbb46.zip |
Diffstat (limited to 'src/WorldStorage/StatSerializer.cpp')
-rw-r--r-- | src/WorldStorage/StatSerializer.cpp | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/WorldStorage/StatSerializer.cpp b/src/WorldStorage/StatSerializer.cpp new file mode 100644 index 000000000..74113941c --- /dev/null +++ b/src/WorldStorage/StatSerializer.cpp @@ -0,0 +1,146 @@ + +// StatSerializer.cpp + + +#include "Globals.h" +#include "StatSerializer.h" + +#include "../Statistics.h" + + + + + +cStatSerializer::cStatSerializer(const AString & a_WorldName, const AString & a_PlayerName, cStatManager * a_Manager) + : m_Manager(a_Manager) +{ + // Even though stats are shared between worlds, they are (usually) saved + // inside the folder of the default world. + + AString StatsPath; + Printf(StatsPath, "%s/stats", a_WorldName.c_str()); + + m_Path = StatsPath + "/" + a_PlayerName + ".json"; + + // Ensure that the directory exists. + 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) +{ + for (unsigned int i = 0; i < (unsigned int)statCount; ++i) + { + StatValue Value = m_Manager->GetValue((eStatistic) i); + + if (Value != 0) + { + const AString & StatName = cStatInfo::GetName((eStatistic) i); + + a_Out[StatName] = Value; + } + + // TODO 2014-05-11 xdot: Save "progress" + } +} + + + + + +bool cStatSerializer::LoadStatFromJSON(const Json::Value & a_In) +{ + m_Manager->Reset(); + + for (Json::ValueIterator it = a_In.begin() ; it != a_In.end() ; ++it) + { + AString StatName = it.key().asString(); + + eStatistic StatType = cStatInfo::GetType(StatName); + + if (StatType == statInvalid) + { + LOGWARNING("Invalid statistic type \"%s\"", StatName.c_str()); + continue; + } + + Json::Value & Node = *it; + + 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()); + } + } + + return true; +} + + + + + + + + |