File: gp_statsDlg.cpp

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (733 lines) | stat: -rw-r--r-- 15,975 bytes parent folder | download | duplicates (2)
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// gp_statsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "gp_stats.h"
#include "gp_statsDlg.h"
#include "../gpersist.h"
#include "../../ghttp/ghttp.h"
#include "../../common/gsAvailable.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// These are controls that are enabled when authenticated,
// and disabled when not authenticated.
//////////////////////////////////////////////////////////
CWnd * ToggleControls[64];

/////////////////////////////////////////////////////////////////////////////
// CGp_statsDlg dialog

CGp_statsDlg::CGp_statsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGp_statsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGp_statsDlg)
	m_email = _T("dan@gamespy.com");
	m_nick = _T("mrpants");
	m_password = _T("mrpants");
	m_profileID = 0;
	m_type = 0;
	m_value = _T("");
	m_newKey = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGp_statsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGp_statsDlg)
	DDX_Control(pDX, IDC_KEYS, m_keys);
	DDX_Text(pDX, IDC_EMAIL, m_email);
	DDX_Text(pDX, IDC_NICK, m_nick);
	DDX_Text(pDX, IDC_PASSWORD, m_password);
	DDX_Text(pDX, IDC_PROFILE_ID, m_profileID);
	DDX_Radio(pDX, IDC_PRIVATE_RW, m_type);
	DDX_Text(pDX, IDC_VALUE, m_value);
	DDX_Text(pDX, IDC_NEW_KEY, m_newKey);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGp_statsDlg, CDialog)
	//{{AFX_MSG_MAP(CGp_statsDlg)
	ON_BN_CLICKED(IDC_AUTHENTICATE, OnAuthenticate)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_GET, OnGet)
	ON_BN_CLICKED(IDC_SET, OnSet)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_LBN_SELCHANGE(IDC_KEYS, OnSelchangeKeys)
	ON_EN_CHANGE(IDC_VALUE, OnChangeValue)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGp_statsDlg message handlers

BOOL CGp_statsDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// Build the list of toggle controls.
	/////////////////////////////////////
	int num = 0;
#define ADD_TOGGLE(id)    ToggleControls[num++] = this->GetDlgItem(id)
	ADD_TOGGLE(IDC_PRIVATE_RW);
	ADD_TOGGLE(IDC_PRIVATE_RO);
	ADD_TOGGLE(IDC_PUBLIC_RW);
	ADD_TOGGLE(IDC_PUBLIC_RO);
	ADD_TOGGLE(IDC_GET);
	ADD_TOGGLE(IDC_SET);
	ADD_TOGGLE(IDC_VALUE);
	ADD_TOGGLE(IDC_KEYS);
	ADD_TOGGLE(IDC_ADD);
	ADD_TOGGLE(IDC_NEW_KEY);
	ToggleControls[num++] = NULL;

	// Init data.
	/////////////
	m_authenticated = FALSE;
	m_gp = NULL;

	// Put ourselves in the unauthenticated stats.
	//////////////////////////////////////////////
	UnAuthenticate();

	// Init the connection to the stats manager.
	////////////////////////////////////////////
	CheckStatsConnection();

	// Init GP.
	///////////
	if(gpInitialize(&m_gp, 0, 0, GP_PARTNERID_GAMESPY) != GP_NO_ERROR)
		MessageBox("FAILED TO INITIALIZE GP!!!");

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGp_statsDlg::OnDestroy() 
{
	CDialog::OnDestroy();

	ClearKeys();

	// Close the stats connection.
	//////////////////////////////
	CloseStatsConnection();

	// Shutdown GP.
	///////////////
	gpDestroy(&m_gp);

	// Shutdown GHTTP.
	//////////////////
	ghttpCleanup();
}

void CGp_statsDlg::Authenticated()
{
	m_authenticated = TRUE;

	// Enable controls.
	///////////////////
	int i;
	for(i = 0 ; ToggleControls[i] ; i++)
		ToggleControls[i]->EnableWindow();
}

void CGp_statsDlg::UnAuthenticate()
{
	m_authenticated = FALSE;
	m_profileID = 0;

	// Disable controls.
	////////////////////
	int i;
	for(i = 0 ; ToggleControls[i] ; i++)
		ToggleControls[i]->EnableWindow(FALSE);
}

