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.

93 lines
4.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "stdafx.h"
  8. #include "gcsdk/enumutils.h"
  9. #include "gcsdk/gcbase.h"
  10. #include "gcsdk/http.h"
  11. #include "gcsdk/job.h"
  12. #include "steam/isteamuserstats.h"
  13. #include "gcleaderboardapi.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. using namespace GCSDK;
  17. // @note Tom Bui: copied from steam
  18. ENUMSTRINGS_START( ELeaderboardSortMethod )
  19. { k_ELeaderboardSortMethodNone, "" },
  20. { k_ELeaderboardSortMethodAscending, "Ascending" },
  21. { k_ELeaderboardSortMethodDescending, "Descending" },
  22. ENUMSTRINGS_REVERSE( ELeaderboardSortMethod, k_ELeaderboardSortMethodNone )
  23. ENUMSTRINGS_START( ELeaderboardDisplayType )
  24. { k_ELeaderboardDisplayTypeNone, "" },
  25. { k_ELeaderboardDisplayTypeNumeric, "Numeric" },
  26. { k_ELeaderboardDisplayTypeTimeSeconds, "Seconds" },
  27. { k_ELeaderboardDisplayTypeTimeMilliSeconds, "MilliSeconds" },
  28. ENUMSTRINGS_REVERSE( ELeaderboardDisplayType, k_ELeaderboardDisplayTypeNone )
  29. ENUMSTRINGS_START( ELeaderboardUploadScoreMethod )
  30. { k_ELeaderboardUploadScoreMethodNone, "" },
  31. { k_ELeaderboardUploadScoreMethodKeepBest, "KeepBest" },
  32. { k_ELeaderboardUploadScoreMethodForceUpdate, "ForceUpdate" },
  33. ENUMSTRINGS_REVERSE( ELeaderboardUploadScoreMethod, k_ELeaderboardUploadScoreMethodNone )
  34. namespace GCSDK
  35. {
  36. uint32 Leaderboard_YieldingFind( const char *pName, ELeaderboardSortMethod eLeaderboardSortMethod, ELeaderboardDisplayType eLeaderboardDisplayType, bool bCreateIfNotFound )
  37. {
  38. CSteamAPIRequest apiRequest( k_EHTTPMethodPOST, "ISteamLeaderboards", "FindOrCreateLeaderboard", 1 );
  39. apiRequest.SetPOSTParamUInt32( "appid", GGCBase()->GetAppID() );
  40. apiRequest.SetPOSTParamString( "name", pName );
  41. apiRequest.SetPOSTParamString( "sortmethod", PchNameFromELeaderboardSortMethod( eLeaderboardSortMethod ) );
  42. apiRequest.SetPOSTParamString( "displaytype", PchNameFromELeaderboardDisplayType( eLeaderboardDisplayType ) );
  43. apiRequest.SetPOSTParamBool( "createifnotfound", bCreateIfNotFound );
  44. KeyValuesAD kvAPIResponse( "response" );
  45. const EResult eCallResult = GGCBase()->YieldingSendHTTPRequestKV( &apiRequest, kvAPIResponse );
  46. const EResult eAPIResult = eCallResult == k_EResultOK ? static_cast<EResult>( kvAPIResponse->GetInt( "result", k_EResultFail ) ) : k_EResultFail;
  47. if ( eAPIResult == k_EResultOK )
  48. {
  49. KeyValues *pKVEntry = kvAPIResponse->FindKey( pName );
  50. if ( pKVEntry )
  51. {
  52. return pKVEntry->GetInt( "leaderBoardID", kInvalidLeaderboardID );
  53. }
  54. }
  55. return kInvalidLeaderboardID;
  56. }
  57. bool Leaderboard_YieldingSetScore( uint32 unLeaderboardID, const CSteamID &steamID, ELeaderboardUploadScoreMethod eLeaderboardUploadScoreMethod, int score )
  58. {
  59. Assert( unLeaderboardID != kInvalidLeaderboardID );
  60. if ( unLeaderboardID == kInvalidLeaderboardID )
  61. return false;
  62. CSteamAPIRequest apiRequest( k_EHTTPMethodPOST, "ISteamLeaderboards", "SetLeaderboardScore", 1 );
  63. apiRequest.SetPOSTParamUInt32( "appid", GGCBase()->GetAppID() );
  64. apiRequest.SetPOSTParamUInt32( "leaderboardid", unLeaderboardID );
  65. apiRequest.SetPOSTParamUInt64( "steamid", steamID.ConvertToUint64() );
  66. apiRequest.SetPOSTParamInt32( "score", score );
  67. apiRequest.SetPOSTParamString( "scoremethod", PchNameFromELeaderboardUploadScoreMethod( eLeaderboardUploadScoreMethod ) );
  68. KeyValuesAD kvAPIResponse( "response" );
  69. const EResult eCallResult = GGCBase()->YieldingSendHTTPRequestKV( &apiRequest, kvAPIResponse );
  70. const EResult eAPIResult = eCallResult == k_EResultOK ? static_cast<EResult>( kvAPIResponse->GetInt( "result", k_EResultFail ) ) : k_EResultFail;
  71. if ( eAPIResult == k_EResultOK )
  72. return true;
  73. EmitError( SPEW_GC, __FUNCTION__ ": error code %u/%u setting leaderboard %u to %i (%s) for user '%s'.\n",
  74. eCallResult, eAPIResult, unLeaderboardID, score, PchNameFromELeaderboardUploadScoreMethod( eLeaderboardUploadScoreMethod ), steamID.Render() );
  75. return false;
  76. }
  77. }; // namespace GCSDK