1
0
Fork 0

Scoreboard serialization

This commit is contained in:
andrew 2014-01-20 16:45:40 +02:00
parent 7728f4bcbe
commit ff2302ebd5
3 changed files with 94 additions and 16 deletions

View File

@ -72,7 +72,10 @@ cObjective::eType cObjective::StringToType(const AString & a_Name)
cObjective::cObjective(const AString & a_DisplayName, cObjective::eType a_Type) : m_DisplayName(a_DisplayName), m_Type(a_Type)
cObjective::cObjective(const AString & a_Name, const AString & a_DisplayName, cObjective::eType a_Type)
: m_DisplayName(a_DisplayName)
, m_Name(a_Name)
, m_Type(a_Type)
{}
@ -90,7 +93,7 @@ void cObjective::Reset(void)
cObjective::Score cObjective::GetScore(const AString & a_Name) const
{
ScoreMap::const_iterator it = m_Scores.find(a_Name);
cScoreMap::const_iterator it = m_Scores.find(a_Name);
if (it == m_Scores.end())
{
@ -226,7 +229,7 @@ cScoreboard::cScoreboard()
cObjective* cScoreboard::RegisterObjective(const AString & a_Name, const AString & a_DisplayName, cObjective::eType a_Type)
{
cObjective Objective(a_DisplayName, a_Type);
cObjective Objective(a_Name, a_DisplayName, a_Type);
std::pair<cObjectiveMap::iterator, bool> Status = m_Objectives.insert(cNamedObjective(a_Name, Objective));
@ -355,7 +358,7 @@ void cScoreboard::SetDisplay(const AString & a_Objective, eDisplaySlot a_Slot)
cObjective* cScoreboard::GetObjectiveIn(eDisplaySlot a_Slot)
cObjective * cScoreboard::GetObjectiveIn(eDisplaySlot a_Slot)
{
ASSERT(a_Slot < E_DISPLAY_SLOT_COUNT);

View File

@ -54,10 +54,11 @@ public:
public:
cObjective(const AString & a_DisplayName, eType a_Type);
cObjective(const AString & a_Name, const AString & a_DisplayName, eType a_Type);
eType GetType(void) const { return m_Type; }
const AString & GetName(void) const { return m_Name; }
const AString & GetDisplayName(void) const { return m_DisplayName; }
/// Resets the objective
@ -80,16 +81,19 @@ public:
private:
typedef std::pair<AString, Score> TrackedPlayer;
typedef std::pair<AString, Score> cTrackedPlayer;
typedef std::map<AString, Score> ScoreMap;
typedef std::map<AString, Score> cScoreMap;
ScoreMap m_Scores;
cScoreMap m_Scores;
AString m_DisplayName;
AString m_Name;
eType m_Type;
friend class cScoreboardSerializer;
};
@ -152,6 +156,8 @@ private:
cPlayerNameSet m_Players;
friend class cScoreboardSerializer;
};
@ -198,7 +204,7 @@ public:
void SetDisplay(const AString & a_Objective, eDisplaySlot a_Slot);
cObjective* GetObjectiveIn(eDisplaySlot a_Slot);
cObjective * GetObjectiveIn(eDisplaySlot a_Slot);
/// Execute callback for each objective with the specified type
void ForEachObjectiveWith(cObjective::eType a_Type, cObjectiveCallback& a_Callback);
@ -223,6 +229,8 @@ private:
cObjective* m_Display[E_DISPLAY_SLOT_COUNT];
friend class cScoreboardSerializer;
} ;

View File

@ -109,21 +109,88 @@ bool cScoreboardSerializer::Save(void)
void cScoreboardSerializer::SaveScoreboardToNBT(cFastNBTWriter & a_Writer)
{
a_Writer.BeginCompound("Data");
a_Writer.BeginList("Objectives", TAG_Compound);
a_Writer.BeginList("Objectives", TAG_Compound);
for (cScoreboard::cObjectiveMap::const_iterator it = m_ScoreBoard->m_Objectives.begin(); it != m_ScoreBoard->m_Objectives.end(); ++it)
{
const cObjective & Objective = it->second;
a_Writer.BeginCompound("");
a_Writer.AddString("CriteriaName", cObjective::TypeToString(Objective.GetType()));
a_Writer.AddString("DisplayName", Objective.GetDisplayName());
a_Writer.AddString("Name", it->first);
a_Writer.EndCompound();
}
a_Writer.EndList();
a_Writer.BeginList("PlayerScores", TAG_Compound);
for (cScoreboard::cObjectiveMap::const_iterator it = m_ScoreBoard->m_Objectives.begin(); it != m_ScoreBoard->m_Objectives.end(); ++it)
{
const cObjective & Objective = it->second;
for (cObjective::cScoreMap::const_iterator it2 = Objective.m_Scores.begin(); it2 != Objective.m_Scores.end(); ++it2)
{
a_Writer.BeginCompound("");
a_Writer.AddInt("Score", it2->second);
a_Writer.AddString("Name", it2->first);
a_Writer.AddString("Objective", it->first);
a_Writer.EndCompound();
}
}
a_Writer.EndList();
a_Writer.BeginList("Teams", TAG_Compound);
for (cScoreboard::cTeamMap::const_iterator it = m_ScoreBoard->m_Teams.begin(); it != m_ScoreBoard->m_Teams.end(); ++it)
{
const cTeam & Team = it->second;
a_Writer.BeginCompound("");
a_Writer.AddByte("AllowFriendlyFire", Team.AllowsFriendlyFire() ? 1 : 0);
a_Writer.AddByte("SeeFriendlyInvisibles", Team.CanSeeFriendlyInvisible() ? 1 : 0);
a_Writer.AddString("DisplayName", Team.GetDisplayName());
a_Writer.AddString("Name", it->first);
a_Writer.AddString("Prefix", Team.GetPrefix());
a_Writer.AddString("Suffix", Team.GetSuffix());
a_Writer.BeginList("Players", TAG_String);
for (cTeam::cPlayerNameSet::const_iterator it2 = Team.m_Players.begin(); it2 != Team.m_Players.end(); ++it2)
{
a_Writer.AddString("", *it2);
}
a_Writer.EndList();
a_Writer.BeginList("PlayerScores", TAG_Compound);
a_Writer.EndCompound();
}
a_Writer.EndList();
a_Writer.BeginList("Teams", TAG_Compound);
a_Writer.EndList();
a_Writer.EndList();
a_Writer.EndCompound();
a_Writer.BeginCompound("DisplaySlots");
cObjective * Objective = m_ScoreBoard->GetObjectiveIn(cScoreboard::E_DISPLAY_SLOT_LIST);
a_Writer.AddString("slot_0", (Objective == NULL) ? "" : Objective->GetName());
Objective = m_ScoreBoard->GetObjectiveIn(cScoreboard::E_DISPLAY_SLOT_SIDEBAR);
a_Writer.AddString("slot_1", (Objective == NULL) ? "" : Objective->GetName());
Objective = m_ScoreBoard->GetObjectiveIn(cScoreboard::E_DISPLAY_SLOT_NAME);
a_Writer.AddString("slot_2", (Objective == NULL) ? "" : Objective->GetName());
a_Writer.EndCompound();
}