· 9 years ago · Nov 27, 2016, 10:16 AM
1#include <sourcemod>
2#include <cstrike>
3#include <sdkhooks>
4#include <sdktools>
5
6new Handle:db = INVALID_HANDLE;
7new score[MAXPLAYERS+1];
8
9/// TOP
10new String:topname[10][32];
11new topscore[10];
12
13/// RANK
14new rank[MAXPLAYERS+1];
15new rankcount[MAXPLAYERS+1];
16new rankscore[MAXPLAYERS+1];
17new bool:showrank[MAXPLAYERS+1];
18
19public Plugin:myinfo =
20{
21 name = "The best rank plugin",
22 author = "ilusion from mevid",
23 description = "",
24 version = "1.0",
25 url = "http://www.mevid.ro"
26};
27
28public OnPluginStart()
29{
30
31 decl String:error[256];
32 error[0] = '\0';
33
34 db = SQL_Connect("stats", true, error, sizeof(error));
35
36 if(db == INVALID_HANDLE)
37 {
38 LogError("Could not connect to default database: %s", error);
39 }
40 else
41 {
42 SQL_TQuery(db, SQLErrorCallback, "CREATE TABLE IF NOT EXISTS sm_easyrank (steamid varchar(32) NOT NULL PRIMARY KEY, name varchar(64) NOT NULL, score int NOT NULL DEFAULT 0)");
43 RegConsoleCmd("sm_rank", sm_rank, "!rank <player> or !rank");
44 RegConsoleCmd("sm_top", sm_top, "!top");
45 RegConsoleCmd("sm_top10", sm_top, "!top");
46 RegAdminCmd("sm_reset_all", resetAll, ADMFLAG_RCON);
47 HookEvent("player_death", OnPlayerDeath);
48 }
49
50 RegConsoleCmd("rs", CommandRs);
51 RegAdminCmd("sm_spec", CommandSpec, ADMFLAG_VOTE, "sm_spec <target> - Moves player to the Spectators.");
52 RegAdminCmd("sm_ct", CommandCT, ADMFLAG_VOTE, "sm_ct <target> - Moves player to the CTs.");
53 RegAdminCmd("sm_t", CommandT, ADMFLAG_VOTE, "sm_t <target> - Moves player to the Ts.");
54
55}
56
57public SQLErrorCallback(Handle:owner, Handle:hndl, const String:error[], any:data) {
58
59 if(!StrEqual("", error))
60 LogError("Query failed: %s", error);
61
62 return false;
63
64}
65
66public OnMapStart ()
67{
68 decl String:query[256];
69 Format(query, sizeof(query), "SELECT name, score FROM sm_easyrank ORDER BY score DESC, name ASC LIMIT 0, 10");
70 SQL_TQuery(db, SQLQueryTop10, query);
71}
72
73public OnClientPostAdminCheck(client)
74{
75
76 if(IsValidClient(client))
77 {
78
79 decl String:query[256];
80
81 new userid = GetClientUserId(client);
82
83 score[client] = 0;
84 showrank[client] = false;
85
86 static char Steam[128];
87 FGetSteam(client, Steam, sizeof(Steam));
88
89 Format(query, sizeof(query), "SELECT * FROM sm_easyrank WHERE steamid = '%s'", Steam);
90 SQL_TQuery(db, SQLQueryConnect, query, userid);
91
92 }
93
94}
95
96public OnClientDisconnect (client)
97{
98
99 if (IsValidClient(client))
100 {
101
102 decl String:query[512];
103
104 showrank[client] = false;
105
106 static char Steam[128];
107 FGetSteam(client, Steam, sizeof(Steam));
108
109 Format(query, sizeof(query), "UPDATE sm_easyrank SET `score` = score + %i WHERE steamid = '%s'", score[client], Steam);
110 SQL_TQuery(db, SQLErrorCallback, query);
111
112 }
113
114}
115
116public SQLQueryConnect(Handle:owner, Handle:hndl, const String:error[], any:data)
117{
118
119 new client;
120
121 if((client = GetClientOfUserId(data))==0)
122 {
123 return;
124 }
125
126 if(hndl==INVALID_HANDLE)
127 {
128 LogError("Query failed: %s", error);
129 }
130 else
131 {
132 decl String:query[512],
133 String:clientname[(MAX_NAME_LENGTH*2)+1],
134 String:unsafeclientname[MAX_NAME_LENGTH];
135
136 query[0] = '\0';
137 clientname[0] = '\0';
138
139 GetClientName(client, unsafeclientname, sizeof(unsafeclientname));
140
141 SQL_EscapeString(db, unsafeclientname, clientname, sizeof(clientname));
142
143 static char Steam[128];
144 FGetSteam(client, Steam, sizeof(Steam));
145
146 if(!SQL_MoreRows(hndl))
147 {
148 Format(query, sizeof(query), "INSERT INTO sm_easyrank (steamid, name, score) VALUES ('%s', '%s', 0.0)", Steam, clientname);
149 SQL_TQuery(db, SQLErrorCallback, query);
150 }
151 else
152 {
153 Format(query, sizeof(query), "UPDATE sm_easyrank SET `name` = '%s' WHERE steamid = '%s'", client, clientname, Steam);
154 SQL_TQuery(db, SQLErrorCallback, query);
155 }
156 }
157
158}
159
160public Action:sm_rank(client,args)
161{
162
163 if (!showrank[client])
164 {
165 static char Steam[128];
166 FGetSteam(client, Steam, sizeof(Steam));
167
168 showrank[client] = true;
169
170 decl String:query[512];
171
172 FormatEx(query, sizeof(query), "SELECT (SELECT count(*) FROM sm_easyrank s JOIN sm_easyrank s2 ON s2.steamid = '%s' AND (s.score > s2.score OR s.score = s2.score AND s.name <= s2.name)) AS rank, (SELECT score FROM sm_easyrank WHERE (steamid = '%s')) AS scores, (SELECT count(*) FROM sm_easyrank) AS total", Steam, Steam);
173 SQL_TQuery(db, SQLQueryRank, query, GetClientUserId(client));
174 }
175 else
176 {
177 PrintToChat(client, "Your rank on this server is\x04 %i\x01 out of \x04%i\x01 with score \x04%i\x01.", rank[client], rankcount[client], rankscore[client]);
178 }
179
180 return Plugin_Handled;
181
182}
183
184public SQLQueryRank(Handle:owner, Handle:hndl, const String:error[], any:data)
185{
186
187 new client;
188 if((client = GetClientOfUserId(data))==0)
189 {
190 return;
191 }
192
193 if(hndl==INVALID_HANDLE)
194 {
195 LogError("Query failed: %s", error);
196 return;
197 }
198
199 while(SQL_FetchRow(hndl))
200 {
201 rank[client] = SQL_FetchInt(hndl, 0);
202 rankscore[client] = SQL_FetchInt(hndl, 1);
203 rankcount[client] = SQL_FetchInt(hndl, 2);
204 }
205
206 PrintToChat(client, "Your rank on this server is\x04 %i\x01 out of \x04%i\x01 with score \x04%i\x01.", rank[client], rankcount[client], rankscore[client]);
207
208}
209
210public SQLQueryTop10(Handle:owner, Handle:hndl, const String:error[], any:data)
211{
212
213 if(hndl==INVALID_HANDLE)
214 LogError("Query failed: %s", error);
215 else
216 {
217
218 new y = 0;
219
220 while(SQL_FetchRow(hndl)) {
221 SQL_FetchString(hndl, 0, topname[y], 32);
222 topscore[y] = SQL_FetchInt(hndl, 1);
223 y++;
224 }
225 }
226
227}
228
229public Action:sm_top(client,args)
230{
231
232 decl String:buffer[68];
233 buffer[0] = '\0';
234 new y = 1;
235 new Handle:panel = CreatePanel();
236 SetPanelTitle(panel, "Top 10 players");
237
238 while(y <= 10) {
239
240 Format(buffer, sizeof(buffer), "%i. %s - %i points", y, topname[y-1], topscore[y-1]);
241 DrawPanelText(panel, buffer);
242 y++;
243
244 }
245
246 DrawPanelItem(panel, "Close");
247 SendPanelToClient(panel, client, PanelHandlerNothing, 15);
248 CloseHandle(panel);
249
250 return Plugin_Handled;
251}
252
253public PanelHandlerNothing(Handle:menu, MenuAction:action, param1, param2) {}
254
255public Action:OnPlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
256{
257
258 new client = GetClientOfUserId(GetEventInt(event, "userid"));
259 new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
260 new assister = GetClientOfUserId(GetEventInt(event, "assister"));
261 new bool:headshot = GetEventBool(event, "headshot");
262
263 if(IsValidClient(client) && IsValidClient(attacker))
264 {
265 if(!IsFakeClient(client)&&!IsFakeClient(attacker))
266 {
267
268 if((assister!=0) && !(IsFakeClient(assister)))
269 score[assister] += 3;
270
271 if(client!=attacker)
272 {
273 score[client] -= 5;
274
275 if (headshot)
276 score[attacker] += 10;
277 else
278 score[attacker] += 5;
279
280 }
281
282 else
283 score[client] -= 10;
284 }
285 }
286
287}
288
289public Action:resetAll(int client, int args)
290{
291
292 new String:arg[65];
293 GetCmdArg(1, arg, sizeof(arg));
294
295 decl String:query[512];
296 query[0] = '\0';
297
298 Format(query, sizeof(query), "truncate sm_easyrank");
299 SQL_TQuery(db, SQLErrorCallback, query);
300 syncDatabase_exec();
301 ReplyToCommand(client, "[SM] Your database was reseted!");
302
303 return Plugin_Handled;
304
305}
306
307public syncDatabase_exec()
308{
309
310 decl String:clientid[32], String:query[256];
311 query[0] = '\0'; clientid[0] = '\0';
312
313 for(new i=1; i<= MaxClients; i++)
314 {
315 if(IsClientInGame(i))
316 {
317 if(!(IsFakeClient(i)))
318 {
319 new userid = GetClientUserId(i);
320 GetClientAuthId(i, AuthId_Steam3, clientid, sizeof(clientid));
321 Format(query, sizeof(query), "SELECT score FROM sm_easyrank WHERE steamid = '%s'", clientid);
322 SQL_TQuery(db, SQLQueryConnect, query, userid);
323 }
324 }
325 }
326
327}
328
329public Action:CommandSpec(client, Args)
330{
331
332 if (Args < 1)
333 {
334 ReplyToCommand(client, "[SM] Usage: sm_spec <target> - Moves player to the Spectators.");
335 return Plugin_Handled;
336 }
337
338 decl String:Arg[64];
339 GetCmdArg(1, Arg, sizeof(Arg));
340
341 new target = FindTarget(client, Arg, true);
342 if (target == -1)
343 {
344 return Plugin_Handled;
345 }
346
347 if (GetClientTeam(target) == CS_TEAM_SPECTATOR)
348 {
349 ReplyToCommand(client, "[SM] This user is already a terrorist.");
350 return Plugin_Handled;
351 }
352
353 new String: playerName[MAX_NAME_LENGTH], String: AdminName[MAX_NAME_LENGTH];
354
355 GetClientName(target, playerName, sizeof(playerName));
356
357 if(IsValidClient(target) && client != target)
358 if(GetClientName(client, AdminName, sizeof(AdminName)))
359 {
360 GetClientName(client, AdminName, sizeof(AdminName));
361 PrintToChatAll("Player \x04%s\x01 has been moved by \x04%s\x01 to the spectators!", playerName, AdminName);
362 }
363 else
364 PrintToChatAll("Player \x04%s\x01 has been moved to the spectators!", playerName);
365
366 LogAction(client, target, "\"%L\" moved \"%L\" to the Spectators.", client, target);
367
368 if (IsPlayerAlive(target))
369 {
370 ForcePlayerSuicide(target);
371 CreateTimer(0.1, ResolveScore, target, TIMER_FLAG_NO_MAPCHANGE);
372 }
373
374 ChangeClientTeam(target, CS_TEAM_SPECTATOR);
375 return Plugin_Handled;
376
377}
378
379public Action:CommandT(client, Args)
380{
381 if (Args < 1)
382 {
383 ReplyToCommand(client, "[SM] Usage: sm_t <target> - Moves player to the Ts.");
384 return Plugin_Handled;
385 }
386
387 decl String:Arg[64];
388 GetCmdArg(1, Arg, sizeof(Arg));
389
390 new target = FindTarget(client, Arg, true);
391
392 if (target == -1)
393 {
394 return Plugin_Handled;
395 }
396
397 if (GetClientTeam(target) == CS_TEAM_T)
398 {
399 ReplyToCommand(client, "[SM] This user is already a terrorist.");
400 return Plugin_Handled;
401 }
402
403 new String: playerName[MAX_NAME_LENGTH], String: AdminName[MAX_NAME_LENGTH];
404
405 GetClientName(target, playerName, sizeof(playerName));
406
407 if(IsValidClient(target) && client != target)
408 if(GetClientName(client, AdminName, sizeof(AdminName)))
409 {
410 GetClientName(client, AdminName, sizeof(AdminName));
411 PrintToChatAll("Player \x04%s\x01 has been moved by \x04%s\x01 to the terrorists!", playerName, AdminName);
412 }
413 else
414 PrintToChatAll("Player \x04%s\x01 has been moved to the terrorists!", playerName);
415
416
417 LogAction(client, target, "\"%L\" moved \"%L\" to the Ts.", client, target);
418
419 if (IsPlayerAlive(target))
420 {
421 ForcePlayerSuicide(target);
422 CreateTimer(0.1, ResolveScore, target, TIMER_FLAG_NO_MAPCHANGE);
423 }
424
425 ChangeClientTeam(target, CS_TEAM_T);
426 return Plugin_Handled;
427}
428
429public Action:CommandCT(client, Args)
430{
431 if (Args < 1)
432 {
433 ReplyToCommand(client, "[SM] Usage: sm_ct <target> - Moves player to the CTs.");
434 return Plugin_Handled;
435 }
436
437 decl String:Arg[64];
438 GetCmdArg(1, Arg, sizeof(Arg));
439
440 new target = FindTarget(client, Arg, true);
441 if (target == -1)
442 {
443 return Plugin_Handled;
444 }
445
446 if (GetClientTeam(target) == CS_TEAM_CT)
447 {
448 ReplyToCommand(client, "[SM] This user is already a terrorist.");
449 return Plugin_Handled;
450 }
451
452 new String: playerName[MAX_NAME_LENGTH], String: AdminName[MAX_NAME_LENGTH];
453
454 GetClientName(target, playerName, sizeof(playerName));
455
456 if(IsValidClient(target) && client != target)
457 if(GetClientName(client, AdminName, sizeof(AdminName)))
458 {
459 GetClientName(client, AdminName, sizeof(AdminName));
460 PrintToChatAll("Player \x04%s\x01 has been moved by \x04%s\x01 to the counter-terrorists!", playerName, AdminName);
461 }
462 else
463 PrintToChatAll("Player \x04%s\x01 has been moved to the counter-terrorists!", playerName);
464
465 LogAction(client, target, "\"%L\" moved \"%L\" to the CTs.", client, target);
466
467 if (IsPlayerAlive(target))
468 {
469 ForcePlayerSuicide(target);
470 CreateTimer(0.1, ResolveScore, target, TIMER_FLAG_NO_MAPCHANGE);
471 }
472
473 ChangeClientTeam(target, CS_TEAM_CT);
474 return Plugin_Handled;
475}
476
477public Action:CommandRs(client, Args)
478{
479
480 SetEntProp(client, Prop_Data, "m_iFrags", 0);
481 SetEntProp(client, Prop_Data, "m_iDeaths", 0);
482
483 CS_SetMVPCount(client, 0);
484 CS_SetClientAssists(client, 0);
485 CS_SetClientContributionScore(client, 0);
486
487 new String: playerName [256];
488
489 if (GetClientName (client, playerName, sizeof(playerName)))
490 GetClientName (client, playerName, sizeof(playerName));
491
492 PrintToChatAll("Player\x04 %s\x01 has reseted his score!", playerName);
493
494}
495
496public Action:ResolveScore(Handle:pTimer, client)
497{
498
499 if (IsValidClient(client))
500 {
501 new Deaths = GetClientDeaths(client) - 1;
502 new Frags = GetClientFrags(client) + 1;
503
504 SetEntProp(client, Prop_Data, "m_iFrags", Frags);
505 SetEntProp(client, Prop_Data, "m_iDeaths", Deaths);
506
507 score[client] += 10;
508 }
509
510}
511
512stock IsValidClient(client) {
513
514 if (!(1 <= client <= MaxClients) || !IsClientInGame(client))
515 return false;
516
517 return true;
518
519}
520
521void FGetSteam(int Who, char[] Out, int maxSize)
522{
523 GetClientAuthId(Who, AuthId_Engine, Out, maxSize);
524}