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