Counter Strike : Global Offensive Source Code
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.

185 lines
6.1 KiB

  1. //====== Copyright � 1996-2006, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose: Uploads gamestats via the SteamWorks API.
  4. //
  5. //=============================================================================//
  6. #if !defined( STEAMWORKS_GAMESTATS_H ) && !defined( _GAMECONSOLE )
  7. #define STEAMWORKS_GAMESTATS_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "igamesystem.h"
  12. #include "GameEventListener.h"
  13. #include "steam/steam_api.h"
  14. #include "utlvector.h"
  15. #include "../../public/tier1/fmtstr.h"
  16. #include "tier1/utlstring.h"
  17. #include "networkvar.h"
  18. #ifndef _GAMECONSOLE
  19. #include "steam/isteamgamestats.h"
  20. #endif
  21. // Container to hold all the KeyValue stats to send only if the convar "steamworks_immediate_upload" is set to 0.
  22. // Otherwise, the stats are uploaded as they are received.
  23. typedef CUtlVector< KeyValues* > KeyValueStatList;
  24. struct ClientServerSession_t
  25. {
  26. uint64 m_ServerSessionID;
  27. RTime32 m_ConnectTime;
  28. RTime32 m_DisconnectTime;
  29. // const char* m_pszGameType;
  30. void Reset()
  31. {
  32. m_ServerSessionID = 0;
  33. m_ConnectTime = 0;
  34. m_DisconnectTime = 0;
  35. }
  36. };
  37. //used to drive most of the game stat event handlers as well as track basic stats under the hood of CBaseGameStats
  38. class CSteamWorksGameStatsUploader : public CAutoGameSystemPerFrame, public CGameEventListener
  39. {
  40. DECLARE_CLASS_NOBASE( CSteamWorksGameStatsUploader )
  41. public:
  42. #ifndef NO_STEAM
  43. CCallResult<CSteamWorksGameStatsUploader, GameStatsSessionIssued_t> m_CallbackSteamSessionInfoIssued;
  44. void Steam_OnSteamSessionInfoIssued( GameStatsSessionIssued_t *pResult, bool bError );
  45. virtual void OnSteamSessionIssued( GameStatsSessionIssued_t *pResult, bool bError ); // virtual for child classes to be notified
  46. CCallResult<CSteamWorksGameStatsUploader, GameStatsSessionClosed_t> m_CallbackSteamSessionInfoClosed;
  47. void Steam_OnSteamSessionInfoClosed( GameStatsSessionClosed_t *pResult, bool bError );
  48. virtual void OnSteamSessionClosed( GameStatsSessionClosed_t *pResult, bool bError ); // virtual for child classes to be notified
  49. #endif
  50. // called after entities think
  51. #if defined ( GAME_DLL )
  52. virtual void FrameUpdatePostEntityThink() OVERRIDE;
  53. #endif
  54. void StartSession();
  55. void EndSession();
  56. virtual void WriteSessionRow();
  57. int GetNumServerConnects() { return m_iServerConnectCount; }
  58. bool IsCollectingDetails() { return m_bCollectingDetails; }
  59. bool IsCollectingAnyData() { return m_bCollectingAny; }
  60. CSteamWorksGameStatsUploader( const char *pszSystemName, const char *pszSessionConVarName );
  61. virtual ~CSteamWorksGameStatsUploader();
  62. // Init, shutdown
  63. // return true on success. false to abort DLL init!
  64. virtual bool Init() OVERRIDE;
  65. virtual bool IsPerFrame() OVERRIDE { return true; }
  66. virtual void FireGameEvent( IGameEvent *event ) OVERRIDE;
  67. EResult AddStatsForUpload( KeyValues *pKV, bool bSendImmediately=true );
  68. time_t GetTimeSinceEpoch();
  69. void FlushStats();
  70. uint32 GetServerIP() { return m_iServerIP; }
  71. const char* GetHostName() { return m_pzHostName; }
  72. bool IsPassworded() { return m_bPassword; }
  73. RTime32 GetStartTime() { return m_StartTime; }
  74. RTime32 GetEndTime() { return m_EndTime; }
  75. uint64 GetSessionID( void ){ return m_SessionID; }
  76. void ClearSessionID();
  77. void ResetServerState();
  78. protected:
  79. virtual EGameStatsAccountType GetGameStatsAccountType() = 0;
  80. bool AccessToSteamAPI();
  81. ISteamGameStats* GetInterface();
  82. // called before a row is committed, allows derived classes to add sessionIDs, etc.
  83. virtual void AddSessionIDsToTable( int iTableID ) = 0;
  84. virtual void Reset();
  85. void UploadCvars();
  86. bool VerifyInterface();
  87. EResult RequestSessionID();
  88. EResult WriteIntToTable( const int value, uint64 iTableID, const char *pzRow );
  89. EResult WriteInt64ToTable( const uint64 value, uint64 iTableID, const char *pzRow );
  90. EResult WriteFloatToTable( const float value, uint64 iTableID, const char *pzRow );
  91. EResult WriteStringToTable( const char *pzValue, uint64 iTableID, const char *pzRow );
  92. EResult ParseKeyValuesAndSendStats( KeyValues *pKV );
  93. void ServerAddressToInt();
  94. ISteamGameStats* m_SteamWorksInterface;
  95. uint64 m_UserID;
  96. uint32 m_iAppID;
  97. uint32 m_iServerIP;
  98. char m_pzServerIP[MAX_PATH];
  99. char m_pzMapStart[MAX_PATH];
  100. char m_pzHostName[MAX_PATH];
  101. RTime32 m_StartTime;
  102. RTime32 m_EndTime;
  103. bool m_bPassword;
  104. // Session IDs
  105. uint64 m_SessionID;
  106. bool m_SessionIDRequestUnsent;
  107. bool m_SessionIDRequestPending;
  108. bool m_bCollectingAny;
  109. bool m_bCollectingDetails;
  110. bool m_ServiceTicking;
  111. float m_LastServiceTick;
  112. bool m_UploadedStats;
  113. KeyValueStatList m_StatsToSend;
  114. ClientServerSession_t m_ActiveSession;
  115. int m_iServerConnectCount;
  116. CUtlString m_sSessionConVarName;
  117. ConVarRef* m_pSessionConVar;
  118. };
  119. //=============================================================================
  120. //
  121. // Helper functions for creating key values
  122. //
  123. void AddDataToKV( KeyValues* pKV, const char* name, int data );
  124. void AddDataToKV( KeyValues* pKV, const char* name, uint64 data );
  125. void AddDataToKV( KeyValues* pKV, const char* name, float data );
  126. void AddDataToKV( KeyValues* pKV, const char* name, bool data );
  127. void AddDataToKV( KeyValues* pKV, const char* name, const char* data );
  128. void AddDataToKV( KeyValues* pKV, const char* name, const Color& data );
  129. void AddDataToKV( KeyValues* pKV, const char* name, short data );
  130. void AddDataToKV( KeyValues* pKV, const char* name, unsigned data );
  131. void AddDataToKV( KeyValues* pKV, const char* name, const Vector& data );
  132. void AddPositionDataToKV( KeyValues* pKV, const char* name, const Vector &data );
  133. //=============================================================================
  134. //=============================================================================
  135. //
  136. // Helper functions for creating key values from arrays
  137. //
  138. void AddArrayDataToKV( KeyValues* pKV, const char* name, const short *data, unsigned size );
  139. void AddArrayDataToKV( KeyValues* pKV, const char* name, const byte *data, unsigned size );
  140. void AddArrayDataToKV( KeyValues* pKV, const char* name, const unsigned *data, unsigned size );
  141. void AddStringDataToKV( KeyValues* pKV, const char* name, const char *data );
  142. //=============================================================================
  143. #endif // STEAMWORKS_GAMESTATS_H