about summary refs log tree commit diff
path: root/src/game/server/ranking.cpp
blob: b5d88514974ad079230e0f288f01144aebed1660 (plain)
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
#if defined(CONF_SQL)
#include <string.h>
#include <algorithm>

#include "ranking.h"
#include <engine/shared/config.h>

static LOCK gs_SqlLock = 0;

CRanking::CRanking(CGameContext *pGameServer) : m_pGameServer(pGameServer),
		m_pServer(pGameServer->Server()),
		m_pDatabase(g_Config.m_SvSqlDatabase),
		m_pUser(g_Config.m_SvSqlUser),
		m_pPass(g_Config.m_SvSqlPass),
		m_pIp(g_Config.m_SvSqlIp),
		m_Port(g_Config.m_SvSqlPort)
{
	m_pDriver = NULL;

	if(gs_SqlLock == 0)
		gs_SqlLock = lock_create();

	Init();
}

CRanking::~CRanking()
{
	lock_wait(gs_SqlLock);
	lock_release(gs_SqlLock);

	try
	{
		delete m_pStatement;
		delete m_pConnection;
		dbg_msg("SQL", "SQL connection disconnected");
	}
	catch (sql::SQLException &e)
	{
		dbg_msg("SQL", "ERROR: No SQL connection");
	}
}
void CRanking::Init()
{
	// create connection
	if(Connect())
	{
		try
		{
			// create tables
			char aBuf[1024];

			str_format(aBuf, sizeof(aBuf), "CREATE TABLE IF NOT EXISTS zcatch_ranks (Name VARCHAR(%d) BINARY NOT NULL, Wins INT DEFAULT 0, UNIQUE KEY Name (Name)) CHARACTER SET utf8 ;", MAX_NAME_LENGTH);
			m_pStatement->execute(aBuf);

			dbg_msg("SQL", "Ranking table was created successfully");
		}
		catch (sql::SQLException &e)
		{
			char aBuf[256];
			str_format(aBuf, sizeof(aBuf), "MySQL Error: %s", e.what());
			dbg_msg("SQL", aBuf);
			dbg_msg("SQL", "ERROR: Table was NOT created");
		}

		// disconnect from database
		Disconnect();
	}
}

bool CRanking::Connect()
{
	if (m_pDriver != NULL && m_pConnection != NULL)
	{
		try
		{
			// Connect to specific database
			m_pConnection->setSchema(m_pDatabase);
		}
		catch (sql::SQLException &e)
		{
			char aBuf[256];
			str_format(aBuf, sizeof(aBuf), "MySQL Error: %s", e.what());
			dbg_msg("SQL", aBuf);

			dbg_msg("SQL", "ERROR: SQL connection failed");
			return false;
		}
		return true;
	}

	try
	{
		char aBuf[256];

		sql::ConnectOptionsMap connection_properties;
		connection_properties["hostName"]      = sql::SQLString(m_pIp);
		connection_properties["port"]          = m_Port;
		connection_properties["userName"]      = sql::SQLString(m_pUser);
		connection_properties["password"]      = sql::SQLString(m_pPass);
		connection_properties["OPT_RECONNECT"] = true;

		// Create connection
		m_pDriver = get_driver_instance();
		m_pConnection = m_pDriver->connect(connection_properties);

		// Create Statement
		m_pStatement = m_pConnection->createStatement();

		// Create database if not exists
		str_format(aBuf, sizeof(aBuf), "CREATE DATABASE IF NOT EXISTS %s", m_pDatabase);
		m_pStatement->execute(aBuf);

		// Connect to specific database
		m_pConnection->setSchema(m_pDatabase);
		dbg_msg("SQL", "SQL connection established");
		return true;
	}
	catch (sql::SQLException &e)
	{
		char aBuf[256];
		str_format(aBuf, sizeof(aBuf), "MySQL Error: %s", e.what());
		dbg_msg("SQL", aBuf);

		dbg_msg("SQL", "ERROR: SQL connection failed");
		return false;
	}
	catch (const std::exception& ex)
	{
		// ...
		dbg_msg("SQL", "1 %s",ex.what());

	}
	catch (const std::string& ex)
	{
		// ...
		dbg_msg("SQL", "2 %s",ex.c_str());
	}
	catch( int )
	{
		dbg_msg("SQL", "3 %s");
	}
	catch( float )
	{
		dbg_msg("SQL", "4 %s");
	}

	catch( char[] )
	{
		dbg_msg("SQL", "5 %s");
	}

	catch( char )
	{
		dbg_msg("SQL", "6 %s");
	}
	catch (...)
	{
		dbg_msg("SQL", "Unknown Error cause by the MySQL/C++ Connector, my advice is to compile server_sql_debug and use it");

		dbg_msg("SQL", "ERROR: SQL connection failed");
		return false;
	}
	return false;
}
void CRanking::Disconnect()
{
}

