· 8 years ago · Jul 26, 2018, 06:52 PM
1#include <sourcemod>
2
3#pragma semicolon 1
4#pragma newdecls required
5
6Database g_hDatabase = null;
7
8int g_iKills[MAXPLAYERS + 1];
9int g_iDeaths[MAXPLAYERS + 1];
10int g_iAssists[MAXPLAYERS + 1];
11int g_iHeadshots[MAXPLAYERS + 1];
12
13char g_sSteamid[MAXPLAYERS + 1][32];
14
15public void OnPluginStart()
16{
17 HookEvent("player_death", Event_Death);
18
19 Database.Connect(SQL_Connection, "test");
20}
21
22public Action Event_Death(Event hEvent, const char[] sName, bool bDontBroadcast)
23{
24 int iAttacker = GetClientOfUserId(hEvent.GetInt("attacker"));
25 int iVictim = GetClientOfUserId(hEvent.GetInt("userid"));
26
27 if (iAttacker != iVictim)
28 {
29 int iAssister = GetClientOfUserId(hEvent.GetInt("assister"));
30
31 g_iKills[iAttacker]++;
32 g_iDeaths[iVictim]++;
33 g_iAssists[iAssister]++;
34
35 if (hEvent.GetBool("headshot"))
36 g_iHeadshots[iAttacker]++;
37
38 PrintToChat(iAttacker, "You've got %d kills, %d deaths, %d assists and %d headshots.", g_iKills[iAttacker], g_iDeaths[iAttacker], g_iAssists[iAttacker], g_iHeadshots[iAttacker]);
39 }
40}
41
42public void SQL_Connection(Database hDatabase, const char[] sError, any iData)
43{
44 if (hDatabase == null)
45 SetFailState(sError);
46
47 else
48 {
49 g_hDatabase = hDatabase;
50
51 g_hDatabase.Query(SQL_Error, "CREATE TABLE IF NOT EXISTS `players` (`id` int(4) NOT NULL AUTO_INCREMENT, `steam_id` VARCHAR(32) NOT NULL PRIMARY KEY, `kills` int(20) NOT NULL, `deaths` int(20) NOT NULL, `assists` int(20) NOT NULL, `headshots` int(20) NOT NULL, PRIMARY KEY (`id`))");
52 }
53}
54
55public void SQL_Error(Database hDatabase, DBResultSet hResults, const char[] sError, any iData)
56{
57 if (hResults == null)
58 ThrowError(sError);
59}
60
61public void OnClientAuthorized(int iClient)
62{
63 if (IsValidClient(iClient))
64 {
65 GetClientAuthId(iClient, AuthId_Steam2, g_sSteamid[iClient], sizeof(g_sSteamid[]));
66
67 char [] sQuery = new char[256];
68 Format(sQuery, 256, "SELECT * FROM `players` WHERE `steam_id` = \"%s\"", g_sSteamid[iClient]);
69
70 g_hDatabase.Query(SQL_Authorization, sQuery, GetClientUserId(iClient));
71 }
72}
73
74public void SQL_Authorization(Database hDatabase, DBResultSet hResults, const char[] sError, any iData)
75{
76 if (hResults == null)
77 ThrowError(sError);
78
79 int iClient = GetClientOfUserId(iData);
80
81 if (IsValidClient(iClient))
82 {
83 char [] sQuery = new char[256];
84
85 if (hResults.RowCount == 1)
86 {
87 hResults.FetchRow();
88
89 g_iKills[iClient] = hResults.FetchInt(2);
90 g_iDeaths[iClient] = hResults.FetchInt(3);
91 g_iAssists[iClient] = hResults.FetchInt(4);
92 g_iHeadshots[iClient] = hResults.FetchInt(5);
93 }
94
95 else
96 {
97 if (hResults.RowCount > 1)
98 {
99 g_hDatabase.Query(SQL_Error, "delete players from players inner join (select min(id) minid, steam_id from players group by steam_id having count(1) > 1) as duplicates on (duplicates.steam_id = players.steam_id and duplicates.minid <> players.id)");
100
101 KickClient(iClient, "Authentication error. Please, reconnect.");
102 }
103
104 else
105 {
106 Format(sQuery, 256, "INSERT INTO `players` (`steam_id`, `kills`, `deaths`, `assists`, `headshots`) VALUES (\"%s\", '%d', '%d', '%d', '%d')", g_sSteamid[iClient], g_iKills[iClient], g_iDeaths[iClient], g_iAssists[iClient], g_iHeadshots[iClient]);
107
108 g_hDatabase.Query(SQL_Error, sQuery);
109 }
110 }
111 }
112}
113
114public void OnClientDisconnect(int iClient)
115{
116 if (IsValidClient(iClient))
117 {
118 char [] sQuery = new char[256];
119 Format(sQuery, 256, "UPDATE `players` SET `kills` = '%d', `deaths` = '%d', `assists` = '%d', `headshots` = '%d' WHERE `steam_id` = \"%s\"", g_iKills[iClient], g_iDeaths[iClient], g_iAssists[iClient], g_iHeadshots[iClient], g_sSteamid[iClient]);
120
121 g_hDatabase.Query(SQL_Error, sQuery);
122 }
123}
124
125stock bool IsValidClient(int iClient)
126{
127 if (!(0 < iClient <= MaxClients) || !IsClientInGame(iClient) || IsFakeClient(iClient))
128 return false;
129
130 return true;
131}