· 5 years ago · Nov 05, 2020, 04:26 PM
1#include <sourcemod>
2#include <cstrike>
3#include <clients>
4
5//new String:file[100]="jail_stats";
6new String:steamid[MAXPLAYERS+1][64];
7new String:nick[MAXPLAYERS+1][64];
8enum PlayerData{
9CT_KILLS = 0,
10TT_KILLS,
11ROUNDS,
12TIME
13};
14
15new g_PlayerData[ MAXPLAYERS+1 ][ PlayerData ];
16new Handle:db = INVALID_HANDLE;
17
18
19public OnPluginStart()
20{
21 HookEvent("player_death", Event_PlayerDeath,EventHookMode_Post); // kille w ct/tt
22 HookEvent("round_start", Round_Start,EventHookMode_Post); // zagrane rundy
23 //RegAdminCmd("sm_stats", statsname, _, "");
24 RegConsoleCmd("sm_stats", statsname, "Uzycie sm_stats Nick/Steamid");
25 RegAdminCmd("sm_resetstats", ResetKills, ADMFLAG_ROOT, "Resets all kills");
26 InitializeDB()
27}
28public InitializeDB()
29{
30 new String:error[255];
31 db = SQL_ConnectEx(SQL_GetDriver("sqlite"), "", "", "", "jail_stats", error, sizeof(error), true, 0);
32 if(db == INVALID_HANDLE)
33 {
34 SetFailState(error);
35 }
36 SQL_LockDatabase(db);
37 SQL_FastQuery(db, "CREATE TABLE IF NOT EXISTS jail_stats (steamid VARCHAR (64) UNIQUE, name VARCHAR (64), kill_ct INTEGER (10) DEFAULT (0), kill_tt INTEGER (10) DEFAULT (0), played_rounds INTEGER (10) DEFAULT (0), played_time INTEGER (10) DEFAULT (0), id INTEGER PRIMARY KEY AUTOINCREMENT);");
38 SQL_UnlockDatabase(db);
39}
40
41//////////////////////
42public OnClientPutInServer(client)//OnClientConnected(int client)
43{
44 if(IsFakeClient(client))
45 {
46 return;
47 }
48 g_PlayerData[client][CT_KILLS]=0;
49 g_PlayerData[client][TT_KILLS]=0;
50 g_PlayerData[client][ROUNDS]=0;
51 g_PlayerData[client][TIME]=0;
52 GetClientAuthString(client, steamid[client], sizeof(steamid));
53 GetClientName(client,nick[client],sizeof(nick));
54 UpdateHistory(client);
55 return;
56}
57public OnClientDisconnect(int client)
58{
59 if(IsFakeClient(client))
60 {
61 return;
62 }
63 decl String:buffer[350];
64 Format(buffer, sizeof(buffer), "UPDATE `jail_stats` SET \
65 `played_time` = `played_time`+'%d', `played_rounds` = `played_rounds`+'%d' WHERE `jail_stats`.`steamid` = '%s';\
66 ",RoundToFloor(GetClientTime(client)), g_PlayerData[client][ROUNDS],steamid[client]);
67 SQL_TQuery(db, SQLErrorCheckCallback, buffer);
68 return;
69}
70
71////// EVENTY //////////
72
73public Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
74{
75 /*new victimId = GetEventInt(event, "userid");
76 new victim = GetClientOfUserId(victimId);
77 new victim_team=GetClientTeam(victim);*/
78 new attackerId = GetEventInt(event, "attacker");
79 new attacker = GetClientOfUserId(attackerId);
80 if(IsValidClient(attacker))
81 {
82 new attacker_team=GetClientTeam(attacker);
83 new client=attacker;
84 decl String:buffer[350];
85 switch(attacker_team)
86 {
87 case 3:{
88 Format(buffer, sizeof(buffer), "UPDATE `jail_stats` SET \
89 `kill_ct` = `kill_ct`+'1' WHERE `jail_stats`.`steamid` = '%s';\
90 ",steamid[client]);
91 }//ct
92 case 2:{
93 Format(buffer, sizeof(buffer), "UPDATE `jail_stats` SET \
94 `kill_tt` = `kill_tt`+'1' WHERE `jail_stats`.`steamid` = '%s';\
95 ",steamid[client]);
96 }//tt
97 }
98 SQL_TQuery(db, SQLErrorCheckCallback, buffer);
99 }
100}
101public Action:Round_Start(Handle:event, const String:name[], bool:dB)
102{
103 for(new i=0;i<=MAXPLAYERS;i++)
104 {
105 if(IsValidClient(i))
106 {
107 new team=GetClientTeam(i);
108 if(team==2 || team==3)
109 {
110 g_PlayerData[i][ROUNDS]++;
111 }
112 }
113 }
114}
115
116public UpdateHistory(client)
117{
118 decl String:buffer[550];
119 GetClientAuthString(client, steamid[client], sizeof(steamid));
120 //if(IsValidClient(client))
121 {
122 Format(buffer, sizeof(buffer), "INSERT OR IGNORE INTO `jail_stats` (`steamid`, `name`, `kill_ct`, `kill_tt`, `played_rounds`, `played_time`) VALUES ('%s', '%s', '%d', '%d', '%d', '%d');", steamid[client], nick[client], g_PlayerData[client][CT_KILLS],g_PlayerData[client][TT_KILLS],g_PlayerData[client][ROUNDS],g_PlayerData[client][TIME]);
123 SQL_TQuery(db, SQLErrorCheckCallback, buffer);
124 }
125}
126
127public SQLErrorCheckCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
128{
129 PrintToChatAll(error);
130 if(!StrEqual("", error))
131 {
132 PrintToServer("Jail Stats SQL Error: %s", error);
133 }
134}
135
136
137public Action:statsname(client,args)
138{
139 decl String:full[256];
140 decl String:buffer[550];
141 //GetCmdArgString(full, sizeof(full));
142 GetCmdArg(1, full, sizeof(full));
143 if(strlen(full)<=1)
144 {
145 PrintToChat(client," \x04[Jail Stats]\x01 Podales zbyt krotka nazwe/steamid conajmniej\x04 1\x01znak.");
146 return Plugin_Handled;
147 }
148 if(StrContains(full, "STEAM_")==-1)
149 {
150 ReplaceString(full, sizeof(full), "'", "");
151 ReplaceString(full, sizeof(full), "\"", "");
152 ReplaceString(full, sizeof(full), "`", "");
153 Format(buffer,sizeof(buffer),"SELECT * FROM `jail_stats` WHERE `name` LIKE '%s%s%s' LIMIT 1","%",full,"%");
154 }
155 else
156 {
157 ReplaceString(full, sizeof(full), "'", "");
158 ReplaceString(full, sizeof(full), "\"", "");
159 ReplaceString(full, sizeof(full), "`", "");
160 Format(buffer,sizeof(buffer),"SELECT * FROM `jail_stats` WHERE `steamid` LIKE '%s%s%s' LIMIT 1","%",full,"%");
161 }
162 //PrintToConsole(client,"%s",buffer);
163 //PrintToConsole(client,"%s",full);
164 SQL_TQuery(db, StatsSearchCallback, buffer, client);
165 return Plugin_Handled;
166}
167
168public StatsSearchCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
169{
170 if(!IsValidClient(data))
171 {
172 return;
173 }
174 new client=data;
175 if(hndl == INVALID_HANDLE)
176 {
177 ThrowError("stats search SQL error: %s", error);
178 return;
179 }
180 decl String:Name[64], String:Steam[64];
181
182 if(SQL_FetchRow(hndl))
183 {
184 SQL_FetchString(hndl, 0, Steam, sizeof(Steam));
185 SQL_FetchString(hndl, 1, Name, sizeof(Name));
186 new czas=SQL_FetchInt(hndl, 5);
187 decl String:buffer[200],String:text_time[70];
188 SecondsToTime(czas, text_time);
189 Format(buffer,sizeof(buffer)," \x04[Przegrany Czas:\x01 %s",text_time);
190 PrintToChat(client," \x04[Jail Stats]");
191 PrintToChat(client," \x04[Gracz:\x01 %s \x04|\x01 STEAMID:\x01 %s\x04]",Name,Steam);
192 PrintToChat(client," \x04[Przegrane Rundy:\x01 %d\x04]",SQL_FetchInt(hndl, 4));
193 PrintToChat(client," \x04[Kille w CT:\x01 %d\x04]",SQL_FetchInt(hndl, 2));
194 PrintToChat(client," \x04[Kille w TT:\x01 %d\x04]",SQL_FetchInt(hndl, 3));
195 PrintToChat(client,"%s", buffer);
196 PrintToChat(client," \x04[Jail Stats]");
197
198 }
199 else
200 {
201 PrintToChat(client,"\x04 [Jail Stats]\01 Nie znaleziono takiego gracza..");
202 }
203}
204
205SecondsToTime(seconds, String:buffer[70])
206{
207 new days, hour, mins, secs;
208 Format(buffer,sizeof(buffer),"");
209 if( seconds >= 86400 )
210 {
211 days = RoundToFloor(float(seconds / 86400));
212 seconds = seconds % 86400;
213 }
214 if( seconds >= 3600)
215 {
216 hour = RoundToFloor(float(seconds / 3600));
217 seconds = seconds % 3600;
218 }
219 if( seconds >= 60)
220 {
221 mins = RoundToFloor(float(seconds / 60));
222 seconds = seconds % 60;
223 }
224 secs = RoundToFloor(float(seconds));
225
226 if( days )
227 Format(buffer, 70, "%s\x01%d\x04 dni, ", buffer, days);
228
229 if( hour )
230 Format(buffer, 70, "%s\x01%d\x04 godzin, ", buffer, hour);
231
232 Format(buffer, 70, "%s\x01%d\x04 minut, ", buffer, mins);
233 Format(buffer, 70, "%s\x01%d\x04 sekund", buffer, secs);
234}
235
236public Action:ResetKills(int client,args)
237{
238 if(!(GetUserFlagBits(client) & ADMFLAG_ROOT))
239 {
240 return Plugin_Handled;
241 }
242 decl String:buffer[350];
243 Format(buffer, sizeof(buffer), "UPDATE `jail_stats` SET `kill_ct` = '0', `kill_tt` = '0' WHERE 1;");
244 SQL_TQuery(db, SQLErrorCheckCallback, buffer);
245 return Plugin_Handled;
246}
247
248stock bool:IsValidClient(client, bool:alive = false)
249{
250 if(client >= 1 && client <= MaxClients && IsClientConnected(client) && IsClientInGame(client) && (alive == false || IsPlayerAlive(client)))
251 {
252 return true;
253 }
254
255 return false;
256}