summaryrefslogtreecommitdiff
path: root/game/client/econ/econ_trading.cpp
blob: 3fb532b25d680780ce53604041536da80ff72cc1 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================
#include "cbase.h"

#include "econ_trading.h"

// for messaging with the GC
#include "econ_gcmessages.h"
#include "econ_item_inventory.h"
#include "econ_gcmessages.h"
#include "econ_ui.h"

// other
#include "c_baseplayer.h"
#include "c_playerresource.h"

// UI
#include "confirm_dialog.h"
#include "econ_controls.h"
#include "vgui/ILocalize.h"
#include "econ_notifications.h"

#ifdef TF_CLIENT_DLL
#include "c_tf_gamestats.h"
#endif

#include "gc_clientsystem.h"

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

//-----------------------------------------------------------------------------

const char *g_FriendRelationship[] =
{
	"none"
	"blocked",
	"request_recipient",
	"friend",
	"request_initiator",
	"ignored",
	"ignored_friend"
};

const char *g_RejectedReasons[] =
{
	"accepted",
	"declined",
	"vac_banned_initiator",
	"vac_banned_target",
	"target_already_trading",
	"trading_disabled",
	"user_not_logged_in"
};

const char *g_ClosedReasons[] =
{
	"traded",
	"canceled",
	"error",
	"does_not_own_items",
	"untradable_items",
	"no_items",
	"trading_disabled"
};

enum
{
	kShowTradeRequestsFrom_FriendsOnly			   = 1,
	kShowTradeRequestsFrom_FriendsAndCurrentServer = 2,
	kShowTradeRequestsFrom_Anyone				   = 3,
	kShowTradeRequestsFrom_NoOne				   = 4,
};

ConVar cl_trading_show_requests_from( "cl_trading_show_requests_from", "3", FCVAR_ARCHIVE, "View trade requests from a certain group only." );

static bool sbTestingSelfTrade = false;

static int iTradeRequests = 0;
static int iTradeAttempts = 0;
static int iTradeOffers = 0;
static int iGiftsGiven = 0;

const uint32 kTradeRequestLifetime = 30.0f;

// waiting dialog
class CTradingWaitDialog : public CGenericWaitingDialog
{
public:
	CTradingWaitDialog( const char *pText = "#TF_Trading_Timeout_Text", const wchar_t *pPlayerName = NULL ) 
		: CGenericWaitingDialog( NULL )
		, m_pText( pText )
		, m_pKeyValues( NULL )
	{
		if ( pPlayerName != NULL && wcsicmp( pPlayerName, L"" ) != 0 )
		{
			m_pKeyValues = new KeyValues( "CTradingWaitDialog" );
			m_pKeyValues->SetWString( "other_player", pPlayerName );
		}
	}

	virtual ~CTradingWaitDialog()
	{
		if ( m_pKeyValues != NULL )
		{
			m_pKeyValues->deleteThis();
		}
	}

protected:
	virtual void OnTimeout()
	{
		ShowMessageBox( "#TF_Trading_Timeout_Title", m_pText, m_pKeyValues, "#GameUI_OK" );
	}

	virtual void OnUserClose()
	{
		GCSDK::CGCMsg< MsgGCTrading_CancelSession_t > msg( k_EMsgGCTrading_CancelSession );
		GCClientSystem()->BSendMessage( msg );
		// @note not sure we need to wait for the GC here, but we will just in case...
		ShowWaitingDialog( new CTradingWaitDialog(), "#TF_Trading_WaitingForCancel", true, false, 20.0f );
	}

	KeyValues *m_pKeyValues;
	const char* m_pText;
};

// used by the waiting dialogs
static void TradeCompleteDialogClosed( bool bConfirmed, void *pContext )
{
	if ( bConfirmed )
	{
		InventoryManager()->ShowItemsPickedUp( true );
	}
}


//-----------------------------------------------------------------------------
// jobs
class CTFTradeRequestNotification : public CEconNotification
{
public:
	CTFTradeRequestNotification( uint64 ulInitiatorSteamID, uint32 unTradeRequestID, const char* pPlayerName ) 
		: CEconNotification() 
		, m_unTradeRequestID( unTradeRequestID )
	{
		SetSteamID( ulInitiatorSteamID );
		g_pVGuiLocalize->ConvertANSIToUnicode( pPlayerName, m_wszPlayerName, sizeof(m_wszPlayerName) );
		SetLifetime( kTradeRequestLifetime );
		SetText( "#TF_Trading_JoinText" );
		AddStringToken( "initiator", m_wszPlayerName );
	}

