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