summaryrefslogtreecommitdiff
path: root/game/shared/econ/econ_claimcode.cpp
blob: 94f15a9af19034c6fc923f8323215c5bab10cb3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Code for the CEconClaimCode object
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"

#include "econ_item_tools.h"
#include "econ_claimcode.h"

using namespace GCSDK;

#ifdef GC
IMPLEMENT_CLASS_MEMPOOL( CEconClaimCode, 10 * 1000, UTLMEMORYPOOL_GROW_SLOW );

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

bool CEconClaimCode::BYieldingAddInsertToTransaction( GCSDK::CSQLAccess & sqlAccess )
{
	CSchAssignedClaimCode schCode;
	WriteToRecord( &schCode );
	return CSchemaSharedObjectHelper::BYieldingAddInsertToTransaction( sqlAccess, &schCode );
}

void CEconClaimCode::WriteToRecord( CSchAssignedClaimCode *pClaimCode )
{
	pClaimCode->m_unAccountID = Obj().account_id();
	pClaimCode->m_unCodeType = Obj().code_type();
	pClaimCode->m_rtime32TimeAcquired = Obj().time_acquired();
	WRITE_VAR_CHAR_FIELD( *pClaimCode, VarCharCode, Obj().code().c_str() );
}

void CEconClaimCode::ReadFromRecord( const CSchAssignedClaimCode & code )
{
	const char *pchCode = READ_VAR_CHAR_FIELD( code, m_VarCharCode );
	Obj().set_code_type( code.m_unCodeType );
	Obj().set_time_acquired( code.m_rtime32TimeAcquired );
	Obj().set_code( pchCode );
}


bool BBuildRedemptionURL( CEconClaimCode *pClaimCode, CUtlString &redemptionURL )
{
	const CEconItemDefinition *pItemDef = GEconManager()->GetItemSchema()->GetItemDefinition( pClaimCode->Obj().code_type() );
	if ( pItemDef )
	{
		const char *pOriginalURL = pItemDef->GetDefinitionString( "redeem_url" );
		const char *code = pClaimCode->Obj().code().c_str();
		char url[1024];
		if ( Q_StrSubst( pOriginalURL, "CLAIMCODE", code, url, sizeof( url ) ) )
		{
			redemptionURL = url;
			return true;
		}
	}
	return false;
}

// -----------------------------------------------------------------------------
// Purpose: Gets a summary of what's going on with the GC
// -----------------------------------------------------------------------------
class CJobWG_GetPromoCodes : public CGCGameBaseWGJob
{
public:
	CJobWG_GetPromoCodes( CGCGameBase *pGC ) : CGCGameBaseWGJob( pGC ) {}

	virtual bool BYieldingRunJobFromRequest( KeyValues *pkvRequest, KeyValues *pkvResponse );

private:
};


bool CJobWG_GetPromoCodes::BYieldingRunJobFromRequest( KeyValues *pkvRequest, KeyValues *pkvResponse )
{
	CSteamID actorID( pkvRequest->GetUint64( "token/steamid" ) );

	KeyValues *pkvPromoCodes = pkvResponse->FindKey( "promo_codes", true );

	CSharedObjectCache *pSOCache = m_pGCGameBase->YieldingFindOrLoadSOCache( actorID );
	if ( pSOCache == NULL )
		return true;

	CSharedObjectTypeCache *pTypeCache = pSOCache->FindBaseTypeCache( k_EEconTypeClaimCode );
	if ( pTypeCache == NULL )
		return true;
	
	for ( uint32 i = 0; i < pTypeCache->GetCount(); ++i )
	{
		CEconClaimCode *pClaimCode = (CEconClaimCode*)pTypeCache->GetObject( i );
		const CEconItemDefinition *pItemDef = GetItemSchema()->GetItemDefinition( pClaimCode->Obj().code_type() );
		if ( pItemDef == NULL )
			continue;

		const CEconTool_ClaimCode *pEconClaimCodeTool = pItemDef->GetTypedEconTool<CEconTool_ClaimCode>();
		if ( pEconClaimCodeTool == NULL )
			continue;

		const char *pClaimCodeName = pEconClaimCodeTool->GetClaimType();
		if ( pClaimCodeName == NULL )
			continue;
		
		CUtlString claimURL;
		if ( BBuildRedemptionURL( pClaimCode, claimURL ) == false )
		{
			SetErrorMessage( pkvResponse, CFmtStr( "Unable to construct redemption url for: %s", pClaimCodeName ), k_EResultFail );
			continue;
		}
		const char *code = pClaimCode->Obj().code().c_str();
		// finally populate the key values
		KeyValues *pkvPromoCode = pkvPromoCodes->CreateNewKey();
		pkvPromoCode->SetString( "code_name", pClaimCodeName );
		pkvPromoCode->SetInt( "timestamp", pClaimCode->Obj().time_acquired() );
		pkvPromoCode->SetString( "code", code );
		pkvPromoCode->SetString( "redeem_url", claimURL.Get() );
	}

	return true;
}

DECLARE_GCWG_JOB( CGCEcon, CJobWG_GetPromoCodes, "GetPromoCodes", k_EGCWebApiPriv_Session )
END_DECLARE_GCWG_JOB( CJobWG_GetPromoCodes);

#endif