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