Ver Fonte

add statistic json serializer

Laserlicht há 4 meses atrás
pai
commit
068aee409a

+ 10 - 0
lib/constants/EntityIdentifiers.cpp

@@ -462,6 +462,16 @@ std::string PlayerColor::entityType()
 	return "playerColor";
 }
 
+std::string TeamID::encode(int32_t index)
+{
+	return std::to_string(index);
+}
+
+si32 TeamID::decode(const std::string & identifier)
+{
+	return std::stoi(identifier);
+}
+
 si32 PrimarySkill::decode(const std::string& identifier)
 {
 	return resolveIdentifier(entityType(), identifier);

+ 3 - 0
lib/constants/EntityIdentifiers.h

@@ -176,6 +176,9 @@ class TeamID : public StaticIdentifier<TeamID>
 public:
 	using StaticIdentifier<TeamID>::StaticIdentifier;
 
+	static si32 decode(const std::string& identifier);
+	static std::string encode(const si32 index);
+
 	DLL_LINKAGE static const TeamID NO_TEAM;
 };
 

+ 75 - 0
lib/gameState/GameStatistics.cpp

@@ -22,6 +22,8 @@
 #include "../mapObjects/MiscObjects.h"
 #include "../mapping/CMap.h"
 #include "../entities/building/CBuilding.h"
+#include "../serializer/JsonDeserializer.h"
+#include "../serializer/JsonUpdater.h"
 
 
 VCMI_LIB_NAMESPACE_BEGIN
@@ -79,6 +81,79 @@ StatisticDataSetEntry StatisticDataSet::createEntry(const PlayerState * ps, cons
 	return data;
 }
 
+void StatisticDataSetEntry::serializeJson(JsonSerializeFormat & handler)
+{
+	handler.serializeString("map", map);
+	handler.serializeInt("timestamp", timestamp);
+	handler.serializeInt("day", day);
+	handler.serializeId("player", player, PlayerColor::CANNOT_DETERMINE);
+	handler.serializeString("playerName", playerName);
+	handler.serializeId("team", team, TeamID::NO_TEAM);
+	handler.serializeBool("isHuman", isHuman);
+	handler.serializeEnum("status", status, {"wrong", "ingame", "loser", "winner"});
+	resources.serializeJson(handler, "resources");
+	handler.serializeInt("numberHeroes", numberHeroes);
+	handler.serializeInt("numberTowns", numberTowns);
+	handler.serializeInt("numberArtifacts", numberArtifacts);
+	handler.serializeInt("numberDwellings", numberDwellings);
+	handler.serializeInt("armyStrength", armyStrength);
+	handler.serializeInt("totalExperience", totalExperience);
+	handler.serializeInt("income", income);
+	handler.serializeFloat("mapExploredRatio", mapExploredRatio);
+	handler.serializeFloat("obeliskVisitedRatio", obeliskVisitedRatio);
+	handler.serializeFloat("townBuiltRatio", townBuiltRatio);
+	handler.serializeBool("hasGrail", hasGrail);
+	{
+		auto zonesData = handler.enterStruct("numMines");
+		for(TResource idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
+			handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], numMines[idx], 0);
+	}
+	handler.serializeInt("score", score);
+	handler.serializeInt("maxHeroLevel", maxHeroLevel);
+	handler.serializeInt("numBattlesNeutral", numBattlesNeutral);
+	handler.serializeInt("numBattlesPlayer", numBattlesPlayer);
+	handler.serializeInt("numWinBattlesNeutral", numWinBattlesNeutral);
+	handler.serializeInt("numWinBattlesPlayer", numWinBattlesPlayer);
+	handler.serializeInt("numHeroSurrendered", numHeroSurrendered);
+	handler.serializeInt("numHeroEscaped", numHeroEscaped);
+	spentResourcesForArmy.serializeJson(handler, "spentResourcesForArmy");
+	spentResourcesForBuildings.serializeJson(handler, "spentResourcesForBuildings");
+	tradeVolume.serializeJson(handler, "tradeVolume");
+	handler.serializeBool("eventCapturedTown", eventCapturedTown);
+	handler.serializeBool("eventDefeatedStrongestHero", eventDefeatedStrongestHero);
+	handler.serializeInt("movementPointsUsed", movementPointsUsed);
+}
+
+void StatisticDataSet::PlayerAccumulatedValueStorage::serializeJson(JsonSerializeFormat & handler)
+{
+	handler.serializeInt("numBattlesNeutral", numBattlesNeutral);
+	handler.serializeInt("numBattlesPlayer", numBattlesPlayer);
+	handler.serializeInt("numWinBattlesNeutral", numWinBattlesNeutral);
+	handler.serializeInt("numWinBattlesPlayer", numWinBattlesPlayer);
+	handler.serializeInt("numHeroSurrendered", numHeroSurrendered);
+	handler.serializeInt("numHeroEscaped", numHeroEscaped);
+	spentResourcesForArmy.serializeJson(handler, "spentResourcesForArmy");
+	spentResourcesForBuildings.serializeJson(handler, "spentResourcesForBuildings");
+	tradeVolume.serializeJson(handler, "tradeVolume");
+	handler.serializeInt("movementPointsUsed", movementPointsUsed);
+	handler.serializeInt("lastCapturedTownDay", lastCapturedTownDay);
+	handler.serializeInt("lastDefeatedStrongestHeroDay", lastDefeatedStrongestHeroDay);
+}
+
+void StatisticDataSet::serializeJson(JsonSerializeFormat & handler)
+{
+	{
+		auto eventsHandler = handler.enterArray("data");
+		eventsHandler.syncSize(data, JsonNode::JsonType::DATA_VECTOR);
+		eventsHandler.serializeStruct(data);
+	}
+	{
+		auto eventsHandler = handler.enterStruct("accumulatedValues");
+		for(auto & val : accumulatedValues)
+			eventsHandler->serializeStruct(GameConstants::PLAYER_COLOR_NAMES[val.first], val.second);
+	}
+}
+
 std::string StatisticDataSet::toCsv(std::string sep) const
 {
 	std::stringstream ss;

+ 8 - 1
lib/gameState/GameStatistics.h

@@ -57,6 +57,8 @@ struct DLL_LINKAGE StatisticDataSetEntry
 	bool eventDefeatedStrongestHero;
 	si64 movementPointsUsed;
 
+	void serializeJson(JsonSerializeFormat & handler);
+
 	template <typename Handler> void serialize(Handler &h)
 	{
 		h & map;
@@ -99,14 +101,17 @@ struct DLL_LINKAGE StatisticDataSetEntry
 
 class DLL_LINKAGE StatisticDataSet
 {
+	void serializeJson(JsonSerializeFormat & handler);
+
 public:
 	void add(StatisticDataSetEntry entry);
 	static StatisticDataSetEntry createEntry(const PlayerState * ps, const CGameState * gs, const StatisticDataSet & accumulatedData);
 	std::string toCsv(std::string sep) const;
 	std::string writeCsv() const;
 
-	struct PlayerAccumulatedValueStorage // holds some actual values needed for stats
+	class PlayerAccumulatedValueStorage // holds some actual values needed for stats
 	{
+	public:
 		int numBattlesNeutral;
 		int numBattlesPlayer;
 		int numWinBattlesNeutral;
@@ -120,6 +125,8 @@ public:
 		int lastCapturedTownDay;
 		int lastDefeatedStrongestHeroDay;
 
+		void serializeJson(JsonSerializeFormat & handler);
+
 		template <typename Handler> void serialize(Handler &h)
 		{
 			h & numBattlesNeutral;