· 5 years ago · Feb 01, 2020, 08:28 PM
1#pragma semicolon 1
2#pragma newdecls required
3
4#include <sourcemod>
5#include <sdktools>
6#include <cstrike>
7
8Database hDatabase;
9int g_Headshots[MAXPLAYERS + 1];
10int g_Kills[MAXPLAYERS + 1];
11int g_Deaths[MAXPLAYERS + 1];
12int g_Score[MAXPLAYERS + 1];
13
14public void OnPluginStart()
15{
16 Database.Connect(Handle_ConnectDatabase, "rank");
17
18 HookEvent("player_team", Event_PlayerTeam);
19 HookEvent("player_death", Event_PlayerDeath);
20 HookEvent("bomb_exploded", Event_BombExploded);
21 HookEvent("bomb_defused", Event_BombDefused);
22 HookEvent("bomb_planted", Event_BombPlanted);
23 HookEvent("hostage_rescued", Event_HostageRescued);
24 HookEvent("hostage_killed", Event_HostageKilled);
25 HookEvent("round_mvp", Event_RoundMvp);
26
27 RegConsoleCmd("sm_rank", Command_Rank);
28 RegConsoleCmd("sm_top", Command_Top);
29
30 for (int i = 1; i <= MaxClients; i++)
31 {
32 OnClientConnected(i);
33 }
34}
35
36public void OnPluginEnd()
37{
38 for (int i = 1; i <= MaxClients; i++)
39 {
40 if (IsClientInGame(i))
41 {
42 OnClientDisconnect(i);
43 }
44 }
45}
46
47public void OnMapEnd()
48{
49 hDatabase.Query(Handle_FastQuery, "DELETE FROM rank_table WHERE date < CURRENT_DATE - INTERVAL 1 MONTH;");
50 hDatabase.Query(Handle_FastQuery, "DELETE a.* FROM name_table a LEFT JOIN rank_table b ON b.steamid = a.steamid WHERE b.steamid IS NULL;");
51}
52
53public void Handle_ConnectDatabase(Database db, const char[] error, any data)
54{
55 if (db)
56 {
57 hDatabase = db;
58
59 db.Query(Handle_FastQuery, "CREATE TABLE IF NOT EXISTS rank_table (steamid INT UNSIGNED, date DATE, score INT, kills INT UNSIGNED, deaths INT UNSIGNED, hs INT UNSIGNED, PRIMARY KEY (steamid, date));");
60 db.Query(Handle_FastQuery, "CREATE TABLE IF NOT EXISTS name_table (steamid INT UNSIGNED PRIMARY KEY, name VARCHAR(65) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci);");
61 }
62 else
63 {
64 SetFailState("Could not connect to the database: %s", error);
65 }
66}
67
68public void OnClientConnected(int client)
69{
70 g_Headshots[client] = 0;
71 g_Kills[client] = 0;
72 g_Deaths[client] = 0;
73 g_Score[client] = 0;
74}
75
76public void Event_PlayerTeam(Event event, const char[] name, bool dontBroadcast)
77{
78 if (event.GetInt("oldteam") == CS_TEAM_NONE)
79 {
80 int userId = event.GetInt("userid"), client = GetClientOfUserId(userId);
81
82 if (client)
83 {
84 int steam = GetSteamAccountID(client);
85
86 if (steam)
87 {
88 char query[512];
89 Format(query, sizeof(query), "SELECT r.rank, (SELECT count(steamid) from name_table) total FROM (SELECT @rank := @rank + 1 rank, s.steamid FROM (SELECT steamid, sum(score) score FROM rank_table GROUP BY steamid ORDER BY score DESC, steamid ASC) s, (SELECT @rank := 0) init) r WHERE steamid = %d;", steam);
90 hDatabase.Query(Handle_GetRankConnect, query, userId);
91 }
92 }
93 }
94}
95
96public void Handle_GetRankConnect(Database db, DBResultSet rs, const char[] error, any data)
97{
98 if (rs)
99 {
100 int client = GetClientOfUserId(view_as<int>(data));
101
102 if (client)
103 {
104 PrintToChat(client, " \x04--------------------------------");
105 PrintToChat(client, "Welcome to \x07CSGO.LALEAGANE.RO");
106
107 if (rs.FetchRow())
108 {
109 PrintToChat(client, "You are ranked \x04%d\x01 of \x04%d", rs.FetchInt(0), rs.FetchInt(1));
110 }
111 PrintToChat(client, " \x04--------------------------------");
112 }
113 }
114 else
115 {
116 LogError("Failed to query database: %s", error);
117 }
118}
119
120public void OnClientDisconnect(int client)
121{
122 int steam = GetSteamAccountID(client);
123
124 if (steam)
125 {
126 char query[512];
127
128 hDatabase.Format(query, sizeof(query), "INSERT INTO name_table (steamid, name) VALUES (%d, '%N') ON DUPLICATE KEY UPDATE name = VALUES(name);", steam, client);
129 hDatabase.Query(Handle_FastQuery, query);
130
131 Format(query, sizeof(query), "INSERT INTO rank_table (steamid, date, score, kills, deaths, hs) VALUES (%d, CURRENT_DATE, %d, %d, %d, %d) ON DUPLICATE KEY UPDATE score = score + VALUES(score), kills = kills + VALUES(kills), deaths = deaths + VALUES(deaths), hs = hs + VALUES(hs);", steam, g_Score[client], g_Kills[client], g_Deaths[client], g_Headshots[client]);
132 hDatabase.Query(Handle_FastQuery, query);
133 }
134}
135
136public void Event_PlayerDeath(Event event, const char[] name, bool dontBroadcast)
137{
138 if (GameRules_GetProp("m_bWarmupPeriod"))
139 {
140 return;
141 }
142
143 int client = GetClientOfUserId(event.GetInt("userid"));
144 int attacker = GetClientOfUserId(event.GetInt("attacker"));
145 int assister = GetClientOfUserId(event.GetInt("assister"));
146
147 if (attacker)
148 {
149 if (attacker != client)
150 {
151 if (event.GetBool("headshot"))
152 {
153 g_Headshots[attacker]++;
154 g_Score[attacker] += 2;
155 }
156
157 g_Kills[attacker]++;
158 g_Score[attacker] += 2;
159 }
160 }
161
162 if (assister)
163 {
164 if (assister != client)
165 {
166 g_Score[assister] += 1;
167 }
168 }
169
170 g_Score[client] -= 2;
171 g_Deaths[client]++;
172}
173
174public void Event_BombExploded(Event event, const char[] name, bool dontBroadcast)
175{
176 if (GetTeamClientCount(CS_TEAM_CT))
177 {
178 int client = GetClientOfUserId(event.GetInt("userid"));
179
180 if (client)
181 {
182 g_Score[client] += 2;
183 }
184 }
185}
186
187public void Event_BombDefused(Event event, const char[] name, bool dontBroadcast)
188{
189 int client = GetClientOfUserId(event.GetInt("userid"));
190
191 if (client)
192 {
193 g_Score[client] += 2;
194 }
195}
196
197public void Event_BombPlanted(Event event, const char[] name, bool dontBroadcast)
198{
199 if (GetTeamClientCount(CS_TEAM_CT))
200 {
201 int client = GetClientOfUserId(event.GetInt("userid"));
202
203 if (client)
204 {
205 g_Score[client] += 1;
206 }
207 }
208}
209
210public void Event_HostageRescued(Event event, const char[] name, bool dontBroadcast)
211{
212 if (GetTeamClientCount(CS_TEAM_T))
213 {
214 int client = GetClientOfUserId(event.GetInt("userid"));
215
216 if (client)
217 {
218 g_Score[client] += 2;
219 }
220 }
221}
222
223public void Event_HostageKilled(Event event, const char[] name, bool dontBroadcast)
224{
225 int client = GetClientOfUserId(event.GetInt("userid"));
226
227 if (client)
228 {
229 g_Score[client] -= 2;
230 }
231}
232
233public void Event_RoundMvp(Event event, const char[] name, bool dontBroadcast)
234{
235 if (GetTeamClientCount(CS_TEAM_T) && GetTeamClientCount(CS_TEAM_CT))
236 {
237 int client = GetClientOfUserId(event.GetInt("userid"));
238
239 if (client)
240 {
241 g_Score[client] += 2;
242 }
243 }
244}
245
246public Action Command_Rank(int client, int args)
247{
248 if (client)
249 {
250 int steam = GetSteamAccountID(client);
251
252 if (steam)
253 {
254 char query[512];
255 Format(query, sizeof(query), "SELECT r.rank, (SELECT count(steamid) from name_table) total, r.score, r.kills, r.deaths, r.hs FROM (SELECT @rank := @rank + 1 rank, s.* FROM (SELECT steamid, sum(score) score, sum(kills) kills, sum(deaths) deaths, sum(hs) hs FROM rank_table GROUP BY steamid ORDER BY score DESC, steamid ASC) s, (SELECT @rank := 0) init) r WHERE steamid = %d;", steam);
256 hDatabase.Query(Handle_GetRankCommand, query, GetClientUserId(client));
257 }
258 }
259
260 return Plugin_Handled;
261}
262
263public void Handle_GetRankCommand(Database db, DBResultSet rs, const char[] error, any data)
264{
265 if (rs)
266 {
267 int client = GetClientOfUserId(view_as<int>(data));
268
269 if (client)
270 {
271 if (rs.FetchRow())
272 {
273 int kills = rs.FetchInt(3);
274 int deaths = rs.FetchInt(4);
275 int headshots = rs.FetchInt(5);
276 float KPD = deaths ? 1.0 * kills / deaths : 1.0;
277 int HPK = kills ? 100 * headshots / kills : 0;
278
279 Panel panel = new Panel();
280 panel.DrawText("Rank");
281 panel.DrawText(" ");
282
283 char row[256];
284
285 Format(row, sizeof(row), "Ranked %d of %d", rs.FetchInt(0), rs.FetchInt(1));
286 panel.DrawText(row);
287
288 Format(row, sizeof(row), "%d points", rs.FetchInt(2));
289 panel.DrawText(row);
290
291 Format(row, sizeof(row), "%d:%d frags (%0.2f)", kills, deaths, KPD);
292 panel.DrawText(row);
293
294 Format(row, sizeof(row), "%d headshots (%d%c)", headshots, HPK, '%');
295 panel.DrawText(row);
296
297 panel.DrawText(" ");
298
299 panel.DrawItem("Close");
300 panel.Send(client, Panel_DoNothing, MENU_TIME_FOREVER);
301
302 delete panel;
303 }
304 }
305 }
306 else
307 {
308 LogError("Failed to query database: %s", error);
309 }
310}
311
312public Action Command_Top(int client, int args)
313{
314 if (client)
315 {
316 hDatabase.Query(Handle_GetTop, "SELECT r.rank, r.score, r.name FROM (SELECT @rank := @rank + 1 rank, s.* FROM (SELECT b.name, sum(a.score) score FROM rank_table a JOIN name_table b ON a.steamid = b.steamid GROUP BY a.steamid ORDER BY score DESC, a.steamid ASC LIMIT 10) s, (SELECT @rank := 0) init) r;", GetClientUserId(client));
317 }
318
319 return Plugin_Handled;
320}
321
322public void Handle_GetTop(Database db, DBResultSet rs, const char[] error, any data)
323{
324 if (rs)
325 {
326 int client = GetClientOfUserId(view_as<int>(data));
327
328 if (client)
329 {
330 if (rs.HasResults)
331 {
332 Panel panel = new Panel();
333
334 panel.DrawText("Top");
335 panel.DrawText(" ");
336
337 for(int i = 0; i < rs.RowCount; i++)
338 {
339 if (rs.FetchRow())
340 {
341 char name[65], row[256];
342 rs.FetchString(2, name, sizeof(name));
343 Format(row, sizeof(row), "%02d. %dp : %s", rs.FetchInt(0), rs.FetchInt(1), name);
344 panel.DrawText(row);
345 }
346 }
347
348 panel.DrawText(" ");
349 panel.DrawItem("Close");
350
351 panel.Send(client, Panel_DoNothing, MENU_TIME_FOREVER);
352 delete panel;
353 }
354 }
355 }
356 else
357 {
358 LogError("Failed to query database: %s", error);
359 }
360}
361
362public int Panel_DoNothing(Menu menu, MenuAction action, int param1, int param2)
363{
364
365}
366
367public void Handle_FastQuery(Database db, DBResultSet rs, const char[] error, any data)
368{
369 if (!rs)
370 {
371 LogError("Failed to query database: %s", error);
372 }
373}