StatsCollector.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: StatsCollector.cpp /////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Electronic Arts Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2002 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // created: Jul 2002
  34. //
  35. // Filename: StatsCollector.cpp
  36. //
  37. // author: Chris Huybregts
  38. //
  39. // purpose: Convinience class to gather player stats
  40. //
  41. //-----------------------------------------------------------------------------
  42. ///////////////////////////////////////////////////////////////////////////////
  43. //-----------------------------------------------------------------------------
  44. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  45. //-----------------------------------------------------------------------------
  46. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  47. //-----------------------------------------------------------------------------
  48. // USER INCLUDES //////////////////////////////////////////////////////////////
  49. //-----------------------------------------------------------------------------
  50. #include "Common/StatsCollector.h"
  51. #include "Common/FileSystem.h"
  52. #include "Common/PlayerList.h"
  53. #include "Common/Player.h"
  54. #include "Common/GlobalData.h"
  55. #include "Common/Money.h"
  56. #include "GameLogic/Object.h"
  57. #include "GameLogic/GameLogic.h"
  58. #include "GameClient/MapUtil.h"
  59. #include "GameNetwork/NetworkUtil.h"
  60. #include "GameNetwork/LANAPICallbacks.h"
  61. //-----------------------------------------------------------------------------
  62. // DEFINES ////////////////////////////////////////////////////////////////////
  63. //-----------------------------------------------------------------------------
  64. StatsCollector *TheStatsCollector = NULL;
  65. static char statsDir[255] = "Stats\\";
  66. //-----------------------------------------------------------------------------
  67. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
  68. //-----------------------------------------------------------------------------
  69. // init all
  70. //=============================================================================
  71. StatsCollector::StatsCollector( void )
  72. {
  73. //Added By Sadullah Nader
  74. //Initialization(s) inserted
  75. m_isScrolling = FALSE;
  76. m_scrollBeginTime = 0;
  77. m_scrollTime = 0;
  78. //
  79. m_timeCount = 0;
  80. m_buildCommands = 0;
  81. m_moveCommands = 0;
  82. m_attackCommands = 0;
  83. m_scrollMapCommands = 0;
  84. m_AIUnits = 0;
  85. m_playerUnits = 0;
  86. m_lastUpdate = 0;
  87. m_startFrame = TheGameLogic->getFrame();
  88. }
  89. //Destructor
  90. //=============================================================================
  91. StatsCollector::~StatsCollector( void )
  92. {
  93. }
  94. // Reset and create the file header
  95. //=============================================================================
  96. void StatsCollector::reset( void )
  97. {
  98. // make sure we have a stats Dir.
  99. #if defined(_DEBUG) || defined(_INTERNAL)
  100. if (TheGlobalData->m_saveStats)
  101. {
  102. AsciiString playtestDir = TheGlobalData->m_baseStatsDir;
  103. playtestDir.concat(statsDir);
  104. if (TheNetwork)
  105. {
  106. if (TheLAN)
  107. {
  108. TheFileSystem->createDirectory(playtestDir);
  109. }
  110. }
  111. }
  112. #endif
  113. TheFileSystem->createDirectory(AsciiString(statsDir));
  114. createFileName();
  115. writeInitialFileInfo();
  116. // zero out
  117. zeroOutStats();
  118. m_lastUpdate = TheGameLogic->getFrame(); // timeGetTime();
  119. }
  120. // Msgs pass through here so we can track whichever ones we want
  121. //=============================================================================
  122. void StatsCollector::collectMsgStats( const GameMessage *msg )
  123. {
  124. // We only care about our own messages.
  125. if(ThePlayerList->getLocalPlayer()->getPlayerIndex() != msg->getPlayerIndex())
  126. return;
  127. switch (msg->getType())
  128. {
  129. case GameMessage::MSG_QUEUE_UNIT_CREATE:
  130. case GameMessage::MSG_DOZER_CONSTRUCT:
  131. case GameMessage::MSG_DOZER_CONSTRUCT_LINE:
  132. {
  133. ++m_buildCommands;
  134. break;
  135. }
  136. }
  137. }
  138. //Loop through all objects and count up the ones we want. (Very Slow!!!)
  139. //=============================================================================
  140. void StatsCollector::collectUnitCountStats( void )
  141. {
  142. for(Object *obj = TheGameLogic->getFirstObject(); obj; obj = obj->getNextObject())
  143. {
  144. if((!(obj->isKindOf(KINDOF_INFANTRY) || obj->isKindOf(KINDOF_VEHICLE))) || ( obj->isNeutralControlled()) ||(obj->getControllingPlayer()->getSide().compare("Civilian") == 0))
  145. continue;
  146. if(obj->getControllingPlayer()->isLocalPlayer())
  147. {
  148. ++m_playerUnits;
  149. }
  150. else
  151. {
  152. ++m_AIUnits;
  153. }
  154. }
  155. }
  156. // call every frame and only do stuff when our time is up
  157. //=============================================================================
  158. void StatsCollector::update( void )
  159. {
  160. if(m_lastUpdate + (TheGlobalData->m_playStats * LOGICFRAMES_PER_SECOND) > TheGameLogic->getFrame())
  161. return;
  162. collectUnitCountStats();
  163. if(m_isScrolling)
  164. {
  165. m_scrollTime += TheGameLogic->getFrame() - m_scrollBeginTime;
  166. m_scrollBeginTime = TheGameLogic->getFrame();
  167. }
  168. m_timeCount += TheGlobalData->m_playStats;
  169. writeStatInfo();
  170. zeroOutStats();
  171. m_lastUpdate = TheGameLogic->getFrame(); //timeGetTime();
  172. }
  173. void StatsCollector::incrementScrollMoveCount( void )
  174. {
  175. ++m_scrollMapCommands;
  176. }
  177. void StatsCollector::incrementAttackCount( void )
  178. {
  179. ++m_attackCommands;
  180. }
  181. void StatsCollector::incrementBuildCount( void )
  182. {
  183. ++m_buildCommands;
  184. }
  185. void StatsCollector::incrementMoveCount( void )
  186. {
  187. ++m_moveCommands;
  188. }
  189. void StatsCollector::writeFileEnd( void )
  190. {
  191. //open the file
  192. FILE *f = fopen(m_statsFileName.str(), "a");
  193. if(!f)
  194. {
  195. DEBUG_ASSERTCRASH(f, ("Unable to open file %s to write", m_statsFileName.str()));
  196. return;
  197. }
  198. m_timeCount += (TheGameLogic->getFrame() - m_lastUpdate) / LOGICFRAMES_PER_SECOND;
  199. writeStatInfo();
  200. fprintf(f, "---------------------------------------------------\n");
  201. // Time
  202. struct tm *newTime;
  203. time_t aclock;
  204. time( &aclock );
  205. newTime = localtime( &aclock );
  206. fprintf(f, "End Time:\t%s\n",asctime(newTime) );
  207. fprintf(f, "=KEY===============================================\n");
  208. fprintf(f, "Time* = The Time Interval\n");
  209. fprintf(f, "BC = Build Commands\n");
  210. fprintf(f, "MC = Move Commands\n");
  211. fprintf(f, "AC = Attack Commands\n");
  212. fprintf(f, "SMC = Scroll Map Commands\n");
  213. fprintf(f, "ST* = Scroll Time in Seconds\n");
  214. fprintf(f, "OC = Other Commands (N/A)\n");
  215. fprintf(f, "$$$ = Local Player's Cash Amount\n");
  216. fprintf(f, "#PU = # of Player's Units\n");
  217. fprintf(f, "#AIU = # of AI's Units\n");
  218. fprintf(f, "===================================================\n");
  219. fprintf(f, "* Times are in Game Seconds which are based off of frames. Current fps is set to %d\n", LOGICFRAMES_PER_SECOND);
  220. #if defined(_DEBUG) || defined(_INTERNAL)
  221. if (TheGlobalData->m_benchmarkTimer > 0)
  222. {
  223. fprintf(f, "\n*** BENCHMARK MODE STATS ***\n");
  224. fprintf(f, " Frames = %d\n", TheGameLogic->getFrame()-m_startFrame);
  225. fprintf(f, "Seconds = %d\n", TheGlobalData->m_benchmarkTimer);
  226. fprintf(f, " FPS = %.2f\n", ((Real)TheGameLogic->getFrame()-(Real)m_startFrame)/(Real)TheGlobalData->m_benchmarkTimer);
  227. }
  228. #endif
  229. fclose(f);
  230. }
  231. void StatsCollector::startScrollTime( void )
  232. {
  233. m_isScrolling = TRUE;
  234. m_scrollBeginTime = TheGameLogic->getFrame();
  235. ++m_scrollMapCommands;
  236. }
  237. void StatsCollector::endScrollTime( void )
  238. {
  239. if(!m_isScrolling)
  240. return;
  241. m_isScrolling = FALSE;
  242. m_scrollTime += TheGameLogic->getFrame() - m_scrollBeginTime;
  243. }
  244. //-----------------------------------------------------------------------------
  245. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
  246. //-----------------------------------------------------------------------------
  247. void StatsCollector::zeroOutStats( void )
  248. {
  249. m_buildCommands = 0;
  250. m_moveCommands = 0;
  251. m_attackCommands = 0;
  252. m_scrollMapCommands = 0;
  253. m_AIUnits = 0;
  254. m_playerUnits = 0;
  255. m_scrollTime = 0;
  256. }
  257. // create the filename based off of map time and date
  258. //=============================================================================
  259. void StatsCollector::createFileName( void )
  260. {
  261. m_statsFileName.clear();
  262. // Date and Time
  263. char datestr[256] = "";
  264. time_t longTime;
  265. struct tm *curtime;
  266. time(&longTime);
  267. curtime = localtime(&longTime);
  268. strftime(datestr, 256, "_%b%d_%I%M%p", curtime);
  269. // const MapMetaData *m = TheMapCache->findMap(TheGlobalData->m_mapName);
  270. AsciiString name = TheGlobalData->m_mapName;
  271. const char *fname = name.reverseFind('\\');
  272. if (fname)
  273. name = fname+1;
  274. name.removeLastChar(); // p
  275. name.removeLastChar(); // a
  276. name.removeLastChar(); // m
  277. name.removeLastChar(); // .
  278. m_statsFileName.clear();
  279. #if defined(_DEBUG) || defined(_INTERNAL)
  280. if (TheGlobalData->m_saveStats)
  281. {
  282. m_statsFileName.set(TheGlobalData->m_baseStatsDir);
  283. m_statsFileName.concat(statsDir);
  284. if (TheNetwork)
  285. {
  286. if (TheLAN)
  287. {
  288. GameInfo *game = TheLAN->GetMyGame();
  289. AsciiString players;
  290. AsciiString full;
  291. AsciiString fullPlusNum;
  292. for (Int i=0; i<MAX_SLOTS; ++i)
  293. {
  294. GameSlot *slot = game->getSlot(i);
  295. if (slot && slot->isHuman())
  296. {
  297. AsciiString player;
  298. player.format("%ls_", slot->getName().str());
  299. players.concat(player);
  300. }
  301. }
  302. full.format("%s%s_%d_%d", players.str(), name.str(), game->getSeed(), game->getLocalSlotNum());
  303. AsciiString testString;
  304. testString.format("%s%s.txt", m_statsFileName.str(), full.str());
  305. m_statsFileName = testString;
  306. }
  307. }
  308. else
  309. {
  310. m_statsFileName.format("%s%s%s.txt",statsDir, name.str(),datestr);
  311. }
  312. }
  313. else
  314. #endif
  315. {
  316. m_statsFileName.format("%s%s%s.txt",statsDir, name.str(),datestr);
  317. }
  318. }
  319. // create the header of the file
  320. //=============================================================================
  321. void StatsCollector::writeInitialFileInfo()
  322. {
  323. //open the file
  324. FILE *f = fopen(m_statsFileName.str(), "w");
  325. if(!f)
  326. {
  327. DEBUG_ASSERTCRASH(f, ("Unable to open file %s to write", m_statsFileName.str()));
  328. return;
  329. }
  330. fprintf(f, "---------------------------------------------------\n");
  331. // Time
  332. struct tm *newTime;
  333. time_t aclock;
  334. time( &aclock );
  335. newTime = localtime( &aclock );
  336. fprintf(f, "Date:\t%s",asctime(newTime) );
  337. // Map
  338. fprintf(f, "Map:\t%s\n", TheGlobalData->m_mapName.str());
  339. // Side
  340. fprintf(f, "Side:\t%s\n", ThePlayerList->getLocalPlayer()->getSide().str());
  341. fprintf(f, "---------------------------------------------------\n\n");
  342. fprintf(f, "Time*\tBC\tMC\tAC\tSMC\tST*\tOC\t$$$\t#PU\t#AIU\n");
  343. collectUnitCountStats();
  344. Money *m = ThePlayerList->getLocalPlayer()->getMoney();
  345. fprintf(f, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", 0, m_buildCommands, m_moveCommands, m_attackCommands,
  346. m_scrollMapCommands, 0 ,/*other commands*/0, m->countMoney(),
  347. m_playerUnits, m_AIUnits );
  348. // initial stats
  349. // we don't want a file pointer open for seconds on end... we'll open it each time.
  350. fclose(f);
  351. }
  352. // Write out the stats
  353. //=============================================================================
  354. void StatsCollector::writeStatInfo()
  355. {
  356. //open the file
  357. FILE *f = fopen(m_statsFileName.str(), "a");
  358. if(!f)
  359. {
  360. DEBUG_ASSERTCRASH(f, ("Unable to open file %s to write", m_statsFileName.str()));
  361. return;
  362. }
  363. Money *m = ThePlayerList->getLocalPlayer()->getMoney();
  364. fprintf(f, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", m_timeCount, m_buildCommands, m_moveCommands, m_attackCommands,
  365. m_scrollMapCommands, m_scrollTime / LOGICFRAMES_PER_SECOND, /*other commands*/0,m->countMoney() ,
  366. m_playerUnits, m_AIUnits );
  367. fclose(f);
  368. }