BOOL CGp_statsDlg::CheckStatsConnection()
{
	// Are we connected?
	////////////////////
	if(IsStatsConnected())
		return TRUE;

	// Set the gamename and secret key.
	///////////////////////////////////
#if 1
	strcpy(gcd_gamename, "gmtest");
	gcd_secret_key[0] = 'H';
	gcd_secret_key[1] = 'A';
	gcd_secret_key[2] = '6';
	gcd_secret_key[3] = 'z';
	gcd_secret_key[4] = 'k';
	gcd_secret_key[5] = 'S';
	gcd_secret_key[7] = '\0';
#else
	strcpy(gcd_gamename, "excessive");
	gcd_secret_key[0] = 'G';
	gcd_secret_key[1] = 'n';
	gcd_secret_key[2] = '3';
	gcd_secret_key[3] = 'a';
	gcd_secret_key[4] = 'Y';
	gcd_secret_key[5] = '9';
	gcd_secret_key[7] = '\0';
#endif
	
	// check that the game's backend is available
	GSIACResult ac_result;
	GSIStartAvailableCheck(gcd_gamename);
	while((ac_result = GSIAvailableCheckThink()) == GSIACWaiting)
		msleep(5);
	if(ac_result != GSIACAvailable)
	{
		MessageBox("The backend is not available\n");
		return TRUE;
	}

	// Try to connect.
	//////////////////
	int result = InitStatsConnection(0);
	if(result == GE_NOERROR)
		return TRUE;

	// Error!
	/////////
	CString message;
	if(result == GE_NOSOCKET)
		message = "Unable to create a socket.";
	else if(result == GE_NODNS)
		message = "Unable to resolve a DNS name.";
	else if(result == GE_NOCONNECT)
		message = "Unable to connect to stats server, or connection lost.";
	else if(result == GE_DATAERROR)
		message = "Bad data from the stats server.";
	else
		message = "Error.";
	MessageBox(message, "Error connecting to the stats server");

	return FALSE;
}

void CGp_statsDlg::SendPassword()
{
	// Form the URL for the request.
	////////////////////////////////
	CString URL = "http://gamespyarcade.com/software/reqemail.asp?email=";
	URL += m_email;

	// Set the wait cursor.
	///////////////////////
	HCURSOR hPrevCursor = GetCursor();
	SetCursor(LoadCursor(NULL, IDC_WAIT));

	// Do the request.
	//////////////////
	ghttpGetFile(URL, GHTTPTrue, NULL, NULL);

	// Reset the previous cursor.
	/////////////////////////////
	SetCursor(hPrevCursor);
}

void CreateAccountCallback(GPConnection * connection, void * arg_, void * param)
{
	GPNewUserResponseArg * arg = (GPNewUserResponseArg *)arg_;
	int * pid = (int *)param;

	// Store the result.
	////////////////////
	if(arg->result == GP_NO_ERROR)
		*pid = arg->profile;
	else
	{
		// If the nick was already in use, just pretend we created it.
		//////////////////////////////////////////////////////////////
		GPErrorCode errorCode;
		gpGetErrorCode(connection, &errorCode);
		if(errorCode == GP_NEWUSER_BAD_NICK)
			*pid = arg->profile;
		else
			*pid = 0;
	}
}

BOOL CGp_statsDlg::CreateAccount()
{
	int pid;
	int rcode;

	// Create the account.
	//////////////////////
	GPResult result = gpNewUser(
		&m_gp,
		m_nick,
		NULL,
		m_email,
		m_password,
		NULL,
		GP_BLOCKING,
		CreateAccountCallback,
		&pid);
	if(result != GP_NO_ERROR)
	{
		MessageBox("There was an error creating the account.");
		return FALSE;
	}

	// Check for success.
	/////////////////////
	if(pid)
	{
		m_profileID = pid;
		return TRUE;
	}

	// Get the error code.
	//////////////////////
	GPErrorCode errorCode;
	gpGetErrorCode(&m_gp, &errorCode);

	// Handle the error code.
	/////////////////////////
	if(errorCode == GP_NEWUSER_BAD_PASSWORD)
	{
		rcode = MessageBox(
			"You have entered an incorrect password for this e-mail address\n"
			"Would you like the password sent to the e-mail address?",
			NULL,
			MB_YESNO);

		// If no, we're done.
		/////////////////////
		if(rcode == IDNO)
			return FALSE;

		// Send the password.
		/////////////////////
		SendPassword();

		return FALSE;
	}

	// An unknown error code.
	/////////////////////////
	MessageBox("There was an error creating the account.");
	return FALSE;
}

void CheckAccountCallback(GPConnection * connection, void * arg_, void * param)
{
	GPCheckResponseArg * arg = (GPCheckResponseArg *)arg_;
	int * pid = (int *)param;

	// Store the result.
	////////////////////
	if(arg->result == GP_NO_ERROR)
		*pid = arg->profile;
	else
		*pid = 0;
}