	virtual EType NotificationType() { return eType_AcceptDecline; }

	/// XXX(JohnS): Dead code? Was always accept/decline AFAICT
	virtual void Trigger()
	{
		CTFGenericConfirmDialog *pDialog = ShowConfirmDialog( "#TF_Trading_JoinTitle",  "#TF_Trading_JoinText", "#GameUI_OK", "#TF_Trading_JoinCancel", &ConfirmJoinTradeSession );
		pDialog->SetContext( this );
		pDialog->AddStringToken( "initiator", m_wszPlayerName );
		// so we aren't deleted
		SetIsInUse( true );
	}

	virtual void Accept()
	{
		ConfirmJoinTradeSession( true, this );
	}
	virtual void Decline()
	{
		ConfirmJoinTradeSession( false, this );
	}
	static void ConfirmJoinTradeSession( bool bConfirmed, void *pContext )
	{
		CTFTradeRequestNotification *pNotification = (CTFTradeRequestNotification*)pContext;
		GCSDK::CGCMsg< MsgGCTrading_InitiateTradeResponse_t > msg( k_EMsgGCTrading_InitiateTradeResponse );
		msg.Body().m_eResponse = bConfirmed ? k_EGCMsgInitiateTradeResponse_Accepted : k_EGCMsgInitiateTradeResponse_Declined;
		msg.Body().m_unTradeRequestID = pNotification->m_unTradeRequestID;
		GCClientSystem()->BSendMessage( msg );
		if ( bConfirmed )
		{
			ShowWaitingDialog( new CTradingWaitDialog(), "#TF_Trading_WaitingForServer", true, false, kTradeRequestLifetime );
		}
		// now we can be deleted
		pNotification->SetIsInUse( false );
		pNotification->MarkForDeletion();
	}

	static bool IsTradingNotification( CEconNotification * pNotification )
	{
		return dynamic_cast< CTFTradeRequestNotification * >( pNotification ) != NULL;
	}

public:
	uint32 m_unTradeRequestID;
	wchar_t m_wszPlayerName[MAX_PLAYER_NAME_LENGTH];
};

// request from party A (through GC) to start trading
class CGCTrading_InitiateTradeRequest : public GCSDK::CGCClientJob
{
public:
	CGCTrading_InitiateTradeRequest( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}

	void SendDeclinedMessage( uint32 unTradeRequestID )
	{
		GCSDK::CGCMsg< MsgGCTrading_InitiateTradeResponse_t > msgResponse( k_EMsgGCTrading_InitiateTradeResponse );
		msgResponse.Body().m_eResponse = k_EGCMsgInitiateTradeResponse_Declined;
		msgResponse.Body().m_unTradeRequestID = unTradeRequestID;
		GCClientSystem()->BSendMessage( msgResponse );
	}

	virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
	{
		GCSDK::CGCMsg<MsgGCTrading_InitiateTradeRequest_t> msg( pNetPacket );
		CUtlString playerName;
		msg.BReadStr( &playerName );

		if ( sbTestingSelfTrade )
		{
			CloseWaitingDialog();
		}
		else if ( msg.Body().m_ulOtherSteamID == Trading_GetLocalPlayerSteamID().ConvertToUint64() )
		{
			CloseWaitingDialog();
		}

		iTradeRequests++;
#ifdef TF_CLIENT_DLL
		C_CTF_GameStats.Event_Trading( IE_TRADING_REQUEST_RECEIVED, msg.Body().m_ulOtherSteamID, iTradeRequests );
#endif

		// auto-decline for ignored or blocked peoples
		if ( steamapicontext == NULL || steamapicontext->SteamFriends() == NULL )
		{
			return true;
		}
		CSteamID steamIDOther( msg.Body().m_ulOtherSteamID );
		EFriendRelationship eRelationship = steamapicontext->SteamFriends()->GetFriendRelationship( steamIDOther );
		switch ( eRelationship )
		{
			case k_EFriendRelationshipBlocked:
			case k_EFriendRelationshipIgnored:
			case k_EFriendRelationshipIgnoredFriend:
			{
				SendDeclinedMessage( msg.Body().m_unTradeRequestID );
				return true;
			}
			break;
		} // switch

		switch ( cl_trading_show_requests_from.GetInt() )
		{
			case kShowTradeRequestsFrom_FriendsOnly:
			{
				if ( eRelationship != k_EFriendRelationshipFriend )
				{
					SendDeclinedMessage( msg.Body().m_unTradeRequestID );
					return true;
				}
			}
			break;
			case kShowTradeRequestsFrom_FriendsAndCurrentServer:
			{
				if ( eRelationship == k_EFriendRelationshipFriend )
					break;
				bool bInCurrentGame = false;
				if ( engine->IsInGame() )
				{
					// otherwise, test if they are in the current game
					for( int iPlayerIndex = 1 ; iPlayerIndex <= MAX_PLAYERS; iPlayerIndex++ )
					{
						if ( g_PR && g_PR->IsConnected( iPlayerIndex ) ) 
						{
							player_info_t pi;
							if ( !engine->GetPlayerInfo( iPlayerIndex, &pi ) )
								continue;
							if ( !pi.friendsID )
								continue;
							
							CSteamID steamID( pi.friendsID, 1, GetUniverse(), k_EAccountTypeIndividual );
							if ( steamID == steamIDOther )
							{
								bInCurrentGame = true;
								break;
							}
						}
					}
				}
				if ( bInCurrentGame == false )
				{
					SendDeclinedMessage( msg.Body().m_unTradeRequestID );
					return true;
				}
			}
			break;
			case kShowTradeRequestsFrom_Anyone:
			{
				// nothing to check
			}
			break;
			case kShowTradeRequestsFrom_NoOne:
			{
				SendDeclinedMessage( msg.Body().m_unTradeRequestID );
				return true;
			}
			break;
		}
		
		NotificationQueue_Add( new CTFTradeRequestNotification( msg.Body().m_ulOtherSteamID, msg.Body().m_unTradeRequestID, playerName.Get() ) );
		return true;
	}
protected:
};
GC_REG_JOB( GCSDK::CGCClient, CGCTrading_InitiateTradeRequest, "CGCTrading_InitiateTradeRequest", k_EMsgGCTrading_InitiateTradeRequest, GCSDK::k_EServerTypeGCClient );

#ifdef _DEBUG
#ifdef CLIENT_DLL
CON_COMMAND( cl_trading_test, "Tests the trade ui notification." )
{
	if ( steamapicontext == NULL || steamapicontext->SteamUser() == NULL )
		return;

	CSteamID steamID = steamapicontext->SteamUser()->GetSteamID();
	NotificationQueue_Add( new CTFTradeRequestNotification( steamID.ConvertToUint64(), steamID.ConvertToUint64(), "Biff" ) );
}
#endif
#endif // _DEBUG

/**
 * Remove notification that matches the trade request
 */
class CEconNotificationVisitor_RemoveTradeRequest	: public CEconNotificationVisitor
{
public: 
	CEconNotificationVisitor_RemoveTradeRequest( uint32 unTradeRequestID ) : m_unTradeRequestID( unTradeRequestID ) {}
	virtual void Visit( CEconNotification &notification )
	{
		if ( CTFTradeRequestNotification::IsTradingNotification( &notification ) )
		{
			CTFTradeRequestNotification &tradeNotification = dynamic_cast< CTFTradeRequestNotification& >( notification );
			if ( tradeNotification.m_unTradeRequestID == m_unTradeRequestID )
			{
				tradeNotification.MarkForDeletion();
			}
		}
	}
private:
	uint32 m_unTradeRequestID;
};

