Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
3.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "gamestats.h"
  8. void BasicGameStatsRecord_t::Clear()
  9. {
  10. m_nCount = 0;
  11. m_nSeconds = 0;
  12. m_nCommentary = 0;
  13. m_nHDR = 0;
  14. m_nCaptions = 0;
  15. m_bSteam = true;
  16. m_bCyberCafe = false;
  17. Q_memset( m_nSkill, 0, sizeof( m_nSkill ) );
  18. m_nDeaths = 0;
  19. }
  20. void BasicGameStatsRecord_t::SaveToBuffer( CUtlBuffer &buf )
  21. {
  22. buf.PutInt( m_nCount );
  23. buf.PutInt( m_nSeconds );
  24. buf.PutInt( m_nCommentary );
  25. buf.PutInt( m_nHDR );
  26. buf.PutInt( m_nCaptions );
  27. for ( int i = 0; i < 3; ++i )
  28. {
  29. buf.PutInt( m_nSkill[ i ] );
  30. }
  31. buf.PutChar( m_bSteam ? 1 : 0 );
  32. buf.PutChar( m_bCyberCafe ? 1 : 0 );
  33. buf.PutInt( m_nDeaths );
  34. }
  35. bool BasicGameStatsRecord_t::ParseFromBuffer( CUtlBuffer &buf, int iBufferStatsVersion )
  36. {
  37. bool bret = true;
  38. m_nCount = buf.GetInt();
  39. if ( m_nCount > 100000 || m_nCount < 0 )
  40. {
  41. bret = false;
  42. }
  43. m_nSeconds = buf.GetInt();
  44. // Note, don't put the buf.GetInt() in the macro since it'll get evaluated twice!!!
  45. m_nSeconds = MAX( m_nSeconds, 0 );
  46. m_nCommentary = buf.GetInt();
  47. if ( m_nCommentary < 0 || m_nCommentary > 100000 )
  48. {
  49. bret = false;
  50. }
  51. m_nHDR = buf.GetInt();
  52. if ( m_nHDR < 0 || m_nHDR > 100000 )
  53. {
  54. bret = false;
  55. }
  56. m_nCaptions = buf.GetInt();
  57. if ( m_nCaptions < 0 || m_nCaptions > 100000 )
  58. {
  59. bret = false;
  60. }
  61. for ( int i = 0; i < 3; ++i )
  62. {
  63. m_nSkill[ i ] = buf.GetInt();
  64. if ( m_nSkill[ i ] < 0 || m_nSkill[ i ] > 100000 )
  65. {
  66. bret = false;
  67. }
  68. }
  69. if ( iBufferStatsVersion > GAMESTATS_FILE_VERSION_OLD )
  70. {
  71. m_bSteam = buf.GetChar() ? true : false;
  72. }
  73. if ( iBufferStatsVersion > GAMESTATS_FILE_VERSION_OLD2 )
  74. {
  75. m_bCyberCafe = buf.GetChar() ? true : false;
  76. }
  77. if ( iBufferStatsVersion > GAMESTATS_FILE_VERSION_OLD5 )
  78. {
  79. m_nDeaths = buf.GetInt();
  80. }
  81. return bret;
  82. }
  83. void BasicGameStats_t::Clear()
  84. {
  85. m_nSecondsToCompleteGame = 0;
  86. m_Summary.Clear();
  87. m_MapTotals.Purge();
  88. }
  89. void BasicGameStats_t::SaveToBuffer( CUtlBuffer& buf )
  90. {
  91. buf.PutInt( m_nSecondsToCompleteGame );
  92. m_Summary.SaveToBuffer( buf );
  93. int c = m_MapTotals.Count();
  94. buf.PutInt( c );
  95. for ( int i = m_MapTotals.First(); i != m_MapTotals.InvalidIndex(); i = m_MapTotals.Next( i ) )
  96. {
  97. char const *name = m_MapTotals.GetElementName( i );
  98. BasicGameStatsRecord_t &rec = m_MapTotals[ i ];
  99. buf.PutString( name );
  100. rec.SaveToBuffer( buf );
  101. }
  102. buf.PutChar( (char)m_nHL2ChaptureUnlocked );
  103. buf.PutChar( m_bSteam ? 1 : 0 );
  104. buf.PutChar( m_bCyberCafe ? 1 : 0 );
  105. buf.PutShort( (short)m_nDXLevel );
  106. }
  107. BasicGameStatsRecord_t *BasicGameStats_t::FindOrAddRecordForMap( char const *mapname )
  108. {
  109. int idx = m_MapTotals.Find( mapname );
  110. if ( idx == m_MapTotals.InvalidIndex() )
  111. {
  112. idx = m_MapTotals.Insert( mapname );
  113. }
  114. return &m_MapTotals[ idx ];
  115. }
  116. bool BasicGameStats_t::ParseFromBuffer( CUtlBuffer& buf, int iBufferStatsVersion )
  117. {
  118. bool bret = true;
  119. m_nSecondsToCompleteGame = buf.GetInt();
  120. if ( m_nSecondsToCompleteGame < 0 || m_nSecondsToCompleteGame > 10000000 )
  121. {
  122. bret = false;
  123. }
  124. m_Summary.ParseFromBuffer( buf, iBufferStatsVersion );
  125. int c = buf.GetInt();
  126. if ( c > 1024 || c < 0 )
  127. {
  128. bret = false;
  129. }
  130. for ( int i = 0; i < c; ++i )
  131. {
  132. char mapname[ 256 ];
  133. buf.GetString( mapname );
  134. BasicGameStatsRecord_t *rec = FindOrAddRecordForMap( mapname );
  135. bool valid= rec->ParseFromBuffer( buf, iBufferStatsVersion );
  136. if ( !valid )
  137. {
  138. bret = false;
  139. }
  140. }
  141. if ( iBufferStatsVersion >= GAMESTATS_FILE_VERSION_OLD2 )
  142. {
  143. m_nHL2ChaptureUnlocked = (int)buf.GetChar();
  144. m_bSteam = buf.GetChar() ? true : false;
  145. }
  146. if ( iBufferStatsVersion > GAMESTATS_FILE_VERSION_OLD2 )
  147. {
  148. m_bCyberCafe = buf.GetChar() ? true : false;
  149. }
  150. if ( iBufferStatsVersion > GAMESTATS_FILE_VERSION_OLD3 )
  151. {
  152. m_nDXLevel = (int)buf.GetShort();
  153. }
  154. return bret;
  155. }