Cuberite
A lightweight, fast and extensible game server for Minecraft
StatSerializer.cpp
Go to the documentation of this file.
1 
2 // StatSerializer.cpp
3 
4 
5 #include "Globals.h"
6 #include "StatSerializer.h"
7 
8 #include "../Statistics.h"
9 
10 
11 
12 
13 
14 cStatSerializer::cStatSerializer(const AString & a_WorldName, const AString & a_PlayerName, const AString & a_FileName, cStatManager * a_Manager)
15  : m_Manager(a_Manager)
16 {
17  // Even though stats are shared between worlds, they are (usually) saved
18  // inside the folder of the default world.
19 
20  AString StatsPath;
21  Printf(StatsPath, "%s%cstats", a_WorldName.c_str(), cFile::PathSeparator());
22 
23  m_LegacyPath = StatsPath + "/" + a_PlayerName + ".json";
24  m_Path = StatsPath + "/" + a_FileName + ".json";
25 
26  // Ensure that the directory exists.
28 }
29 
30 
31 
32 
33 
35 {
37  if (Data.empty())
38  {
40  if (Data.empty())
41  {
42  return false;
43  }
44  }
45 
46  Json::Value Root;
47  Json::Reader Reader;
48 
49  if (Reader.parse(Data, Root, false))
50  {
51  return LoadStatFromJSON(Root);
52  }
53 
54  return false;
55 }
56 
57 
58 
59 
60 
62 {
63  Json::Value Root;
64  SaveStatToJSON(Root);
65 
66  cFile File;
68  {
69  return false;
70  }
71 
72  Json::StyledWriter Writer;
73  AString JsonData = Writer.write(Root);
74 
75  File.Write(JsonData.data(), JsonData.size());
76  File.Close();
77 
78  return true;
79 }
80 
81 
82 
83 
84 
85 void cStatSerializer::SaveStatToJSON(Json::Value & a_Out)
86 {
87  for (unsigned int i = 0; i < static_cast<unsigned int>(statCount); ++i)
88  {
89  StatValue Value = m_Manager->GetValue(static_cast<eStatistic>(i));
90 
91  if (Value != 0)
92  {
93  const AString & StatName = cStatInfo::GetName(static_cast<eStatistic>(i));
94 
95  a_Out[StatName] = Value;
96  }
97 
98  // TODO 2014-05-11 xdot: Save "progress"
99  }
100 }
101 
102 
103 
104 
105 
106 bool cStatSerializer::LoadStatFromJSON(const Json::Value & a_In)
107 {
108  m_Manager->Reset();
109 
110  for (Json::Value::const_iterator it = a_In.begin() ; it != a_In.end() ; ++it)
111  {
112  AString StatName = it.key().asString();
113 
114  eStatistic StatType = cStatInfo::GetType(StatName);
115 
116  if (StatType == statInvalid)
117  {
118  LOGWARNING("Invalid statistic type \"%s\"", StatName.c_str());
119  continue;
120  }
121 
122  const Json::Value & Node = *it;
123 
124  if (Node.isInt())
125  {
126  m_Manager->SetValue(StatType, Node.asInt());
127  }
128  else if (Node.isObject())
129  {
130  StatValue Value = Node.get("value", 0).asInt();
131 
132  // TODO 2014-05-11 xdot: Load "progress"
133 
134  m_Manager->SetValue(StatType, Value);
135  }
136  else
137  {
138  LOGWARNING("Invalid statistic value for type \"%s\"", StatName.c_str());
139  }
140  }
141 
142  return true;
143 }
144 
145 
146 
147 
148 
149 
150 
151 
StatValue GetValue(const eStatistic a_Stat) const
Return the value of the specified stat.
Definition: Statistics.cpp:150
Class that manages the statistics and achievements of a single player.
Definition: Statistics.h:127
AString m_LegacyPath
bool LoadStatFromJSON(const Json::Value &a_In)
#define FILE_IO_PREFIX
Definition: Globals.h:196
static bool CreateFolder(const AString &a_FolderPath)
Creates a new folder with the specified name.
Definition: File.cpp:454
cStatSerializer(const AString &a_WorldName, const AString &a_PlayerName, const AString &a_FileName, cStatManager *a_Manager)
static AString ReadWholeFile(const AString &a_FileName)
Returns the entire contents of the specified file as a string.
Definition: File.cpp:573
static char PathSeparator()
Definition: File.h:42
cStatManager * m_Manager
Definition: File.h:37
bool Open(const AString &iFileName, eMode iMode)
Definition: File.cpp:50
AString & Printf(AString &str, const char *format, fmt::ArgList args)
Output the formatted text into the string.
Definition: StringUtils.cpp:55
void LOGWARNING(const char *a_Format, fmt::ArgList a_ArgList)
Definition: Logger.cpp:174
static eStatistic GetType(const AString &a_Name)
Name -> Type.
Definition: Statistics.cpp:113
std::string AString
Definition: StringUtils.h:13
eStatistic
Definition: Statistics.h:13
int Write(const void *a_Buffer, size_t a_NumBytes)
Writes up to a_NumBytes bytes from a_Buffer, returns the number of bytes actually written...
Definition: File.cpp:179
void SetValue(const eStatistic a_Stat, const StatValue a_Value)
Set the value of the specified stat.
Definition: Statistics.cpp:161
int StatValue
Definition: Statistics.h:120
void Close(void)
Definition: File.cpp:100
void Reset()
Reset everything.
Definition: Statistics.cpp:185
static const AString & GetName(const eStatistic a_Type)
Type -> Name.
Definition: Statistics.cpp:102
void SaveStatToJSON(Json::Value &a_Out)