// GC notification of trade request status
static const char *g_pszTradeResponseDescLocKeys[] =
{
	"#TF_Trading_BusyText",					// k_EGCMsgInitiateTradeResponse_Accepted (should never be used!)
	"#TF_Trading_DeclinedText",				// k_EGCMsgInitiateTradeResponse_Declined
	"#TF_Trading_VACBannedText",			// k_EGCMsgInitiateTradeResponse_VAC_Banned_Initiator
	"#TF_Trading_VACBanned2Text",			// k_EGCMsgInitiateTradeResponse_VAC_Banned_Target
	"#TF_Trading_BusyText",					// k_EGCMsgInitiateTradeResponse_Target_Already_Trading
	"#TF_Trading_DisabledText",				// k_EGCMsgInitiateTradeResponse_Disabled
	"#TF_Trading_NotLoggedIn",				// k_EGCMsgInitiateTradeResponse_NotLoggedIn
	"#TF_Trading_BusyText",					// k_EGCMsgInitiateTradeResponse_Cancel (should never be used!)
	"#TF_Trading_TooSoon",					// k_EGCMsgInitiateTradeResponse_TooSoon
	"#TF_Trading_TooSoonPenalty",			// k_EGCMsgInitiateTradeResponse_TooSoonPenalty
	"#TF_Trading_TradeBannedText",			// (was k_EGCMsgInitiateTradeResponse_Free_Account_Initiator_DEPRECATED)
	"#TF_Trading_TradeBanned2Text",			// k_EGCMsgInitiateTradeResponse_Trade_Banned_Target
	"#TF_Trading_FreeAccountInitiate",		// k_EGCMsgInitiateTradeResponse_Free_Account_Initiator
	"#TF_Trading_SharedAccountInitiate",	// k_EGCMsgInitiateTradeResponse_Shared_Account_Initiator
	"#TF_Trading_Service_Unavailable",		// k_EGCMsgInitiateTradeResponse_Service_Unavailable
	"#TF_Trading_YouBlockedThem",			// k_EGCMsgInitiateTradeResponse_Target_Blocked
	"#TF_Trading_NeedVerifiedEmail",		// k_EGCMsgInitiateTradeResponse_NeedVerifiedEmail
	"#TF_Trading_NeedSteamGuard",			// k_EGCMsgInitiateTradeResponse_NeedSteamGuard
	"#TF_Trading_SteamGuardDuration",		// k_EGCMsgInitiateTradeResponse_SteamGuardDuration
	"#TF_Trading_TheyCannotTrade",			// k_EGCMsgInitiateTradeResponse_TheyCannotTrade
	"#TF_Trading_PasswordChanged",			// k_EGCMsgInitiateTradeResponse_Recent_Password_Reset = 20,
	"#TF_Trading_NewDevice",				// k_EGCMsgInitiateTradeResponse_Using_New_Device = 21,
	"#TF_Trading_InvalidCookie"				// k_EGCMsgInitiateTradeResponse_Sent_Invalid_Cookie = 22,
};

class CGCTrading_InitiateTradeResponse : public GCSDK::CGCClientJob
{
public:
	CGCTrading_InitiateTradeResponse( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}
	virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
	{
		// If this assertion fails it probably means you added a new value to EGCMsgInitiateTradeResponse
		// but didn't add a string to the array that tracks the user-facing response strings.
		Assert( ARRAYSIZE( g_pszTradeResponseDescLocKeys ) == k_EGCMsgInitiateTradeResponse_Count );

		CloseWaitingDialog();

		GCSDK::CGCMsg<MsgGCTrading_InitiateTradeResponse_t> msg( pNetPacket );
		const uint32 eResponse = msg.Body().m_eResponse;
		switch ( eResponse )
		{
			case k_EGCMsgInitiateTradeResponse_Accepted:
#ifdef TF_CLIENT_DLL
				C_CTF_GameStats.Event_Trading( IE_TRADING_REQUEST_ACCEPTED, iTradeRequests, g_RejectedReasons[msg.Body().m_eResponse]  );
#endif
				ShowWaitingDialog( new CTradingWaitDialog(), "#TF_Trading_WaitingForStart", true, false, 30.0f );
				return true; // !

			case k_EGCMsgInitiateTradeResponse_Cancel:
			{
				CEconNotificationVisitor_RemoveTradeRequest visitor( msg.Body().m_unTradeRequestID );
				NotificationQueue_Visit( visitor );
				break;
			}

			case k_EGCMsgInitiateTradeResponse_Declined:
			case k_EGCMsgInitiateTradeResponse_VAC_Banned_Initiator:
			case k_EGCMsgInitiateTradeResponse_VAC_Banned_Target:
			case k_EGCMsgInitiateTradeResponse_Target_Already_Trading:
			case k_EGCMsgInitiateTradeResponse_Disabled:
			case k_EGCMsgInitiateTradeResponse_NotLoggedIn:
			case k_EGCMsgInitiateTradeResponse_TooSoon:
			case k_EGCMsgInitiateTradeResponse_TooSoonPenalty:
			case k_EGCMsgInitiateTradeResponse_Trade_Banned_Initiator:
			case k_EGCMsgInitiateTradeResponse_Trade_Banned_Target:
			case k_EGCMsgInitiateTradeResponse_Shared_Account_Initiator:
			case k_EGCMsgInitiateTradeResponse_Service_Unavailable:
			case k_EGCMsgInitiateTradeResponse_Target_Blocked:
			case k_EGCMsgInitiateTradeResponse_NeedVerifiedEmail:
			case k_EGCMsgInitiateTradeResponse_NeedSteamGuard:
			case k_EGCMsgInitiateTradeResponse_TheyCannotTrade:
			case k_EGCMsgInitiateTradeResponse_Recent_Password_Reset:
			case k_EGCMsgInitiateTradeResponse_Using_New_Device:
			case k_EGCMsgInitiateTradeResponse_Sent_Invalid_Cookie:

				ShowMessageBox( "#TF_Trading_StatusTitle",  g_pszTradeResponseDescLocKeys[eResponse], "#GameUI_OK" );
				break;

			case k_EGCMsgInitiateTradeResponse_SteamGuardDuration:
			{

				KeyValuesAD kvTokens( "CTradingWaitDialog" );
				kvTokens->SetWString( "days", L"15" ); // Ideally this would come from the GC, which would get the value from Steam
				ShowMessageBox( "#TF_Trading_StatusTitle",  g_pszTradeResponseDescLocKeys[eResponse], kvTokens, "#GameUI_OK" );
				break;
			}

			default:
#ifdef TF_CLIENT_DLL
				C_CTF_GameStats.Event_Trading( IE_TRADING_REQUEST_REJECTED, iTradeRequests, "unknown" );
#endif
				ShowMessageBox( "#TF_Trading_StatusTitle",  "#TF_Trading_BusyText", "#GameUI_OK" );
				return true;
		} // switch

#ifdef TF_CLIENT_DLL
		C_CTF_GameStats.Event_Trading( IE_TRADING_REQUEST_REJECTED, iTradeRequests, g_RejectedReasons[msg.Body().m_eResponse] );
#endif

		return true;
	}
};
GC_REG_JOB( GCSDK::CGCClient, CGCTrading_InitiateTradeResponse, "CGCTrading_InitiateTradeResponse", k_EMsgGCTrading_InitiateTradeResponse, GCSDK::k_EServerTypeGCClient );

