· 9 years ago · Jan 29, 2017, 03:06 PM
1/* *********************************************************************
2
3Thanks for people that help me:
4- dalto (http://forums.alliedmods.net/member.php?u=29575)
5
6
7Updates:
8 * Display FIX
9 * Query FIXES
10 * New: OLD STATS deleting
11 * Update TABLE on UTF8 and Setting NAMES to UTF8 on DatabaseConnect
12 * Changes in Database Connect
13 * New: Added "/headhunters"
14 * New: played_time, we will creat most active players TOP
15 * New: last_active ON this we will base is this player stats not to old.. (we dont want have BIG MySQL DataBase)
16 in future, maybe i will creat archive table, that there we will be have those player's, and they will be come back from archive when they will connect.
17 * Anti Flood Protection (new userFlood[64];)
18 * New announce message on: onClientPutInServer(client)
19 * WWW sie Cvar ("sm_lrcss_www",)
20 * PHP site nick protection:
21 [ ReplaceString(name, sizeof(name), "'", "");
22 ReplaceString(name, sizeof(name), "<", "");
23 ReplaceString(name, sizeof(name), "\"", "");
24]
25* Changed SQL_Query on SQL_TQuery Functions, Helper: - dalto
26* Added Debug Mode for MySQL Queries: new DEBUG = 0;
27* Added: char steamIdSave[64][255]; for Player Disconnect useage in: SQL_TQuery
28* Added: if(hndl == INVALID_HANDLE) function check in all: SQL_TQuery
29
30MySQL Query:
31
32CREATE TABLE `tf2warerank`(
33`rank_id` int(64) NOT NULL auto_increment,
34`steamId` varchar(32) NOT NULL default '',
35`nick` varchar(128) NOT NULL default '',
36`minigamewon` int(12) NOT NULL default '0',
37`minigameslose` int(12) NOT NULL default '0',
38`bossstagewon` int(12) NOT NULL default '0',
39`totalwins` int(12) NOT NULL default '0',
40`last_active` int(12) NOT NULL default '0',
41`played_time` int(12) NOT NULL default '0',
42PRIMARY KEY (`rank_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;
43
44database.cfg
45
46 "tf2warerank"
47 {
48 "driver" "default"
49 "host" "127.0.0.1"
50 "database" "database_name"
51 "user" "database_user"
52 "pass" "PASSWORD"
53 //"timeout" "0"
54 "port" "3306"
55 }
56
57*************************************************************************** */
58
59// KOLOROWE KREDKI
60#define YELLOW 0x01
61#define GREEN 0x04
62
63// DEBUG MODE (1 = ON, 0 = OFF)
64bool DEBUG;
65
66// SOME DEFINES
67#define MAX_LINE_WIDTH 60
68
69// STATS TIME (SET DAYS AFTER STATS ARE DELETE OF NONACTIVE PLAYERS)
70#define PLAYER_STATSOLD 30
71
72// STATS DEFINATION FOR PLAYERS
73int MinigameWon[MAXPLAYERS+1];
74int MinigameLose[MAXPLAYERS+1];
75int BossStageWon[MAXPLAYERS+1];
76int TotalRoundWon[MAXPLAYERS+1];
77int userInit[MAXPLAYERS+1];
78int userFlood[MAXPLAYERS+1];
79int userPtime[MAXPLAYERS+1];
80char steamIdSave[MAXPLAYERS+1][255];
81
82// HANDLE OF DATABASE
83Handle db;
84
85void ConnectSQL()
86{
87 if (db != INVALID_HANDLE)
88 CloseHandle(db);
89
90 db = INVALID_HANDLE;
91
92 if (SQL_CheckConfig("tf2warerank"))
93 {
94 SQL_TConnect(LoadMySQLBase, "tf2warerank");
95 }
96 else
97 {
98 SetFailState("PLUGIN STOPPED - Reason: no config entry found for 'traderooms' in databases.cfg - PLUGIN STOPPED");
99 }
100}
101public void LoadMySQLBase(Handle owner, Handle hndl, const char[] error, any data)
102{
103 if (hndl == INVALID_HANDLE)
104 {
105 PrintToChatAll("Failed to connect: %s", error);
106 db = INVALID_HANDLE;
107 return;
108 } else {
109 PrintToChatAll("DEBUG: DatabaseInit (CONNECTED)");
110 }
111
112 db = hndl;
113 char query[1024];
114 char query2[1024];
115 char driver[16];
116 SQL_GetDriverIdent(owner, driver, sizeof(driver));
117 FormatEx(query, sizeof(query), "SET NAMES \"UTF8\"");
118 if (StrEqual(driver, "mysql", false))
119 {
120 SQL_TQuery(db, SQLErrorCheckCallback, "CREATE TABLE IF NOT EXISTS `tf2warerank` (`rank_id` int(64) NOT NULL AUTO_INCREMENT, `steamid` varchar(32) NOT NULL, `nick` varchar(128) NOT NULL, `minigamewon` int(12) NOT NULL 0, `minigamelose` int(12) NOT NULL 0, `bossstagewon` int(12) NOT NULL 0, `totalwins` int(12) NOT NULL 0, `last_active` int(12) NOT NULL 0, `played_time` int(12) NOT NULL 0, PRIMARY KEY (`rank_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
121 }
122 SQL_TQuery(db, SQLErrorCheckCallback, query);
123 FormatEx(query2, sizeof(query2), "DELETE FROM tf2warerank WHERE last_active <= %i", GetTime() - PLAYER_STATSOLD * 12 * 60 * 60);
124 SQL_TQuery(db, SQLErrorCheckCallback, query2);
125}
126
127
128public void OnClientAuthorized(int client, const char[] auth)
129{
130 ConnectSQL();
131 InitializeClient(client);
132}
133
134
135public int InitializeClient( int client )
136{
137 if (!IsFakeClient(client))
138 {
139 MinigameWon[client]=0;
140 MinigameLose[client]=0;
141 BossStageWon[client]=0;
142 TotalRoundWon[client]=0;
143 userFlood[client]=0;
144 userPtime[client]=GetTime();
145 char steamId[64];
146 GetClientAuthId(client, AuthId_Steam2, steamId, sizeof(steamId));
147 steamIdSave[client] = steamId;
148 PrintToChatAll("client %s", steamId);
149 CreateTimer(1.0, initPlayerBase, client);
150 }
151}
152
153public Action initPlayerBase(Handle timer, any client){
154 if (db != INVALID_HANDLE)
155 {
156 char buffer[200];
157 Format(buffer, sizeof(buffer), "SELECT * FROM tf2warerank WHERE steamId = '%s'", steamIdSave[client]);
158 PrintToChatAll("DEBUG: Action:initPlayerBase (%s)", buffer);
159 SQL_TQuery(db, SQLUserLoad, buffer, client);
160 }
161}
162public int saveUser(int client){
163 if ( !IsFakeClient(client) && userInit[client] == 1)
164 {
165 if (db != INVALID_HANDLE)
166 {
167 char buffer[200];
168 Format(buffer, sizeof(buffer), "SELECT * FROM tf2warerank WHERE steamId = '%s'", steamIdSave[client]);
169 PrintToChatAll("DEBUG: saveUser (%s)", buffer);
170
171 SQL_TQuery(db, SQLUserSave, buffer, client);
172 }
173 }
174}
175
176public Action Command_Say(int client, int args){
177
178 char text[192]; char command[64];
179
180 int startidx = 0;
181
182 GetCmdArgString(text, sizeof(text));
183
184 if (text[strlen(text)-1] == '"')
185 {
186 text[strlen(text)-1] = '\0';
187 startidx = 1;
188 }
189 if (strcmp(command, "say2", false) == 0)
190
191 startidx += 4;
192
193 if (strcmp(text[startidx], "/rank", false) == 0) {
194 if(userFlood[client] != 1){
195 saveUser(client);
196 GetMyRank(client);
197 userFlood[client]=1;
198 CreateTimer(10.0, removeFlood, client);
199 } else {
200 PrintToChat(client,"%cDo not flood the server!", GREEN);
201 }
202 } else if (strcmp(text[startidx], "/top10", false) == 0)
203 {
204 if(userFlood[client] != 1){
205 saveUser(client);
206 showTOP(client);
207 userFlood[client]=1;
208 CreateTimer(10.0, removeFlood, client);
209 } else {
210 PrintToChat(client,"%cDo not flood the server!", GREEN);
211 }
212 } else if (strcmp(text[startidx], "/topboss", false) == 0)
213 {
214 if(userFlood[client] != 1){
215 saveUser(client);
216 showTOPHeadHunter(client);
217 userFlood[client]=1;
218 CreateTimer(10.0, removeFlood, client);
219 } else {
220 PrintToChat(client,"%cDo not flood the server!", GREEN);
221 }
222 }
223 return Plugin_Continue;
224}
225
226public Action removeFlood(Handle timer, any client){
227 userFlood[client]=0;
228}
229
230public int GetMyRank(int client){
231 if (db != INVALID_HANDLE)
232 {
233 if(userInit[client] == 1){
234
235 char buffer[200];
236 Format(buffer, sizeof(buffer), "SELECT `minigamewon`, `minigameslose`, `bossstagewon`, `totalwins` FROM `tf2warerank` WHERE `steamId` = '%s' LIMIT 1", steamIdSave[client]);
237 PrintToChatAll("DEBUG: GetMyRank (%s)", buffer);
238 SQL_TQuery(db, SQLGetMyRank, buffer, client);
239
240 } else {
241
242 PrintToChat(client,"%cWait for system load you from database", GREEN);
243
244 }
245 } else {
246 PrintToChat(client, "%cRank System is now not avilable", GREEN);
247 }
248}
249
250public int showTOP(int client){
251
252 if (db != INVALID_HANDLE)
253 {
254 char buffer[200];
255 Format(buffer, sizeof(buffer), "SELECT *, (`minigameslose`/`minigamewon`) / `played_time` AS rankn FROM `tf2warerank` WHERE `minigamewon` > 0 AND `minigameslose` > 0 ORDER BY rankn ASC LIMIT 10");
256 PrintToChatAll("DEBUG: showTOP (%s)", buffer);
257 SQL_TQuery(db, SQLTopShow, buffer, client);
258 } else {
259 PrintToChat(client, "%cRank System is now not avilable", GREEN);
260 }
261}
262
263public int showTOPHeadHunter(int client){
264
265 if (db != INVALID_HANDLE)
266 {
267 char buffer[200];
268 Format(buffer, sizeof(buffer), "SELECT * FROM tf2warerank ORDER BY headshots DESC LIMIT 10");
269 PrintToChatAll("DEBUG: showTOPHeadHunter (%s)", buffer);
270 SQL_TQuery(db, SQLTopShowHS, buffer, client);
271 } else {
272 PrintToChat(client, "%cRank System is now not avilable", GREEN);
273 }
274}
275
276public int TopMenu(Menu menu, MenuAction action, int param1, int param2)
277{
278}
279
280// ================================================================================
281
282public int SQLErrorCheckCallback(Handle owner, Handle hndl, const char[] error, any data)
283{
284 if(!StrEqual("", error))
285 {
286 PrintToChatAll("Last Connect SQL Error: %s", error);
287 }
288}
289
290
291public int SQLUserLoad(Handle owner, Handle hndl, const char[] error, any client){
292 if(SQL_FetchRow(hndl))
293 {
294 char name[MAX_LINE_WIDTH];
295 GetClientName( client, name, sizeof(name) );
296
297 ReplaceString(name, sizeof(name), "'", "");
298 ReplaceString(name, sizeof(name), "<", "");
299 ReplaceString(name, sizeof(name), "\"", "");
300
301 char buffer[512];
302 Format(buffer, sizeof(buffer), "UPDATE tf2warerank SET nick = '%s', last_active = '%i' WHERE steamId = '%s'", name, GetTime(), steamIdSave[client]);
303 PrintToChatAll("DEBUG: SQLUserLoad (%s)", buffer);
304 SQL_TQuery(db, SQLErrorCheckCallback, buffer);
305
306 userInit[client] = 1;
307 } else {
308
309 char name[MAX_LINE_WIDTH];
310 char buffer[200];
311
312 GetClientName( client, name, sizeof(name) );
313
314 ReplaceString(name, sizeof(name), "'", "");
315 ReplaceString(name, sizeof(name), "<", "");
316 ReplaceString(name, sizeof(name), "\"", "");
317
318 Format(buffer, sizeof(buffer), "INSERT INTO tf2warerank (steamId, nick, last_active) VALUES('%s','%s', '%i')", steamIdSave[client], name, GetTime());
319 PrintToChatAll("DEBUG: SQLUserLoad (%s)", buffer);
320 SQL_TQuery(db, SQLErrorCheckCallback, buffer);
321
322 userInit[client] = 1;
323 }
324}
325
326public int SQLUserSave(Handle owner, Handle hndl, const char[] error, any client){
327 if(hndl == INVALID_HANDLE)
328 {
329 LogError(error);
330 PrintToChatAll("Last Connect SQL Error: %s", error);
331 return;
332 }
333
334 int QueryReadRow_MGWON;
335 int QueryReadRow_MGLOSE;
336 int QueryReadRow_BOSSWON;
337 int QueryReadRow_TOTALROUNDS;
338 int QueryReadRow_PTIME;
339
340 if(SQL_FetchRow(hndl))
341 {
342 QueryReadRow_MGWON=SQL_FetchInt(hndl,3) + MinigameWon[client];
343 QueryReadRow_MGLOSE=SQL_FetchInt(hndl,4) + MinigameLose[client];
344 QueryReadRow_BOSSWON=SQL_FetchInt(hndl,5) + BossStageWon[client];
345 QueryReadRow_TOTALROUNDS=SQL_FetchInt(hndl,6) + TotalRoundWon[client];
346 QueryReadRow_PTIME=SQL_FetchInt(hndl,8) + GetTime() - userPtime[client];
347 MinigameWon[client] = 0;
348 MinigameLose[client] = 0;
349 BossStageWon[client] = 0;
350 TotalRoundWon[client] = 0;
351 userPtime[client] = GetTime();
352 char buffer[512];
353 Format(buffer, sizeof(buffer), "UPDATE tf2warerank SET minigamewon = '%i', minigameslose = '%i', bossstagewon = '%i', totalwins = '%i', played_time = '%i' WHERE steamId = '%s'", QueryReadRow_MGWON, QueryReadRow_MGLOSE, QueryReadRow_BOSSWON, QueryReadRow_TOTALROUNDS, QueryReadRow_PTIME, steamIdSave[client]);
354
355
356 PrintToChatAll("DEBUG: SQLUserSave (%s)", buffer);
357
358 SQL_TQuery(db, SQLErrorCheckCallback, buffer);
359 }
360
361}
362
363public int SQLGetMyRank(Handle owner, Handle hndl, const char[] error, any client){
364 if(hndl == INVALID_HANDLE)
365 {
366 LogError(error);
367 PrintToChatAll("Last Connect SQL Error: %s", error);
368 return;
369 }
370
371 int RAmgwon;
372 int RAmglose;
373 int RABossWon;
374 int RATotalwins;
375
376 if(SQL_FetchRow(hndl))
377 {
378 RAmgwon=SQL_FetchInt(hndl, 0);
379 RAmglose=SQL_FetchInt(hndl, 1);
380 RABossWon=SQL_FetchInt(hndl, 2);
381 RATotalwins=SQL_FetchInt(hndl, 3);
382 char buffer[512];
383 //test
384 // 0.00027144
385 //STEAM_0:1:13462423
386 Format(buffer, sizeof(buffer), "SELECT ((`minigameslose`/`minigamewon`)/`played_time`) AS rankn FROM `tf2warerank` WHERE (`minigamewon` > 0 AND `minigameslose` > 0) AND ((`minigameslose`/`minigamewon`)/`played_time`) < (SELECT ((`minigameslose`/`minigamewon`)/`played_time`) FROM `tf2warerank` WHERE steamId = '%s' LIMIT 1) AND `steamId` != '%s' ORDER BY rankn ASC", steamIdSave[client], steamIdSave[client]);
387
388 PrintToChatAll("DEBUG: SQLGetMyRank (%s)", buffer);
389 SQL_TQuery(db, SQLShowRank, buffer, client);
390 PrintToChat(client,"%cMinigame Won: %i | Minigame Failed: %i | Boss Stage Won: %i | Total TF2Ware Rounds Wins: %i", GREEN, RAmgwon, RAmglose, RABossWon, RATotalwins);
391 } else {
392 PrintToChat(client, "%cYour rank is not avlilable!", GREEN);
393 }
394}
395
396public int SQLShowRank(Handle owner, Handle hndl, const char[] error, any client){
397 if (SQL_HasResultSet(hndl))
398 {
399 int rows = SQL_GetRowCount(hndl);
400 PrintToChat(client,"%cYour rank is: %i.", GREEN, rows);
401 }
402}
403
404
405public int SQLTopShow(Handle owner, Handle hndl, const char[] error, any client){
406
407 if(hndl == INVALID_HANDLE)
408 {
409 LogError(error);
410 PrintToChatAll("Last Connect SQL Error: %s", error);
411 return;
412 }
413
414 //Handle Panel = CreatePanel(GetMenuStyleHandle(MenuStyle_Valve));
415 Panel panel = CreatePanel(GetMenuStyleHandle(MenuStyle_Valve));
416 char text[128];
417 Format(text,127,"Top 10 Players");
418 SetPanelTitle(panel,text);
419
420 int row;
421 char name[64];
422 int mgwin;
423 int mglose;
424
425 if (SQL_HasResultSet(hndl))
426 {
427 while (SQL_FetchRow(hndl))
428 {
429 row++;
430 SQL_FetchString(hndl, 2, name, sizeof(name));
431 mgwin=SQL_FetchInt(hndl,3);
432 mglose=SQL_FetchInt(hndl,4);
433 Format(text,127,"%d) %s", row, name);
434 DrawPanelText(panel, text);
435 Format(text,127,"Minigame Won: %i - Minigame Failed: %i", mgwin, mglose);
436 DrawPanelText(panel, text);
437
438 }
439 } else {
440 Format(text,127,"TOP 10 is empty!");
441 DrawPanelText(panel, text);
442 }
443
444 DrawPanelItem(panel, " ", ITEMDRAW_SPACER|ITEMDRAW_RAWLINE);
445
446 Format(text,59,"Exit");
447 DrawPanelItem(panel, text);
448
449 SendPanelToClient(panel, client, TopMenu, 20);
450 //panel.Send(client, TopMenu, 20);
451
452 //delete panel
453
454 CloseHandle(panel);
455
456}
457
458public int SQLTopShowHS(Handle owner, Handle hndl, const char[] error, any client){
459
460 if(hndl == INVALID_HANDLE)
461 {
462 LogError(error);
463 PrintToChatAll("Last Connect SQL Error: %s", error);
464 return;
465 }
466
467 Panel panel = CreatePanel(GetMenuStyleHandle(MenuStyle_Valve));
468 char text[128];
469 Format(text,127,"Top 10 BossStage");
470 SetPanelTitle(panel,text);
471
472 int row;
473 char name[64];
474 int shoths;
475 int ptimed;
476 char textime[64];
477
478 if (SQL_HasResultSet(hndl))
479 {
480 while (SQL_FetchRow(hndl))
481 {
482 row++;
483 SQL_FetchString(hndl, 2, name, sizeof(name));
484 shoths=SQL_FetchInt(hndl,5);
485 ptimed=SQL_FetchInt(hndl,8);
486
487 if(ptimed <= 3600){
488 Format(textime,63,"%i m.", ptimed / 60);
489 } else if(ptimed <= 43200){
490 Format(textime,63,"%i h.", ptimed / 60 / 60);
491 } else if(ptimed <= 1339200){
492 Format(textime,63,"%i d.", ptimed / 60 / 60 / 12);
493 } else {
494 Format(textime,63,"%i mo.", ptimed / 60 / 60 / 12 / 31);
495 }
496
497 Format(text,127,"%d: %s", row, name);
498 DrawPanelText(panel, text);
499 Format(text,127,"HS: %i - In Time: %s", shoths, textime);
500 DrawPanelText(panel, text);
501
502 }
503 } else {
504 Format(text,127,"TOP BossStageWon is empty!");
505 DrawPanelText(panel, text);
506 }
507
508 DrawPanelItem(panel, " ", ITEMDRAW_SPACER|ITEMDRAW_RAWLINE);
509
510 Format(text,59,"Exit");
511 DrawPanelItem(panel, text);
512
513 SendPanelToClient(panel, client, TopMenu, 20);
514
515 CloseHandle(panel);
516
517}
518
519/* int PrintQueryData(Handle query)
520{
521 if (!SQL_HasResultSet(query))
522 {
523 PrintToChatAll("Query Handle %x has no results", query)
524 return
525 }
526
527 int rows = SQL_GetRowCount(query)
528 int fields = SQL_GetFieldCount(query)
529
530 char fieldNames[fields][32]
531 PrintToChatAll("Fields: %d", fields)
532 for (int i=0; i<fields; i++)
533 {
534 SQL_FieldNumToName(query, i, fieldNames[i], 32)
535 PrintToChatAll("-> Field %d: \"%s\"", i, fieldNames[i])
536 }
537
538 PrintToChatAll("Rows: %d", rows)
539 char result[255]
540 int row
541 while (SQL_FetchRow(query))
542 {
543 row++
544 PrintToChatAll("Row %d:", row)
545 for (int i=0; i<fields; i++)
546 {
547 SQL_FetchString(query, i, result, sizeof(result))
548 PrintToChatAll(" [%s] %s", fieldNames[i], result)
549 }
550 }
551} */