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.

126 lines
4.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Code for the CEconClaimCode object
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "econ_item_tools.h"
  9. #include "econ_claimcode.h"
  10. using namespace GCSDK;
  11. #ifdef GC
  12. IMPLEMENT_CLASS_MEMPOOL( CEconClaimCode, 10 * 1000, UTLMEMORYPOOL_GROW_SLOW );
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. bool CEconClaimCode::BYieldingAddInsertToTransaction( GCSDK::CSQLAccess & sqlAccess )
  16. {
  17. CSchAssignedClaimCode schCode;
  18. WriteToRecord( &schCode );
  19. return CSchemaSharedObjectHelper::BYieldingAddInsertToTransaction( sqlAccess, &schCode );
  20. }
  21. void CEconClaimCode::WriteToRecord( CSchAssignedClaimCode *pClaimCode )
  22. {
  23. pClaimCode->m_unAccountID = Obj().account_id();
  24. pClaimCode->m_unCodeType = Obj().code_type();
  25. pClaimCode->m_rtime32TimeAcquired = Obj().time_acquired();
  26. WRITE_VAR_CHAR_FIELD( *pClaimCode, VarCharCode, Obj().code().c_str() );
  27. }
  28. void CEconClaimCode::ReadFromRecord( const CSchAssignedClaimCode & code )
  29. {
  30. const char *pchCode = READ_VAR_CHAR_FIELD( code, m_VarCharCode );
  31. Obj().set_code_type( code.m_unCodeType );
  32. Obj().set_time_acquired( code.m_rtime32TimeAcquired );
  33. Obj().set_code( pchCode );
  34. }
  35. bool BBuildRedemptionURL( CEconClaimCode *pClaimCode, CUtlString &redemptionURL )
  36. {
  37. const CEconItemDefinition *pItemDef = GEconManager()->GetItemSchema()->GetItemDefinition( pClaimCode->Obj().code_type() );
  38. if ( pItemDef )
  39. {
  40. const char *pOriginalURL = pItemDef->GetDefinitionString( "redeem_url" );
  41. const char *code = pClaimCode->Obj().code().c_str();
  42. char url[1024];
  43. if ( Q_StrSubst( pOriginalURL, "CLAIMCODE", code, url, sizeof( url ) ) )
  44. {
  45. redemptionURL = url;
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. // -----------------------------------------------------------------------------
  52. // Purpose: Gets a summary of what's going on with the GC
  53. // -----------------------------------------------------------------------------
  54. class CJobWG_GetPromoCodes : public CGCGameBaseWGJob
  55. {
  56. public:
  57. CJobWG_GetPromoCodes( CGCGameBase *pGC ) : CGCGameBaseWGJob( pGC ) {}
  58. virtual bool BYieldingRunJobFromRequest( KeyValues *pkvRequest, KeyValues *pkvResponse );
  59. private:
  60. };
  61. bool CJobWG_GetPromoCodes::BYieldingRunJobFromRequest( KeyValues *pkvRequest, KeyValues *pkvResponse )
  62. {
  63. CSteamID actorID( pkvRequest->GetUint64( "token/steamid" ) );
  64. KeyValues *pkvPromoCodes = pkvResponse->FindKey( "promo_codes", true );
  65. CSharedObjectCache *pSOCache = m_pGCGameBase->YieldingFindOrLoadSOCache( actorID );
  66. if ( pSOCache == NULL )
  67. return true;
  68. CSharedObjectTypeCache *pTypeCache = pSOCache->FindBaseTypeCache( k_EEconTypeClaimCode );
  69. if ( pTypeCache == NULL )
  70. return true;
  71. for ( uint32 i = 0; i < pTypeCache->GetCount(); ++i )
  72. {
  73. CEconClaimCode *pClaimCode = (CEconClaimCode*)pTypeCache->GetObject( i );
  74. const CEconItemDefinition *pItemDef = GetItemSchema()->GetItemDefinition( pClaimCode->Obj().code_type() );
  75. if ( pItemDef == NULL )
  76. continue;
  77. const CEconTool_ClaimCode *pEconClaimCodeTool = pItemDef->GetTypedEconTool<CEconTool_ClaimCode>();
  78. if ( pEconClaimCodeTool == NULL )
  79. continue;
  80. const char *pClaimCodeName = pEconClaimCodeTool->GetClaimType();
  81. if ( pClaimCodeName == NULL )
  82. continue;
  83. CUtlString claimURL;
  84. if ( BBuildRedemptionURL( pClaimCode, claimURL ) == false )
  85. {
  86. SetErrorMessage( pkvResponse, CFmtStr( "Unable to construct redemption url for: %s", pClaimCodeName ), k_EResultFail );
  87. continue;
  88. }
  89. const char *code = pClaimCode->Obj().code().c_str();
  90. // finally populate the key values
  91. KeyValues *pkvPromoCode = pkvPromoCodes->CreateNewKey();
  92. pkvPromoCode->SetString( "code_name", pClaimCodeName );
  93. pkvPromoCode->SetInt( "timestamp", pClaimCode->Obj().time_acquired() );
  94. pkvPromoCode->SetString( "code", code );
  95. pkvPromoCode->SetString( "redeem_url", claimURL.Get() );
  96. }
  97. return true;
  98. }
  99. DECLARE_GCWG_JOB( CGCEcon, CJobWG_GetPromoCodes, "GetPromoCodes", k_EGCWebApiPriv_Session )
  100. END_DECLARE_GCWG_JOB( CJobWG_GetPromoCodes);
  101. #endif