· 8 years ago · Mar 15, 2018, 06:22 PM
1#include <amxmodx>
2#include <fakemeta>
3#include <sqlx>
4
5static const
6 HOST[]="localhost",
7 USER[]="valiboss",
8 PASS[]="justdapz",
9 DB[]="admins",
10 COL_ACC[]="unique",
11 COL_USER[]="auth"
12
13new Handle:g_SqlTuple,query[512]
14
15#define MAX_NAMES 65
16#define TASK_MESS 1000
17#define TASK_KICK 2000
18#define TASK_TIMER 4000
19new g_count,g_logtime,g_player_time[33],bool:is_logged[33],bool:changing_name[33],bool:data_ready = false,password[33][34],g_saytxt,g_sync_hud,
20g_maxplayers,type,sz_time[9],count,col_name,input[32],part_names[MAX_NAMES][32],ending_names[MAX_NAMES][32],starting_names[MAX_NAMES][32],
21Trie:g_full_nmaes,bool:is_registered[33],g_client_data[33][35],check_pass[34],params[2]
22new const separator_1[] = "==============================================================================="
23new const separator_2[] = "-------------------------------------------------------------------------------"
24new const prefix[] = "[LOG-IN SYSTEM]";
25
26public plugin_init()
27{
28 g_count=register_cvar("vjd_logcount", "1")
29 g_logtime=register_cvar("vjd_logtime", "60.0")
30
31 register_concmd("amx_passcode","VERIFY_USER")
32
33 register_forward(FM_ClientUserInfoChanged, "ClientInfoChanged")
34
35 g_full_nmaes = TrieCreate()
36 g_saytxt = get_user_msgid("SayText")
37 g_sync_hud = CreateHudSyncObj()
38 g_maxplayers = get_maxplayers()
39
40 set_task(1.0, "init_mysql")
41}
42
43public init_mysql()
44{
45 g_SqlTuple = SQL_MakeDbTuple(HOST, USER, PASS, DB)
46
47 formatex(query, charsmax(query), "CREATE TABLE IF NOT EXISTS %s (Name VARCHAR(35)) ; CREATE TABLE IF NOT EXISTS %s (Password VARCHAR(34))",COL_ACC,COL_USER)
48 SQL_ThreadQuery(g_SqlTuple, "QueryCreateTable", query)
49}
50public QueryCreateTable(failstate, Handle:query1, error[], errcode, data[], datasize, Float:queuetime)
51{
52 if(failstate == TQUERY_CONNECT_FAILED) set_fail_state("[LOG-IN SYSTEM] Could not connect to database!")
53 else if(failstate == TQUERY_QUERY_FAILED) set_fail_state("[LOG-IN SYSTEM] Query failed!")
54 else if(errcode) server_print("%s Error on query: %s", prefix, error)
55 else
56 {
57 server_print("%s MYSQL connection succesful!", prefix)
58
59 data_ready = true
60
61 for(new i = 1 ; i <= g_maxplayers ; i++)
62 {
63 if(!is_user_connecting(i) && !is_user_connected(i)) continue//da
64
65 CheckClient(i)
66 }
67 }
68}
69public QueryCollectData(failstate, Handle:query, error[], errcode, data[], datasize, Float:queuetime)
70{
71 if(failstate == TQUERY_CONNECT_FAILED || failstate == TQUERY_QUERY_FAILED)
72 {
73 log_amx("%s", error)
74 return
75 }
76 else
77 {
78 type = data[0]
79 get_time("%H:%M:%S", sz_time, charsmax(sz_time))
80
81 if(type)
82 {
83 new count1 = 0, count2 = 0, count3 = 0, count4 = 0
84 count = 0
85 col_name = SQL_FieldNameToNum(query, COL_USER)
86
87 server_print(" ")
88 server_print(separator_1)
89 server_print(prefix)
90 server_print(separator_2)
91 server_print("[%s] > Loading SQL table ^"%s^"", sz_time,COL_USER)
92
93 while(SQL_MoreResults(query))
94 {
95 SQL_ReadResult(query, col_name, input, charsmax(input))
96
97 if(input[0] == '%')
98 {
99 if(input[strlen(input) - 1] == '%') //Part name
100 {
101 if(count1 >= MAX_NAMES) continue
102
103 replace_all(input, charsmax(input), "%", "")
104 part_names[count1++] = input
105 }
106 else // Starting name
107 {
108 if(count2 >= MAX_NAMES) continue
109
110 replace_all(input, charsmax(input), "%", "")
111 ending_names[count2++] = input
112 }
113 }
114 else
115 {
116 if(input[strlen(input) - 1] == '%') //Ending name
117 {
118 if(count3 >= MAX_NAMES) continue
119
120 replace_all(input, charsmax(input), "%", "")
121 starting_names[count3++] = input
122 }
123 else //Full name
124 {
125 if(++count4 >= MAX_NAMES) continue
126
127 replace_all(input, charsmax(input), "%", "")
128 TrieSetCell(g_full_nmaes, input, 1)
129 }
130 }
131
132 count++
133 SQL_NextRow(query)
134 }
135
136
137 if(count)
138 {
139 server_print("[%s] [OK] > %d name%s loaded!", sz_time, count, count > 1 ? "s" : "")
140 server_print("[%s] [OK] > SQL table ^"%s^" loaded successfully!", sz_time,COL_USER)
141 }
142 else
143 {
144 server_print("[%s] [ERROR] > There were no columns in SQL table ^"%s^"", sz_time,COL_USER)
145 server_print("[%s] [WARNING] > Reading some data from the table failed!", sz_time)
146 server_print("> Please check [ERROR] messages above for solving this problem!")
147 }
148
149 server_print(separator_1)
150 server_print(" ")
151
152 data_ready = true
153
154 for(new i = 1 ; i <= g_maxplayers ; i++)
155 {
156 if(!is_user_connecting(i) && !is_user_connected(i)) continue
157
158 CheckClient(i)
159 }
160 }
161 }
162}
163
164public client_putinserver(id)
165{
166 if(!is_user_connected(id)||is_user_bot(id)||is_user_hltv(id)) return
167
168 clear_user(id)
169 remove_tasks(id)
170
171 if(data_ready) CheckClient(id)
172
173 /*new name[32],authid[65],data[1]
174 get_user_name(id,name,charsmax(name))
175 get_user_authid(id,authid,charsmax(authid))
176
177 if(!is_user_steam(id)) formatex(query, charsmax(query), "SELECT `%s` FROM `%s` WHERE %s = ^"%s^";",COL_USER,DB,Name,g_client_data[id])
178 else if(is_user_steam(id)) formatex(query, charsmax(query), "SELECT `%s` FROM `%s` WHERE %s = ^"%s^";",authid,DB,COL_USER)
179
180 data[0] = id
181
182 SQL_ThreadQuery(g_SqlTuple, "QuerySelectData", query, data, 1)*/
183}
184public CheckClient(id)
185{
186 if(is_user_bot(id) || is_user_hltv(id) || !data_ready) return PLUGIN_HANDLED
187
188 remove_tasks(id)
189 is_registered[id] = false
190 is_logged[id] = false
191
192 if(!is_user_steam(id)) get_user_name(id, g_client_data[id], charsmax(g_client_data))
193 else if(is_user_steam(id)) get_user_authid(id, g_client_data[id], charsmax(g_client_data))
194
195 new data[1]
196 data[0] = id
197
198 formatex(query, charsmax(query), "SELECT `%s` FROM `%s` WHERE %s = ^"%s^";",COL_ACC,DB,COL_USER,g_client_data[id])
199 SQL_ThreadQuery(g_SqlTuple, "QuerySelectData", query, data, 1)
200
201 return PLUGIN_CONTINUE
202}
203public QuerySelectData(FailState, Handle:Query, error[], errorcode, data[], datasize, Float:fQueueTime)
204{
205 if(FailState == TQUERY_CONNECT_FAILED || FailState == TQUERY_QUERY_FAILED)
206 {
207 log_amx("%s", error)
208 return
209 }
210 else
211 {
212 new id = data[0];
213 new col_pass = SQL_FieldNameToNum(Query, COL_ACC)
214
215 while(SQL_MoreResults(Query))
216 {
217 SQL_ReadResult(Query, col_pass, check_pass, charsmax(check_pass))
218 is_registered[id] = true
219 password[id] = check_pass
220
221 if(is_user_connected(id))
222 {
223 //user_silentkill(id)
224 //cs_set_user_team(id, CS_TEAM_UNASSIGNED)
225 ShowMsg(id)
226 }
227
228 SQL_NextRow(Query)
229 }
230 }
231}
232public ShowMsg(id)
233{
234 remove_tasks(id)
235
236 set_task(5.0, "Messages", id+TASK_MESS)
237
238 params[0] = id
239
240 if(!is_logged[id])
241 {
242 if(!changing_name[id])
243 {
244 if(get_pcvar_num(g_count))
245 {
246 g_player_time[id] = get_pcvar_num(g_logtime)
247 ShowTimer(id+TASK_TIMER)
248 }
249 params[1] = 2
250 set_task(get_pcvar_float(g_logtime) + 3, "KickPlayer", id+TASK_KICK, params, sizeof params)
251 return PLUGIN_HANDLED
252 }
253 else
254 {
255 g_player_time[id] = -1
256 set_task(1.0, "ShowTimer", id+TASK_TIMER)
257 }
258 }
259 return PLUGIN_CONTINUE
260}
261public KickPlayer(parameters[])
262{
263 new id = parameters[0]
264 new reason = parameters[1]
265
266 if(!is_user_connecting(id) && !is_user_connected(id)) return PLUGIN_HANDLED
267
268 new userid = get_user_userid(id)
269
270 switch(reason)
271 {
272 case 1:
273 {
274 if(is_registered[id]) return PLUGIN_HANDLED
275
276 console_print(id, "^n* Cont invalid!")
277 server_cmd("kick #%i ^"You must register your name. Please, retry and register it.^"", userid)
278 }
279 case 2:
280 {
281 if(is_logged[id]) return PLUGIN_HANDLED
282
283 console_print(id, "^n* Cont invalid!")
284 server_cmd("kick #%i ^"This name is registered. Please, retry and login.^"", userid)
285 }
286 case 3:
287 {
288 console_print(id, "^n* Cont invalid!")
289 server_cmd("kick #%i ^"You have successfully Logged out! Your Auto Login function is currently disabled!^"", userid)
290 }
291 }
292 return PLUGIN_CONTINUE
293}
294public ShowTimer(id)
295{
296 id -= TASK_TIMER
297
298 if(!is_user_connected(id)) return PLUGIN_HANDLED
299
300 switch(g_player_time[id])
301 {
302 case 10..19: set_hudmessage(255, 255, 0, -1.0, -1.0, 0, 0.02, 1.0,_,_, -1)
303 case 0..9: set_hudmessage(255, 0, 0, -1.0, -1.0, 1, 0.02, 1.0,_,_, -1)
304 case -1: set_hudmessage(255, 255, 255, -1.0, -1.0, 1, 0.02, 1.0,_,_, -1)
305 default: set_hudmessage(0, 255, 0, -1.0, -1.0, 0, 0.02, 1.0,_,_, -1)
306 }
307
308 if(g_player_time[id] == 0)
309 {
310 ShowSyncHudMsg(id, g_sync_hud, "You will be kicked!")
311 return PLUGIN_CONTINUE
312 }
313 else if(!is_logged[id])
314 {
315 if(g_player_time[id] == -1)
316 {
317 ShowSyncHudMsg(id, g_sync_hud, "You will can login after the your name is changed!")
318 set_task(1.0, "ShowTimer", id+TASK_TIMER)
319 return PLUGIN_HANDLED
320 }
321
322 ShowSyncHudMsg(id, g_sync_hud, "You have %d second%s to Login!", g_player_time[id] > 1 ? "s" : "", g_player_time[id])
323 }
324 else return PLUGIN_HANDLED
325
326 g_player_time[id]--
327
328 set_task(1.0, "ShowTimer", id+TASK_TIMER)
329
330 return PLUGIN_CONTINUE
331}
332public Messages(id)
333{
334 id -= TASK_MESS
335
336 if(!is_logged[id]) client_printcolor(id, "!g%s!t You have!g %d!t seconds to login!", prefix, get_pcvar_num(g_logtime))
337}
338
339public ClientInfoChanged(id)
340{
341 if(!is_user_connected(id)) return FMRES_IGNORED
342
343 new oldname[32], newname[32];
344 get_user_name(id, oldname, charsmax(oldname))
345 get_user_info(id, "name", newname, charsmax(newname))
346
347 if(!equal(oldname, newname))
348 {
349 replace_all(newname, charsmax(newname), "%", " ")
350
351 changing_name[id] = false
352
353 if(!is_user_alive(id)) changing_name[id] = true
354 else
355 {
356 if(is_logged[id])
357 {
358 set_user_info(id, "name", oldname)
359 client_printcolor(id, "!g%s!t Sorry, but name change is not allowed for Logged players!", prefix)
360 return FMRES_HANDLED
361 }
362
363 set_task(1.0, "CheckClient", id)
364 }
365 }
366 return FMRES_IGNORED
367}
368
369public VERIFY_USER(id)
370{
371 new arg[32]
372 read_argv(1,arg,charsmax(arg))
373 if(!is_logged[id]&&is_registered[id])
374 {
375 if(!(equali(arg,password[id]))) server_cmd("kick #%d ^"Nu te-ai putut autentifica^"",get_user_userid(id))
376 else is_logged[id]=true
377 }
378}
379
380public client_disconnect(id)
381{
382 clear_user(id)
383 remove_tasks(id)
384}
385
386stock clear_user(const id)
387{
388 is_logged[id] = false
389 is_registered[id] = false
390 changing_name[id] = false
391}
392
393stock remove_tasks(const id)
394{
395 remove_task(id+TASK_MESS)
396 remove_task(id+TASK_KICK)
397 remove_task(id+TASK_TIMER)
398 remove_task(id)
399}
400
401stock client_printcolor(const id, const message[], any:...)
402{
403 new g_message[191];
404 new i = 1, players[32];
405
406 vformat(g_message, charsmax(g_message), message, 3)
407
408 replace_all(g_message, charsmax(g_message), "!g", "^4")
409 replace_all(g_message, charsmax(g_message), "!n", "^1")
410 replace_all(g_message, charsmax(g_message), "!t", "^3")
411
412 if(id) players[0] = id
413 else get_players(players, i, "ch")
414
415 for(new j = 0; j < i; j++)
416 {
417 if(is_user_connected(players[j]))
418 {
419 message_begin(MSG_ONE_UNRELIABLE, g_saytxt,_, players[j])
420 write_byte(players[j])
421 write_string(g_message)
422 message_end()
423 }
424 }
425}
426
427stock bool: is_user_steam(id)
428{
429 static dp_pointer
430 if(dp_pointer||(dp_pointer=get_cvar_pointer("dp_r_id_provider")))
431 {
432 server_cmd("dp_clientinfo %d",id)
433 server_exec()
434 return (get_pcvar_num(dp_pointer)==2) ? true : false
435 }
436 return false
437}