GameStatistics.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * GameSTatistics.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../GameConstants.h"
  12. #include "../ResourceSet.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class PlayerState;
  15. class CGameState;
  16. class CGHeroInstance;
  17. class CGMine;
  18. struct TeamState;
  19. struct DLL_LINKAGE StatisticDataSetEntry
  20. {
  21. std::string map;
  22. time_t timestamp;
  23. int day;
  24. PlayerColor player;
  25. std::string playerName;
  26. TeamID team;
  27. bool isHuman;
  28. EPlayerStatus status;
  29. TResources resources;
  30. int numberHeroes;
  31. int numberTowns;
  32. int numberArtifacts;
  33. int numberDwellings;
  34. si64 armyStrength;
  35. si64 totalExperience;
  36. int income;
  37. float mapExploredRatio;
  38. float obeliskVisitedRatio;
  39. float townBuiltRatio;
  40. bool hasGrail;
  41. std::map<EGameResID, int> numMines;
  42. int score;
  43. int maxHeroLevel;
  44. int numBattlesNeutral;
  45. int numBattlesPlayer;
  46. int numWinBattlesNeutral;
  47. int numWinBattlesPlayer;
  48. int numHeroSurrendered;
  49. int numHeroEscaped;
  50. TResources spentResourcesForArmy;
  51. TResources spentResourcesForBuildings;
  52. TResources tradeVolume;
  53. bool eventCapturedTown;
  54. bool eventDefeatedStrongestHero;
  55. si64 movementPointsUsed;
  56. void serializeJson(JsonSerializeFormat & handler);
  57. template <typename Handler> void serialize(Handler &h)
  58. {
  59. h & map;
  60. h & timestamp;
  61. h & day;
  62. h & player;
  63. h & playerName;
  64. h & team;
  65. h & isHuman;
  66. h & status;
  67. h & resources;
  68. h & numberHeroes;
  69. h & numberTowns;
  70. h & numberArtifacts;
  71. h & numberDwellings;
  72. h & armyStrength;
  73. h & totalExperience;
  74. h & income;
  75. h & mapExploredRatio;
  76. h & obeliskVisitedRatio;
  77. h & townBuiltRatio;
  78. h & hasGrail;
  79. h & numMines;
  80. h & score;
  81. h & maxHeroLevel;
  82. h & numBattlesNeutral;
  83. h & numBattlesPlayer;
  84. h & numWinBattlesNeutral;
  85. h & numWinBattlesPlayer;
  86. h & numHeroSurrendered;
  87. h & numHeroEscaped;
  88. h & spentResourcesForArmy;
  89. h & spentResourcesForBuildings;
  90. h & tradeVolume;
  91. h & eventCapturedTown;
  92. h & eventDefeatedStrongestHero;
  93. h & movementPointsUsed;
  94. }
  95. };
  96. class DLL_LINKAGE StatisticDataSet
  97. {
  98. public:
  99. void add(StatisticDataSetEntry entry);
  100. static StatisticDataSetEntry createEntry(const PlayerState * ps, const CGameState * gs, const StatisticDataSet & accumulatedData);
  101. std::string toCsv(std::string sep) const;
  102. std::string writeCsv() const;
  103. void serializeJson(JsonSerializeFormat & handler);
  104. struct PlayerAccumulatedValueStorage // holds some actual values needed for stats
  105. {
  106. int numBattlesNeutral;
  107. int numBattlesPlayer;
  108. int numWinBattlesNeutral;
  109. int numWinBattlesPlayer;
  110. int numHeroSurrendered;
  111. int numHeroEscaped;
  112. TResources spentResourcesForArmy;
  113. TResources spentResourcesForBuildings;
  114. TResources tradeVolume;
  115. si64 movementPointsUsed;
  116. int lastCapturedTownDay;
  117. int lastDefeatedStrongestHeroDay;
  118. void serializeJson(JsonSerializeFormat & handler);
  119. template <typename Handler> void serialize(Handler &h)
  120. {
  121. h & numBattlesNeutral;
  122. h & numBattlesPlayer;
  123. h & numWinBattlesNeutral;
  124. h & numWinBattlesPlayer;
  125. h & numHeroSurrendered;
  126. h & numHeroEscaped;
  127. h & spentResourcesForArmy;
  128. h & spentResourcesForBuildings;
  129. h & tradeVolume;
  130. h & movementPointsUsed;
  131. h & lastCapturedTownDay;
  132. h & lastDefeatedStrongestHeroDay;
  133. }
  134. };
  135. std::vector<StatisticDataSetEntry> data;
  136. PlayerAccumulatedValueStorage & getPlayerAccumulator(PlayerColor player);
  137. const PlayerAccumulatedValueStorage & getPlayerAccumulator(PlayerColor player) const;
  138. void filterByTeam(const TeamState * team);
  139. private:
  140. std::map<PlayerColor, PlayerAccumulatedValueStorage> accumulatedValues;
  141. public:
  142. template <typename Handler> void serialize(Handler &h)
  143. {
  144. h & data;
  145. h & accumulatedValues;
  146. }
  147. };
  148. class DLL_LINKAGE Statistic
  149. {
  150. public:
  151. static int getNumberOfArts(const PlayerState * ps);
  152. static int getNumberOfDwellings(const PlayerState * ps);
  153. static si64 getArmyStrength(const PlayerState * ps, bool withTownGarrison = false);
  154. static si64 getTotalExperience(const PlayerState * ps);
  155. static int getIncome(const CGameState * gs, const PlayerState * ps);
  156. static float getMapExploredRatio(const CGameState * gs, PlayerColor player);
  157. static const CGHeroInstance * findBestHero(const CGameState * gs, const PlayerColor & color);
  158. static std::vector<std::vector<PlayerColor>> getRank(std::vector<std::pair<PlayerColor, si64>> stats);
  159. static int getObeliskVisited(const CGameState * gs, const TeamID & t);
  160. static float getObeliskVisitedRatio(const CGameState * gs, const TeamID & t);
  161. static std::map<EGameResID, int> getNumMines(const CGameState * gs, const PlayerState * ps);
  162. static float getTownBuiltRatio(const PlayerState * ps);
  163. };
  164. VCMI_LIB_NAMESPACE_END