void CRanking::SaveRanking(int ClientID){

	CSqlRankData *Tmp = new CSqlRankData();
	Tmp->m_ClientID = ClientID;
	str_copy(Tmp->m_aName, Server()->ClientName(ClientID), MAX_NAME_LENGTH);
	Tmp->m_pSqlData = this;

	void *SaveRanking = thread_create(SaveRankingThread, Tmp);
#if defined(CONF_FAMILY_UNIX)
	pthread_detach((pthread_t)SaveRanking);
#endif
}
void CRanking::SaveRankingThread(void *pUser){

	lock_wait(gs_SqlLock);
	CSqlRankData *pData = (CSqlRankData *)pUser;

	// Connect to database
	if(pData->m_pSqlData->Connect())
	{
		try
		{
			char aBuf[768];
			char aBuf2[768];
			// check strings
			pData->m_pSqlData->ClearString(pData->m_aName);

			str_format(aBuf, sizeof(aBuf), "SELECT * FROM zcatch_ranks WHERE Name='%s' ORDER BY Wins ASC LIMIT 1;", pData->m_aName);
			pData->m_pSqlData->m_pResults = pData->m_pSqlData->m_pStatement->executeQuery(aBuf);

			if(pData->m_pSqlData->m_pResults->next())
			{
				str_format(aBuf, sizeof(aBuf), "SELECT Wins FROM zcatch_ranks WHERE Name ='%s'",pData->m_aName);
				pData->m_pSqlData->m_pResults = pData->m_pSqlData->m_pStatement->executeQuery(aBuf);

				if(pData->m_pSqlData->m_pResults->rowsCount() == 1){
					pData->m_pSqlData->m_pResults->next();
					int wins = (int)pData->m_pSqlData->m_pResults->getInt("Wins");

					str_format(aBuf, sizeof(aBuf), "INSERT INTO zcatch_ranks(Name, Wins) VALUES ('%s', '%d') ON duplicate key UPDATE Name=VALUES(Name), Wins=Wins+1;", pData->m_aName, wins);
					pData->m_pSqlData->m_pStatement->execute(aBuf);

					if(wins == 1){
						str_format(aBuf2, sizeof(aBuf), "Woah! You won for the first time! Now you have 1 win.");
						pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf2);
					}else{
						str_format(aBuf2, sizeof(aBuf), "You're the winner! Now you have %d wins!", wins+1);
						pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf2);
					}
				}
			}

			delete pData->m_pSqlData->m_pResults;

			// if no entry found... create a new one
			str_format(aBuf, sizeof(aBuf), "INSERT IGNORE INTO zcatch_ranks(Name, Wins) VALUES ('%s', '1');",pData->m_aName);
			pData->m_pSqlData->m_pStatement->execute(aBuf);

			dbg_msg("SQL", "Updating rank done");
		}
		catch (sql::SQLException &e)
		{
			char aBuf[256];
			str_format(aBuf, sizeof(aBuf), "MySQL Error: %s", e.what());
			dbg_msg("SQL", aBuf);
			dbg_msg("SQL", "ERROR: Could not update rank");
		}
		pData->m_pSqlData->Disconnect();
	}

	delete pData;
	lock_release(gs_SqlLock);
}


void CRanking::ShowRanking(int ClientID, const char* pName){

	CSqlRankData *Tmp = new CSqlRankData();
	Tmp->m_ClientID = ClientID;
	str_copy(Tmp->m_aName, pName, MAX_NAME_LENGTH);
	str_format(Tmp->m_aRequestingPlayer, sizeof(Tmp->m_aRequestingPlayer), "%s", Server()->ClientName(ClientID));
	Tmp->m_pSqlData = this;

	void *ShowRanking = thread_create(ShowRankingThread, Tmp);
#if defined(CONF_FAMILY_UNIX)
	pthread_detach((pthread_t)ShowRanking);
#endif
}

void CRanking::ShowRankingThread(void *pUser){

	lock_wait(gs_SqlLock);
	CSqlRankData *pData = (CSqlRankData *)pUser;

	// Connect to database
	if(pData->m_pSqlData->Connect())
	{
		try
		{
			// check strings

			char originalName[MAX_NAME_LENGTH];
			strcpy(originalName,pData->m_aName);
			pData->m_pSqlData->ClearString(pData->m_aName);

			char aBuf[768];
			char aBuf2[768];
			pData->m_pSqlData->m_pStatement->execute("SET @pos := 0;");
			pData->m_pSqlData->m_pStatement->execute("SET @prev := NULL;");
			pData->m_pSqlData->m_pStatement->execute("SET @rank := 1;");

			str_format(aBuf, sizeof(aBuf), "SELECT Wins,Name, rank FROM (SELECT (@pos := @pos+1) pos, (@rank := IF(@prev = Wins,@rank, @pos)) rank, Name, (@prev := Wins) Wins FROM zcatch_ranks ORDER BY Wins DESC) as result WHERE Name='%s';", pData->m_aName);
			pData->m_pSqlData->m_pResults = pData->m_pSqlData->m_pStatement->executeQuery(aBuf);

			if(pData->m_pSqlData->m_pResults->next())
			{
				if(pData->m_pSqlData->m_pResults->rowsCount() == 1){
					int wins = (int)pData->m_pSqlData->m_pResults->getInt("Wins");
					int rank = (int)pData->m_pSqlData->m_pResults->getInt("rank");
					str_format(aBuf2, sizeof(aBuf2), "%d. %s has %d %s, requested by %s.", rank,originalName,wins, (wins != 1) ? "wins" : "win", pData->m_aRequestingPlayer);
					pData->m_pSqlData->GameServer()->SendChat(-1, CGameContext::CHAT_ALL, aBuf2);
				}
			}else{
				str_format(aBuf2, sizeof(aBuf2), "%s is not ranked.", originalName);
				pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf2);

			}

			// delete results and statement
			delete pData->m_pSqlData->m_pResults;
		}
		catch (sql::SQLException &e)
		{
			char aBuf[256];
			str_format(aBuf, sizeof(aBuf), "MySQL Error: %s", e.what());
			dbg_msg("SQL", aBuf);
			dbg_msg("SQL", "ERROR: Could not update rank");
		}
		pData->m_pSqlData->Disconnect();
	}

	delete pData;
	lock_release(gs_SqlLock);
}


