· 6 years ago · Jul 17, 2019, 08:14 PM
1#pragma semicolon 1
2#pragma newdecls required
3
4#include <sourcemod>
5#include <sdktools>
6
7int g_iPlayTimeT[MAXPLAYERS+1] = 0;
8int g_iPlayTimeCT[MAXPLAYERS+1] = 0;
9
10bool g_bChecked[MAXPLAYERS + 1];
11bool g_bIsMySQl;
12
13char g_sSQLBuffer[3096];
14
15Handle g_hDB = INVALID_HANDLE;
16
17int g_iHours;
18int g_iMinutes;
19int g_iSeconds;
20
21public Plugin myinfo = {
22 name = "TOPTIME",
23 author = "",
24 description = "",
25 version = "",
26 url = ""
27};
28
29public void OnPluginStart()
30{
31 RegConsoleCmd("sm_toppsc", DOMenu);
32 SQL_TConnect(OnSQLConnect, "toppsc");
33}
34
35public int OnSQLConnect(Handle owner, Handle hndl, char [] error, any data)
36{
37 if(hndl == INVALID_HANDLE)
38 {
39 LogError("Database failure: %s", error);
40
41 SetFailState("Databases dont work");
42 }
43 else
44 {
45 g_hDB = hndl;
46
47 SQL_GetDriverIdent(SQL_ReadDriver(g_hDB), g_sSQLBuffer, sizeof(g_sSQLBuffer));
48 g_bIsMySQl = StrEqual(g_sSQLBuffer,"mysql", false) ? true : false;
49
50 if(g_bIsMySQl)
51 {
52 Format(g_sSQLBuffer, sizeof(g_sSQLBuffer), "CREATE TABLE IF NOT EXISTS `toppsc` (`playername` varchar(128) NOT NULL, `steamid` varchar(32) PRIMARY KEY NOT NULL,`last_accountuse` int(64) NOT NULL, `timeCT` INT( 16 ), `timeTT` INT( 16 ), `total` INT( 16 ))");
53
54 SQL_TQuery(g_hDB, OnSQLConnectCallback, g_sSQLBuffer);
55 }
56 }
57}
58
59public int OnSQLConnectCallback(Handle owner, Handle hndl, char [] error, any data)
60{
61 if(hndl == INVALID_HANDLE)
62 {
63 LogError("Query failure: %s", error);
64 return;
65 }
66
67 for(int client = 1; client <= MaxClients; client++)
68 {
69 if(IsClientInGame(client))
70 OnClientPostAdminCheck(client);
71 }
72}
73
74public void InsertSQLNewPlayer(int client)
75{
76 char query[255], steamid[32];
77 GetClientAuthId(client, AuthId_Steam2,steamid, sizeof(steamid));
78 int userid = GetClientUserId(client);
79
80 char Name[MAX_NAME_LENGTH+1];
81 char SafeName[(sizeof(Name)*2)+1];
82 if(!GetClientName(client, Name, sizeof(Name)))
83 Format(SafeName, sizeof(SafeName), "<noname>");
84 else
85 {
86 TrimString(Name);
87 SQL_EscapeString(g_hDB, Name, SafeName, sizeof(SafeName));
88 }
89
90 Format(query, sizeof(query), "INSERT INTO toppsc(playername, steamid, last_accountuse, timeCT, timeTT, total) VALUES('%s', '%s', '%d', '0', '0', '0');", SafeName, steamid, GetTime());
91 SQL_TQuery(g_hDB, SaveSQLPlayerCallback, query, userid);
92 g_iPlayTimeCT[client] = 0;
93 g_iPlayTimeT[client] = 0;
94
95 g_bChecked[client] = true;
96}
97
98public void CheckSQLSteamID(int client)
99{
100 char query[255], steamid[32];
101 GetClientAuthId(client, AuthId_Steam2,steamid, sizeof(steamid) );
102
103 Format(query, sizeof(query), "SELECT timeCT, timeTT FROM toppsc WHERE steamid = '%s'", steamid);
104 SQL_TQuery(g_hDB, CheckSQLSteamIDCallback, query, GetClientUserId(client));
105}
106
107public int CheckSQLSteamIDCallback(Handle owner, Handle hndl, char [] error, any data)
108{
109 int client;
110
111 if((client = GetClientOfUserId(data)) == 0)
112 return;
113
114 if(hndl == INVALID_HANDLE)
115 {
116 LogError("Query failure: %s", error);
117 return;
118 }
119 if(!SQL_GetRowCount(hndl) || !SQL_FetchRow(hndl))
120 {
121 InsertSQLNewPlayer(client);
122 return;
123 }
124
125 g_iPlayTimeCT[client] = SQL_FetchInt(hndl, 0);
126 g_iPlayTimeT[client] = SQL_FetchInt(hndl, 1);
127 g_bChecked[client] = true;
128}
129
130public void SaveSQLCookies(int client)
131{
132 char steamid[32];
133 GetClientAuthId(client, AuthId_Steam2,steamid, sizeof(steamid) );
134 char Name[MAX_NAME_LENGTH+1];
135 char SafeName[(sizeof(Name)*2)+1];
136 if(!GetClientName(client, Name, sizeof(Name)))
137 Format(SafeName, sizeof(SafeName), "<noname>");
138 else
139 {
140 TrimString(Name);
141 SQL_EscapeString(g_hDB, Name, SafeName, sizeof(SafeName));
142 }
143
144 char buffer[3096];
145 Format(buffer, sizeof(buffer), "UPDATE toppsc SET last_accountuse = %d, playername = '%s',timeCT = '%i',timeTT = '%i',total = '%i' WHERE steamid = '%s';",GetTime(), SafeName, g_iPlayTimeCT[client],g_iPlayTimeT[client],g_iPlayTimeCT[client]+g_iPlayTimeT[client], steamid);
146 SQL_TQuery(g_hDB, SaveSQLPlayerCallback, buffer);
147 g_bChecked[client] = false;
148}
149
150public void OnPluginEnd()
151{
152 for(int client = 1; client <= MaxClients; client++)
153 {
154 if(IsClientInGame(client))
155 {
156 OnClientDisconnect(client);
157 }
158 }
159}
160
161public void OnClientDisconnect(int client)
162{
163 if(!IsFakeClient(client) && g_bChecked[client]) SaveSQLCookies(client);
164}
165
166public void OnClientPostAdminCheck(int client)
167{
168 if(!IsFakeClient(client)) CheckSQLSteamID(client);
169}
170
171public void OnMapStart()
172{
173 CreateTimer(1.0, PlayTimeTimer, _, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
174}
175
176public Action PlayTimeTimer(Handle timer)
177{
178 for(int i = 1; i <= MaxClients; i++)
179 {
180 if(IsClientInGame(i))
181 {
182 int team = GetClientTeam(i);
183
184 if(team == 2)
185 {
186 ++g_iPlayTimeT[i];
187 }
188 else if(team == 3)
189 {
190 ++g_iPlayTimeCT[i];
191 }
192 }
193 }
194}
195
196void ShowTimer(int Time)
197{
198 g_iHours = 0;
199 g_iMinutes = 0;
200 g_iSeconds = Time;
201
202 while(g_iSeconds > 3600)
203 {
204 g_iHours++;
205 g_iSeconds -= 3600;
206 }
207 while(g_iSeconds > 60)
208 {
209 g_iMinutes++;
210 g_iSeconds -= 60;
211 }
212}
213
214public Action DOMenu(int client, int args)
215{
216 if(g_hDB != INVALID_HANDLE)
217 {
218 char buffer[200];
219 Format(buffer, sizeof(buffer), "SELECT playername, total, steamid FROM toppsc ORDER BY total DESC LIMIT 999");
220 SQL_TQuery(g_hDB, ShowTotalCallback, buffer, client);
221 }
222 else
223 PrintToChat(client, " \x03Rank System is now not avilable");
224
225 return Plugin_Handled;
226}
227
228public int ShowTotalCallback(Handle owner, Handle hndl, char [] error, any client)
229{
230 if(hndl == INVALID_HANDLE)
231 {
232 LogError(error);
233 return;
234 }
235
236 Menu menu = CreateMenu(DIDMenuHandler);
237 menu.SetTitle("TOP Time PSC Ranking");
238
239 int order = 0;
240 char number[64];
241 char name[64];
242 char textbuffer[128];
243 char steamid[128];
244
245 if(SQL_HasResultSet(hndl))
246 {
247 while (SQL_FetchRow(hndl))
248 {
249 order++;
250 Format(number,64, "option%i", order);
251 SQL_FetchString(hndl, 0, name, sizeof(name));
252 SQL_FetchString(hndl, 2, steamid, sizeof(steamid));
253 g_iHours = 0;
254 g_iMinutes = 0;
255 g_iSeconds = 0;
256 ShowTimer(SQL_FetchInt(hndl, 1));
257 Format(textbuffer,128, "%s - %d h. %d m. %d s.", name,g_iHours, g_iMinutes, g_iSeconds);
258 menu.AddItem(steamid, textbuffer);
259 }
260 }
261 if(order < 1)
262 menu.AddItem("empty", "Ranking TOP PSC jest pusty!");
263
264 menu.ExitButton = true;
265 menu.ExitBackButton = true;
266 menu.Display(client,MENU_TIME_FOREVER);
267}
268
269public int DIDMenuHandler(Menu menu, MenuAction action, int client, int itemNum)
270{
271 if(action == MenuAction_Cancel)
272 {
273 if(itemNum == MenuCancel_ExitBack)
274 DOMenu(client,0);
275 }
276 else if(action == MenuAction_End)
277 CloseHandle(menu);
278
279}
280
281public int SaveSQLPlayerCallback(Handle owner, Handle hndl, char [] error, any data)
282{
283 if(hndl == INVALID_HANDLE)
284 LogError("Query failure: %s", error);
285}