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.

193 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Uploads gamestats via the SteamWorks API.
  4. //
  5. //=============================================================================//
  6. #ifndef STEAMWORKS_GAMESTATS_H
  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. #ifndef _X360
  16. #include "steam/isteamgamestats.h"
  17. #endif
  18. // Container to hold all the KeyValue stats to send only if the convar "steamworks_immediate_upload" is set to 0.
  19. // Otherwise, the stats are uploaded as they are received.
  20. typedef CUtlVector< KeyValues* > KeyValueStatList;
  21. struct ClientServerSession_t
  22. {
  23. uint64 m_ServerSessionID;
  24. RTime32 m_ConnectTime;
  25. RTime32 m_DisconnectTime;
  26. void Reset()
  27. {
  28. m_ServerSessionID = 0;
  29. m_ConnectTime = 0;
  30. m_DisconnectTime = 0;
  31. }
  32. };
  33. //used to drive most of the game stat event handlers as well as track basic stats under the hood of CBaseGameStats
  34. class CSteamWorksGameStatsUploader : public CAutoGameSystemPerFrame, public CGameEventListener
  35. {
  36. public:
  37. #ifdef CLIENT_DLL
  38. // Called before rendering
  39. virtual void PreRender() {}
  40. // Gets called each frame
  41. virtual void Update( float frametime ) {}
  42. // Called after rendering
  43. virtual void PostRender() {}
  44. void AddClientPerfData( KeyValues *pKV );
  45. void SetServerSessionID( uint64 serverID );
  46. int GetFriendCountInGame();
  47. #ifndef NO_STEAM
  48. STEAM_CALLBACK_MANUAL( CSteamWorksGameStatsUploader, Steam_OnSteamSessionInfoIssued, GameStatsSessionIssued_t, m_CallbackSteamSessionInfoIssued );
  49. STEAM_CALLBACK_MANUAL( CSteamWorksGameStatsUploader, Steam_OnSteamSessionInfoClosed, GameStatsSessionClosed_t, m_CallbackSteamSessionInfoClosed );
  50. #endif
  51. #endif
  52. #ifdef GAME_DLL
  53. #ifndef NO_STEAM
  54. STEAM_GAMESERVER_CALLBACK( CSteamWorksGameStatsUploader, Steam_OnSteamSessionInfoIssued, GameStatsSessionIssued_t, m_CallbackSteamSessionInfoIssued );
  55. STEAM_GAMESERVER_CALLBACK( CSteamWorksGameStatsUploader, Steam_OnSteamSessionInfoClosed, GameStatsSessionClosed_t, m_CallbackSteamSessionInfoClosed );
  56. #endif
  57. #endif
  58. // Called each frame before entities think
  59. virtual void FrameUpdatePreEntityThink() {}
  60. // called after entities think
  61. virtual void FrameUpdatePostEntityThink();
  62. virtual void PreClientUpdate() {}
  63. void StartSession();
  64. void EndSession();
  65. void WriteSessionRow();
  66. #ifdef GAME_DLL
  67. void WriteHostsRow();
  68. #endif
  69. #ifdef CLIENT_DLL
  70. void ClientDisconnect();
  71. void ClearServerSessionID() { m_ServerSessionID = 0 ;}
  72. int GetNumServerConnects() { return m_iServerConnectCount; }
  73. #endif
  74. bool IsCollectingDetails() { return m_bCollectingDetails; }
  75. bool IsCollectingAnyData() { return m_bCollectingAny; }
  76. CSteamWorksGameStatsUploader();
  77. // Init, shutdown
  78. // return true on success. false to abort DLL init!
  79. virtual bool Init();
  80. virtual void PostInit() {}
  81. virtual void Shutdown() {}
  82. // Level init, shutdown
  83. virtual void LevelInitPreEntity() {}
  84. virtual void LevelInitPostEntity() {}
  85. virtual void LevelShutdown();
  86. virtual void OnSave() {}
  87. virtual void OnRestore() {}
  88. virtual void SafeRemoveIfDesired() {}
  89. virtual bool IsPerFrame() { return true; }
  90. virtual void FireGameEvent( IGameEvent *event );
  91. EResult AddStatsForUpload( KeyValues *pKV, bool bSendImmediately=true );
  92. time_t GetTimeSinceEpoch();
  93. void FlushStats();
  94. uint32 GetServerIP() { return m_iServerIP; }
  95. const char* GetHostName() { return m_pzHostName; }
  96. bool IsPassworded() { return m_bPassword; }
  97. RTime32 GetStartTime() { return m_StartTime; }
  98. RTime32 GetEndTime() { return m_EndTime; }
  99. #ifdef CLIENT_DLL
  100. uint64 GetServerSessionID() { return m_ServerSessionID; }
  101. #endif
  102. uint64 GetSessionID() { return m_SessionID; }
  103. void ClearSessionID();
  104. private:
  105. void UploadCvars();
  106. void Reset();
  107. bool VerifyInterface();
  108. int GetHumanCountInGame();
  109. EResult RequestSessionID();
  110. bool AccessToSteamAPI();
  111. EResult WriteIntToTable( const int value, uint64 iTableID, const char *pzRow );
  112. EResult WriteInt64ToTable( const uint64 value, uint64 iTableID, const char *pzRow );
  113. EResult WriteFloatToTable( const float value, uint64 iTableID, const char *pzRow );
  114. EResult WriteStringToTable( const char *pzValue, uint64 iTableID, const char *pzRow );
  115. EResult WriteOptionalFloatToTable( KeyValues *pKV, const char* keyName, uint64 iTableID, const char *pzRow);
  116. EResult WriteOptionalIntToTable( KeyValues *pKV, const char* keyName, uint64 iTableID, const char *pzRow );
  117. ISteamGameStats* GetInterface( void );
  118. EResult ParseKeyValuesAndSendStats( KeyValues *pKV, bool bIncludeClientsServerSessionID = true );
  119. void ServerAddressToInt();
  120. ISteamGameStats* m_SteamWorksInterface;
  121. uint64 m_UserID;
  122. uint32 m_iAppID;
  123. uint32 m_iServerIP;
  124. int m_nClientJoinMethod;
  125. char m_pzServerIP[MAX_PATH];
  126. char m_pzMapStart[MAX_PATH];
  127. char m_pzHostName[MAX_PATH];
  128. RTime32 m_StartTime;
  129. RTime32 m_EndTime;
  130. int m_HumanCntInGame;
  131. int m_FriendCntInGame;
  132. bool m_bPassword;
  133. // Session IDs
  134. uint64 m_SessionID;
  135. bool m_SessionIDRequestUnsent;
  136. bool m_SessionIDRequestPending;
  137. bool m_bCollectingAny;
  138. bool m_bCollectingDetails;
  139. #ifdef CLIENT_DLL
  140. uint64 m_ServerSessionID;
  141. #endif
  142. bool m_ServiceTicking;
  143. float m_LastServiceTick;
  144. bool m_UploadedStats;
  145. KeyValueStatList m_StatsToSend;
  146. ClientServerSession_t m_ActiveSession;
  147. int m_iServerConnectCount;
  148. };
  149. CSteamWorksGameStatsUploader& GetSteamWorksSGameStatsUploader();
  150. #endif // STEAMWORKS_GAMESTATS_H