GameStatistics.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. template <typename Handler> void serialize(Handler &h)
  56. {
  57. h & map;
  58. h & timestamp;
  59. h & day;
  60. h & player;
  61. if(h.version >= Handler::Version::STATISTICS_SCREEN)
  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. if(h.version >= Handler::Version::STATISTICS_SCREEN)
  91. {
  92. h & eventCapturedTown;
  93. h & eventDefeatedStrongestHero;
  94. }
  95. h & movementPointsUsed;
  96. }
  97. };
  98. class DLL_LINKAGE StatisticDataSet
  99. {
  100. public:
  101. void add(StatisticDataSetEntry entry);
  102. static StatisticDataSetEntry createEntry(const PlayerState * ps, const CGameState * gs);
  103. std::string toCsv(std::string sep);
  104. std::string writeCsv();
  105. struct PlayerAccumulatedValueStorage // holds some actual values needed for stats
  106. {
  107. int numBattlesNeutral;
  108. int numBattlesPlayer;
  109. int numWinBattlesNeutral;
  110. int numWinBattlesPlayer;
  111. int numHeroSurrendered;
  112. int numHeroEscaped;
  113. TResources spentResourcesForArmy;
  114. TResources spentResourcesForBuildings;
  115. TResources tradeVolume;
  116. si64 movementPointsUsed;
  117. int lastCapturedTownDay;
  118. int lastDefeatedStrongestHeroDay;
  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. if(h.version >= Handler::Version::STATISTICS_SCREEN)
  132. {
  133. h & lastCapturedTownDay;
  134. h & lastDefeatedStrongestHeroDay;
  135. }
  136. }
  137. };
  138. std::vector<StatisticDataSetEntry> data;
  139. std::map<PlayerColor, PlayerAccumulatedValueStorage> accumulatedValues;
  140. template <typename Handler> void serialize(Handler &h)
  141. {
  142. h & data;
  143. h & accumulatedValues;
  144. }
  145. };
  146. class DLL_LINKAGE Statistic
  147. {
  148. static std::vector<const CGMine *> getMines(const CGameState * gs, const PlayerState * ps);
  149. public:
  150. static int getNumberOfArts(const PlayerState * ps);
  151. static int getNumberOfDwellings(const PlayerState * ps);
  152. static si64 getArmyStrength(const PlayerState * ps, bool withTownGarrison = false);
  153. static si64 getTotalExperience(const PlayerState * ps);
  154. static int getIncome(const CGameState * gs, const PlayerState * ps);
  155. static float getMapExploredRatio(const CGameState * gs, PlayerColor player);
  156. static const CGHeroInstance * findBestHero(const CGameState * gs, const PlayerColor & color);
  157. static std::vector<std::vector<PlayerColor>> getRank(std::vector<std::pair<PlayerColor, si64>> stats);
  158. static int getObeliskVisited(const CGameState * gs, const TeamID & t);
  159. static float getObeliskVisitedRatio(const CGameState * gs, const TeamID & t);
  160. static std::map<EGameResID, int> getNumMines(const CGameState * gs, const PlayerState * ps);
  161. static float getTownBuiltRatio(const PlayerState * ps);
  162. };
  163. VCMI_LIB_NAMESPACE_END