void CRanking::ShowTop5(int ClientID, int Offset){

	CSqlTop5Data *Tmp = new CSqlTop5Data();
	Tmp->m_ClientID = ClientID;
	Tmp->m_Offset = Offset;
	str_format(Tmp->m_aRequestingPlayer, sizeof(Tmp->m_aRequestingPlayer), "%s", Server()->ClientName(ClientID));
	Tmp->m_pSqlData = this;

	void *ShowTop5 = thread_create(ShowTop5Thread, Tmp);
#if defined(CONF_FAMILY_UNIX)
	pthread_detach((pthread_t)ShowTop5);
#endif
}

void CRanking::ShowTop5Thread(void *pUser){

	lock_wait(gs_SqlLock);
	CSqlTop5Data *pData = (CSqlTop5Data *)pUser;

	// Connect to database
	if(pData->m_pSqlData->Connect())
	{
		try
		{

			char aBuf[768];
			char aBuf2[768];

			str_format(aBuf, sizeof(aBuf2), "SELECT Name FROM zcatch_ranks;");
			pData->m_pSqlData->m_pResults = pData->m_pSqlData->m_pStatement->executeQuery(aBuf);
			str_format(aBuf2, sizeof(aBuf2), "------=====] Top5 (%d records)", (int)pData->m_pSqlData->m_pResults->rowsCount());

			pData->m_pSqlData->m_pStatement->execute("SET @pos := 0;");
			pData->m_pSqlData->m_pStatement->execute("SET @prev := NULL;");
			pData->m_pSqlData->m_pStatement->execute("SET @rank := 1;");

			str_format(aBuf, sizeof(aBuf), "SELECT Wins,Name, rank FROM (SELECT (@pos := @pos+1) pos, (@rank := IF(@prev = Wins,@rank, @pos)) rank, Name, (@prev := Wins) Wins FROM zcatch_ranks ORDER BY Wins DESC) as result ORDER BY rank Limit %d,5;", pData->m_Offset);
			pData->m_pSqlData->m_pResults = pData->m_pSqlData->m_pStatement->executeQuery(aBuf);


			pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf2);
			while(pData->m_pSqlData->m_pResults->next()){
					int wins = (int)pData->m_pSqlData->m_pResults->getInt("Wins");
					int rank = (int)pData->m_pSqlData->m_pResults->getInt("rank");
					str_format(aBuf2, sizeof(aBuf2), "%d. %s has %d %s", rank, pData->m_pSqlData->m_pResults->getString("Name").c_str(),wins, (wins != 1) ? "wins" : "win");
					pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf2);
			}
			str_format(aBuf2, sizeof(aBuf2), "------=====]");
			pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf2);


			// delete results and statement
			delete pData->m_pSqlData->m_pResults;
		}
		catch (sql::SQLException &e)
		{
			char aBuf[256];
			str_format(aBuf, sizeof(aBuf), "MySQL Error: %s", e.what());
			dbg_msg("SQL", aBuf);
			dbg_msg("SQL", "ERROR: Could not update rank");
		}
		pData->m_pSqlData->Disconnect();
	}

	delete pData;
	lock_release(gs_SqlLock);
}

// anti SQL injection
void CRanking::ClearString(char *pString, int size)
{
	char newString[size*2-1];
	int pos = 0;

	for(int i=0;i<size;i++)
	{
		if(pString[i] == '\\')
		{
			newString[pos++] = '\\';
			newString[pos++] = '\\';
		}
		else if(pString[i] == '\'')
		{
			newString[pos++] = '\\';
			newString[pos++] = '\'';
		}
		else if(pString[i] == '"')
		{
			newString[pos++] = '\\';
			newString[pos++] = '"';
		}
		else
		{
			newString[pos++] = pString[i];
		}
	}

	newString[pos] = '\0';

	strcpy(pString,newString);
}
#endif