From c7c3724a3ee0e7a77fe9924ad25c36b6ec8fdd14 Mon Sep 17 00:00:00 2001 From: andrew Date: Sun, 11 May 2014 14:57:06 +0300 Subject: Statistic Manager --- src/WorldStorage/StatSerializer.cpp | 126 ++++++++++++++++++++++++++++++++++++ src/WorldStorage/StatSerializer.h | 53 +++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 src/WorldStorage/StatSerializer.cpp create mode 100644 src/WorldStorage/StatSerializer.h (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/StatSerializer.cpp b/src/WorldStorage/StatSerializer.cpp new file mode 100644 index 000000000..5c6724c60 --- /dev/null +++ b/src/WorldStorage/StatSerializer.cpp @@ -0,0 +1,126 @@ + +// StatSerializer.cpp + + +#include "Globals.h" +#include "StatSerializer.h" + +#include "../Statistics.h" + +#include + + + + + +cStatSerializer::cStatSerializer(const AString& a_WorldName, const AString& a_PlayerName, cStatManager* a_Manager) + : m_Manager(a_Manager) +{ + AString StatsPath; + Printf(StatsPath, "%s/stats", a_WorldName.c_str()); + + m_Path = StatsPath + "/" + a_PlayerName + ".dat"; + + /* 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; + } + } +} + + + + + +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; + } + + m_Manager->SetValue(StatType, (*it).asInt()); + } + + return true; +} + + + + + + + + diff --git a/src/WorldStorage/StatSerializer.h b/src/WorldStorage/StatSerializer.h new file mode 100644 index 000000000..43514465b --- /dev/null +++ b/src/WorldStorage/StatSerializer.h @@ -0,0 +1,53 @@ + +// StatSerializer.h + +// Declares the cStatSerializer class that is used for saving stats into JSON + + + + + +#pragma once + +#include "json/json.h" + + + + + +// fwd: +class cStatManager; + + + + +class cStatSerializer +{ +public: + + cStatSerializer(const AString& a_WorldName, const AString& a_PlayerName, cStatManager* a_Manager); + + bool Load(void); + + bool Save(void); + + +protected: + + void SaveStatToJSON(Json::Value & a_Out); + + bool LoadStatFromJSON(const Json::Value & a_In); + + +private: + + cStatManager* m_Manager; + + AString m_Path; + + +} ; + + + + -- cgit v1.2.3 From e3c6c8f3ddffdc368ebbc1a7688de2ca1b97167c Mon Sep 17 00:00:00 2001 From: andrew Date: Sun, 11 May 2014 20:30:54 +0300 Subject: Fixed stat serialization --- src/WorldStorage/StatSerializer.cpp | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/StatSerializer.cpp b/src/WorldStorage/StatSerializer.cpp index 5c6724c60..66e5e1e9e 100644 --- a/src/WorldStorage/StatSerializer.cpp +++ b/src/WorldStorage/StatSerializer.cpp @@ -7,8 +7,6 @@ #include "../Statistics.h" -#include - @@ -19,7 +17,7 @@ cStatSerializer::cStatSerializer(const AString& a_WorldName, const AString& a_Pl AString StatsPath; Printf(StatsPath, "%s/stats", a_WorldName.c_str()); - m_Path = StatsPath + "/" + a_PlayerName + ".dat"; + m_Path = StatsPath + "/" + a_PlayerName + ".json"; /* Ensure that the directory exists. */ cFile::CreateFolder(FILE_IO_PREFIX + StatsPath); @@ -88,6 +86,8 @@ void cStatSerializer::SaveStatToJSON(Json::Value & a_Out) a_Out[StatName] = Value; } + + // TODO 2014-05-11 xdot: Save "progress" } } @@ -107,11 +107,28 @@ bool cStatSerializer::LoadStatFromJSON(const Json::Value & a_In) if (StatType == statInvalid) { - LOGWARNING("Invalid statistic type %s", StatName.c_str()); + LOGWARNING("Invalid statistic type \"%s\"", StatName.c_str()); continue; } - m_Manager->SetValue(StatType, (*it).asInt()); + 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; -- cgit v1.2.3 From 6c57b38b741787cd53a42b905d27e2d137ae194b Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 11 May 2014 13:44:30 -0700 Subject: Fixed a warning and a complaint about a never-read variable. --- src/WorldStorage/WorldStorage.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/WorldStorage.cpp b/src/WorldStorage/WorldStorage.cpp index 54eaaca5c..6867ad5bc 100644 --- a/src/WorldStorage/WorldStorage.cpp +++ b/src/WorldStorage/WorldStorage.cpp @@ -243,7 +243,6 @@ void cWorldStorage::Execute(void) bool Success; do { - Success = false; if (m_ShouldTerminate) { return; -- cgit v1.2.3 From 3f9e00a3f3486c2845115e495d66f438f90825f3 Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 11 May 2014 16:25:21 -0700 Subject: Fixed a few more switch warnings. --- src/WorldStorage/NBTChunkSerializer.cpp | 37 ++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/NBTChunkSerializer.cpp b/src/WorldStorage/NBTChunkSerializer.cpp index 2ac1d7962..ab80dd7c8 100644 --- a/src/WorldStorage/NBTChunkSerializer.cpp +++ b/src/WorldStorage/NBTChunkSerializer.cpp @@ -391,38 +391,41 @@ void cNBTChunkSerializer::AddFallingBlockEntity(cFallingBlock * a_FallingBlock) void cNBTChunkSerializer::AddMinecartEntity(cMinecart * a_Minecart) { - const char * EntityClass = NULL; - switch (a_Minecart->GetPayload()) - { - case cMinecart::mpNone: EntityClass = "MinecartRideable"; break; - case cMinecart::mpChest: EntityClass = "MinecartChest"; break; - case cMinecart::mpFurnace: EntityClass = "MinecartFurnace"; break; - case cMinecart::mpTNT: EntityClass = "MinecartTNT"; break; - case cMinecart::mpHopper: EntityClass = "MinecartHopper"; break; - default: - { - ASSERT(!"Unhandled minecart payload type"); - return; - } - } // switch (payload) - m_Writer.BeginCompound(""); - AddBasicEntity(a_Minecart, EntityClass); + switch (a_Minecart->GetPayload()) { case cMinecart::mpChest: { + AddBasicEntity(a_Minecart, "MinecartChest"); // Add chest contents into the Items tag: AddMinecartChestContents((cMinecartWithChest *)a_Minecart); break; } - case cMinecart::mpFurnace: { + AddBasicEntity(a_Minecart, "MinecartFurnace"); // TODO: Add "Push" and "Fuel" tags break; } + case cMinecart::mpHopper: + { + AddBasicEntity(a_Minecart, "MinecartHopper"); + // TODO: Add hopper contents? + break; + } + case cMinecart::mpTNT: + { + AddBasicEntity(a_Minecart, "MinecartTNT"); + break; + } + case cMinecart::mpNone: + { + AddBasicEntity(a_Minecart, "MinecartRideable"); + break; + } } // switch (Payload) + m_Writer.EndCompound(); } -- cgit v1.2.3 From 3a5e04d118a88dc33930866546007f59fdba951f Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 11 May 2014 16:54:42 -0700 Subject: More switch warnings. --- src/WorldStorage/NBTChunkSerializer.cpp | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/NBTChunkSerializer.cpp b/src/WorldStorage/NBTChunkSerializer.cpp index ab80dd7c8..7b7cd3c7e 100644 --- a/src/WorldStorage/NBTChunkSerializer.cpp +++ b/src/WorldStorage/NBTChunkSerializer.cpp @@ -653,6 +653,13 @@ void cNBTChunkSerializer::AddHangingEntity(cHangingEntity * a_Hanging) case BLOCK_FACE_YP: m_Writer.AddByte("Dir", (unsigned char)1); break; case BLOCK_FACE_ZM: m_Writer.AddByte("Dir", (unsigned char)0); break; case BLOCK_FACE_ZP: m_Writer.AddByte("Dir", (unsigned char)3); break; + + case BLOCK_FACE_XM: + case BLOCK_FACE_XP: + case BLOCK_FACE_NONE: + { + break; + } } } -- cgit v1.2.3 From 466ff2204f18fda5f4f0f0b3e19f671d57747c24 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 13 May 2014 14:53:15 +0300 Subject: Fixes --- src/WorldStorage/StatSerializer.cpp | 7 +++++-- src/WorldStorage/StatSerializer.h | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/StatSerializer.cpp b/src/WorldStorage/StatSerializer.cpp index 66e5e1e9e..74113941c 100644 --- a/src/WorldStorage/StatSerializer.cpp +++ b/src/WorldStorage/StatSerializer.cpp @@ -11,15 +11,18 @@ -cStatSerializer::cStatSerializer(const AString& a_WorldName, const AString& a_PlayerName, cStatManager* a_Manager) +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. */ + // Ensure that the directory exists. cFile::CreateFolder(FILE_IO_PREFIX + StatsPath); } diff --git a/src/WorldStorage/StatSerializer.h b/src/WorldStorage/StatSerializer.h index 43514465b..72f8d74f1 100644 --- a/src/WorldStorage/StatSerializer.h +++ b/src/WorldStorage/StatSerializer.h @@ -25,10 +25,12 @@ class cStatSerializer { public: - cStatSerializer(const AString& a_WorldName, const AString& a_PlayerName, cStatManager* a_Manager); + cStatSerializer(const AString & a_WorldName, const AString & a_PlayerName, cStatManager * a_Manager); + /* Try to load the player statistics. Returns whether the operation was successful or not. */ bool Load(void); + /* Try to save the player statistics. Returns whether the operation was successful or not. */ bool Save(void); -- cgit v1.2.3