BOOL CGp_statsDlg::CheckAccount()
{
	int pid;
	int rcode;

	// Check for the account.
	/////////////////////////
	GPResult result = gpCheckUser(
		&m_gp,
		m_nick,
		m_email,
		m_password,
		GP_BLOCKING,
		CheckAccountCallback,
		&pid);
	if(result != GP_NO_ERROR)
	{
		MessageBox("There was an error authenticating the account.");
		return FALSE;
	}

	// Check for success.
	/////////////////////
	if(pid)
	{
		m_profileID = pid;
		return TRUE;
	}

	// Get the error code.
	//////////////////////
	GPErrorCode errorCode;
	gpGetErrorCode(&m_gp, &errorCode);

	// Handle the error code.
	/////////////////////////
	if(errorCode == GP_CHECK_BAD_EMAIL)
	{
		// Ask if they want to create the account.
		//////////////////////////////////////////
		rcode = MessageBox(
			"This account does not exist.\n"
			"Would you like to create it?",
			NULL,
			MB_YESNO);

		// If no, we're done.
		/////////////////////
		if(rcode == IDNO)
			return FALSE;

		// Create the account.
		//////////////////////
		return CreateAccount();
	}
	else if(errorCode == GP_CHECK_BAD_NICK)
	{
		rcode = MessageBox(
			"There are no profiles under this account with the nick you have entered\n"
			"Would you like to create one?",
			NULL,
			MB_YESNO);

		// If no, we're done.
		/////////////////////
		if(rcode == IDNO)
			return FALSE;

		// Create the account.
		//////////////////////
		return CreateAccount();
	}
	else if(errorCode == GP_CHECK_BAD_PASSWORD)
	{
		rcode = MessageBox(
			"You have entered an incorrect password for this e-mail address\n"
			"Would you like the password sent to the e-mail address?",
			NULL,
			MB_YESNO);

		// If no, we're done.
		/////////////////////
		if(rcode == IDNO)
			return FALSE;

		// Send the password.
		/////////////////////
		SendPassword();

		return FALSE;
	}

	// An unknown error code.
	/////////////////////////
	MessageBox("There was an error authenticating the account.");
	return FALSE;
}

BOOL statsAuthFinished;
void StatsAuthenticationCallback(int localid, int profileid, int authenticated, char *errmsg, void *instance)
{
	int * result = (int *)instance;

	*result = authenticated;

	if(authenticated != 1)
		MessageBox(NULL, errmsg, "Error authenticating with the stats backend", MB_OK);

	statsAuthFinished = TRUE;
}

BOOL CGp_statsDlg::StatsAuthentication()
{
	char response[33];
	int result;

	// The auth call.
	/////////////////
	statsAuthFinished = FALSE;
	PreAuthenticatePlayerPM(
		0,
		m_profileID,
		GenerateAuth(GetChallenge(NULL), (char *)(LPCSTR)m_password, response),
		StatsAuthenticationCallback,
		&result);

	// Wait for it to finish.
	/////////////////////////
	while(!statsAuthFinished)
		if(!PersistThink())
			CheckStatsConnection();

	return (result == 1);
}

void CGp_statsDlg::OnAuthenticate()
{
	// Update dialog members.
	/////////////////////////
	UpdateData();

	// Sanity check args.
	/////////////////////
	if(!m_email.GetLength() || !m_nick.GetLength() || !m_password.GetLength())
	{
		MessageBox("E-mail, nick, and password must not be blank.");
		return;
	}

	// If we're authenticated, unauthenticate.
	//////////////////////////////////////////
	if(m_authenticated)
		UnAuthenticate();

	// Check the account.
	/////////////////////
	if(!CheckAccount())
		return;

	// Do stats authentication.
	///////////////////////////
	if(!StatsAuthentication())
		return;

	// We're authenticated.
	///////////////////////
	Authenticated();

	// Update dialog display.
	/////////////////////////
	UpdateData(FALSE);
}

persisttype_t TypeConversion(int type)
{
	if(type == 0)
		return pd_private_rw;
	if(type == 1)
		return pd_private_ro;
	if(type == 2)
		return pd_public_rw;
	return pd_public_ro;
}

