GameStatistics.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. h & playerName;
  62. h & team;
  63. h & isHuman;
  64. h & status;
  65. h & resources;
  66. h & numberHeroes;
  67. h & numberTowns;
  68. h & numberArtifacts;
  69. h & numberDwellings;
  70. h & armyStrength;
  71. h & totalExperience;
  72. h & income;
  73. h & mapExploredRatio;
  74. h & obeliskVisitedRatio;
  75. h & townBuiltRatio;
  76. h & hasGrail;
  77. h & numMines;
  78. h & score;
  79. h & maxHeroLevel;
  80. h & numBattlesNeutral;
  81. h & numBattlesPlayer;
  82. h & numWinBattlesNeutral;
  83. h & numWinBattlesPlayer;
  84. h & numHeroSurrendered;
  85. h & numHeroEscaped;
  86. h & spentResourcesForArmy;
  87. h & spentResourcesForBuildings;
  88. h & tradeVolume;
  89. h & eventCapturedTown;
  90. h & eventDefeatedStrongestHero;
  91. h & movementPointsUsed;
  92. }
  93. };
  94. class DLL_LINKAGE StatisticDataSet
  95. {
  96. public:
  97. void add(StatisticDataSetEntry entry);
  98. static StatisticDataSetEntry createEntry(const PlayerState * ps, const CGameState * gs);
  99. std::string toCsv(std::string sep);
  100. std::string writeCsv();
  101. struct PlayerAccumulatedValueStorage // holds some actual values needed for stats
  102. {
  103. int numBattlesNeutral;
  104. int numBattlesPlayer;
  105. int numWinBattlesNeutral;
  106. int numWinBattlesPlayer;
  107. int numHeroSurrendered;
  108. int numHeroEscaped;
  109. TResources spentResourcesForArmy;
  110. TResources spentResourcesForBuildings;
  111. TResources tradeVolume;
  112. si64 movementPointsUsed;
  113. int lastCapturedTownDay;
  114. int lastDefeatedStrongestHeroDay;
  115. template <typename Handler> void serialize(Handler &h)
  116. {
  117. h & numBattlesNeutral;
  118. h & numBattlesPlayer;
  119. h & numWinBattlesNeutral;
  120. h & numWinBattlesPlayer;
  121. h & numHeroSurrendered;
  122. h & numHeroEscaped;
  123. h & spentResourcesForArmy;
  124. h & spentResourcesForBuildings;
  125. h & tradeVolume;
  126. h & movementPointsUsed;
  127. h & lastCapturedTownDay;
  128. h & lastDefeatedStrongestHeroDay;
  129. }
  130. };
  131. std::vector<StatisticDataSetEntry> data;
  132. std::map<PlayerColor, PlayerAccumulatedValueStorage> accumulatedValues;
  133. template <typename Handler> void serialize(Handler &h)
  134. {
  135. h & data;
  136. h & accumulatedValues;
  137. }
  138. };
  139. class DLL_LINKAGE Statistic
  140. {
  141. public:
  142. static int getNumberOfArts(const PlayerState * ps);
  143. static int getNumberOfDwellings(const PlayerState * ps);
  144. static si64 getArmyStrength(const PlayerState * ps, bool withTownGarrison = false);
  145. static si64 getTotalExperience(const PlayerState * ps);
  146. static int getIncome(const CGameState * gs, const PlayerState * ps);
  147. static float getMapExploredRatio(const CGameState * gs, PlayerColor player);
  148. static const CGHeroInstance * findBestHero(const CGameState * gs, const PlayerColor & color);
  149. static std::vector<std::vector<PlayerColor>> getRank(std::vector<std::pair<PlayerColor, si64>> stats);
  150. static int getObeliskVisited(const CGameState * gs, const TeamID & t);
  151. static float getObeliskVisitedRatio(const CGameState * gs, const TeamID & t);
  152. static std::map<EGameResID, int> getNumMines(const CGameState * gs, const PlayerState * ps);
  153. static float getTownBuiltRatio(const PlayerState * ps);
  154. };
  155. VCMI_LIB_NAMESPACE_END