· 6 years ago · Nov 05, 2019, 06:30 PM
1
2#define PLUGIN_AUTHOR "PyNiO ™"
3#define PLUGIN_VERSION "1.00"
4
5#include <sourcemod>
6#include <cstrike>
7#include <sdkhooks>
8#include <sdktools>
9#include <clientprefs>
10#include <scp>
11
12ConVar g_iPlayerOn;
13ConVar g_iPlayerKills;
14ConVar g_iPlayerAssists;
15ConVar g_iPlayerHeadshots;
16ConVar g_iPlayerWin;
17ConVar g_iPlayerLose;
18ConVar g_iPlayerPlant;
19ConVar g_iPlayerDefuse;
20
21int g_iKills[MAXPLAYERS];
22int g_iAssists[MAXPLAYERS];
23int g_iHeadshots[MAXPLAYERS];
24int g_iWin[MAXPLAYERS];
25int g_iLose[MAXPLAYERS];
26int g_iPlant[MAXPLAYERS];
27int g_iDefuse[MAXPLAYERS];
28int g_iPlayerTag[MAXPLAYERS];
29
30Handle g_hSQL = INVALID_HANDLE;
31bool g_bPlayerWczytane [MAXPLAYERS+1];
32int g_iPolaczenia;
33
34new String:Error[100];
35
36char g_sNames[][128] =
37{
38 "",
39 "Killer",
40 "Helpful guy",
41 "HeadHunter",
42 "Winner",
43 "Loser",
44 "Planter",
45 "Sapper",
46};
47
48public Plugin myinfo =
49{
50 name = "Achievements",
51 author = PLUGIN_AUTHOR,
52 description = "Custom achievements for cs:go server",
53 version = PLUGIN_VERSION,
54 url = "asd"
55};
56public void OnMapStart()
57{
58 g_iPolaczenia=0;
59 ConnectSQL();
60
61 AddFolderToDownloadsTable("materials/overlays/achievements");
62 AddFileToDownloadsTable("sound/achievements/achievement.mp3");
63 PrecacheSound("achievements/achievement.mp3", true);
64}
65
66void sound(int client)
67{
68 ClientCommand(client, "play *achievements/achievement.mp3");
69}
70
71public OnClientConnected(client)
72{
73 ClientCommand(client,"r_drawscreenoverlay 1");
74}
75
76public void OnClientPutInServer(int client)
77{
78 g_bPlayerWczytane[client]=false;
79 Load(client);
80 givetags(client);
81}
82
83public void OnPluginStart()
84{
85 RegConsoleCmd("sm_achievements", cmd_achivements);
86 RegConsoleCmd("sm_tag", cmd_tag);
87 RegConsoleCmd("sm_stats", cmd_stats);
88
89 HookEvent("player_death", Player_Death);
90 HookEvent("round_end", Round_End);
91 HookEvent("bomb_planted", BombPlanted);
92 HookEvent("bomb_defused", BombDefused);
93
94 g_iPlayerOn = CreateConVar("achievements_players", "2", "How many players for getting achievement?", _, true, 1.0, true, 64.0);
95 g_iPlayerKills = CreateConVar("achievements_kills", "20", "How many kills for achievement?", _, true, 1.0, true, 1000.0);
96 g_iPlayerAssists = CreateConVar("achievements_assists", "5", "How many assists for achievement?", _, true, 1.0, true, 1000.0);
97 g_iPlayerHeadshots = CreateConVar("achivements_headshots", "5", "How many headshots for achievement?", _, true, 1.0, true, 1000.0);
98 g_iPlayerWin = CreateConVar("achievements_win", "10", "How many rounds wins for achievement?", _, true, 1.0, true, 1000.0);
99 g_iPlayerLose = CreateConVar("achievements_lose", "10", "How many rounds lost for achievement?", _, true, 1.0, true, 1000.0);
100 g_iPlayerPlant = CreateConVar("achievements_plant", "5", "How many bomb plants for achievement?", _, true, 1.0, true, 1000.0);
101 g_iPlayerDefuse = CreateConVar("achievements_defuse", "5", "How many bomb defuses for achievement?", _, true, 1.0, true, 1000.0);
102
103 AutoExecConfig(true, "Achievements_Config");
104}
105
106public Action cmd_tag(int client, int args)
107{
108 if(IsValidPlayer(client))
109 {
110 playertag(client);
111 }
112}
113
114public Action cmd_stats(int client, int args)
115{
116 if(IsValidPlayer(client))
117 {
118 stats(client);
119 }
120}
121
122public Action cmd_achivements(int client, int args)
123{
124 Handle menu = CreateMenu(MenuAchivementsHand);
125
126 SetMenuTitle(menu, "Achievements: Menu");
127
128 AddMenuItem(menu, "op1","My Achievements");
129 AddMenuItem(menu, "op2","My Stats");
130 AddMenuItem(menu, "op3","All Achievements");
131 AddMenuItem(menu, "op4","Achievements Tags");
132
133 SetMenuExitButton(menu, true);
134 DisplayMenu(menu, client, MENU_TIME_FOREVER);
135}
136
137public MenuAchivementsHand(Handle menu, MenuAction:action, client, itemNum)
138{
139 if ( action == MenuAction_Select && IsValidPlayer(client))
140 {
141 char info[255];
142 GetMenuItem(menu, itemNum, info, sizeof(info));
143
144 if(StrEqual(info, "op1"))
145 {
146 PrintToChat(client, " \x02---------------- \x01My Achievements \x02----------------");
147
148 if(g_iKills[client]>=g_iPlayerKills.IntValue)
149 {
150 PrintToChat(client, " \x04%s [Owned]",g_sNames[1]);
151 }
152 if(g_iAssists[client]>=g_iPlayerAssists.IntValue)
153 {
154 PrintToChat(client, " \x04%s [Owned]",g_sNames[2]);
155 }
156 if(g_iHeadshots[client]>=g_iPlayerHeadshots.IntValue)
157 {
158 PrintToChat(client, " \x04%s [Owned]",g_sNames[3]);
159 }
160 if(g_iWin[client]>=g_iPlayerWin.IntValue)
161 {
162 PrintToChat(client, " \x04%s [Owned]",g_sNames[4]);
163 }
164 if(g_iLose[client]>=g_iPlayerLose.IntValue)
165 {
166 PrintToChat(client, " \x04%s [Owned]",g_sNames[5]);
167 }
168 if(g_iPlant[client]>=g_iPlayerPlant.IntValue)
169 {
170 PrintToChat(client, " \x04%s [Owned]",g_sNames[6]);
171 }
172 if(g_iDefuse[client]>=g_iPlayerDefuse.IntValue)
173 {
174 PrintToChat(client, " \x04%s [Owned]",g_sNames[7]);
175 }
176
177 PrintToChat(client, " \x02---------------------------------------------");
178 }
179 if(StrEqual(info, "op2"))
180 {
181 PrintToChat(client, " \x02---------------- \x01My Statistics \x02----------------");
182 PrintToChat(client, " \x04Kills: %i",g_iKills[client]);
183 PrintToChat(client, " \x04Assists: %i",g_iAssists[client]);
184 PrintToChat(client, " \x04Headshots %i",g_iHeadshots[client]);
185 PrintToChat(client, " \x04Win: %i",g_iWin[client]);
186 PrintToChat(client, " \x04Lose: %i",g_iLose[client]);
187 PrintToChat(client, " \x04Bomb planted: %i",g_iPlant[client]);
188 PrintToChat(client, " \x04Bomb defused: %i",g_iDefuse[client]);
189 PrintToChat(client, " \x02---------------------------------------------");
190 }
191 if(StrEqual(info, "op3"))
192 {
193 stats(client);
194 }
195 if(StrEqual(info, "op4"))
196 {
197 playertag(client);
198 }
199 }
200}
201
202public stats(int client)
203{
204 if(IsValidPlayer(client))
205 {
206 PrintToChat(client, " \x02---------------- \x01All Achievements \x02----------------");
207
208 PrintToChat(client, " \x04%s: [%i / %i]",g_sNames[1],g_iKills[client],g_iPlayerKills.IntValue);
209 PrintToChat(client, " \x04%s: [%i / %i]",g_sNames[2],g_iAssists[client],g_iPlayerAssists.IntValue);
210 PrintToChat(client, " \x04%s: [%i / %i]",g_sNames[3],g_iHeadshots[client],g_iPlayerHeadshots.IntValue);
211 PrintToChat(client, " \x04%s: [%i / %i]",g_sNames[4],g_iWin[client],g_iPlayerWin.IntValue);
212 PrintToChat(client, " \x04%s: [%i / %i]",g_sNames[5],g_iLose[client],g_iPlayerLose.IntValue);
213 PrintToChat(client, " \x04%s: [%i / %i]",g_sNames[6],g_iPlant[client],g_iPlayerPlant.IntValue);
214 PrintToChat(client, " \x04%s: [%i / %i]",g_sNames[7],g_iDefuse[client],g_iPlayerDefuse.IntValue);
215
216 PrintToChat(client, " \x02---------------------------------------------");
217 }
218}
219
220
221public playertag(int client)
222{
223 Menu menu = new Menu(MenuTagHandler, MenuAction_Start|MenuAction_Select|MenuAction_Cancel|MenuAction_End);
224
225 SetMenuTitle(menu, "Achievements: Tags");
226
227 menu.AddItem("op1", "▸ Killer", g_iKills[client] < g_iPlayerKills.IntValue ? ITEMDRAW_DISABLED : ITEMDRAW_DEFAULT);
228 menu.AddItem("op2", "▸ HelpfulGuy", g_iAssists[client] < g_iPlayerAssists.IntValue ? ITEMDRAW_DISABLED : ITEMDRAW_DEFAULT);
229 menu.AddItem("op3", "▸ HeadHunter", g_iHeadshots[client] < g_iPlayerHeadshots.IntValue ? ITEMDRAW_DISABLED : ITEMDRAW_DEFAULT);
230 menu.AddItem("op4", "▸ Winner", g_iWin[client] < g_iPlayerWin.IntValue ? ITEMDRAW_DISABLED : ITEMDRAW_DEFAULT);
231 menu.AddItem("op5", "▸ Loser", g_iLose[client] < g_iPlayerLose.IntValue ? ITEMDRAW_DISABLED : ITEMDRAW_DEFAULT);
232 menu.AddItem("op6", "▸ Planter", g_iPlant[client] < g_iPlayerPlant.IntValue ? ITEMDRAW_DISABLED : ITEMDRAW_DEFAULT);
233 menu.AddItem("op7", "▸ Sapper", g_iDefuse[client] < g_iPlayerDefuse.IntValue ? ITEMDRAW_DISABLED : ITEMDRAW_DEFAULT);
234
235 menu.ExitButton = true;
236 DisplayMenu(menu, client, MENU_TIME_FOREVER);
237}
238
239public MenuTagHandler(Handle menu, MenuAction:action, client, itemNum)
240{
241 if ( action == MenuAction_Select && IsValidPlayer(client))
242 {
243 char info[255];
244 GetMenuItem(menu, itemNum, info, sizeof(info));
245
246 if(StrEqual(info, "op1"))
247 {
248 g_iPlayerTag[client] = 1;
249 Save(client);
250 }
251 if(StrEqual(info, "op2"))
252 {
253 g_iPlayerTag[client] = 2;
254 Save(client);
255 }
256 if(StrEqual(info, "op3"))
257 {
258 g_iPlayerTag[client] = 3;
259 Save(client);
260 }
261 if(StrEqual(info, "op4"))
262 {
263 g_iPlayerTag[client] = 4;
264 Save(client);
265 }
266 if(StrEqual(info, "op5"))
267 {
268 g_iPlayerTag[client] = 5;
269 Save(client);
270 }
271 if(StrEqual(info, "op6"))
272 {
273 g_iPlayerTag[client] = 6;
274 Save(client);
275 }
276 if(StrEqual(info, "op7"))
277 {
278 g_iPlayerTag[client] = 7;
279 Save(client);
280 }
281 }
282}
283
284void givetags(int client)
285{
286 CS_SetClientClanTag(client, "[%s] ",g_sNames[g_iPlayerTag[client]]);
287}
288
289public Action OnChatMessage(&author, Handle recipients, String:name[], String:message[])
290{
291 if(IsValidPlayer(author))
292 {
293 if(g_iPlayerTag[author]>=0)
294 {
295 Format(name, MAXLENGTH_NAME, "%s", name);
296 new MaxMessageLength = MAXLENGTH_MESSAGE - strlen(name) - 5;
297 Format(name, MaxMessageLength, " \x02[%s] %s",g_sNames[g_iPlayerTag[author]],name);
298 return Plugin_Changed;
299 }
300 else
301 {
302 Format(name, MAXLENGTH_NAME, "%s", name);
303 new MaxMessageLength = MAXLENGTH_MESSAGE - strlen(name) - 5;
304 Format(name, MaxMessageLength, " %s",name);
305 return Plugin_Changed;
306 }
307 }
308 return Plugin_Continue;
309}
310
311public Action Player_Death(Handle event, char[] name2, bool dontBroadcast)
312{
313
314 int attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
315 int assister = GetClientOfUserId(GetEventInt(event, "assister"));
316 bool headshot = GetEventBool(event, "headshot");
317
318 if(IsValidPlayers() >= g_iPlayerOn.IntValue)
319 {
320 if(IsValidPlayer(attacker))
321 {
322 g_iKills[attacker] = g_iKills[attacker] + 1;
323 Save(attacker);
324
325 if(g_iKills[attacker]==g_iPlayerKills.IntValue)
326 {
327 sound(attacker);
328 ClientCommand(attacker, "r_screenoverlay overlays/achievements/killer");
329 CreateTimer(3.0, offover, attacker);
330 Save(attacker);
331 PrintToChatAll(" \x04Player \x02%N \x04gets achievement: \x02%s \x04!",g_sNames[1], attacker);
332 }
333
334 }
335
336 if(IsValidPlayer(assister))
337 {
338 g_iAssists[assister] = g_iAssists[assister] + 1;
339 Save(assister);
340
341 if(g_iAssists[assister]==g_iPlayerAssists.IntValue)
342 {
343 sound(assister);
344 ClientCommand(assister, "r_screenoverlay overlays/achievements/help");
345 CreateTimer(3.0, offover, assister);
346 Save(assister);
347 PrintToChatAll(" \x04Player \x02%N \x04gets achievement: \x02%s \x04!",g_sNames[2], assister);
348 }
349 }
350
351 if(headshot)
352 {
353 if(IsValidPlayer(attacker))
354 {
355 g_iHeadshots[attacker] = g_iHeadshots[attacker] + 1;
356 Save(attacker);
357
358 if(g_iHeadshots[attacker]==g_iPlayerHeadshots.IntValue)
359 {
360 sound(attacker);
361 ClientCommand(attacker, "r_screenoverlay overlays/achievements/head");
362 CreateTimer(3.0, offover, attacker);
363 Save(attacker);
364 PrintToChatAll(" \x04Player \x02%N \x04gets achievement: \x02%s \x04!",g_sNames[3], attacker);
365 }
366 }
367 }
368 }
369
370}
371
372public BombPlanted(Handle event, const String:name[], bool dontBroadcast)
373{
374 new userid = GetClientOfUserId(GetEventInt(event, "userid"));
375
376 if(IsValidPlayers() >= g_iPlayerOn.IntValue)
377 {
378 if(IsValidPlayer(userid))
379 {
380 g_iPlant[userid] = g_iPlant[userid] + 1;
381 Save(userid);
382
383 if(g_iPlant[userid]==g_iPlayerPlant.IntValue)
384 {
385 sound(userid);
386 ClientCommand(userid, "r_screenoverlay overlays/achievements/planter");
387 CreateTimer(3.0, offover, userid);
388 Save(userid);
389 PrintToChatAll(" \x04Player \x02%N \x04gets achievement: \x02%s \x04!",g_sNames[6], userid);
390 }
391 }
392 }
393}
394
395public BombDefused(Handle:event, const String:name[], bool:dontBroadcast)
396{
397 new userid = GetClientOfUserId(GetEventInt(event, "userid"));
398
399 if(IsValidPlayers() >= g_iPlayerOn.IntValue)
400 {
401 if(IsValidPlayer(userid))
402 {
403 g_iDefuse[userid] = g_iDefuse[userid] + 1;
404 Save(userid);
405
406 if(g_iDefuse[userid]==g_iPlayerDefuse.IntValue)
407 {
408 sound(userid);
409 ClientCommand(userid, "r_screenoverlay overlays/achievements/sapper");
410 CreateTimer(3.0, offover, userid);
411 Save(userid);
412 PrintToChatAll(" \x04Player \x02%N \x04gets achievement: \x02%s \x04!",g_sNames[7], userid);
413 }
414 }
415 }
416}
417
418public Round_End(Handle:event, const String:name[], bool:dontBroadcast)
419{
420 if(IsValidPlayers() >= g_iPlayerOn.IntValue)
421 {
422 new wygrana_druzyna = GetEventInt(event, "winner");
423 for(new i = 1; i <= MaxClients; i ++)
424 {
425 if(!IsClientInGame(i))
426 continue;
427
428 if(GetClientTeam(i) != ((wygrana_druzyna == 2)? CS_TEAM_T: CS_TEAM_CT))
429 {
430
431 if(IsValidPlayer(i))
432 {
433 g_iLose[i] = g_iLose[i] + 1;
434 Save(i);
435
436 if(g_iLose[i]==g_iPlayerLose.IntValue)
437 {
438 sound(i);
439 ClientCommand(i, "r_screenoverlay overlays/achievements/loser");
440 CreateTimer(3.0, offover, i);
441 Save(i);
442 PrintToChatAll(" \x04Player \x02%N \x04gets achievement: \x02%s \x04!",g_sNames[4], i);
443 }
444 }
445 }
446 else
447 {
448 if(IsValidPlayer(i))
449 {
450 g_iWin[i] = g_iWin[i] + 1;
451 Save(i);
452
453 if(g_iWin[i]==g_iPlayerWin.IntValue)
454 {
455 sound(i);
456 ClientCommand(i, "r_screenoverlay overlays/achievements/winner");
457 CreateTimer(3.0, offover, i);
458 Save(i);
459 PrintToChatAll(" \x04Player \x02%N \x04gets achievement: \x02%s \x04!",g_sNames[5], i);
460 }
461 }
462 }
463 }
464 }
465}
466
467public Action offover(Handle:timer, any:client)
468{
469 if(IsValidPlayer(client))
470 {
471 ClientCommand(client, "r_screenoverlay \"\"");
472 }
473}
474
475public void ConnectSQL()
476{
477 if(g_hSQL != null)
478 {
479 CloseHandle(g_hSQL);
480 }
481
482 if(SQL_CheckConfig("achievements"))
483 {
484 char _cError[255];
485
486 if(!(g_hSQL = SQL_Connect("achievements", true, _cError, 255)))
487 {
488 if(g_iPolaczenia < 5)
489 {
490 g_iPolaczenia++;
491 LogError("ERROR: %s", _cError);
492 ConnectSQL();
493
494 return;
495 }
496 g_iPolaczenia=0;
497 }
498 }
499
500 new Handle:queryH = SQL_Query(g_hSQL, "CREATE TABLE IF NOT EXISTS `Players` ( `STEAMID64` VARCHAR(128) NOT NULL , `name` VARCHAR(128) NOT NULL , `killq` INT(11) NOT NULL , `assist` INT(11) NOT NULL , `head` INT(11) NOT NULL , `win` INT(11) NOT NULL , `lose` INT(11) NOT NULL , `plant` INT(11) NOT NULL , `defuse` INT(11) NOT NULL , `tag` INT(11) NOT NULL)");
501 if(queryH != INVALID_HANDLE)
502 {
503 PrintToServer("Succesfully create database.");
504 }
505 else
506 {
507 SQL_GetError(g_hSQL, Error, sizeof(Error));
508 PrintToServer("Database wasn't created. Error: %s",Error);
509 }
510}
511
512public void Load(int client)
513{
514
515 if(!IsValidPlayer(client))
516 return;
517
518 if(!g_hSQL)
519 {
520 ConnectSQL();
521 Load(client);
522 return;
523 }
524
525
526 char _cBuffer[1024];
527 char _cSteamID64[64];
528 char s_name[64];
529
530 GetClientAuthId(client, AuthId_SteamID64, _cSteamID64, sizeof(_cSteamID64));
531 Format(_cBuffer,sizeof(_cBuffer),"SELECT * FROM Players WHERE STEAMID64 = '%s'",_cSteamID64);
532 GetClientName(client, s_name, sizeof(s_name));
533
534 Handle _HQuery = SQL_Query(g_hSQL, _cBuffer);
535
536 if (_HQuery != INVALID_HANDLE)
537 {
538
539 bool _bFetch=SQL_FetchRow(_HQuery);
540 if(_bFetch)
541 {
542 Format(_cSteamID64,sizeof(_cSteamID64),"");
543 SQL_FetchString(_HQuery, 0, _cSteamID64, 63);
544
545 Format(s_name,sizeof(s_name),"");
546 SQL_FetchString(_HQuery, 1, s_name, 64);
547
548 g_iKills[client]=SQL_FetchInt(_HQuery,2);
549 g_iAssists[client]=SQL_FetchInt(_HQuery,3);
550 g_iHeadshots[client]=SQL_FetchInt(_HQuery,4);
551 g_iWin[client]=SQL_FetchInt(_HQuery,5);
552 g_iLose[client]=SQL_FetchInt(_HQuery,6);
553 g_iPlant[client]=SQL_FetchInt(_HQuery,7);
554 g_iDefuse[client]=SQL_FetchInt(_HQuery,8);
555 g_iPlayerTag[client]=SQL_FetchInt(_HQuery,9);
556
557 if(_cSteamID64[0])
558 {
559 g_bPlayerWczytane[client]=true;
560 }
561
562 CloseHandle(_HQuery);
563 return;
564 }
565 }
566
567 CloseHandle(_HQuery);
568 Format(_cBuffer,sizeof(_cBuffer),"INSERT IGNORE INTO Players VALUES ('%s', '%s','0','0','0','0','0','0','0','0')",_cSteamID64,s_name);
569
570 SQL_Query(g_hSQL, _cBuffer);
571 Load(client);
572}
573
574public void Save(int client)
575{
576 if(!IsValidPlayer(client))
577 return;
578
579 if(!g_hSQL)
580 {
581 ConnectSQL();
582 return;
583 }
584
585 char _cBuffer[1024];
586 char _cSteamID64[64];
587
588 GetClientAuthId(client, AuthId_SteamID64, _cSteamID64, sizeof(_cSteamID64));
589
590
591 Format(_cBuffer,sizeof(_cBuffer),"UPDATE Players SET killq=%i, assist=%i, head=%i, win=%i, lose=%i, plant=%i, defuse=%i tag=%i WHERE STEAMID64 = '%s'",g_iKills[client],g_iAssists[client],g_iHeadshots[client],g_iWin[client],g_iLose[client],g_iPlant[client],g_iDefuse[client],g_iPlayerTag[client],_cSteamID64);
592
593 SQL_Query(g_hSQL, _cBuffer);
594
595}
596
597stock AddFolderToDownloadsTable(const String:sDirectory[])
598{
599 decl String:sFile[64], String:sPath[512];
600 new FileType:iType, Handle:hDir = OpenDirectory(sDirectory);
601 while(ReadDirEntry(hDir, sFile, sizeof(sFile), iType))
602 {
603 if(iType == FileType_File)
604 {
605 Format(sPath, sizeof(sPath), "%s/%s", sDirectory, sFile);
606 AddFileToDownloadsTable(sPath);
607 }
608 }
609}
610
611public IsValidPlayers()
612{
613 int players;
614 for(new i = 1; i <= MaxClients; i ++)
615 {
616 if(!IsClientInGame(i) || IsFakeClient(i))
617 continue;
618
619 players ++;
620 }
621
622 return players;
623}
624
625stock bool IsValidPlayer(int client)
626{
627 if(client >= 1 && client <= MaxClients && IsClientConnected(client) && !IsFakeClient(client) && IsClientInGame(client) )
628 return true;
629
630 return false;
631}