-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf2_rank.sp
629 lines (483 loc) · 15.3 KB
/
tf2_rank.sp
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
#pragma semicolon 1
#pragma tabsize 0
#include <sourcemod>
#include <scp>
#include <clientprefs>
#include <adminmenu>
#include <tf2>
#include <tf2_stocks>
#include <morecolors>
#include <advanced_motd>
new bool:_IsClientInGame[MAXPLAYERS+1];
new Handle:db = INVALID_HANDLE;
char steamid[MAXPLAYERS+1][128];
enum struct Player {
int Kills;
int Deaths;
int Assists;
int Exp;
int FakeDeaths;
int HeadShot;
int BackStab;
int Deflect;
bool Adverts;
bool StalyGracz;
int Ranga;
}
int RanksPoints[][] = {0, 100, 200, 500, 1000};
char Ranks[][] = {"Gość", "Grajek", "Odwiedzający", "Przyjaciel Serwera", "Stały Gracz"};
char RanksColor[][] = {"fed766", "Grajek", "Odwiedzający", "Przyjaciel Serwera", "63ace5"};
bool:Spectate[MAXPLAYERS+1];
Player g_Player[MAXPLAYERS + 1];
#define SQL_CREATETABLE "CREATE TABLE IF NOT EXISTS `players` (steamid VARCHAR(20) NOT NULL PRIMARY KEY, nickname TEXT, kills INT, deaths INT, assists INT, exp INT, fakedeaths INT, headshot INT, backstab INT, deflect INT);"
char UpdateUser[] = "UPDATE players SET kills='%i',deaths='%i',assists='%i',exp='%i',fakedeaths='%i',headshot='%i',backstab='%i',deflect='%i' WHERE steamid='%s';";
public Plugin:myinfo =
{
name = "Rank",
author = "Rolowy",
description = "#",
version = "1.2",
url = "#"
};
public OnClientPutInServer(client)
{
_IsClientInGame[client] = true;
Spectate[client] = true;
g_Player[client].Adverts = true;
char buffer[5000];
GetClientAuthId(client, AuthId_SteamID64, steamid[client], sizeof(steamid));
if(!StrEqual(steamid[client], "STEAM_ID_STOP_IGNORING_RETVALS"))
{
PrintToServer("[Rank] %N: Rozpoczęto wyszukiwanie zmiennych", client);
Format(buffer, sizeof(buffer), "SELECT kills, deaths, assists, exp, fakedeaths, headshot, backstab, deflect FROM players WHERE steamid='%s'", steamid[client]);
PrintToServer(buffer);
int userid = GetClientUserId(client);
SQL_TQuery(db, SQLQuery_Exit, buffer, userid);
}
else
{
PrintToServer("[Rank] %N zalogował się jako STEAM_ID_STOP_IGNORING_RETVALS", client);
}
}
public OnClientDisconnect(client)
{
SavePlayersClient(client);
_IsClientInGame[client] = false;
}
ConnectDB()
{
PrintToServer("[Rank] Loading..");
PrintToServer("[Rank] Checking databases.cfg..");
decl String:error[256];
error[0] = '\0';
if(SQL_CheckConfig("ranking"))
{
db = SQL_Connect("ranking", true, error, sizeof(error));
}
PrintToServer("[Ranks] Conecting to database..");
if(db==INVALID_HANDLE)
{
LogError("[Ranks] Could not connect to default database: %s", error);
return;
}
PrintToServer("[Ranks] Connection successful.");
PrintToServer("[Rank] Creating tables (if not exists)..");
SQL_TQuery(db, SQLErrorCallback, SQL_CREATETABLE);
syncDatabase_exec();
PrintToServer("[Rank] Loaded.");
}
public SQLErrorCallback(Handle:owner, Handle:hndl, const String:error[], any:data) {
if(!StrEqual("", error))
{
LogError("Query failed: %s", error);
}
return false;
}
public syncDatabase_exec()
{
for(new i=1; i<= MaxClients; i++)
{
if(_IsClientInGame[i])
{
OnClientPutInServer(i);
}
}
}
public SQLQuery_Exit(Handle:owner, Handle:hndl, const String:error[], any:data)
{
new client;
if((client = GetClientOfUserId(data))==0)
{
return;
}
PrintToServer("%N", client);
if(hndl==INVALID_HANDLE)
{
LogError("[Rank] Query failed: %s", error);
}
else
{
if (!SQL_GetRowCount(hndl))
{
char buffer[5000];
Format(buffer, sizeof(buffer), "INSERT INTO players (steamid, nickname, kills, deaths, assists, exp, fakedeaths, headshot, backstab, deflect) VALUES (\"%s\", \"%N\", 0, 0, 0, 0, 0, 0, 0, 0)", steamid[client], client);
SQL_TQuery(db, SQLErrorCallback, buffer);
PrintToServer("[Rank] Dodano nowego gracza o nazwie %N [%s]", client, steamid[client]);
}
else
{
SQL_FetchRow(hndl);
g_Player[client].Kills = SQL_FetchInt(hndl, 0);
g_Player[client].Deaths = SQL_FetchInt(hndl, 1);
g_Player[client].Assists = SQL_FetchInt(hndl, 2);
g_Player[client].Exp = SQL_FetchInt(hndl, 3);
g_Player[client].FakeDeaths = SQL_FetchInt(hndl, 4);
g_Player[client].HeadShot = SQL_FetchInt(hndl, 5);
g_Player[client].BackStab = SQL_FetchInt(hndl, 6);
g_Player[client].Deflect = SQL_FetchInt(hndl, 7);
PrintToServer("%N: %d %d %d %d %d %d %d %d", client, g_Player[client].Kills,g_Player[client].Deaths,g_Player[client].Assists,g_Player[client].Exp,g_Player[client].FakeDeaths,g_Player[client].HeadShot,g_Player[client].BackStab,g_Player[client].Deflect);
PrintToServer("[Rank] Załadowano dane z bazy danych na gracza %N", client);
}
}
return;
}
public OnPluginStart() {
UpdatePlayers();
ConnectDB();
HookEvent("player_death", Event_Player_Death, EventHookMode_Pre);
RegConsoleCmd("sm_rank", Rank);
RegConsoleCmd("sm_webrank", WebRank);
//RegConsoleCmd("sm_testrank", TestRank);
HookEvent("player_team", Event_playerTeam, EventHookMode_Post);
CreateTimer(0.1, MyTimer, _, TIMER_REPEAT);
}
public Action Event_playerTeam(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
int team = GetClientTeam(client);
if (team == 1)
Spectate[client] = true;
else
Spectate[client] = false;
return Plugin_Continue;
}
public Action MyTimer(Handle timer)
{
DrawList();
}
public UpdatePlayers()
{
for(new i=1; i<= MaxClients; i++)
{
if(IsValidClient(i))
{
Spectate[i] = false;
_IsClientInGame[i] = true;
}
}
}
public Action TestRank(int client, int args)
{
char buffer[5000];
GetClientAuthId(client, AuthId_SteamID64, steamid[client], sizeof(steamid));
Format(buffer, sizeof(buffer), "SELECT kills, deaths, assists, exp, fakedeaths, headshot, backstab, deflect FROM players WHERE steamid='%s'", steamid[client]);
PrintToServer(buffer);
SQL_TQuery(db, SQLQuery_Exit, buffer, client);
}
public Action WebRank(int client, int args)
{
char URL[512];
Format(URL, sizeof(URL), "http://167.86.127.8:4200/rank");
AdvMOTD_ShowMOTDPanel(client, "Web Rank", URL, MOTDPANEL_TYPE_URL, true, true, true, OnMOTDFailure);
}
public Action Rank(int client, int args)
{
char buffer[5000];
Format(buffer, sizeof(buffer), "[Main Ranks] \n \nPoints: %d\n ", g_Player[client].Exp);
Menu menu = new Menu(Rank_Exit);
menu.SetTitle(buffer);
menu.AddItem("1", "My Statistics");
menu.AddItem("1", "Badges", ITEMDRAW_DISABLED);
menu.AddItem("1", "Quests\n ", ITEMDRAW_DISABLED);
menu.AddItem("rank", "Rank List\n ");
if(g_Player[client].Adverts == true)
menu.AddItem("ad", "Adverts - [√]");
else
menu.AddItem("ad", "Adverts - [ ]");
//menu.AddItem("x", "[Gracz] - [♜]", ITEMDRAW_DISABLED);
//menu.AddItem("x", "[Stały Gracz] - [ ] (1000 points)", ITEMDRAW_DISABLED);
menu.ExitButton = true;
menu.ExitBackButton = true;
menu.Display(client, MENU_TIME_FOREVER);
}
public int Rank_Exit(Menu menu, MenuAction action, int client, int item)
{
switch(action)
{
case MenuAction_Select:
{
char info[32];
menu.GetItem(item, info, sizeof(info));
if(StrEqual(info, "1"))
{
OpenStatistic(client, 0);
}
else if(StrEqual(info, "ad"))
{
if(g_Player[client].Adverts == true)
g_Player[client].Adverts = false;
else
g_Player[client].Adverts = true;
Rank(client, 0);
}
else if(StrEqual(info, "rank"))
{
RankList(client, 0);
}
else
{
CloseHandle(menu);
}
}
case MenuAction_Cancel:
{
ClientCommand(client, "sm_menu");
}
case MenuAction_End:
{
CloseHandle(menu);
}
}
}
public Action RankList(int client, int args)
{
char buffer[100];
Format(buffer, sizeof(buffer), "[Rank List]\n ");
Menu menu = new Menu(RankList_Exit);
menu.SetTitle(buffer);
for(new x = 0; x<sizeof(Ranks); x++)
{
if(x == g_Player[client].Ranga)
{
Format(buffer, sizeof(buffer), "%s - [√]", Ranks[x]);
menu.AddItem("x", buffer);
}
else
{
char value[32];
IntToString(x, value, sizeof(value));
//if(g_Player[client].Exp < RanksPoints[x])
//{
// Format(buffer, sizeof(buffer), "%s - [ ]", Ranks[x]);
// menu.AddItem(value, buffer);
//}
//else
//{
Format(buffer, sizeof(buffer), "%s - [ ]", Ranks[x]);
menu.AddItem(value, buffer, ITEMDRAW_DISABLED);
//}
}
}
menu.ExitButton = true;
menu.ExitBackButton = true;
menu.Display(client, MENU_TIME_FOREVER);
}
public int RankList_Exit(Menu menu, MenuAction action, int client, int item)
{
switch(action)
{
case MenuAction_Select:
{
}
case MenuAction_Cancel:
{
Rank(client, 0);
}
case MenuAction_End:
{
CloseHandle(menu);
}
}
}
public SavePlayersClient(int client)
{
char buffer[5000];
Format(buffer, sizeof(buffer), UpdateUser, g_Player[client].Kills, g_Player[client].Deaths, g_Player[client].Assists, g_Player[client].Exp, g_Player[client].FakeDeaths, g_Player[client].HeadShot, g_Player[client].BackStab, g_Player[client].Deflect, steamid[client]);
//PrintToConsole(client, buffer);
if(SQL_FastQuery(db, buffer))
{
PrintToServer("[Rank- Save] - %N - [%s] Zapisano dane gracza poprawnie", client, steamid[client]);
}
else
{
LogError("[Rank - Save] Error: %s", buffer);
}
}
public SavePlayers()
{
for(new i = 1; i <= MaxClients; i++)
{
if(_IsClientInGame[i] == true)
{
SavePlayersClient(i);
}
}
}
public Action OpenStatistic(int client, int args)
{
char buffer[64];
Format(buffer, sizeof(buffer), "[Statistics]\n ");
Menu menu = new Menu(BackToRank);
menu.SetTitle(buffer);
Format(buffer, sizeof(buffer), "[Points]: %d\n ", g_Player[client].Exp);
menu.AddItem("0", buffer, ITEMDRAW_DISABLED);
Format(buffer, sizeof(buffer), "[Kills]: %d", g_Player[client].Kills);
menu.AddItem("0", buffer, ITEMDRAW_DISABLED);
Format(buffer, sizeof(buffer), "[Deaths]: %d", g_Player[client].Deaths);
menu.AddItem("0", buffer, ITEMDRAW_DISABLED);
Format(buffer, sizeof(buffer), "[Assists]: %d", g_Player[client].Assists);
menu.AddItem("0", buffer, ITEMDRAW_DISABLED);
Format(buffer, sizeof(buffer), "[Fake Deaths]: %d", g_Player[client].FakeDeaths);
menu.AddItem("0", buffer, ITEMDRAW_DISABLED);
Format(buffer, sizeof(buffer), "[HeadShot]: %d", g_Player[client].HeadShot);
menu.AddItem("0", buffer, ITEMDRAW_DISABLED);
Format(buffer, sizeof(buffer), "[Deflect]: %d", g_Player[client].Deflect);
menu.AddItem("0", buffer, ITEMDRAW_DISABLED);
Format(buffer, sizeof(buffer), "[Backstab]: %d", g_Player[client].BackStab);
menu.AddItem("0", buffer, ITEMDRAW_DISABLED);
menu.ExitButton = true;
menu.ExitBackButton = true;
menu.Display(client, MENU_TIME_FOREVER);
}
public int BackToRank(Menu menu, MenuAction action, int client, int item)
{
switch(action)
{
case MenuAction_Select:
{
Rank(client, 0);
}
case MenuAction_Cancel:
{
Rank(client, 0);
}
case MenuAction_End:
{
CloseHandle(menu);
}
}
}
public OnPluginEnd()
{
SavePlayers();
}
public CheckRanks(int client) {
if(g_Player[client].Exp == 100)
{
CPrintToChat(client, "\x07fe8a71[Rank]\x07fed766 Gratulację, udało Ci się wbić nową rangę ! :)");
}
}
public Event_Player_Death(Handle:event, const String:name[], bool:dontBroadcast)
{
new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
new assister = GetClientOfUserId(GetEventInt(event, "assister"));
new victim = GetClientOfUserId(GetEventInt(event, "userid"));
if(attacker != victim && attacker != 0 && victim != 0)
{
new bool:victimFeigned = bool:(GetEventInt(event, "death_flags") & TF_DEATHFLAG_DEADRINGER);
if (victimFeigned == false) {
g_Player[attacker].Kills++;
CheckRanks(attacker);
if(assister != 0)
{
g_Player[assister].Assists++;
}
g_Player[victim].Deaths++;
new customKill = GetEventInt(event, "customkill");
if (customKill == TF_CUSTOM_HEADSHOT || customKill == TF_CUSTOM_HEADSHOT_DECAPITATION)
{
g_Player[attacker].HeadShot++;
}
else if (customKill == TF_CUSTOM_BACKSTAB)
{
g_Player[attacker].BackStab++;
}
else {
new String:weapon[9];
GetEventString(event, "weapon", weapon, 9);
bool deflection = StrEqual(weapon, "deflect_");
if (deflection == true) {
g_Player[attacker].Deflect++;
}
}
g_Player[attacker].Exp++;
}
else
{
g_Player[attacker].FakeDeaths++;
}
}
}
bool IsValidClient(int client, bool allowBots=true) {
return ( 1<=client<=MaxClients && IsClientInGame(client) ) && ( allowBots || !IsFakeClient(client) );
}
public Action:OnChatMessage(&author, Handle:recipients, String:name[], String:message[]) {
if(IsValidClient(author))
{
char sTime[32];
FormatTime(sTime, sizeof(sTime), "%H:%M", GetTime());
//if(CheckCommandAccess(author, "", ADMFLAG_GENERIC, false) == false)
Format(name, MAX_NAME_LENGTH, "%s \x07%s[%s] \x03%s", sTime, RanksColor[g_Player[author].Ranga], Ranks[g_Player[author].Ranga], name);
return Plugin_Changed;
}
return Plugin_Continue;
}
public DrawList()
{
new String:player_list[2048];
for(new client=1; client<= MaxClients; client++)
{
//if(Spectate[client])
if(IsValidClient(client))
{
int frags = GetClientFrags(client);
int deaths = GetClientDeaths(client);
if (frags > 0 && deaths > 0)
{
new Float:KDRate = float(g_Player[client].Kills/g_Player[client].Deaths);
Format(player_list, 512, "♚ [Rank Panel] ♚\n \nRank: [%s]\nExp: [%d]\n \nFrags/Deaths: [%i/%i]\nRatio: %2.f\n \nQuest: Not selected", Ranks[g_Player[client].Ranga], g_Player[client].Exp, frags, deaths, KDRate);
Client_PrintKeyHintText(client, "%s", player_list);
}
else
{
Format(player_list, 512, "♚ [Rank Panel] ♚\n \nRank: [%s]\nExp: [%d]\n \nFrags/Deaths: [0/0]\nRatio: 0.0\n \nQuest: Not selected", Ranks[g_Player[client].Ranga], g_Player[client].Exp);
Client_PrintKeyHintText(client, "%s", player_list);
}
}
}
}
stock bool:Client_PrintKeyHintText(client, const String:format[], any:...)
{
new Handle:userMessage = StartMessageOne("KeyHintText", client);
if (userMessage == INVALID_HANDLE) {
return false;
}
decl String:buffer[1000];
SetGlobalTransTarget(client);
VFormat(buffer, sizeof(buffer), format, 3);
BfWriteByte(userMessage, 1);
BfWriteString(userMessage, buffer);
EndMessage();
return true;
}
public void OnMOTDFailure(int client, MOTDFailureReason reason) {
if(reason == MOTDFailure_Disabled) {
PrintToChat(client, "[SM] You have HTML MOTDs disabled.");
} else if(reason == MOTDFailure_Matchmaking) {
PrintToChat(client, "[SM] You cannot view HTML MOTDs because you joined via Quickplay.");
} else if(reason == MOTDFailure_QueryFailed) {
PrintToChat(client, "[SM] Unable to verify that you can view HTML MOTDs.");
} else {
PrintToChat(client, "[SM] Unable to verify that you can view HTML MOTDs for an unknown reason.");
}
}