// start trading session
class CGCTrading_StartSession : public GCSDK::CGCClientJob
{
public:
	CGCTrading_StartSession( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}
	virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
	{
		GCSDK::CGCMsg<MsgGCTrading_StartSession_t> msg( pNetPacket );

		steamapicontext->SteamFriends()->ActivateGameOverlayToUser( "jointrade", msg.Body().m_ulSteamIDPartyB );

		CloseWaitingDialog();

		// remove all trading notifications
		NotificationQueue_Remove( &CTFTradeRequestNotification::IsTradingNotification );

		return true;
	}
};
GC_REG_JOB( GCSDK::CGCClient, CGCTrading_StartSession, "CGCTrading_StartSession", k_EMsgGCTrading_StartSession, GCSDK::k_EServerTypeGCClient );


//-----------------------------------------------------------------------------
// External interface

CSteamID Trading_GetLocalPlayerSteamID()
{
	if ( steamapicontext && steamapicontext->SteamUser() )
	{
		return steamapicontext->SteamUser()->GetSteamID();
	}
	return CSteamID();
}

void Trading_RequestTrade( int iPlayerIdx )
{
	CSteamID steamID;
	C_BasePlayer *pPlayer = ToBasePlayer( UTIL_PlayerByIndex( iPlayerIdx ) );
	if ( pPlayer && pPlayer->GetSteamID( &steamID ) )
	{
		Trading_RequestTrade( steamID );
	}
}

void Trading_RequestTrade( const CSteamID &steamID )
{								
	sbTestingSelfTrade = false;
	GCSDK::CGCMsg< MsgGCTrading_InitiateTradeRequest_t > msg( k_EMsgGCTrading_InitiateTradeRequest );
	msg.Body().m_ulOtherSteamID = steamID.ConvertToUint64();
	bool bSent = GCClientSystem()->BSendMessage( msg );
	if ( bSent )
	{
		iTradeRequests++;
#ifdef TF_CLIENT_DLL
		C_CTF_GameStats.Event_Trading( IE_TRADING_REQUEST_SENT, msg.Body().m_ulOtherSteamID, iTradeRequests );
#endif

		const char* pPlayerName = InventoryManager()->PersonaName_Get( steamID.GetAccountID() );
		if ( pPlayerName != NULL && FStrEq( pPlayerName, "" ) == false )
		{
			wchar_t wszPlayerName[MAX_PLAYER_NAME_LENGTH];
			g_pVGuiLocalize->ConvertANSIToUnicode( pPlayerName, wszPlayerName, sizeof(wszPlayerName) );
			CTradingWaitDialog *pDialog = new CTradingWaitDialog( "#TF_Trading_TimeoutPartyB_Named", wszPlayerName );
			ShowWaitingDialog( pDialog, "#TF_Trading_WaitingForPartyB", true, true, 30.0f );
			wchar_t wszConstructedString[1024];
			g_pVGuiLocalize->ConstructString_safe( wszConstructedString, g_pVGuiLocalize->Find( "#TF_Trading_WaitingForPartyB_Named" ), 1, wszPlayerName );
			pDialog->SetDialogVariable( "updatetext", wszConstructedString );
		}
		else
		{
			CTradingWaitDialog *pDialog = new CTradingWaitDialog( "#TF_Trading_TimeoutPartyB" );
			ShowWaitingDialog( pDialog, "#TF_Trading_WaitingForPartyB", true, true, 30.0f );
		}
	}
}

