· 7 years ago · Jan 20, 2019, 07:00 PM
1#pragma semicolon 1
2#pragma newdecls required
3
4#include <sourcemod>
5#include <sdktools>
6#include <toptime>
7
8int g_iPlayTimeSpec[MAXPLAYERS+1] = 0;
9int g_iPlayTimeT[MAXPLAYERS+1] = 0;
10int g_iPlayTimeCT[MAXPLAYERS+1] = 0;
11
12bool g_bChecked[MAXPLAYERS + 1];
13
14char g_sSQLBuffer[3096];
15
16bool g_bIsMySQl;
17
18// DB handle
19Handle g_hDB = INVALID_HANDLE;
20Handle gF_OnInsertNewPlayer;
21
22int g_iHours;
23int g_iMinutes;
24int g_iSeconds;
25
26public Plugin myinfo = {
27 name = "TOPTIME",
28 author = "",
29 description = "",
30 version = "",
31 url = ""
32};
33
34public APLRes AskPluginLoad2(Handle myself, bool late, char [] error, int err_max)
35{
36 gF_OnInsertNewPlayer = CreateGlobalForward("Toptime_OnInsertNewPlayer", ET_Event, Param_Cell);
37
38 RegPluginLibrary("toptime");
39
40 return APLRes_Success;
41}
42
43public void OnPluginStart()
44{
45 RegConsoleCmd("sm_toptime", DOMenu);
46
47 SQL_TConnect(OnSQLConnect, "toptime");
48}
49
50public int OnSQLConnect(Handle owner, Handle hndl, char [] error, any data)
51{
52 if(hndl == INVALID_HANDLE)
53 {
54 LogError("Database failure: %s", error);
55
56 SetFailState("Databases dont work");
57 }
58 else
59 {
60 g_hDB = hndl;
61
62 SQL_GetDriverIdent(SQL_ReadDriver(g_hDB), g_sSQLBuffer, sizeof(g_sSQLBuffer));
63 g_bIsMySQl = StrEqual(g_sSQLBuffer,"mysql", false) ? true : false;
64
65 if(g_bIsMySQl)
66 {
67 Format(g_sSQLBuffer, sizeof(g_sSQLBuffer), "CREATE TABLE IF NOT EXISTS `toptime` (`playername` varchar(128) NOT NULL, `steamid` varchar(32) PRIMARY KEY NOT NULL,`last_accountuse` int(64) NOT NULL, `timeCT` INT( 16 ), `timeTT` INT( 16 ),`timeSPE` INT( 16 ), `total` INT( 16 ))");
68
69 SQL_TQuery(g_hDB, OnSQLConnectCallback, g_sSQLBuffer);
70 }
71 }
72}
73
74public int OnSQLConnectCallback(Handle owner, Handle hndl, char [] error, any data)
75{
76 if(hndl == INVALID_HANDLE)
77 {
78 LogError("Query failure: %s", error);
79 return;
80 }
81
82 for(int client = 1; client <= MaxClients; client++)
83 {
84 if(IsClientInGame(client))
85 {
86 OnClientPostAdminCheck(client);
87 }
88 }
89}
90
91public void InsertSQLNewPlayer(int client)
92{
93 char query[255], steamid[32];
94 GetClientAuthId(client, AuthId_Steam2,steamid, sizeof(steamid));
95 int userid = GetClientUserId(client);
96
97 char Name[MAX_NAME_LENGTH+1];
98 char SafeName[(sizeof(Name)*2)+1];
99 if(!GetClientName(client, Name, sizeof(Name)))
100 Format(SafeName, sizeof(SafeName), "<noname>");
101 else
102 {
103 TrimString(Name);
104 SQL_EscapeString(g_hDB, Name, SafeName, sizeof(SafeName));
105 }
106
107 Format(query, sizeof(query), "INSERT INTO toptime(playername, steamid, last_accountuse, timeCT, timeTT, timeSPE, total) VALUES('%s', '%s', '%d', '0', '0', '0', '0');", SafeName, steamid, GetTime());
108 SQL_TQuery(g_hDB, SaveSQLPlayerCallback, query, userid);
109 g_iPlayTimeCT[client] = 0;
110 g_iPlayTimeT[client] = 0;
111 g_iPlayTimeSpec[client] = 0;
112
113 Call_StartForward(gF_OnInsertNewPlayer);
114 Call_PushCell(client);
115 Call_Finish();
116
117 g_bChecked[client] = true;
118}
119
120public void CheckSQLSteamID(int client)
121{
122 char query[255], steamid[32];
123 GetClientAuthId(client, AuthId_Steam2,steamid, sizeof(steamid) );
124
125 Format(query, sizeof(query), "SELECT timeCT, timeTT, timeSPE FROM toptime WHERE steamid = '%s'", steamid);
126 SQL_TQuery(g_hDB, CheckSQLSteamIDCallback, query, GetClientUserId(client));
127}
128
129public int CheckSQLSteamIDCallback(Handle owner, Handle hndl, char [] error, any data)
130{
131 int client;
132
133 /* Make sure the client didn't disconnect while the thread was running */
134
135 if((client = GetClientOfUserId(data)) == 0)
136 {
137 return;
138 }
139
140 if(hndl == INVALID_HANDLE)
141 {
142 LogError("Query failure: %s", error);
143 return;
144 }
145 if(!SQL_GetRowCount(hndl) || !SQL_FetchRow(hndl))
146 {
147 InsertSQLNewPlayer(client);
148 return;
149 }
150
151 g_iPlayTimeCT[client] = SQL_FetchInt(hndl, 0);
152 g_iPlayTimeT[client] = SQL_FetchInt(hndl, 1);
153 g_iPlayTimeSpec[client] = SQL_FetchInt(hndl, 2);
154 g_bChecked[client] = true;
155}
156
157public void SaveSQLCookies(int client)
158{
159 char steamid[32];
160 GetClientAuthId(client, AuthId_Steam2,steamid, sizeof(steamid) );
161 char Name[MAX_NAME_LENGTH+1];
162 char SafeName[(sizeof(Name)*2)+1];
163 if(!GetClientName(client, Name, sizeof(Name)))
164 Format(SafeName, sizeof(SafeName), "<noname>");
165 else
166 {
167 TrimString(Name);
168 SQL_EscapeString(g_hDB, Name, SafeName, sizeof(SafeName));
169 }
170
171 char buffer[3096];
172 Format(buffer, sizeof(buffer), "UPDATE toptime SET last_accountuse = %d, playername = '%s',timeCT = '%i',timeTT = '%i', timeSPE = '%i',total = '%i' WHERE steamid = '%s';",GetTime(), SafeName, g_iPlayTimeCT[client],g_iPlayTimeT[client],g_iPlayTimeSpec[client],g_iPlayTimeCT[client]+g_iPlayTimeT[client]+g_iPlayTimeSpec[client], steamid);
173 SQL_TQuery(g_hDB, SaveSQLPlayerCallback, buffer);
174 g_bChecked[client] = false;
175}
176
177public void OnPluginEnd()
178{
179 for(int client = 1; client <= MaxClients; client++)
180 {
181 if(IsClientInGame(client))
182 {
183 OnClientDisconnect(client);
184 }
185 }
186}
187
188public void OnClientDisconnect(int client)
189{
190 if(!IsFakeClient(client) && g_bChecked[client]) SaveSQLCookies(client);
191}
192
193public void OnClientPostAdminCheck(int client)
194{
195 if(!IsFakeClient(client)) CheckSQLSteamID(client);
196}
197
198public void OnMapStart()
199{
200 CreateTimer(1.0, PlayTimeTimer, _, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
201}
202
203public Action PlayTimeTimer(Handle timer)
204{
205 for(int i = 1; i <= MaxClients; i++)
206 {
207 if(IsClientInGame(i))
208 {
209 int team = GetClientTeam(i);
210
211 if(team == 2)
212 {
213 ++g_iPlayTimeT[i];
214 }
215 else if(team == 3)
216 {
217 ++g_iPlayTimeCT[i];
218 }
219 else
220 {
221 ++g_iPlayTimeSpec[i];
222 }
223 }
224 }
225}
226
227public void ShowTotal(int client)
228{
229 if(g_hDB != INVALID_HANDLE)
230 {
231 char buffer[200];
232 Format(buffer, sizeof(buffer), "SELECT playername, total, steamid FROM toptime ORDER BY total DESC LIMIT 999");
233 SQL_TQuery(g_hDB, ShowTotalCallback, buffer, client);
234 }
235 else
236 {
237 PrintToChat(client, " \x03Rank System is now not avilable");
238 }
239}
240
241public int ShowTotalCallback(Handle owner, Handle hndl, char [] error, any client)
242{
243 if(hndl == INVALID_HANDLE)
244 {
245 LogError(error);
246 PrintToServer("Last Connect SQL Error: %s", error);
247 return;
248 }
249
250 Menu menu2 = CreateMenu(DIDMenuHandler2);
251 menu2.SetTitle("Top Total played");
252
253 int order = 0;
254 char number[64];
255 char name[64];
256 char textbuffer[128];
257 char steamid[128];
258
259 if(SQL_HasResultSet(hndl))
260 {
261 while (SQL_FetchRow(hndl))
262 {
263 order++;
264 Format(number,64, "option%i", order);
265 SQL_FetchString(hndl, 0, name, sizeof(name));
266 SQL_FetchString(hndl, 2, steamid, sizeof(steamid));
267 g_iHours = 0;
268 g_iMinutes = 0;
269 g_iSeconds = 0;
270 ShowTimer2(SQL_FetchInt(hndl, 1));
271 Format(textbuffer,128, "n%i %s - %d h. %d m. %d s.", order,name,g_iHours, g_iMinutes, g_iSeconds);
272 menu2.AddItem(steamid, textbuffer);
273 }
274 }
275 if(order < 1)
276 {
277 menu2.AddItem("empty", "TopTime jest pusty!");
278 }
279
280 menu2.ExitButton = true;
281 menu2.ExitBackButton = true;
282 menu2.Display(client,MENU_TIME_FOREVER);
283}
284
285int ShowTimer(int Time, char[] buffer,int sizef)
286{
287 g_iHours = 0;
288 g_iMinutes = 0;
289 g_iSeconds = Time;
290
291 while(g_iSeconds > 3600)
292 {
293 g_iHours++;
294 g_iSeconds -= 3600;
295 }
296 while(g_iSeconds > 60)
297 {
298 g_iMinutes++;
299 g_iSeconds -= 60;
300 }
301 if(g_iHours >= 1)
302 {
303 Format(buffer, sizef, "%d h %d m %d s", g_iHours, g_iMinutes, g_iSeconds );
304 }
305 else if(g_iMinutes >= 1)
306 {
307 Format(buffer, sizef, "%d m %d s", g_iMinutes, g_iSeconds );
308 }
309 else
310 {
311 Format(buffer, sizef, "%d s", g_iSeconds );
312 }
313}
314
315void ShowTimer2(int Time)
316{
317 g_iHours = 0;
318 g_iMinutes = 0;
319 g_iSeconds = Time;
320
321 while(g_iSeconds > 3600)
322 {
323 g_iHours++;
324 g_iSeconds -= 3600;
325 }
326 while(g_iSeconds > 60)
327 {
328 g_iMinutes++;
329 g_iSeconds -= 60;
330 }
331}
332
333bool GetCommunityID(char [] AuthID, char [] FriendID, int size)
334{
335 if(strlen(AuthID) < 11 || AuthID[0]!='S' || AuthID[6]=='I')
336 {
337 FriendID[0] = 0;
338 return false;
339 }
340 int iUpper = 765611979;
341 int iFriendID = StringToInt(AuthID[10])*2 + 60265728 + AuthID[8]-48;
342 int iDiv = iFriendID/100000000;
343 int iIdx = 9-(iDiv?iDiv/10+1:0);
344 iUpper += iDiv;
345 IntToString(iFriendID, FriendID[iIdx], size-iIdx);
346 iIdx = FriendID[9];
347 IntToString(iUpper, FriendID, size);
348 FriendID[9] = iIdx;
349 return true;
350}
351
352public int DIDMenuHandler2(Menu menu2, MenuAction action, int client, int itemNum)
353{
354 if( action == MenuAction_Select )
355 {
356 char info[128], community[128];
357
358 GetMenuItem(menu2, itemNum, info, sizeof(info));
359 GetCommunityID(info, community, sizeof(community));
360
361 Format(community, sizeof(community), "http://steamcommunity.com/profiles/%s", community);
362 PrintToChat(client, community);
363 PrintToConsole(client, community);
364 }
365 else if(action == MenuAction_Cancel)
366 {
367 if(itemNum==MenuCancel_ExitBack)
368 {
369 DOMenu(client,0);
370 }
371 //PrintToServer("Client %d's menu was cancelled.Reason: %d", client, itemNum);
372 }
373 else if(action == MenuAction_End)
374 {
375 CloseHandle(menu2);
376 }
377}
378
379public Action DOMenu(int client, int args)
380{
381 //PrintToChat(client, "number de arg %i", args);
382 if(args > 0)
383 {
384 char steamid[64];
385 GetCmdArgString(steamid, sizeof(steamid));
386
387 char buffer[200];
388 Format(buffer, sizeof(buffer), "SELECT timeCT, timeTT, timeSPE, total, playername FROM toptime WHERE steamid = '%s'", steamid);
389 SQL_TQuery(g_hDB, SQLShowPlayTime, buffer, GetClientUserId(client));
390 }
391 else
392 ShowTotal(client);
393 return Plugin_Handled;
394}
395
396public int SQLShowPlayTime(Handle owner, Handle hndl, char [] error, any data)
397{
398 int client;
399
400 /* Make sure the client didn't disconnect while the thread was running */
401 if((client = GetClientOfUserId(data)) == 0)
402 {
403 return;
404 }
405
406 if(hndl == INVALID_HANDLE)
407 {
408 LogError("Query failure: %s", error);
409 return;
410 }
411 if(!SQL_GetRowCount(hndl) || !SQL_FetchRow(hndl))
412 {
413 PrintToChat(client, " \x03steamid not found in the database");
414 return;
415 }
416 char name[124];
417 SQL_FetchString(hndl, 4, name, 124);
418
419 Menu menu = CreateMenu(DIDMenuHandlerHandler);
420 menu.SetTitle("Czas gracza %s", name);
421
422 char buffer[124];
423
424 ShowTimer(SQL_FetchInt(hndl, 2), buffer, sizeof(buffer));
425 Format(buffer, 124, "Obserwator: %s", buffer);
426 menu.AddItem("", buffer, ITEMDRAW_DISABLED);
427
428 ShowTimer(SQL_FetchInt(hndl, 1), buffer, sizeof(buffer));
429 Format(buffer, 124, "Terrorysta: %s", buffer);
430 menu.AddItem("", buffer, ITEMDRAW_DISABLED);
431
432 ShowTimer(SQL_FetchInt(hndl, 0), buffer, sizeof(buffer));
433 Format(buffer, 124, "Anty-Terrorysta: %s", buffer);
434 menu.AddItem("", buffer, ITEMDRAW_DISABLED);
435
436 ShowTimer(SQL_FetchInt(hndl, 3), buffer, sizeof(buffer));
437 Format(buffer, 124, "ÅÄ…czny czas: %s", buffer);
438 menu.AddItem("", buffer, ITEMDRAW_DISABLED);
439
440 menu.ExitButton = true;
441 menu.ExitBackButton = true;
442 menu.Display(client,MENU_TIME_FOREVER);
443}
444
445public int SaveSQLPlayerCallback(Handle owner, Handle hndl, char [] error, any data)
446{
447 if(hndl == INVALID_HANDLE)
448 {
449 LogError("Query failure: %s", error);
450 }
451}
452
453public int DIDMenuHandlerHandler(Menu menu, MenuAction action, int client, int itemNum)
454{
455 if(action == MenuAction_Cancel)
456 {
457 if(itemNum==MenuCancel_ExitBack)
458 {
459 DOMenu(client,0);
460 }
461 }
462 else if(action == MenuAction_End)
463 {
464 CloseHandle(menu);
465 }
466}