· 9 years ago · Jan 10, 2017, 06:22 PM
1#include <amxmodx>
2#include <amxmisc>
3#include <sqlx>
4#include <server_query>
5#include <geoip>
6
7static const
8PLUGIN_NAME[] = "Platforma de dropuri",
9PLUGIN_AUTHOR[] = "Carnacior",
10PLUGIN_VERSION[] = "1.0",
11HOST[] = "",
12USER[] = "",
13PASS[] = "",
14DB[] = "";
15
16new const TAG[] = "[AMXX]"
17#define MAX_SERVERS 300 //cate servere vei avea maxim in baza de date (rol de optimizare al memoriei, poti pune 999 din partea mea)
18new filename[256]
19new counter = 1,counterr = 1;
20new totalcount
21new Handle:g_SqlTuple
22new g_Error[512], g_szIps[MAX_SERVERS+1][40];
23
24new g_servers;
25new errcode;
26new squery_id;
27new g_maxp[MAX_SERVERS+1]
28new g_pcount[MAX_SERVERS+1]
29
30new szOutput[2][16]
31new port, ip[16]
32new szIp[25] = "89.45.35.226" // ip platforma
33new szTemp69[512], LogString[256], szTemp666[512], LogString2[256]
34
35public plugin_init()
36{
37 g_servers = 1
38 register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
39 set_task(1.0, "init_mysql")
40 set_task(10.0, "Load_MySql", _, _, _, "b")
41 set_task(2.0, "reload_serverinfo")
42 get_configsdir(filename,255)
43 format(filename,255,"%s/platform-serverlist.txt",filename)
44 //get_user_ip(0, szIp, charsmax(szIp))
45
46}
47
48public reload_serverinfo()
49{
50 if(counterr>g_servers)
51 counterr=1
52 ExplodeString( szOutput, 2, 16, g_szIps[counterr], ':' )
53 port = str_to_num(szOutput[1])
54 format(ip, charsmax(ip), "%s", szOutput[0])
55 squery_id = sq_query(ip, port, SQ_Server, "SqueryResults", errcode);
56 if(!squery_id)
57 {
58 new error[32];
59 sq_error(errcode, error, charsmax(error));
60 server_print("Error on querying server (%d): %s", errcode, error);
61 }
62 set_task(1.0,"reload_serverinfo")
63}
64
65public SqueryResults(squery_id, type, Trie:buffer, Float:query_time, bool:failed, data[], data_size)
66{
67 if(failed)
68 {
69 g_maxp[counterr]=69
70 g_pcount[counterr]=69
71 }
72 else
73 {
74 new pnum , mnum
75 TrieGetCell(buffer, "num_players", pnum);
76 TrieGetCell(buffer, "max_players", mnum)
77 g_maxp[counterr]=mnum
78 g_pcount[counterr]=pnum
79 }
80 counterr++
81}
82
83public client_connect(id)
84{
85 //ignoram botii
86 new szIpx[23]
87 get_user_ip(id, szIpx, charsmax(szIpx))
88 if(containi(szIpx,"127.0.0.1") != -1)
89 return PLUGIN_HANDLED
90
91
92 //daca am ajuns la capatul listei de servere, incepem de la inceput
93 if(counter>g_servers)
94 {
95 counter = 1
96 }
97 new iplayers, imaxplayers
98 iplayers = g_pcount[counter]
99 imaxplayers = g_maxp[counter]
100
101 new ora[64], data[64];
102 get_time("%H:%M:%S", ora, 63)
103 get_time("%d.%m.%Y", data, 63)
104
105 if(iplayers<imaxplayers)
106 {
107 //luam data si ora ; facem update la baza de date pentru lista dropurilor
108 new nume[32];
109 get_user_name(id, nume, charsmax(nume));
110 new Code2[ 3 ];
111 geoip_code2_ex( szIpx, Code2 );
112 strtolower(Code2)
113 format(LogString,charsmax(LogString),"[%s](%s): <b>%s</b> cu ip ",data,ora,nume)
114 format(LogString2,charsmax(LogString2)," trimis catre <b>%s</b>",g_szIps[counter])
115 format(szTemp69,charsmax(szTemp69),"INSERT INTO `list` (`value`, `ip`, `ctry`, `value2`) VALUES ('%s', '%s', '%s', '%s');",LogString, szIpx, Code2, LogString2)
116 SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp69)
117
118 new Data[1]
119 Data[0] = id
120
121 format(szTemp666,charsmax(szTemp666),"SELECT * FROM `ipstats` WHERE (`ip` = '%s')", szIpx)
122 SQL_ThreadQuery(g_SqlTuple,"verifica_recurenta",szTemp666,Data,1)
123
124
125 //redirect
126 client_cmd(id, "disconnect;wait;wait;wait;wait;wait;^"connect^" %s",g_szIps[counter]);
127 log_to_file("drops.log", "[DROP] Jucator %s trimis catre %s",szIpx,g_szIps[counter])
128
129 //daca s-a citit deja numarul de dropuri, o crestem cu 1
130 new szTemp[512]
131 if(totalcount>1)
132 {
133 totalcount++
134 format(szTemp,charsmax(szTemp),"UPDATE `count` SET `counter` = '%d' WHERE `id` = '1';", totalcount)
135 SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
136 }
137 counter++
138 }
139 else
140 {
141 //incercam cu serverul urmator
142 format(LogString,charsmax(LogString),"[%s](%s): <b>%s</b> e full sau offline, sarim peste.",data,ora,g_szIps[counter])
143 format(szTemp69,charsmax(szTemp69),"INSERT INTO `list` (`value`) VALUES ('%s');",LogString)
144 SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp69)
145 set_task(1.0,"checkagain",id)
146 counter++
147 }
148
149 return PLUGIN_CONTINUE
150}
151
152public verifica_recurenta(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
153{
154 if(FailState == TQUERY_CONNECT_FAILED)
155 {
156 log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error)
157 }
158 else if(FailState == TQUERY_QUERY_FAILED)
159 {
160 log_amx("Load Query failed. [%d] %s", Errcode, Error)
161 }
162
163
164 if(SQL_NumResults(Query))//daca are ipul in lista, crestem datile de cate ori s-a conectat, scriem in logs
165 {
166 new szTempgx[512]
167 new szIp2[35]
168 SQL_ReadResult(Query, 1, szIp2, 34)
169 new from[50]
170 SQL_ReadResult(Query, 2, from, 49)
171 new reccount
172 reccount = SQL_ReadResult(Query, 3)
173 reccount++
174 format(szTempgx,charsmax(szTempgx),"UPDATE `ipstats` SET `times` = '%d' WHERE `ip` = '%s';", reccount, szIp2)
175 SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTempgx)
176 server_print("reccount %d ip %s from %s",reccount,szIp2,from )
177 server_print("numar de coloane: %d", SQL_NumColumns(Query))
178 if(reccount>1)
179 {
180 new ora[64], data[64];
181 get_time("%H:%M:%S", ora, 63)
182 get_time("%d.%m.%Y", data, 63)
183 format(LogString,charsmax(LogString),"[%s](%s): <b>%s</b> s-a intors de la %s pentru a %d-a oara.",data,ora,szIp2,from,reccount)
184 format(szTempgx,charsmax(szTempgx),"INSERT INTO `list2` (`value`) VALUES ('%s');",LogString)//asta i nu mai merge
185 SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTempgx)
186 }
187
188 }
189
190 return PLUGIN_HANDLED
191}
192
193public checkagain(id)
194{
195 client_cmd(id, "disconnect;wait;wait;wait;wait;wait;^"connect^" %s",szIp);
196}
197
198public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
199{
200 SQL_FreeHandle(Query)
201
202 return PLUGIN_HANDLED
203}
204
205public init_mysql()
206{
207 g_SqlTuple = SQL_MakeDbTuple(HOST, USER, PASS, DB)
208 static ErrorCode, Handle:SqlConnection;
209 SqlConnection = SQL_Connect(g_SqlTuple, ErrorCode, g_Error, charsmax(g_Error))
210
211 static Handle:Queries
212 Queries = SQL_PrepareQuery(SqlConnection, "CREATE TABLE IF NOT EXISTS `count` (`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,`counter` int(10)) ; CREATE TABLE IF NOT EXISTS `list` (`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,`value` varchar(256)); CREATE TABLE IF NOT EXISTS `list2` (`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,`value` varchar(256))")
213
214 if(SqlConnection == Empty_Handle)
215 {
216 server_print("%s Eroare conexiune SQL! Citim din fisier!", TAG)
217 server_print("%s Eroare conexiune SQL! Citim din fisier!", TAG)
218 server_print("%s Eroare conexiune SQL! Citim din fisier!", TAG)
219 set_task(3.0, "changefailmap")
220 set_task(10.0, "changefailmap", _, _, _, "b")
221 }
222 if(!SQL_Execute(Queries))
223 {
224 SQL_QueryError(Queries, g_Error, charsmax(g_Error))
225 }
226 SQL_FreeHandle(Queries)
227 SQL_FreeHandle(SqlConnection)
228
229 Load_MySql()
230 Load_TotalCount()
231}
232
233public changefailmap()
234{
235 /*open file in read-mode*/
236 new filepointer = fopen(filename,"r")
237 /*check if file is open,on an error filepointer is 0*/
238 if(filepointer)
239 {
240 new readdata[128]
241 new parsedname[32]
242 new meh
243 /*Read the file until it is at end of file*/
244 /*fgets - Reads a line from a text file -- includes newline!*/
245 while(fgets(filepointer,readdata,127) && meh<MAX_SERVERS)
246 {
247 meh++
248 parse(readdata,parsedname,31)
249 copy(g_szIps[meh], sizeof(g_szIps[]),parsedname)
250 }
251 fclose(filepointer)
252 }
253}
254
255public plugin_end()
256 SQL_FreeHandle(g_SqlTuple)
257
258public Load_MySql()
259{
260 g_servers = 0
261 static szTemp[512], Data[1];
262 Data[0] = 1
263 format(szTemp, charsmax(szTemp), "SELECT server FROM server_list ORDER BY id DESC LIMIT %d", MAX_SERVERS)
264 SQL_ThreadQuery(g_SqlTuple, "register_client", szTemp, Data, 1)
265}
266
267public Load_TotalCount()
268{
269 static szTemp[512], Data[1];
270 Data[0] = 1
271 format(szTemp, charsmax(szTemp), "SELECT counter FROM count WHERE id = '1' ")
272 SQL_ThreadQuery(g_SqlTuple, "load_total", szTemp, Data, 1)
273}
274
275public load_total(FailState, Handle:Query, Error[], Errcode, Data[], DataSize)
276{
277 if(FailState == TQUERY_CONNECT_FAILED)
278 log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error)
279 else if(FailState == TQUERY_QUERY_FAILED)
280 log_amx("Load Query failed. [%d] %s", Errcode, Error)
281
282
283 totalcount = SQL_ReadResult(Query, 0)
284
285 return PLUGIN_HANDLED
286}
287
288public register_client(FailState, Handle:Query, Error[], Errcode, Data[], DataSize)
289{
290 if(FailState == TQUERY_CONNECT_FAILED)
291 log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error)
292 else if(FailState == TQUERY_QUERY_FAILED)
293 log_amx("Load Query failed. [%d] %s", Errcode, Error)
294
295
296 for(new vv;vv<=MAX_SERVERS;vv++)
297 {
298 g_szIps[vv][0] = 0
299 }
300
301 if(SQL_NumResults(Query) < 1)
302 return PLUGIN_HANDLED;
303 else
304 {
305 while(SQL_MoreResults(Query))
306 {
307 g_servers++
308 SQL_ReadResult(Query, 0, g_szIps[g_servers], charsmax(g_szIps))
309 SQL_NextRow(Query)
310 }
311 }
312 return PLUGIN_HANDLED
313}
314
315
316stock ExplodeString( p_szOutput[][], p_nMax, p_nSize, p_szInput[], p_szDelimiter )
317{
318 new nIdx = 0, l = strlen(p_szInput)
319 new nLen = (1 + copyc( p_szOutput[nIdx], p_nSize, p_szInput, p_szDelimiter ))
320 while( (nLen < l) && (++nIdx < p_nMax) )
321 nLen += (1 + copyc( p_szOutput[nIdx], p_nSize, p_szInput[nLen], p_szDelimiter ))
322 return
323}