BOOL GetKeyValue(LPCSTR src, CString & key, CString & value)
{
	const char * str;
	const char * keyStart;
	const char * valueStart;
	int keyLen;
	int valueLen;

	// Check for no input.
	//////////////////////
	if(!src)
		return FALSE;

	// Check the starting \.
	////////////////////////
	if(src[0] != '\\')
		return FALSE;

	// Clear the key and value.
	///////////////////////////
	key.Empty();
	value.Empty();

	// Find the key and value, plus lengths.
	////////////////////////////////////////
	keyStart = (src + 1);
	valueStart = strchr(keyStart, '\\');
	if(!valueStart || (valueStart == keyStart))
		return FALSE;
	keyLen = (valueStart - keyStart);
	valueStart++;
	str = strchr(valueStart, '\\');
	if(str)
		valueLen = (str - valueStart);
	else
		valueLen = strlen(valueStart);

	// Copy off the key.
	////////////////////
	char * keyStr = key.GetBuffer(keyLen);
	memcpy(keyStr, keyStart, keyLen);
	key.ReleaseBuffer(keyLen);

	// Copy off the value.
	////////////////////
	char * valueStr = value.GetBuffer(valueLen);
	memcpy(valueStr, valueStart, valueLen);
	value.ReleaseBuffer(valueLen);

	return TRUE;
}

void CGp_statsDlg::ClearKeys()
{
	int count = m_keys.GetCount();
	int i;
	for(i = 0 ; i < count ; i++)
		delete (CString *)m_keys.GetItemDataPtr(i);
	m_keys.ResetContent();
}

void CGp_statsDlg::GotData(LPCSTR data)
{
	CString key;
	CString value;
	int nIndex;

	// Go through the keys/values.
	//////////////////////////////
	while(GetKeyValue(data, key, value))
	{
		// Adjust the data based on the key and value lengths.
		//////////////////////////////////////////////////////
		data += (key.GetLength() + value.GetLength() + 2);

		// Add the new key/value.
		/////////////////////////
		nIndex = m_keys.AddString(key);
		if(nIndex != -1)
			m_keys.SetItemDataPtr(nIndex, new CString(value));
	}
}

BOOL getDataFinished;
void GetDataCallback(int localid, int profileid, persisttype_t type, int index, int success, time_t modified, char *data, int len, void *instance)
{
	getDataFinished = TRUE;

	if(success)
	{
		CGp_statsDlg * dlg = (CGp_statsDlg *)instance;
		dlg->GotData(data);
	}
}

void CGp_statsDlg::OnGet() 
{
	// Update dialog members.
	/////////////////////////
	UpdateData();

	// Clear the keys and value.
	////////////////////////////
	ClearKeys();
	m_value.Empty();

	// Make sure we're connected to the stats server.
	/////////////////////////////////////////////////
	if(!CheckStatsConnection())
		return;

	// Get the data.
	////////////////
	getDataFinished = FALSE;
	GetPersistDataValues(0, m_profileID, TypeConversion(m_type), 0, "", GetDataCallback, this);
	while(!getDataFinished)
		if(!PersistThink())
			CheckStatsConnection();

	// Update dialog display.
	/////////////////////////
	UpdateData(FALSE);
}

BOOL setDataFinished;
void SetDataCallback(int localid, int profileid, persisttype_t type, int index, int success, time_t modified, void *instance)
{
	setDataFinished = TRUE;
}

void CGp_statsDlg::OnSet() 
{
	// Update dialog members.
	/////////////////////////
	UpdateData();

	// Make sure we're connected to the stats server.
	/////////////////////////////////////////////////
	if(!CheckStatsConnection())
		return;

	// Build the data string.
	/////////////////////////
	int count = m_keys.GetCount();
	int i;
	CString data;
	CString key;
	CString * value;
	for(i = 0 ; i < count ; i++)
	{
		m_keys.GetText(i, key);
		value = (CString *)m_keys.GetItemDataPtr(i);

		data += '\\';
		data += key;
		data += '\\';
		data += *value;
	}

	// Set some data.
	/////////////////
	setDataFinished = FALSE;
	SetPersistDataValues(0, m_profileID, TypeConversion(m_type), 0, (char *)(LPCSTR)data, SetDataCallback, this);
	while(!setDataFinished)
		if(!PersistThink())
			CheckStatsConnection();
}

void CGp_statsDlg::OnSelchangeKeys() 
{
	UpdateData();

	// Get the new key/value.
	/////////////////////////
	int nIndex = m_keys.GetCurSel();
	if(nIndex != -1)
		m_value = *(CString *)m_keys.GetItemDataPtr(nIndex);
	
	UpdateData(FALSE);
}

void CGp_statsDlg::OnAdd() 
{
	UpdateData();

	if(m_newKey.IsEmpty())
		return;

	int nIndex = m_keys.AddString(m_newKey);
	if(nIndex != -1)
		m_keys.SetItemDataPtr(nIndex, new CString);

	m_newKey.Empty();

	UpdateData(FALSE);
}

void CGp_statsDlg::OnChangeValue() 
{
	UpdateData();
	int nIndex = m_keys.GetCurSel();
	if(nIndex != -1)
	{
		CString * string = (CString *)m_keys.GetItemDataPtr(nIndex);
		*string = m_value;
	}
}