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.

1154 lines
39 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Uploads KeyValue stats to the new SteamWorks gamestats system.
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "cdll_int.h"
  8. #include "tier2/tier2.h"
  9. #include <time.h>
  10. #ifdef GAME_DLL
  11. #include "gameinterface.h"
  12. #elif CLIENT_DLL
  13. #include "c_playerresource.h"
  14. #endif
  15. #include "steam/isteamutils.h"
  16. #include "steamworks_gamestats.h"
  17. #include "achievementmgr.h"
  18. #include "icommandline.h"
  19. // NOTE: This has to be the last file included!
  20. #include "tier0/memdbgon.h"
  21. #if defined(CLIENT_DLL) || defined(CSTRIKE_DLL)
  22. ConVar steamworks_sessionid_client( "steamworks_sessionid_client", "0", FCVAR_HIDDEN, "The client session ID for the new steamworks gamestats." );
  23. #endif
  24. // This is used to replicate our server id to the client so that client data can be associated with the server's.
  25. ConVar steamworks_sessionid_server( "steamworks_sessionid_server", "0", FCVAR_REPLICATED | FCVAR_HIDDEN, "The server session ID for the new steamworks gamestats." );
  26. // This is used to show when the steam works is uploading stats
  27. #define steamworks_show_uploads 0
  28. // This is a stop gap to disable the steam works game stats from ever initializing in the event that we need it
  29. #if defined(CSTRIKE_DLL)
  30. #define steamworks_stats_disable 1
  31. #else
  32. #define steamworks_stats_disable 0
  33. #endif
  34. // This is used to control when the stats get uploaded. If we wait until the end of the session, we miss out on all the stats if the server crashes. If we upload as we go, then we will have the data
  35. #define steamworks_immediate_upload 1
  36. // If set to zero all cvars will be tracked.
  37. #define steamworks_track_cvar 0
  38. // If set to non zero only cvars that are don't have default values will be tracked, but only if steamworks_track_cvar is also not zero.
  39. #define steamworks_track_cvar_diff_only 1
  40. // Methods that clients connect to servers. note that positive numbers are reserved
  41. // for quickplay sessions
  42. enum
  43. {
  44. k_ClientJoinMethod_Unknown = -1,
  45. k_ClientJoinMethod_ListenServer = -2,
  46. k_ClientJoinMethod_ServerBrowser_UNKNOWN = -3, // server browser, unknown tab
  47. k_ClientJoinMethod_Steam = -4,
  48. k_ClientJoinMethod_Matchmaking = -5,
  49. k_ClientJoinMethod_Coaching = -6,
  50. k_ClientJoinMethod_Redirect = -7,
  51. k_ClientJoinMethod_ServerBrowserInternet = -10,
  52. k_ClientJoinMethod_ServerBrowserFriends = -11,
  53. k_ClientJoinMethod_ServerBrowserFavorites = -12,
  54. k_ClientJoinMethod_ServerBrowserHistory = -13,
  55. k_ClientJoinMethod_ServerBrowserLAN = -14,
  56. k_ClientJoinMethod_ServerBrowserSpectator = -15
  57. };
  58. static CSteamWorksGameStatsUploader g_SteamWorksGameStats;
  59. extern ConVar developer;
  60. #if defined(CLIENT_DLL) || defined(CSTRIKE_DLL)
  61. void Show_Steam_Stats_Session_ID( void )
  62. {
  63. DevMsg( "Client session ID (%s).\n", steamworks_sessionid_client.GetString() );
  64. DevMsg( "Server session ID (%s).\n", steamworks_sessionid_server.GetString() );
  65. }
  66. static ConCommand ShowSteamStatsSessionID( "ShowSteamStatsSessionID", Show_Steam_Stats_Session_ID, "Prints out the game stats session ID's (developer convar must be set to non-zero).", FCVAR_DEVELOPMENTONLY );
  67. #endif
  68. #ifdef CLIENT_DLL
  69. //-----------------------------------------------------------------------------
  70. // Purpose: Clients store the server's session IDs so we can associate client rows with server rows.
  71. //-----------------------------------------------------------------------------
  72. void ServerSessionIDChangeCallback( IConVar *pConVar, const char *pOldString, float flOldValue )
  73. {
  74. ConVarRef var( pConVar );
  75. if ( var.IsValid() )
  76. {
  77. // Treat the variable as a string, since the sessionID is 64 bit and the convar int interface is only 32 bit.
  78. const char* pVarString = var.GetString();
  79. uint64 newServerSessionID = Q_atoi64( pVarString );
  80. g_SteamWorksGameStats.SetServerSessionID( newServerSessionID );
  81. }
  82. }
  83. #endif
  84. //-----------------------------------------------------------------------------
  85. // Purpose: Returns the time since the epoch
  86. //-----------------------------------------------------------------------------
  87. time_t CSteamWorksGameStatsUploader::GetTimeSinceEpoch( void )
  88. {
  89. if ( steamapicontext && steamapicontext->SteamUtils() )
  90. return steamapicontext->SteamUtils()->GetServerRealTime();
  91. else
  92. {
  93. // Default to system time.
  94. time_t aclock;
  95. time( &aclock );
  96. return aclock;
  97. }
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Purpose: Returns a reference to the global object
  101. //-----------------------------------------------------------------------------
  102. CSteamWorksGameStatsUploader& GetSteamWorksSGameStatsUploader()
  103. {
  104. return g_SteamWorksGameStats;
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose: Constructor. Sets up the steam callbacks accordingly depending on client/server dll
  108. //-----------------------------------------------------------------------------
  109. CSteamWorksGameStatsUploader::CSteamWorksGameStatsUploader() : CAutoGameSystemPerFrame( "CSteamWorksGameStatsUploader" )
  110. #if !defined(NO_STEAM) && defined(GAME_DLL)
  111. , m_CallbackSteamSessionInfoIssued( this, &CSteamWorksGameStatsUploader::Steam_OnSteamSessionInfoIssued )
  112. , m_CallbackSteamSessionInfoClosed( this, &CSteamWorksGameStatsUploader::Steam_OnSteamSessionInfoClosed )
  113. #endif
  114. {
  115. #if !defined(NO_STEAM) && defined(CLIENT_DLL)
  116. m_CallbackSteamSessionInfoIssued.Register( this, &CSteamWorksGameStatsUploader::Steam_OnSteamSessionInfoIssued );
  117. m_CallbackSteamSessionInfoClosed.Register( this, &CSteamWorksGameStatsUploader::Steam_OnSteamSessionInfoClosed );
  118. #endif
  119. Reset();
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Purpose: Checks to see if Cvar diff tracking is enabled and if so uploads the diffs
  123. //-----------------------------------------------------------------------------
  124. void CSteamWorksGameStatsUploader::UploadCvars()
  125. {
  126. if ( steamworks_track_cvar )
  127. {
  128. bool bOnlyDiffCvars = steamworks_track_cvar_diff_only;
  129. // Get all the Cvar Differences...
  130. const ConCommandBase *var = g_pCVar->GetCommands();
  131. // Loop through vars and print out findings
  132. for ( ; var; var=var->GetNext() )
  133. {
  134. if ( var->IsCommand() )
  135. continue;
  136. if ( var->IsFlagSet(FCVAR_DEVELOPMENTONLY) || var->IsFlagSet(FCVAR_NEVER_AS_STRING) )
  137. continue;
  138. const char* pDefValue = ((ConVar*)var)->GetDefault();
  139. const char* pValue = ((ConVar*)var)->GetString();
  140. if ( bOnlyDiffCvars && !Q_stricmp( pDefValue, pValue ) )
  141. continue;
  142. KeyValues *pKV = new KeyValues( "Cvars" );
  143. pKV->SetUint64( "TimeSubmitted", GetTimeSinceEpoch() );
  144. pKV->SetString( "CvarID", var->GetName() );
  145. pKV->SetString( "CvarDefValue", pDefValue );
  146. pKV->SetString( "CvarValue", pValue );
  147. ParseKeyValuesAndSendStats( pKV, false );
  148. pKV->deleteThis();
  149. }
  150. static const char* csCLcvars[] =
  151. {
  152. "-threads",
  153. "-high",
  154. "-heap",
  155. "-dxlevel"
  156. };
  157. for ( int hh=0; hh <ARRAYSIZE(csCLcvars) ; ++hh )
  158. {
  159. const char *pszParamValue = CommandLine()->ParmValue( csCLcvars[hh] );
  160. if ( pszParamValue )
  161. {
  162. KeyValues *pKV = new KeyValues( "Cvars" );
  163. pKV->SetUint64( "TimeSubmitted", GetTimeSinceEpoch() );
  164. pKV->SetString( "CvarID", csCLcvars[hh] );
  165. pKV->SetString( "CvarDefValue", "commandline" );
  166. pKV->SetString( "CvarValue", pszParamValue );
  167. ParseKeyValuesAndSendStats( pKV, false );
  168. pKV->deleteThis();
  169. }
  170. }
  171. }
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Purpose: Reset uploader state.
  175. //-----------------------------------------------------------------------------
  176. void CSteamWorksGameStatsUploader::Reset()
  177. {
  178. ClearSessionID();
  179. #ifdef CLIENT_DLL
  180. steamworks_sessionid_client.SetValue( 0 );
  181. ClearServerSessionID();
  182. #endif
  183. m_ServiceTicking = false;
  184. m_LastServiceTick = 0;
  185. m_SessionIDRequestUnsent = false;
  186. m_SessionIDRequestPending = false;
  187. m_UploadedStats = false;
  188. m_bCollectingAny = false;
  189. m_bCollectingDetails = false;
  190. m_UserID = 0;
  191. m_iAppID = 0;
  192. m_iServerIP = 0;
  193. m_nClientJoinMethod = k_ClientJoinMethod_Unknown;
  194. memset( m_pzServerIP, 0, ARRAYSIZE(m_pzServerIP) );
  195. memset( m_pzMapStart, 0, ARRAYSIZE(m_pzMapStart) );
  196. memset( m_pzHostName, 0, ARRAYSIZE(m_pzHostName) );
  197. m_StartTime = 0;
  198. m_EndTime = 0;
  199. m_HumanCntInGame = 0;
  200. m_FriendCntInGame = 0;
  201. m_ActiveSession.Reset();
  202. m_iServerConnectCount = 0;
  203. for ( int i=0; i<m_StatsToSend.Count(); ++i )
  204. {
  205. m_StatsToSend[i]->deleteThis();
  206. }
  207. m_StatsToSend.RemoveAll();
  208. }
  209. //-----------------------------------------------------------------------------
  210. // Purpose: Init function from CAutoGameSystemPerFrame and must return true.
  211. //-----------------------------------------------------------------------------
  212. bool CSteamWorksGameStatsUploader::Init()
  213. {
  214. // ListenForGameEvent( "hostname_changed" );
  215. ListenForGameEvent( "server_spawn" );
  216. #ifdef CLIENT_DLL
  217. ListenForGameEvent( "client_disconnect" );
  218. ListenForGameEvent( "client_beginconnect" );
  219. steamworks_sessionid_server.InstallChangeCallback( ServerSessionIDChangeCallback );
  220. #endif
  221. return true;
  222. }
  223. //-----------------------------------------------------------------------------
  224. // Purpose: Event handler for gathering basic info as well as ending sessions.
  225. //-----------------------------------------------------------------------------
  226. void CSteamWorksGameStatsUploader::FireGameEvent( IGameEvent *event )
  227. {
  228. if ( !event )
  229. {
  230. return;
  231. }
  232. const char *pEventName = event->GetName();
  233. if ( FStrEq( "hostname_changed", pEventName ) )
  234. {
  235. const char *pzHostname = event->GetString( "hostname" );
  236. if ( pzHostname )
  237. {
  238. V_strncpy( m_pzHostName, pzHostname, sizeof( m_pzHostName ) );
  239. }
  240. else
  241. {
  242. V_strncpy( m_pzHostName, "No Host Name", sizeof( m_pzHostName ) );
  243. }
  244. }
  245. else if ( FStrEq( "server_spawn", pEventName ) )
  246. {
  247. #if 0 // the next three disabled if()'s are in the latest l4d2 branch, I'm not sure if they should be brought over
  248. if ( !m_pzServerIP[0] )
  249. #endif
  250. {
  251. const char *pzAddress = event->GetString( "address" );
  252. if ( pzAddress )
  253. {
  254. V_snprintf( m_pzServerIP, ARRAYSIZE(m_pzServerIP), "%s:%d", pzAddress, event->GetInt( "port" ) );
  255. ServerAddressToInt();
  256. }
  257. else
  258. {
  259. V_strncpy( m_pzServerIP, "No Server Address", sizeof( m_pzServerIP ) );
  260. m_iServerIP = 0;
  261. }
  262. }
  263. #if 0
  264. if ( !m_pzHostName[0] )
  265. #endif
  266. {
  267. const char *pzHostname = event->GetString( "hostname" );
  268. if ( pzHostname )
  269. {
  270. V_strncpy( m_pzHostName, pzHostname, sizeof( m_pzHostName ) );
  271. }
  272. else
  273. {
  274. V_strncpy( m_pzHostName, "No Host Name", sizeof( m_pzHostName ) );
  275. }
  276. }
  277. #if 0
  278. if ( !m_pzMapStart[0] )
  279. #endif
  280. {
  281. const char *pzMapName = event->GetString( "mapname" );
  282. if ( pzMapName )
  283. {
  284. V_strncpy( m_pzMapStart, pzMapName, sizeof( m_pzMapStart ) );
  285. }
  286. else
  287. {
  288. V_strncpy( m_pzMapStart, "No Map Name", sizeof( m_pzMapStart ) );
  289. }
  290. }
  291. m_bPassword = event->GetBool( "password" );
  292. }
  293. #ifdef CLIENT_DLL
  294. // Started attempting connection to gameserver
  295. else if ( FStrEq( "client_beginconnect", pEventName ) )
  296. {
  297. const char *pszSource = event->GetString( "source", "" );
  298. m_nClientJoinMethod = k_ClientJoinMethod_Unknown;
  299. if ( pszSource[0] != '\0' )
  300. {
  301. if ( FStrEq( "listenserver", pszSource ) ) m_nClientJoinMethod = k_ClientJoinMethod_ListenServer;
  302. else if ( FStrEq( "serverbrowser", pszSource ) ) m_nClientJoinMethod = k_ClientJoinMethod_ServerBrowser_UNKNOWN;
  303. else if ( FStrEq( "serverbrowser_internet", pszSource ) ) m_nClientJoinMethod = k_ClientJoinMethod_ServerBrowserInternet;
  304. else if ( FStrEq( "serverbrowser_friends", pszSource ) ) m_nClientJoinMethod = k_ClientJoinMethod_ServerBrowserFriends;
  305. else if ( FStrEq( "serverbrowser_favorites", pszSource ) ) m_nClientJoinMethod = k_ClientJoinMethod_ServerBrowserFavorites;
  306. else if ( FStrEq( "serverbrowser_history", pszSource ) ) m_nClientJoinMethod = k_ClientJoinMethod_ServerBrowserHistory;
  307. else if ( FStrEq( "serverbrowser_lan", pszSource ) ) m_nClientJoinMethod = k_ClientJoinMethod_ServerBrowserLAN;
  308. else if ( FStrEq( "serverbrowser_spectator", pszSource ) ) m_nClientJoinMethod = k_ClientJoinMethod_ServerBrowserSpectator;
  309. else if ( FStrEq( "steam", pszSource ) ) m_nClientJoinMethod = k_ClientJoinMethod_Steam;
  310. else if ( FStrEq( "matchmaking", pszSource ) ) m_nClientJoinMethod = k_ClientJoinMethod_Matchmaking;
  311. else if ( FStrEq( "coaching", pszSource ) ) m_nClientJoinMethod = k_ClientJoinMethod_Coaching;
  312. else if ( FStrEq( "redirect", pszSource ) ) m_nClientJoinMethod = k_ClientJoinMethod_Redirect;
  313. else if ( sscanf( pszSource, "quickplay_%d", &m_nClientJoinMethod ) == 1 )
  314. Assert( m_nClientJoinMethod > 0 );
  315. else
  316. Warning("Unrecognized client_beginconnect event 'source' argument: '%s'\n", pszSource );
  317. }
  318. }
  319. else if ( FStrEq( "client_disconnect", pEventName ) )
  320. {
  321. ClientDisconnect();
  322. }
  323. #endif
  324. }
  325. #ifdef CLIENT_DLL
  326. //-----------------------------------------------------------------------------
  327. // Purpose: Sets the server session ID but ONLY if it's not 0. We are using this to avoid a race
  328. // condition where a server sends their session stats before a client does, thereby,
  329. // resetting the client's server session ID to 0.
  330. //-----------------------------------------------------------------------------
  331. void CSteamWorksGameStatsUploader::SetServerSessionID( uint64 serverSessionID )
  332. {
  333. if ( !serverSessionID )
  334. return;
  335. if ( serverSessionID != m_ActiveSession.m_ServerSessionID )
  336. {
  337. m_ActiveSession.m_ServerSessionID = serverSessionID;
  338. m_ActiveSession.m_ConnectTime = GetTimeSinceEpoch();
  339. m_ActiveSession.m_DisconnectTime = 0;
  340. m_iServerConnectCount++;
  341. }
  342. m_ServerSessionID = serverSessionID;
  343. }
  344. //-----------------------------------------------------------------------------
  345. // Purpose: Writes the disconnect time to the current server session entry.
  346. //-----------------------------------------------------------------------------
  347. void CSteamWorksGameStatsUploader::ClientDisconnect()
  348. {
  349. // Save client join method, reset it to make sure it gets set properly
  350. // next time and we don't just reuse the current method
  351. int nClientJoinMethod = m_nClientJoinMethod;
  352. m_nClientJoinMethod = k_ClientJoinMethod_Unknown;
  353. if ( m_ActiveSession.m_ServerSessionID == 0 )
  354. return;
  355. m_SteamWorksInterface = GetInterface();
  356. if ( !m_SteamWorksInterface )
  357. return;
  358. if ( !IsCollectingAnyData() )
  359. return;
  360. uint64 ulRowID = 0;
  361. m_SteamWorksInterface->AddNewRow( &ulRowID, m_SessionID, "ClientSessionLookup" );
  362. WriteInt64ToTable( m_SessionID, ulRowID, "SessionID" );
  363. WriteInt64ToTable( m_ActiveSession.m_ServerSessionID, ulRowID, "ServerSessionID" );
  364. WriteIntToTable( m_ActiveSession.m_ConnectTime, ulRowID, "ConnectTime" );
  365. WriteIntToTable( GetTimeSinceEpoch(), ulRowID, "DisconnectTime" );
  366. WriteIntToTable( nClientJoinMethod, ulRowID, "ClientJoinMethod" );
  367. m_SteamWorksInterface->CommitRow( ulRowID );
  368. m_ActiveSession.Reset();
  369. }
  370. #endif
  371. //-----------------------------------------------------------------------------
  372. // Purpose: Called when the level shuts down.
  373. //-----------------------------------------------------------------------------
  374. void CSteamWorksGameStatsUploader::LevelShutdown()
  375. {
  376. #ifdef CLIENT_DLL
  377. ClearServerSessionID();
  378. #endif
  379. }
  380. //-----------------------------------------------------------------------------
  381. // Purpose: Requests a session ID from steam.
  382. //-----------------------------------------------------------------------------
  383. EResult CSteamWorksGameStatsUploader::RequestSessionID()
  384. {
  385. // If we have disabled steam works game stats, don't request ids.
  386. if ( steamworks_stats_disable )
  387. {
  388. DevMsg( "Steamworks Stats: No stats collection because steamworks_stats_disable is set to 1.\n" );
  389. return k_EResultAccessDenied;
  390. }
  391. if ( developer.GetInt() == 1 )
  392. {
  393. // DevMsg( "Steamworks Stats: No stats collection because developer is set to 1.\n" );
  394. // return k_EResultAccessDenied;
  395. }
  396. // Do not continue if we already have a session id.
  397. // We must end a session before we can begin a new one.
  398. if ( m_SessionID )
  399. {
  400. return k_EResultOK;
  401. }
  402. // Do not continue if we are waiting a server response for this session request.
  403. if ( m_SessionIDRequestPending )
  404. {
  405. return k_EResultPending;
  406. }
  407. // If a request is unsent, it will be sent as soon as the steam API is available.
  408. m_SessionIDRequestUnsent = true;
  409. // Turn on polling.
  410. m_ServiceTicking = true;
  411. // If we can't use Steam at the moment, we need to wait.
  412. if ( !AccessToSteamAPI() )
  413. {
  414. return k_EResultNoConnection;
  415. }
  416. m_SteamWorksInterface = GetInterface();
  417. if ( m_SteamWorksInterface )
  418. {
  419. int accountType = k_EGameStatsAccountType_Steam;
  420. #ifdef GAME_DLL
  421. if ( engine->IsDedicatedServer() )
  422. {
  423. accountType = k_EGameStatsAccountType_SteamGameServer;
  424. }
  425. #endif
  426. #ifdef CLIENT_DLL
  427. DevMsg( "Steamworks Stats: Requesting CLIENT session id.\n" );
  428. #else
  429. DevMsg( "Steamworks Stats: Requesting SERVER session id.\n" );
  430. #endif
  431. m_SessionIDRequestUnsent = false;
  432. m_SessionIDRequestPending = true;
  433. // This initiates a callback that will get us our session ID.
  434. // Callback: Steam_OnSteamSessionInfoIssued
  435. m_SteamWorksInterface->GetNewSession( accountType, m_UserID, m_iAppID, GetTimeSinceEpoch() );
  436. }
  437. return k_EResultOK;
  438. }
  439. //-----------------------------------------------------------------------------
  440. // Purpose: Clears our session id and session id convar.
  441. //-----------------------------------------------------------------------------
  442. void CSteamWorksGameStatsUploader::ClearSessionID()
  443. {
  444. m_SessionID = 0;
  445. steamworks_sessionid_server.SetValue( 0 );
  446. }
  447. #ifndef NO_STEAM
  448. //-----------------------------------------------------------------------------
  449. // Purpose: The steam callback used to get our session IDs.
  450. //-----------------------------------------------------------------------------
  451. void CSteamWorksGameStatsUploader::Steam_OnSteamSessionInfoIssued( GameStatsSessionIssued_t *pGameStatsSessionInfo )
  452. {
  453. if ( !m_SessionIDRequestPending )
  454. {
  455. // There is no request outstanding.
  456. return;
  457. }
  458. m_SessionIDRequestPending = false;
  459. if ( !pGameStatsSessionInfo )
  460. {
  461. // Empty callback!
  462. ClearSessionID();
  463. return;
  464. }
  465. if ( pGameStatsSessionInfo->m_eResult != k_EResultOK )
  466. {
  467. #ifdef CLIENT_DLL
  468. DevMsg( "Steamworks Stats: CLIENT session id not available.\n" );
  469. #else
  470. DevMsg( "Steamworks Stats: SERVER session id not available.\n" );
  471. #endif
  472. m_SessionIDRequestUnsent = true; // Queue to re-request a session ID.
  473. ClearSessionID();
  474. return;
  475. }
  476. #ifdef CLIENT_DLL
  477. DevMsg( "Steamworks Stats: Received CLIENT session id: %llu\n", pGameStatsSessionInfo->m_ulSessionID );
  478. #else
  479. DevMsg( "Steamworks Stats: Received SERVER session id: %llu\n", pGameStatsSessionInfo->m_ulSessionID );
  480. #endif
  481. m_StartTime = GetTimeSinceEpoch();
  482. m_SessionID = pGameStatsSessionInfo->m_ulSessionID;
  483. m_bCollectingAny = pGameStatsSessionInfo->m_bCollectingAny;
  484. m_bCollectingDetails = pGameStatsSessionInfo->m_bCollectingDetails;
  485. char sessionIDString[ 32 ];
  486. Q_snprintf( sessionIDString, sizeof( sessionIDString ), "%llu", m_SessionID );
  487. #ifdef CLIENT_DLL
  488. steamworks_sessionid_client.SetValue( sessionIDString );
  489. m_FriendCntInGame = GetFriendCountInGame();
  490. m_HumanCntInGame = GetHumanCountInGame();
  491. #else
  492. steamworks_sessionid_server.SetValue( sessionIDString );
  493. #endif
  494. UploadCvars();
  495. }
  496. //-----------------------------------------------------------------------------
  497. // Purpose: The steam callback to notify us that we've submitted stats.
  498. //-----------------------------------------------------------------------------
  499. void CSteamWorksGameStatsUploader::Steam_OnSteamSessionInfoClosed( GameStatsSessionClosed_t *pGameStatsSessionInfo )
  500. {
  501. if ( !m_UploadedStats )
  502. return;
  503. m_UploadedStats = false;
  504. }
  505. //-----------------------------------------------------------------------------
  506. // Purpose: Per frame think. Used to periodically check if we have queued operations.
  507. // For example: we may request a session id before steam is ready.
  508. //-----------------------------------------------------------------------------
  509. void CSteamWorksGameStatsUploader::FrameUpdatePostEntityThink()
  510. {
  511. if ( !m_ServiceTicking )
  512. return;
  513. if ( gpGlobals->realtime - m_LastServiceTick < 3 )
  514. return;
  515. m_LastServiceTick = gpGlobals->realtime;
  516. if ( !AccessToSteamAPI() )
  517. return;
  518. // Try to resend our request.
  519. if ( m_SessionIDRequestUnsent )
  520. {
  521. RequestSessionID();
  522. return;
  523. }
  524. // If we had nothing to resend, stop ticking.
  525. m_ServiceTicking = false;
  526. }
  527. #endif
  528. //-----------------------------------------------------------------------------
  529. // Purpose: Opens a session: requests the session id, etc.
  530. //-----------------------------------------------------------------------------
  531. void CSteamWorksGameStatsUploader::StartSession()
  532. {
  533. RequestSessionID();
  534. }
  535. //-----------------------------------------------------------------------------
  536. // Purpose: Completes a session for the given type.
  537. //-----------------------------------------------------------------------------
  538. void CSteamWorksGameStatsUploader::EndSession()
  539. {
  540. m_EndTime = GetTimeSinceEpoch();
  541. if ( !m_SessionID )
  542. {
  543. // No session to end.
  544. return;
  545. }
  546. m_SteamWorksInterface = GetInterface();
  547. if ( m_SteamWorksInterface )
  548. {
  549. #ifdef CLIENT_DLL
  550. DevMsg( "Steamworks Stats: Ending CLIENT session id: %llu\n", m_SessionID );
  551. #else
  552. DevMsg( "Steamworks Stats: Ending SERVER session id: %llu\n", m_SessionID );
  553. #endif
  554. // Flush any stats that haven't been sent yet.
  555. FlushStats();
  556. // Always need some data in the session row or we'll crash steam.
  557. WriteSessionRow();
  558. m_SteamWorksInterface->EndSession( m_SessionID, m_EndTime, 0 );
  559. Reset();
  560. }
  561. }
  562. //-----------------------------------------------------------------------------
  563. // Purpose: Flush any unsent rows.
  564. //-----------------------------------------------------------------------------
  565. void CSteamWorksGameStatsUploader::FlushStats()
  566. {
  567. for ( int i=0; i<m_StatsToSend.Count(); ++i )
  568. {
  569. ParseKeyValuesAndSendStats( m_StatsToSend[i] );
  570. m_StatsToSend[i]->deleteThis();
  571. }
  572. m_StatsToSend.RemoveAll();
  573. }
  574. //-----------------------------------------------------------------------------
  575. // Purpose: Uploads any end of session rows.
  576. //-----------------------------------------------------------------------------
  577. void CSteamWorksGameStatsUploader::WriteSessionRow()
  578. {
  579. m_SteamWorksInterface = GetInterface();
  580. if ( !m_SteamWorksInterface )
  581. return;
  582. // The Session row is common to both client and server sessions.
  583. // It enables keying to other tables.
  584. // Don't send SessionID. It's provided by steam. Same with the account's id and type.
  585. // m_SteamWorksInterface->AddSessionAttributeInt64( m_SessionID, "SessionID", m_SessionID );
  586. #ifdef CLIENT_DLL
  587. // m_SteamWorksInterface->AddSessionAttributeInt64( m_SessionID, "ServerSessionID", m_ServerSessionID );
  588. #endif
  589. m_SteamWorksInterface->AddSessionAttributeInt( m_SessionID, "AppID", m_iAppID );
  590. m_SteamWorksInterface->AddSessionAttributeInt( m_SessionID, "StartTime", m_StartTime );
  591. m_SteamWorksInterface->AddSessionAttributeInt( m_SessionID, "EndTime", m_EndTime );
  592. #ifndef CLIENT_DLL
  593. m_SteamWorksInterface->AddSessionAttributeString( m_SessionID, "ServerIP", m_pzServerIP );
  594. m_SteamWorksInterface->AddSessionAttributeString( m_SessionID, "ServerName", m_pzHostName );
  595. m_SteamWorksInterface->AddSessionAttributeString( m_SessionID, "StartMap", m_pzMapStart );
  596. #endif
  597. #ifdef CLIENT_DLL
  598. // TODO CAB: Need to get these added into the clientsessionlookup table
  599. m_SteamWorksInterface->AddSessionAttributeInt( m_SessionID, "PlayersInGame", m_HumanCntInGame );
  600. m_SteamWorksInterface->AddSessionAttributeInt( m_SessionID, "FriendsInGame", m_FriendCntInGame );
  601. #endif
  602. }
  603. //-----------------------------------------------------------------------------
  604. // DATA ACCESS UTILITIES
  605. //-----------------------------------------------------------------------------
  606. //-----------------------------------------------------------------------------
  607. // Purpose: Verifies that we have a valid interface and will attempt to obtain a new one if we don't.
  608. //-----------------------------------------------------------------------------
  609. bool CSteamWorksGameStatsUploader::VerifyInterface( void )
  610. {
  611. if ( !m_SteamWorksInterface )
  612. {
  613. m_SteamWorksInterface = GetInterface();
  614. if ( !m_SteamWorksInterface )
  615. {
  616. return false;
  617. }
  618. }
  619. return true;
  620. }
  621. //-----------------------------------------------------------------------------
  622. // Purpose: Wrapper function to write an int32 to a table given the row name
  623. //-----------------------------------------------------------------------------
  624. EResult CSteamWorksGameStatsUploader::WriteIntToTable( const int value, uint64 iTableID, const char *pzRow )
  625. {
  626. if ( !VerifyInterface() )
  627. return k_EResultNoConnection;
  628. return m_SteamWorksInterface->AddRowAttributeInt( iTableID, pzRow, value );
  629. }
  630. //-----------------------------------------------------------------------------
  631. // Purpose: Wrapper function to write an int64 to a table given the row name
  632. //-----------------------------------------------------------------------------
  633. EResult CSteamWorksGameStatsUploader::WriteInt64ToTable( const uint64 value, uint64 iTableID, const char *pzRow )
  634. {
  635. if ( !VerifyInterface() )
  636. return k_EResultNoConnection;
  637. return m_SteamWorksInterface->AddRowAttributeInt64( iTableID, pzRow, value );
  638. }
  639. //-----------------------------------------------------------------------------
  640. // Purpose: Wrapper function to write an float to a table given the row name
  641. //-----------------------------------------------------------------------------
  642. EResult CSteamWorksGameStatsUploader::WriteFloatToTable( const float value, uint64 iTableID, const char *pzRow )
  643. {
  644. if ( !VerifyInterface() )
  645. return k_EResultNoConnection;
  646. return m_SteamWorksInterface->AddRowAttributeFloat( iTableID, pzRow, value );
  647. }
  648. //-----------------------------------------------------------------------------
  649. // Purpose: Wrapper function to write an string to a table given the row name
  650. //-----------------------------------------------------------------------------
  651. EResult CSteamWorksGameStatsUploader::WriteStringToTable( const char *value, uint64 iTableID, const char *pzRow )
  652. {
  653. if ( !VerifyInterface() )
  654. return k_EResultNoConnection;
  655. return m_SteamWorksInterface->AddRowAtributeString( iTableID, pzRow, value );
  656. }
  657. //-----------------------------------------------------------------------------
  658. // Purpose: Wrapper function to search a KeyValues for a value with the given keyName and add the result to the
  659. // row. If the key isn't present, return ResultNoMatch to indicate such.
  660. //-----------------------------------------------------------------------------
  661. EResult CSteamWorksGameStatsUploader::WriteOptionalFloatToTable( KeyValues *pKV, const char* keyName, uint64 iTableID, const char *pzRow )
  662. {
  663. if ( !VerifyInterface() )
  664. return k_EResultNoConnection;
  665. KeyValues* key = pKV->FindKey( keyName );
  666. if ( !key )
  667. return k_EResultNoMatch;
  668. float value = key->GetFloat();
  669. return m_SteamWorksInterface->AddRowAttributeFloat( iTableID, pzRow, value );
  670. }
  671. //-----------------------------------------------------------------------------
  672. // Purpose: Wrapper function to search a KeyValues for a value with the given keyName and add the result to the
  673. // row. If the key isn't present, return ResultNoMatch to indicate such.
  674. //-----------------------------------------------------------------------------
  675. EResult CSteamWorksGameStatsUploader::WriteOptionalIntToTable( KeyValues *pKV, const char* keyName, uint64 iTableID, const char *pzRow )
  676. {
  677. if ( !VerifyInterface() )
  678. return k_EResultNoConnection;
  679. KeyValues* key = pKV->FindKey( keyName );
  680. if ( !key )
  681. return k_EResultNoMatch;
  682. int value = key->GetInt();
  683. return m_SteamWorksInterface->AddRowAttributeInt( iTableID, pzRow, value );
  684. }
  685. //-----------------------------------------------------------------------------
  686. // STEAM ACCESS UTILITIES
  687. //-----------------------------------------------------------------------------
  688. //-----------------------------------------------------------------------------
  689. // Purpose: Determines if the system can connect to steam
  690. //-----------------------------------------------------------------------------
  691. bool CSteamWorksGameStatsUploader::AccessToSteamAPI( void )
  692. {
  693. #ifdef GAME_DLL
  694. return ( steamgameserverapicontext && steamgameserverapicontext->SteamGameServer() && g_pSteamClientGameServer && steamgameserverapicontext->SteamGameServerUtils() );
  695. #elif CLIENT_DLL
  696. return ( steamapicontext && steamapicontext->SteamUser() && steamapicontext->SteamUser()->BLoggedOn() && steamapicontext->SteamFriends() && steamapicontext->SteamMatchmaking() );
  697. #endif
  698. return false;
  699. }
  700. //-----------------------------------------------------------------------------
  701. // Purpose: There's no guarantee that your interface pointer will persist across level transitions,
  702. // so this function will update your interface.
  703. //-----------------------------------------------------------------------------
  704. ISteamGameStats* CSteamWorksGameStatsUploader::GetInterface( void )
  705. {
  706. HSteamUser hSteamUser = 0;
  707. HSteamPipe hSteamPipe = 0;
  708. #ifdef GAME_DLL
  709. if ( steamgameserverapicontext && steamgameserverapicontext->SteamGameServer() && steamgameserverapicontext->SteamGameServerUtils() )
  710. {
  711. m_UserID = steamgameserverapicontext->SteamGameServer()->GetSteamID().ConvertToUint64();
  712. m_iAppID = steamgameserverapicontext->SteamGameServerUtils()->GetAppID();
  713. hSteamUser = SteamGameServer_GetHSteamUser();
  714. hSteamPipe = SteamGameServer_GetHSteamPipe();
  715. }
  716. // Now let's get the interface for dedicated servers
  717. if ( g_pSteamClientGameServer && engine && (/*engine->IsDedicatedServerForXbox() ||*/ engine->IsDedicatedServer()) )
  718. {
  719. return (ISteamGameStats*)g_pSteamClientGameServer->GetISteamGenericInterface( hSteamUser, hSteamPipe, STEAMGAMESTATS_INTERFACE_VERSION );
  720. }
  721. #elif CLIENT_DLL
  722. if ( steamapicontext && steamapicontext->SteamUser() && steamapicontext->SteamUtils() )
  723. {
  724. m_UserID = steamapicontext->SteamUser()->GetSteamID().ConvertToUint64();
  725. m_iAppID = steamapicontext->SteamUtils()->GetAppID();
  726. hSteamUser = steamapicontext->SteamUser()->GetHSteamUser();
  727. hSteamPipe = GetHSteamPipe();
  728. }
  729. #endif
  730. // Listen server have access to SteamClient
  731. if ( SteamClient() )
  732. {
  733. return (ISteamGameStats*)SteamClient()->GetISteamGenericInterface( hSteamUser, hSteamPipe, STEAMGAMESTATS_INTERFACE_VERSION );
  734. }
  735. // If we haven't returned already, then we can't get access to the interface
  736. return NULL;
  737. }
  738. //-----------------------------------------------------------------------------
  739. // Purpose: Creates a table from the KeyValue file. Do NOT send nested KeyValue objects into this function!
  740. //-----------------------------------------------------------------------------
  741. EResult CSteamWorksGameStatsUploader::AddStatsForUpload( KeyValues *pKV, bool bSendImmediately )
  742. {
  743. // If the stat system is disabled, then don't accept the keyvalue
  744. if ( steamworks_stats_disable )
  745. {
  746. if ( pKV )
  747. {
  748. pKV->deleteThis();
  749. }
  750. return k_EResultNoConnection;
  751. }
  752. if ( pKV )
  753. {
  754. // Do we want to immediately upload the stats?
  755. if ( bSendImmediately && steamworks_immediate_upload )
  756. {
  757. ParseKeyValuesAndSendStats( pKV );
  758. pKV->deleteThis();
  759. }
  760. else
  761. {
  762. m_StatsToSend.AddToTail( pKV );
  763. }
  764. return k_EResultOK;
  765. }
  766. return k_EResultFail;
  767. }
  768. //-----------------------------------------------------------------------------
  769. // Purpose: Parses all the keyvalue files we've been sent and creates tables from them and uploads them
  770. //-----------------------------------------------------------------------------
  771. double g_rowCommitTime = 0.0f;
  772. double g_rowWriteTime = 0.0f;
  773. EResult CSteamWorksGameStatsUploader::ParseKeyValuesAndSendStats( KeyValues *pKV, bool bIncludeClientsServerSessionID )
  774. {
  775. if ( !pKV )
  776. {
  777. return k_EResultFail;
  778. }
  779. if ( !IsCollectingAnyData() )
  780. {
  781. return k_EResultFail;
  782. }
  783. // Refresh the interface in case steam has unloaded
  784. m_SteamWorksInterface = GetInterface();
  785. if ( !m_SteamWorksInterface )
  786. {
  787. DevMsg( "WARNING: Attempted to send a steamworks gamestats row when the steamworks interface was not available!" );
  788. return k_EResultNoConnection;
  789. }
  790. const char *pzTable = pKV->GetName();
  791. /*
  792. if ( steamworks_show_uploads )
  793. {
  794. #ifdef CLIENT_DLL
  795. // DevMsg( "Client submitting row (%s).\n", pzTable );
  796. #elif GAME_DLL
  797. // DevMsg( "Server submitting row (%s).\n", pzTable );
  798. #endif
  799. }
  800. */
  801. uint64 iTableID = 0;
  802. m_SteamWorksInterface->AddNewRow( &iTableID, m_SessionID, pzTable );
  803. if ( !iTableID )
  804. {
  805. return k_EResultFail;
  806. }
  807. WriteInt64ToTable( m_SessionID, iTableID, "SessionID" );
  808. #ifdef CLIENT_DLL
  809. if ( bIncludeClientsServerSessionID )
  810. {
  811. WriteInt64ToTable( m_ServerSessionID, iTableID, "ServerSessionID" );
  812. }
  813. #endif
  814. // Now we need to loop over all the keys in pKV and add the name and value
  815. for ( KeyValues *pData = pKV->GetFirstSubKey() ; pData != NULL ; pData = pData->GetNextKey() )
  816. {
  817. const char *name = pData->GetName();
  818. CFastTimer writeTimer;
  819. writeTimer.Start();
  820. switch ( pData->GetDataType() )
  821. {
  822. case KeyValues::TYPE_STRING: WriteStringToTable( pKV->GetString( name ), iTableID, name );
  823. break;
  824. case KeyValues::TYPE_INT: WriteIntToTable( pKV->GetInt( name ), iTableID, name );
  825. break;
  826. case KeyValues::TYPE_FLOAT: WriteFloatToTable( pKV->GetFloat( name ), iTableID, name );
  827. break;
  828. case KeyValues::TYPE_UINT64: WriteInt64ToTable( pKV->GetUint64( name ), iTableID, name );
  829. break;
  830. };
  831. writeTimer.End();
  832. g_rowWriteTime += writeTimer.GetDuration().GetMillisecondsF();
  833. }
  834. CFastTimer commitTimer;
  835. commitTimer.Start();
  836. EResult res = m_SteamWorksInterface->CommitRow( iTableID );
  837. commitTimer.End();
  838. g_rowCommitTime += commitTimer.GetDuration().GetMillisecondsF();
  839. if ( res != k_EResultOK )
  840. {
  841. AssertMsg( false, "Failed To Submit table %s", pzTable );
  842. }
  843. return res;
  844. }
  845. #ifdef CLIENT_DLL
  846. //-----------------------------------------------------------------------------
  847. // Purpose: Reports client's perf data at the end of a client session.
  848. //---------------------------------`--------------------------------------------
  849. void CSteamWorksGameStatsUploader::AddClientPerfData( KeyValues *pKV )
  850. {
  851. m_SteamWorksInterface = GetInterface();
  852. if ( !m_SteamWorksInterface )
  853. return;
  854. if ( !IsCollectingAnyData() )
  855. return;
  856. RTime32 currentTime = GetTimeSinceEpoch();
  857. uint64 uSessionID = m_SessionID;
  858. uint64 ulRowID = 0;
  859. m_SteamWorksInterface->AddNewRow( &ulRowID, uSessionID, "TF2ClientPerfData" );
  860. if ( !ulRowID )
  861. return;
  862. WriteInt64ToTable( m_SessionID, ulRowID, "SessionID" );
  863. // WriteInt64ToTable( m_ServerSessionID, ulRowID, "ServerSessionID" );
  864. WriteIntToTable( currentTime, ulRowID, "TimeSubmitted" );
  865. WriteStringToTable( pKV->GetString( "Map/mapname" ), ulRowID, "MapID");
  866. WriteIntToTable( pKV->GetInt( "appid" ), ulRowID, "AppID");
  867. WriteFloatToTable( pKV->GetFloat( "Map/perfdata/AvgFPS" ), ulRowID, "AvgFPS");
  868. WriteFloatToTable( pKV->GetFloat( "map/perfdata/MinFPS" ), ulRowID, "MinFPS");
  869. WriteFloatToTable( pKV->GetFloat( "Map/perfdata/MaxFPS" ), ulRowID, "MaxFPS");
  870. WriteFloatToTable( pKV->GetFloat( "Map/perfdata/StdDevFPS" ), ulRowID, "StdDevFPS");
  871. WriteStringToTable( pKV->GetString( "CPUID" ), ulRowID, "CPUID");
  872. WriteFloatToTable( pKV->GetFloat( "CPUGhz" ), ulRowID, "CPUGhz");
  873. WriteInt64ToTable( pKV->GetUint64( "CPUModel" ), ulRowID, "CPUModel" );
  874. WriteInt64ToTable( pKV->GetUint64( "CPUFeatures0" ), ulRowID, "CPUFeatures0" );
  875. WriteInt64ToTable( pKV->GetUint64( "CPUFeatures1" ), ulRowID, "CPUFeatures1" );
  876. WriteInt64ToTable( pKV->GetUint64( "CPUFeatures2" ), ulRowID, "CPUFeatures2" );
  877. WriteIntToTable( pKV->GetInt( "NumCores" ), ulRowID, "NumCores");
  878. WriteStringToTable( pKV->GetString( "GPUDrv" ), ulRowID, "GPUDrv");
  879. WriteIntToTable( pKV->GetInt( "GPUVendor" ), ulRowID, "GPUVendor");
  880. WriteIntToTable( pKV->GetInt( "GPUDeviceID" ), ulRowID, "GPUDeviceID");
  881. WriteIntToTable( pKV->GetInt( "GPUDriverVersion" ), ulRowID, "GPUDriverVersion");
  882. WriteIntToTable( pKV->GetInt( "DxLvl" ), ulRowID, "DxLvl");
  883. WriteIntToTable( pKV->GetBool( "Map/Windowed" ), ulRowID, "Windowed");
  884. // WriteIntToTable( pKV->GetBool( "Map/WindowedNoBorder" ), ulRowID, "WindowedNoBorder");
  885. WriteIntToTable( pKV->GetInt( "width" ), ulRowID, "Width");
  886. WriteIntToTable( pKV->GetInt( "height" ), ulRowID, "Height");
  887. WriteIntToTable( pKV->GetInt( "Map/UsedVoice" ), ulRowID, "Usedvoiced");
  888. WriteStringToTable( pKV->GetString( "Map/Language" ), ulRowID, "Language");
  889. WriteFloatToTable( pKV->GetFloat( "Map/perfdata/AvgServerPing" ), ulRowID, "AvgServerPing");
  890. WriteIntToTable( pKV->GetInt( "Map/Caption" ), ulRowID, "IsCaptioned");
  891. WriteIntToTable( pKV->GetInt( "IsPC" ), ulRowID, "IsPC");
  892. WriteIntToTable( pKV->GetBool( "Windowed" ), ulRowID, "Windowed");
  893. WriteIntToTable( pKV->GetInt( "Map/Cheats" ), ulRowID, "Cheats");
  894. WriteIntToTable( pKV->GetInt( "Map/MapTime" ), ulRowID, "MapTime");
  895. WriteIntToTable( pKV->GetInt( "MaxDxLevel" ), ulRowID, "MaxDxLvl" );
  896. WriteIntToTable( pKV->GetBool( "Map/SteamControllerActive" ), ulRowID, "UsingController" );
  897. // Loading time information
  898. // If a player exits a map and then exits the game, LoadTimeMap will not be in the key list the second time
  899. // (when we are sending for app shutdown), so only write it if it's here.
  900. WriteOptionalFloatToTable( pKV, "Map/LoadTimeMap", ulRowID, "SessionLoadTime");
  901. // Main menu load time is only added once, even if we play many games in a row. This prevents a stats bias for
  902. // people who replay over and over again.
  903. WriteOptionalFloatToTable( pKV, "Map/LoadTimeMainMenu", ulRowID, "MainmenuLoadTime");
  904. m_SteamWorksInterface->CommitRow( ulRowID );
  905. }
  906. #endif
  907. //-------------------------------------------------------------------------------------------------
  908. /**
  909. * Purpose: Calculates the number of humans in the game
  910. */
  911. int CSteamWorksGameStatsUploader::GetHumanCountInGame()
  912. {
  913. int iHumansInGame = 0;
  914. // TODO: Need to add server/client code to count the number of connected humans.
  915. return iHumansInGame;
  916. }
  917. #ifdef CLIENT_DLL
  918. //-------------------------------------------------------------------------------------------------
  919. /**
  920. * Purpose: Calculates the number of friends in the game
  921. */
  922. int CSteamWorksGameStatsUploader::GetFriendCountInGame()
  923. {
  924. // Get the number of steam friends in game
  925. int friendsInOurGame = 0;
  926. // Do we have access to the steam API?
  927. if ( AccessToSteamAPI() )
  928. {
  929. CSteamID m_SteamID = steamapicontext->SteamUser()->GetSteamID();
  930. // Let's get our game info so we can use that to test if our friends are connected to the same game as us
  931. FriendGameInfo_t myGameInfo;
  932. steamapicontext->SteamFriends()->GetFriendGamePlayed( m_SteamID, &myGameInfo );
  933. CSteamID myLobby = steamapicontext->SteamMatchmaking()->GetLobbyOwner( myGameInfo.m_steamIDLobby );
  934. // This returns the number of friends that are playing a game
  935. int activeFriendCnt = steamapicontext->SteamFriends()->GetFriendCount( k_EFriendFlagImmediate );
  936. // Check each active friend's lobby ID to see if they are in our game
  937. for ( int h=0; h< activeFriendCnt ; ++h )
  938. {
  939. FriendGameInfo_t friendInfo;
  940. CSteamID friendID = steamapicontext->SteamFriends()->GetFriendByIndex( h, k_EFriendFlagImmediate );
  941. if ( steamapicontext->SteamFriends()->GetFriendGamePlayed( friendID, &friendInfo ) )
  942. {
  943. // Does our friend have a valid lobby ID?
  944. if ( friendInfo.m_gameID.IsValid() )
  945. {
  946. // Get our friend's lobby info
  947. CSteamID friendLobby = steamapicontext->SteamMatchmaking()->GetLobbyOwner( friendInfo.m_steamIDLobby );
  948. // Double check the validity of the friend lobby ID then check to see if they are in our game
  949. if ( friendLobby.IsValid() && myLobby == friendLobby )
  950. {
  951. ++friendsInOurGame;
  952. }
  953. }
  954. }
  955. }
  956. }
  957. return friendsInOurGame;
  958. }
  959. #endif
  960. void CSteamWorksGameStatsUploader::ServerAddressToInt()
  961. {
  962. CUtlStringList IPs;
  963. V_SplitString( m_pzServerIP, ".", IPs );
  964. if ( IPs.Count() < 4 )
  965. {
  966. // Not an actual IP.
  967. m_iServerIP = 0;
  968. return;
  969. }
  970. byte ip[4];
  971. m_iServerIP = 0;
  972. for ( int i=0; i<IPs.Count() && i<4; ++i )
  973. {
  974. ip[i] = (byte) Q_atoi( IPs[i] );
  975. }
  976. m_iServerIP = (ip[0]<<24) + (ip[1]<<16) + (ip[2]<<8) + ip[3];
  977. }