2
0

GameStatistics.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 DLL_LINKAGE StatisticDataSetEntry
  19. {
  20. std::string map;
  21. time_t timestamp;
  22. int day;
  23. PlayerColor player;
  24. std::string playerName;
  25. TeamID team;
  26. bool isHuman;
  27. EPlayerStatus status;
  28. TResources resources;
  29. int numberHeroes;
  30. int numberTowns;
  31. int numberArtifacts;
  32. int numberDwellings;
  33. si64 armyStrength;
  34. si64 totalExperience;
  35. int income;
  36. float mapExploredRatio;
  37. float obeliskVisitedRatio;
  38. float townBuiltRatio;
  39. bool hasGrail;
  40. std::map<EGameResID, int> numMines;
  41. int score;
  42. int maxHeroLevel;
  43. int numBattlesNeutral;
  44. int numBattlesPlayer;
  45. int numWinBattlesNeutral;
  46. int numWinBattlesPlayer;
  47. int numHeroSurrendered;
  48. int numHeroEscaped;
  49. TResources spentResourcesForArmy;
  50. TResources spentResourcesForBuildings;
  51. TResources tradeVolume;
  52. bool eventCapturedTown;
  53. bool eventDefeatedStrongestHero;
  54. si64 movementPointsUsed;
  55. void serializeJson(JsonSerializeFormat & handler);
  56. template <typename Handler> void serialize(Handler &h)
  57. {
  58. h & map;
  59. h & timestamp;
  60. h & day;
  61. h & player;
  62. h & playerName;
  63. h & team;
  64. h & isHuman;
  65. h & status;
  66. h & resources;
  67. h & numberHeroes;
  68. h & numberTowns;
  69. h & numberArtifacts;
  70. h & numberDwellings;
  71. h & armyStrength;
  72. h & totalExperience;
  73. h & income;
  74. h & mapExploredRatio;
  75. h & obeliskVisitedRatio;
  76. h & townBuiltRatio;
  77. h & hasGrail;
  78. h & numMines;
  79. h & score;
  80. h & maxHeroLevel;
  81. h & numBattlesNeutral;
  82. h & numBattlesPlayer;
  83. h & numWinBattlesNeutral;
  84. h & numWinBattlesPlayer;
  85. h & numHeroSurrendered;
  86. h & numHeroEscaped;
  87. h & spentResourcesForArmy;
  88. h & spentResourcesForBuildings;
  89. h & tradeVolume;
  90. h & eventCapturedTown;
  91. h & eventDefeatedStrongestHero;
  92. h & movementPointsUsed;
  93. }
  94. };
  95. class DLL_LINKAGE StatisticDataSet
  96. {
  97. public:
  98. void add(StatisticDataSetEntry entry);
  99. static StatisticDataSetEntry createEntry(const PlayerState * ps, const CGameState * gs, const StatisticDataSet & accumulatedData);
  100. std::string toCsv(std::string sep) const;
  101. std::string writeCsv() const;
  102. void serializeJson(JsonSerializeFormat & handler);
  103. struct PlayerAccumulatedValueStorage // holds some actual values needed for stats
  104. {
  105. int numBattlesNeutral;
  106. int numBattlesPlayer;
  107. int numWinBattlesNeutral;
  108. int numWinBattlesPlayer;
  109. int numHeroSurrendered;
  110. int numHeroEscaped;
  111. TResources spentResourcesForArmy;
  112. TResources spentResourcesForBuildings;
  113. TResources tradeVolume;
  114. si64 movementPointsUsed;
  115. int lastCapturedTownDay;
  116. int lastDefeatedStrongestHeroDay;
  117. void serializeJson(JsonSerializeFormat & handler);
  118. template <typename Handler> void serialize(Handler &h)
  119. {
  120. h & numBattlesNeutral;
  121. h & numBattlesPlayer;
  122. h & numWinBattlesNeutral;
  123. h & numWinBattlesPlayer;
  124. h & numHeroSurrendered;
  125. h & numHeroEscaped;
  126. h & spentResourcesForArmy;
  127. h & spentResourcesForBuildings;
  128. h & tradeVolume;
  129. h & movementPointsUsed;
  130. h & lastCapturedTownDay;
  131. h & lastDefeatedStrongestHeroDay;
  132. }
  133. };
  134. std::vector<StatisticDataSetEntry> data;
  135. std::map<PlayerColor, PlayerAccumulatedValueStorage> accumulatedValues;
  136. template <typename Handler> void serialize(Handler &h)
  137. {
  138. h & data;
  139. h & accumulatedValues;
  140. }
  141. };
  142. class DLL_LINKAGE Statistic
  143. {
  144. public:
  145. static int getNumberOfArts(const PlayerState * ps);
  146. static int getNumberOfDwellings(const PlayerState * ps);
  147. static si64 getArmyStrength(const PlayerState * ps, bool withTownGarrison = false);
  148. static si64 getTotalExperience(const PlayerState * ps);
  149. static int getIncome(const CGameState * gs, const PlayerState * ps);
  150. static float getMapExploredRatio(const CGameState * gs, PlayerColor player);
  151. static const CGHeroInstance * findBestHero(const CGameState * gs, const PlayerColor & color);
  152. static std::vector<std::vector<PlayerColor>> getRank(std::vector<std::pair<PlayerColor, si64>> stats);
  153. static int getObeliskVisited(const CGameState * gs, const TeamID & t);
  154. static float getObeliskVisitedRatio(const CGameState * gs, const TeamID & t);
  155. static std::map<EGameResID, int> getNumMines(const CGameState * gs, const PlayerState * ps);
  156. static float getTownBuiltRatio(const PlayerState * ps);
  157. };
  158. VCMI_LIB_NAMESPACE_END