· 8 years ago · Dec 11, 2017, 07:52 PM
1#include <hattrick_csgo>
2
3public Plugin myinfo =
4{
5 name = "NewStats",
6 author = "CS KING",
7 description = "Statistics And Team Transfer Commands",
8 version = "2.0",
9 url = "www.pglarena.ro"
10};
11
12Handle g_pDb = INVALID_HANDLE;
13Handle g_pTimer[128 + 1] = { INVALID_HANDLE, ... };
14
15bool g_InGame[128 + 1] = { false, ... };
16
17char g_Steam[128 + 1][PLATFORM_MAX_PATH];
18char g_TimeStr[128 + 1][PLATFORM_MAX_PATH];
19
20int g_Time[128 + 1] = { 0, ... };
21
22char g_Buffer[PLATFORM_MAX_PATH * 8];
23
24public APLRes AskPluginLoad2(Handle pMyself, bool bLate, char[] Error, int maxLen)
25{
26 CreateNative("GetClientTotalMinutes", __GetClientTotalMinutes);
27
28 RegPluginLibrary("NewStats");
29
30 return APLRes_Success;
31}
32
33public int __GetClientTotalMinutes(Handle pPlugin, int Params)
34{
35 static int Plr;
36
37 Plr = GetNativeCell(1);
38
39 if (Plr > 0)
40 {
41 return g_Time[Plr];
42 }
43
44 return 0;
45}
46
47public void OnMapEnd()
48{
49 StopBackupingRounds();
50}
51
52public void OnMySQLThreadedConnection(Handle pOwner, Handle pChild, const char[] Error, any Data)
53{
54 g_pDb = pChild;
55
56 if (strlen(Error) > 0)
57 {
58 LogError("SQL_TConnect() @ OnMySQLThreadedConnection() reported an error!");
59 LogError(Error);
60 }
61
62 else
63 {
64 SQL_SetCharset(g_pDb, "utf8");
65
66 SQL_TQuery(g_pDb, EmptyFunction, "CREATE TABLE IF NOT EXISTS PlayersTime (Steam VARCHAR(128) UNIQUE NOT NULL default '', Time INT(11) NOT NULL default 0, timeString VARCHAR(256) NOT NULL default '', PRIMARY KEY(Steam));");
67 SQL_TQuery(g_pDb, EmptyFunction, "CREATE TABLE IF NOT EXISTS Maps (Name VARCHAR(128) NOT NULL default '', Stamp INT(11) NOT NULL default 0);");
68 }
69}
70
71public void OnPluginStart()
72{
73 SQL_TConnect(OnMySQLThreadedConnection, "local");
74
75 RegConsoleCmd("say", CommandSay);
76 RegConsoleCmd("say_team", CommandSay);
77
78 StopBackupingRounds();
79}
80
81void StopBackupingRounds()
82{
83 ServerCommand("mp_backup_round_auto \"\";");
84 ServerCommand("mp_backup_round_file \"\";");
85 ServerCommand("mp_backup_round_file_last \"\";");
86 ServerCommand("mp_backup_round_file_pattern \"\";");
87}
88
89public Action OnMapStart_Emulator(Handle pTimer, any Data)
90{
91 OnMapStart();
92}
93
94public void OnMapStart()
95{
96 static char Map[PLATFORM_MAX_PATH];
97
98 GetCurrentMap(Map, sizeof(Map));
99
100 StopBackupingRounds();
101
102 if (g_pDb == INVALID_HANDLE)
103 {
104 CreateTimer(0.1, OnMapStart_Emulator, _, TIMER_FLAG_NO_MAPCHANGE);
105 }
106
107 else
108 {
109 FormatEx(g_Buffer, sizeof(g_Buffer), "INSERT INTO Maps VALUES ('%s', %d);", Map, GetTime());
110 SQL_TQuery(g_pDb, EmptyFunction, g_Buffer);
111 }
112}
113
114public Action CommandSay(int Client, int Args)
115{
116 static char Raw[PLATFORM_MAX_PATH], Command[PLATFORM_MAX_PATH];
117
118 if (Client > 0 && g_InGame[Client])
119 {
120 GetCmdArgString(Raw, sizeof(Raw));
121
122 if (hCSGO_PureChatCommand(Raw, Command, sizeof(Command)) >= 2)
123 {
124 if (!strcmp(Command, "Maps", false) || !strcmp(Command[1], "Maps", false) || \
125 !strcmp(Command, "Harti", false) || !strcmp(Command[1], "Harti", false))
126 {
127 if (g_pDb != INVALID_HANDLE)
128 {
129 SQL_TQuery(g_pDb, ShowMaps, "SELECT Name FROM Maps ORDER BY Stamp DESC LIMIT 4;", Client);
130 }
131 }
132
133 else if (!strcmp(Command, "RS", false) || !strcmp(Command[1], "RS", false) || \
134 !strcmp(Command, "Reset", false) || !strcmp(Command[1], "Reset", false) || \
135 !strcmp(Command, "ResetScore", false) || !strcmp(Command[1], "ResetScore", false) || \
136 !strcmp(Command, "Restart", false) || !strcmp(Command[1], "Restart", false) || \
137 !strcmp(Command, "RestartScore", false) || !strcmp(Command[1], "RestartScore", false))
138 {
139 SetEntProp(Client, Prop_Data, "m_iFrags", 0);
140 SetEntProp(Client, Prop_Data, "m_iDeaths", 0);
141
142 SetEntProp(Client, Prop_Data, "m_iFrags", 0);
143 SetEntProp(Client, Prop_Data, "m_iDeaths", 0);
144
145 CS_SetMVPCount(Client, 0);
146 CS_SetClientAssists(Client, 0);
147 CS_SetClientContributionScore(Client, 0);
148
149 PrintToChat(Client, ">> You've\x07 reset\x01 your\x09 score\x01.");
150 }
151
152 else if (!strcmp(Command, "Time", false) || !strcmp(Command[1], "Time", false) || \
153 !strcmp(Command, "Timp", false) || !strcmp(Command[1], "Timp", false) || \
154 !strcmp(Command, "Played", false) || !strcmp(Command[1], "Played", false) || \
155 !strcmp(Command, "Ore", false) || !strcmp(Command[1], "Ore", false))
156 {
157 PrintToChat(Client, ">> You've spent\x05 %s\x01 on the server.", g_TimeStr[Client]);
158 }
159 }
160 }
161}
162
163public void ShowMaps(Handle pDb, Handle pQuery, char[] Error, any Data)
164{
165 static char Name[PLATFORM_MAX_PATH], Message[PLATFORM_MAX_PATH];
166
167 if (strlen(Error) > 0)
168 {
169 LogError("SQL_TQuery() @ ShowMaps() reported an error!");
170 LogError(Error);
171 }
172
173 else if (g_InGame[Data])
174 {
175 FormatEx(Message, sizeof(Message), "* Maps were\x09");
176
177 while (SQL_FetchRow(pQuery))
178 {
179 SQL_FetchString(pQuery, 0, Name, sizeof(Name));
180
181 hCSGO_RemoveWorkshop(Name, sizeof(Name), true);
182 Format(Message, sizeof(Message), "%s %s\x01,\x09", Message, Name);
183 }
184
185 Format(Message, sizeof(Message), "%s ..", Message);
186 PrintToChat(Data, Message);
187 }
188}
189
190public void EmptyFunction(Handle pDb, Handle pQuery, char[] Error, any Data)
191{
192 if (strlen(Error) > 0)
193 {
194 LogError("SQL_TQuery() @ EmptyFunction() reported an error!");
195 LogError(Error);
196 }
197}
198
199public void OnClientDisconnect(int Client)
200{
201 g_Time[Client] = 0;
202 g_TimeStr[Client] = "0 m";
203
204 if (g_pTimer[Client] != INVALID_HANDLE)
205 {
206 KillTimer(g_pTimer[Client]);
207 g_pTimer[Client] = INVALID_HANDLE;
208 }
209
210 g_InGame[Client] = false;
211}
212
213public void OnClientPutInServer(int Client)
214{
215 g_InGame[Client] = true;
216
217 g_Time[Client] = 0;
218 g_TimeStr[Client] = "0 m";
219
220 GetClientAuthId(Client, AuthId_Engine, g_Steam[Client], sizeof(g_Steam[]));
221
222 if (!strcmp(g_Steam[Client], "BOT"))
223 {
224 FormatEx(g_Steam[Client], sizeof(g_Steam[]), "BOT@%N", Client);
225 }
226
227 if (g_pDb != INVALID_HANDLE)
228 {
229 FormatEx(g_Buffer, sizeof(g_Buffer), "SELECT Time, timeString FROM PlayersTime WHERE Steam = '%s' LIMIT 1;", g_Steam[Client]);
230 SQL_TQuery(g_pDb, RetrieveOrCreatePlayer, g_Buffer, Client);
231 }
232}
233
234public void RetrieveOrCreatePlayer(Handle pDb, Handle pQuery, char[] Error, any Client)
235{
236 if (strlen(Error) > 0)
237 {
238 LogError("SQL_TQuery() @ RetrieveOrCreatePlayer() reported an error!");
239 LogError(Error);
240 }
241
242 else if (g_InGame[Client])
243 {
244 if (!SQL_FetchRow(pQuery))
245 {
246 if (g_pDb != INVALID_HANDLE)
247 {
248 FormatEx(g_Buffer, sizeof(g_Buffer), "INSERT INTO PlayersTime VALUES ('%s', %d, '%s');", g_Steam[Client], g_Time[Client], g_TimeStr[Client]);
249 SQL_TQuery(g_pDb, EmptyFunction, g_Buffer);
250 }
251 }
252
253 else
254 {
255 g_Time[Client] = SQL_FetchInt(pQuery, 0);
256 SQL_FetchString(pQuery, 1, g_TimeStr[Client], sizeof(g_TimeStr[]));
257 }
258
259 g_pTimer[Client] = CreateTimer(120.0, TimeUpdate, Client, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
260 }
261}
262
263public Action TimeUpdate(Handle pTimer, any Client)
264{
265 if (g_InGame[Client])
266 {
267 g_Time[Client] += 2;
268 hCSGO_GetTimeStringMinutes(g_Time[Client], g_TimeStr[Client], sizeof(g_TimeStr[]));
269
270 if (g_pDb != INVALID_HANDLE)
271 {
272 FormatEx(g_Buffer, sizeof(g_Buffer), "UPDATE PlayersTime SET Time = Time + 2, timeString = '%s' WHERE Steam = '%s';", g_TimeStr[Client], g_Steam[Client]);
273 SQL_TQuery(g_pDb, EmptyFunction, g_Buffer);
274 }
275 }
276}