summaryrefslogtreecommitdiff
path: root/game/client/econ/item_rental_ui.cpp
blob: 44025a52ca13b55ec5572875a5e5256bf8528283 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//


#include "cbase.h"
#include "rtime.h"
#include "vgui_controls/EditablePanel.h"
#include "vgui_controls/TextEntry.h"
#include "vgui/IInput.h"
#include "econ_item_system.h"
#include "econ_item_constants.h"
#include "econ_gcmessages.h"
#include "econ_item_inventory.h"
#include "item_rental_ui.h"

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

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

//-----------------------------------------------------------------------------
// Purpose: Confirm item preview.
//-----------------------------------------------------------------------------
class CConfirmItemPreviewDialog : public CBaseToolUsageDialog
{
	DECLARE_CLASS_SIMPLE( CConfirmItemPreviewDialog, CBaseToolUsageDialog );

public:
	CConfirmItemPreviewDialog( vgui::Panel *pParent, CEconItemView *pPreviewItem );
	~CConfirmItemPreviewDialog();

	virtual void	ApplySchemeSettings( vgui::IScheme *scheme );
	virtual void	Apply( void );

private:

	CEconItemView* m_pPreviewItem;
};

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CConfirmItemPreviewDialog::CConfirmItemPreviewDialog( vgui::Panel *parent, CEconItemView *pPreviewItem ) : CBaseToolUsageDialog( parent, "ConfirmItemPreviewDialog", pPreviewItem, pPreviewItem )
{
	m_pPreviewItem = pPreviewItem;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CConfirmItemPreviewDialog::~CConfirmItemPreviewDialog()
{
	delete m_pPreviewItem;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CConfirmItemPreviewDialog::ApplySchemeSettings( vgui::IScheme *pScheme )
{
	LoadControlSettings( "Resource/UI/econ/ConfirmItemPreviewDialog.res" );

	BaseClass::ApplySchemeSettings( pScheme );

	m_pTitleLabel = dynamic_cast<vgui::Label*>( FindChildByName("TitleLabel") );
	if ( m_pTitleLabel )
	{
		wchar_t	*pszBaseString = g_pVGuiLocalize->Find( "ItemPreviewDialogTitle" );
		if ( pszBaseString )
		{
			wchar_t	wTemp[256];
			g_pVGuiLocalize->ConstructString_safe( wTemp, pszBaseString, 1, m_pToolModelPanel->GetItem()->GetItemName() );
			m_pTitleLabel->SetText( wTemp );
			m_pTitleLabel->GetTextImage()->ClearColorChangeStream();
		}
	}

	m_pSubjectModelPanel->SetVisible( false );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CConfirmItemPreviewDialog::Apply( void )
{
	// Notify the GC that the player wants to preview this item.
	GCSDK::CGCMsg< MsgGCItemPreviewRequest_t > msg( k_EMsgGCItemPreviewRequest );

	msg.Body().m_unItemDefIndex = m_pToolModelPanel->GetItem()->GetItemDefIndex();

	// OGS LOGGING HERE

	GCClientSystem()->BSendMessage( msg );

	EconUI()->SetPreventClosure( false );
}

//-----------------------------------------------------------------------------
// Purpose: GC Msg handler to receive the item preview query response.
//-----------------------------------------------------------------------------
class CGCItemPreviewStatusResponse : public GCSDK::CGCClientJob
{
public:
	CGCItemPreviewStatusResponse( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}

	virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
	{
		GCSDK::CGCMsg<MsgGCItemPreviewCheckStatusResponse_t> msg( pNetPacket );

		CStorePanel *pStorePanel = EconUI()->GetStorePanel();
		if ( !pStorePanel )
			return true;

		if ( msg.Body().m_eResponse == k_EGCMsgResponseOK )
		{
			// We can preview the item.
			CEconItemView *pPreviewItem = new CEconItemView();
			pPreviewItem->Init( msg.Body().m_unItemDefIndex, AE_UNIQUE, AE_USE_SCRIPT_VALUE, true );
			CConfirmItemPreviewDialog *dialog = vgui::SETUP_PANEL( new CConfirmItemPreviewDialog( pStorePanel->GetPropertySheet()->GetActivePage(), pPreviewItem ) );
			MakeModalAndBringToFront( dialog );
		}
		else
		{
#ifdef TF_CLIENT_DLL
			C_CTFGameStats::ImmediateWriteInterfaceEvent( "store_preview_item_denied", CFmtStr( "%i", msg.Body().m_unItemDefIndex ).Access() );
#endif

			// We aren't allowed to preview an item right now.
			CTFMessageBoxDialog* pDialog = ShowMessageBox( "#ItemPreview_PreviewStartFailedTitle", "#ItemPreview_PreviewStartFailedText", "#GameUI_OK" );
			RTime32 nextTime = msg.Body().m_timePreviewTime + EconUI()->GetStorePanel()->GetPriceSheet()->GetPreviewPeriod();
			
			locchar_t wzValue[64];
			char time_buf[k_RTimeRenderBufferSize];
			GLocalizationProvider()->ConvertUTF8ToLocchar( CRTime::Render( nextTime, time_buf ), wzValue, sizeof( wzValue ) );
			pDialog->AddStringToken( "date_time", wzValue );
		}

		return true;
	}

};

GC_REG_JOB( GCSDK::CGCClient, CGCItemPreviewStatusResponse, "CGCItemPreviewStatusResponse", k_EMsgGCItemPreviewStatusResponse, GCSDK::k_EServerTypeGCClient );

//-----------------------------------------------------------------------------
// Purpose: The GC is telling us our request has been granted.
//-----------------------------------------------------------------------------
class CGCItemPreviewRequestResponse : public GCSDK::CGCClientJob
{
public:
	CGCItemPreviewRequestResponse( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}

	static void OnPreviewItemConfirm( bool bConfirmed, void *pContext )
	{
		InventoryManager()->ShowItemsPickedUp( true, false );

		CStorePage* pStorePage = dynamic_cast<CStorePage*>( EconUI()->GetStorePanel()->GetActivePage() );
		if ( pStorePage )
		{
			pStorePage->UpdateModelPanels();
		}
	}

	virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
	{
		GCSDK::CGCMsg<MsgGCItemPreviewRequestResponse_t> msg( pNetPacket );

		CStorePanel *pStorePanel = EconUI()->GetStorePanel();
		if ( !pStorePanel )
			return true;

		if ( msg.Body().m_eResponse == k_EGCMsgResponseOK )
		{
			// The preview has started.
			ShowMessageBox( "#ItemPreview_PreviewStartedTitle", "#ItemPreview_PreviewStartedText", "#GameUI_OK", OnPreviewItemConfirm );
		}
		else
		{
			// The preview cannot start right now for some reason.
		}

		return true;
	}

};

GC_REG_JOB( GCSDK::CGCClient, CGCItemPreviewRequestResponse, "CGCItemPreviewRequestResponse", k_EMsgGCItemPreviewRequestResponse, GCSDK::k_EServerTypeGCClient );

void OpenStoreToItem( bool bConfirmed, void *pContext )
{
	CEconPreviewExpiredNotification* pNotification = (CEconPreviewExpiredNotification*) pContext;
	if ( pNotification )
	{
		pNotification->SetIsInUse( false );
		pNotification->MarkForDeletion();
		if ( bConfirmed )
		{
			EconUI()->OpenStorePanel( pNotification->GetItemDefIndex(), true );
		}
	}
}

CEconPreviewNotification::CEconPreviewNotification( uint64 ulSteamID, uint32 iItemDef ) 
	: CEconNotification() 
{
	SetSteamID( ulSteamID );
	SetLifetime( 20.0f );

	m_pItemDef = GetItemSchema()->GetItemDefinition( iItemDef );
	if ( !m_pItemDef )
		return;

	AddStringToken( "item_name",  g_pVGuiLocalize->Find(m_pItemDef->GetItemBaseName()) );
}

void CEconPreviewExpiredNotification::Trigger()
{
	CTFGenericConfirmDialog *pDialog = ShowConfirmDialog( "#TF_PreviewItem_Expired_Title",  "#TF_PreviewItem_Expired_Text", "#TF_PreviewItem_BuyIt", "#TF_PreviewItem_NotNow", &OpenStoreToItem );
	pDialog->SetContext( this );
	pDialog->AddStringToken( "item_name",  g_pVGuiLocalize->Find(m_pItemDef->GetItemBaseName()) );
	SetIsInUse( true );
}

//-----------------------------------------------------------------------------
// Purpose: The GC is telling us our preview item has expired.
//-----------------------------------------------------------------------------
class CGCItemPreviewExpireNotification : public GCSDK::CGCClientJob
{
public:
	CGCItemPreviewExpireNotification( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}

	virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
	{
		GCSDK::CGCMsg<MsgGCItemPreviewExpireNotification_t> msg( pNetPacket );

		CEconPreviewExpiredNotification *pNotification = new CEconPreviewExpiredNotification( msg.Hdr().m_ulSteamID, msg.Body().m_unItemDefIndex );
		pNotification->SetText( "#TF_PreviewItem_Expired" );
		NotificationQueue_Add( pNotification );

		return true;
	}

};

GC_REG_JOB( GCSDK::CGCClient, CGCItemPreviewExpireNotification, "CGCItemPreviewExpireNotification", k_EMsgGCItemPreviewExpireNotification, GCSDK::k_EServerTypeGCClient );


//-----------------------------------------------------------------------------
// Purpose: The GC is telling us we bought our preview item!
//-----------------------------------------------------------------------------
class CGCItemPreviewItemBoughtNotification : public GCSDK::CGCClientJob
{
public:
	CGCItemPreviewItemBoughtNotification( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}

	virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
	{
		GCSDK::CProtoBufMsg<CMsgGCItemPreviewItemBoughtNotification> msg( pNetPacket );

		CEconPreviewItemBoughtNotification *pNotification = new CEconPreviewItemBoughtNotification( msg.Hdr().client_steam_id(), msg.Body().item_def_index() );
		pNotification->SetText( "#TF_PreviewItem_ItemBought" );
		NotificationQueue_Add( pNotification );

		return true;
	}

};

GC_REG_JOB( GCSDK::CGCClient, CGCItemPreviewItemBoughtNotification, "CGCItemPreviewItemBoughtNotification", k_EMsgGCItemPreviewItemBoughtNotification, GCSDK::k_EServerTypeGCClient );