const char* UniverseToCommunityURL( EUniverse universe )
{
	switch( universe )
	{
	default: // return public if we don't have a better guess.
	case k_EUniversePublic: return "https://steamcommunity.com";
	case k_EUniverseBeta: return "https://beta.steamcommunity.com";
	case k_EUniverseDev: return "https://localhost/community";
	}

	// Should never get here.
	return UniverseToCommunityURL( k_EUniversePublic ); 
}

const char* GetCommunityURL()
{
	if ( GetUniverse() == k_EUniverseInvalid )
	{
		Assert( !"calling GetCommunityURL when not connected. This is allowed, but will return public universe." );
		return UniverseToCommunityURL( k_EUniversePublic );
	}

	return UniverseToCommunityURL( GetUniverse() );
}

void Trading_SendGift( const CSteamID& steamID, const CEconItemView& giftItem )
{
	if ( !steamapicontext || !steamapicontext->SteamFriends() )
	{
		// TODO: Error dialog.
		return;
	}

#ifdef TF_CLIENT_DLL
	C_CTF_GameStats.Event_Trading( IE_TRADING_ITEM_GIFTED, steamID.ConvertToUint64(), iGiftsGiven );
#endif

	// Build up the steam URL and send it over. 
	// Should look like this: https://steamcommunity.com/trade/1/sendgift/?appid=&contextid=&assetid=&steamid_target=

	steamapicontext->SteamFriends()->ActivateGameOverlayToWebPage( 
		CFmtStrMax( "%s/trade/1/sendgift/?appid=%d&contextid=%d&assetid=%llu&steamid_target=%llu",
					GetCommunityURL(), 
					engine->GetAppID(),
					2, // k_EEconContextBackpack
					giftItem.GetItemID(),
					steamID.ConvertToUint64()
		)
	);

}

CON_COMMAND( cl_trade, "Trade with a person by player name" )
{
	if ( args.ArgC() < 2 )
		return;

	if ( GetUniverse() == k_EUniverseInvalid )
		return;

	int iLocalPlayerIndex =  GetLocalPlayerIndex();
	for( int iPlayerIndex = 1 ; iPlayerIndex <= MAX_PLAYERS; iPlayerIndex++ )
	{
		if( ( iPlayerIndex != iLocalPlayerIndex ) && ( g_PR->IsConnected( iPlayerIndex ) ) )
		{
			player_info_t pi;
			if ( !engine->GetPlayerInfo( iPlayerIndex, &pi ) )
				continue;
			if ( !pi.friendsID )
				continue;

			if ( FStrEq( pi.name, args[1] ) == false )
				continue;
			
			CSteamID steamID( pi.friendsID, 1, GetUniverse(), k_EAccountTypeIndividual );
			Trading_RequestTrade( steamID );
			return;
		}
	}
}

CON_COMMAND( cl_trade_steamid, "Trade with a person by steam id" )
{
	if ( args.ArgC() < 2 )
		return;

	if ( GetUniverse() == k_EUniverseInvalid )
		return;

	const char *pInput = args[1];
	if ( pInput[0] >= '0' && pInput[0] <= '9' )
	{
		CSteamID steamID;
		steamID.SetFromString( pInput, GetUniverse() );
		if ( steamID.IsValid() )
			Trading_RequestTrade( steamID );
	}
}

#ifdef _DEBUG
CON_COMMAND( cl_trading_test_self_trade, "Test self-trading" )
{
	Trading_RequestTrade( Trading_GetLocalPlayerSteamID() );
	sbTestingSelfTrade = true;
}
#endif