· 8 years ago · Jul 26, 2018, 03:14 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 SQL_Update(iAttacker);
41 SQL_Update(iVictim);
42 SQL_Update(iAssister);
43 }
44}
45
46public void SQL_Connection(Database hDatabase, const char[] sError, any iData)
47{
48 if (hDatabase == null)
49 SetFailState(sError);
50
51 else
52 {
53 g_hDatabase = hDatabase;
54
55 g_hDatabase.Query(SQL_Error, "CREATE TABLE IF NOT EXISTS `players` (`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)");
56 }
57}
58
59public void SQL_Error(Database hDatabase, DBResultSet hResults, const char[] sError, any iData)
60{
61 if (hResults == null)
62 ThrowError(sError);
63}
64
65public void OnClientAuthorized(int iClient)
66{
67 if (IsValidClient(iClient))
68 {
69 GetClientAuthId(iClient, AuthId_Steam2, g_sSteamid[iClient], sizeof(g_sSteamid[]));
70
71 char[] sQuery = new char[256];
72 Format(sQuery, 256, "SELECT * FROM `players` WHERE `steam_id` = '%s'", g_sSteamid[iClient]);
73
74 g_hDatabase.Query(SQL_Authorization, sQuery, GetClientUserId(iClient));
75 }
76}
77
78public void SQL_Authorization(Database hDatabase, DBResultSet hResults, const char[] sError, any iData)
79{
80 if (hResults == null)
81 ThrowError(sError);
82
83 int iClient = GetClientOfUserId(iData);
84
85 if (IsValidClient(iClient))
86 {
87 if (hResults.RowCount == 1)
88 {
89 hResults.FetchRow();
90
91 g_iKills[iClient] = hResults.FetchInt(2);
92 g_iDeaths[iClient] = hResults.FetchInt(3);
93 g_iAssists[iClient] = hResults.FetchInt(4);
94 g_iHeadshots[iClient] = hResults.FetchInt(5);
95 }
96
97 else
98 {
99 char[] sQuery = new char[256];
100 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]);
101
102 g_hDatabase.Query(SQL_Error, sQuery);
103 }
104 }
105}
106
107public void SQL_Update(int iClient)
108{
109 if (IsValidClient(iClient))
110 {
111 char[] sQuery = new char[256];
112 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]);
113
114 g_hDatabase.Query(SQL_Error, sQuery);
115 }
116}
117
118stock bool IsValidClient(int iClient)
119{
120 if (!(0 < iClient <= MaxClients) || !IsClientInGame(iClient) || IsFakeClient(iClient))
121 return false;
122
123 return true;
124}