/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <fakemeta>
#include <fvault>

#define PLUGIN "System Rejestracji"
#define VERSION "1.0"
#define AUTHOR "Play"


new const g_fvault[ ] = "dane_graczy"
new const prefix[] = "[System Rejestracji]"

new nazwa_forum[64], forum;

enum _:TOTAL_FORWARDS {	
			
	FW_USER_REGISTER,
	FW_USER_LOGGED,
	FW_USER_LOGGED_INVITED,
	FW_USER_ACCOUNT_DELETED
}

new g_Forwards[TOTAL_FORWARDS]
new g_ForwardResult

new g_logged[33], g_registered[33], g_invited[33]

new g_password[33], g_password_again[33]

new g_attempts[33], g_unlock[33], g_deleted_account[33]

new cvar_attempts, cvar_bantime, cvar_logintime, cvar_screenfade, cvar_mim_characters, cvar_max_characters

new g_screenfade

public plugin_init() 
{	
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_cvar("registration_system", VERSION, FCVAR_SPONLY | FCVAR_SERVER )
	
	
	register_clcmd("chooseteam", "message_team")
	register_clcmd("jointeam", "message_team")
	register_clcmd("joinclass", "message_team")
	
	register_forward(FM_ClientUserInfoChanged, "ClientUserInfoChanged")
	register_forward(FM_PlayerPreThink, "PlayerPreThink")
	
	register_clcmd("WPISZ_SWOJE_HASLO","cmd_password")
	register_clcmd("WPROWADZ_PONOWNIE_HASLO","cmd_password_again")
	register_clcmd("WPROWADZ_OBECNE_HASLO","cmd_password_current")
	
	register_clcmd("say", "commands")
	register_clcmd("say_team", "commands")
	
	forum = register_cvar("cod_nazwa_forum", "TwojaNazwa.pl");
		
	cvar_mim_characters = register_cvar("reg_mim_characters", "5")
	cvar_max_characters = register_cvar("reg_max_characters", "20")
	
	cvar_attempts = register_cvar("reg_wrong_passowrd_limit", "3")
	cvar_bantime = register_cvar("reg_wrong_passowrd_time_ban", "5")
	cvar_logintime = register_cvar("reg_time_to_log", "60")
	cvar_screenfade = register_cvar("reg_screenfade", "1")
	
	g_screenfade = get_user_msgid("ScreenFade")
	
	g_Forwards[FW_USER_REGISTER] = CreateMultiForward("reg_user_register", ET_IGNORE, FP_CELL)
	g_Forwards[FW_USER_LOGGED] = CreateMultiForward("reg_user_logged", ET_IGNORE, FP_CELL)
	g_Forwards[FW_USER_LOGGED_INVITED] = CreateMultiForward("reg_user_logged_invited", ET_IGNORE, FP_CELL)
	g_Forwards[FW_USER_ACCOUNT_DELETED] = CreateMultiForward("reg_user_account_deleted", ET_IGNORE, FP_CELL)
}

public plugin_natives() 
{	
	register_native("reg_is_user_logged", "native_is_user_logged", 1)
	register_native("reg_is_user_registered", "native_is_user_registered", 1)
	register_native("reg_is_user_invited", "native_is_user_invited", 1)
}

public client_connect(id) {
	client_cmd(id, "setinfo ^"_vgui_menus^" ^"1^"");
	get_pcvar_string(forum, nazwa_forum, 63);
}

public client_putinserver(id) 
{	
	static szName[32], szData[64]
	get_user_name(id, szName, charsmax(szName))
	set_task(get_pcvar_float(cvar_logintime), "login_time", id )
	
	g_unlock[id] = true
	
	if(fvault_get_data(g_fvault, szName, szData, charsmax(szData)))
		g_registered[id] = true
	else
		g_registered[id] = false	
}

public client_disconnect(id) 
{	
	remove_task(id)
	g_logged[id] = false
	g_invited[id] = false
	g_unlock[id] = false
}

public message_team(id) {
	
	if(!g_logged[id] && !g_invited[id] && !(get_user_flags(id) & ADMIN_BAN) && !is_user_bot(id) && !is_user_hltv(id)) 
	{	
		menu_account(id)
		return PLUGIN_HANDLED
	}
	return PLUGIN_CONTINUE
}

public ClientUserInfoChanged(id) 
{ 	
	static szOldName[32]
	pev(id, pev_netname, szOldName, charsmax(szOldName))
	
	if(szOldName[0]) 
	{	
		static const name[] = "name"
		static szNewName[32]
		get_user_info(id, name, szNewName, charsmax(szNewName))
		
		if(!equal(szOldName, szNewName)) 
		{	
			set_user_info(id, name, szOldName)
			return FMRES_HANDLED
		}
	}
	return FMRES_IGNORED
}

public menu_account(id) 
{	
	new szMenu[128], szName[32]
	get_user_name(id, szName, charsmax(szName))
		
	formatex(szMenu, 127, "\rForum:\y %s^n\rSystem Rejestracji^n^nNick\d: \w[\y%s\w]^n\rStatus\d: \w[\y%s\w]", nazwa_forum, szName, g_invited[id] ? "Gosc" : g_logged[id] ? "Zalogowany" : g_registered[id] ? "Zarejestrowany" : "Nie Zarejestrowany");
	
	new Menu = menu_create(szMenu, "handler_menu_account")
	
	formatex(szMenu, 63, "%sLogin", g_logged[id] ? "\d" : g_registered[id] ? "\y" : "\d")
	menu_additem(Menu, szMenu, "1", 0)
	
	formatex(szMenu, 63, "%sRejestracja^n", g_registered[id] ? "\d":"\r")
	menu_additem(Menu, szMenu, "2", 0)
	
	formatex(szMenu, 63, "%sZmien Haslo", g_logged[id] ? "\w" : "\d")
	menu_additem(Menu, szMenu, "3", 0)
	
	formatex(szMenu, 63, "%sUsun Konto^n", g_logged[id] ? "\w" :  "\d")
	menu_additem(Menu, szMenu, "4", 0)
	
	formatex(szMenu, 63, "\d Wyjdz")
	menu_setprop(Menu, MPROP_EXITNAME, szMenu)
	menu_display(id, Menu, 0)
}

public handler_menu_account(id, menu, item) 
{	
	if(item == MENU_EXIT) {
		menu_destroy(menu)
		return PLUGIN_HANDLED
	}
	switch(item) 
	{
		case 0: login(id)
		case 1: register(id)
		case 2: change(id)	
		case 3: delete_account(id) 
	}
	menu_destroy(menu)
	return PLUGIN_HANDLED
}
public login(id) 
{
	if(g_logged[id]) 
	{
		client_print_color(id, "!g%s !tJestes juz zalogowany", prefix);
		menu_account(id)	
	}
	else if(!g_registered[id]) 
	{
		client_print_color(id, "!g%s !tTen nick, nie jest zarejestrowany!", prefix);
		menu_account(id)	
	}
	else 
	{
		client_print_color(id, "!g%s !tWprowadz Haslo", prefix);
		
		client_cmd(id,"messagemode WPISZ_SWOJE_HASLO")
		g_unlock[id] = false
	}
	return PLUGIN_HANDLED
}

public register(id) 
{	
	if(g_registered[id]) 
	{	
		client_print_color(id, "!g%s !tTen nick, jest juz zarejestrowany", prefix);		
		menu_account(id)
	}
	else 
	{	
		client_print_color(id, "!g%s !tWprowadz Haslo", prefix);		
		client_cmd(id,"messagemode WPISZ_SWOJE_HASLO")
		g_unlock[id] = false
	}
	return PLUGIN_HANDLED
}

public change(id) 
{	
	if(!g_logged[id]) 
	{	

		client_print_color(id, "!g%s !tNie jestes Zalogowany", prefix);
		menu_account(id)
	}
	else
	{	
		client_print_color(id, "!g%s !tWprowadz aktualne haslo", prefix);		
		client_cmd(id,"messagemode WPROWADZ_OBECNE_HASLO")
		g_unlock[id] = false
	}	
	return PLUGIN_HANDLED
}

public delete_account(id) 
{	
	if(!g_logged[id]) {
		client_print_color(id, "!g%s !tNie jestes Zalogowany", prefix);		
		menu_account(id)
	}
	else 
	{

		client_print_color(id, "!g%s !tWpisz swoje haslo", prefix);			
		client_cmd(id,"messagemode WPROWADZ_OBECNE_HASLO")
		g_unlock[id] = false
		g_deleted_account[id] = true
	}
	return PLUGIN_HANDLED
}

public invited(id) 
{
	new szName[32]
	get_user_name(id, szName, charsmax(szName))
	
	if(g_registered[id]) 
	{	

		client_print_color(id, "!g%s !tZarejestrowani gracze nie moga wejsc jako Gosc", prefix);		
		menu_account(id)
	}	
	else if(g_invited[id]) 
	{	

		client_print_color(id, "!g%s !tJestes juz polaczony jako Gosc", prefix);
		menu_account(id)
	}	
	else 
	{	
		client_print_color(id, "!g%s !tWszedles jako gosc", prefix);		
		client_cmd(id, "jointeam")
		ExecuteForward(g_Forwards[FW_USER_LOGGED_INVITED], g_ForwardResult, id)
		g_invited[id] = true
	}	
	return PLUGIN_HANDLED
}

public cmd_password(id) {
	
	if(g_unlock[id]) return PLUGIN_HANDLED
	
	new szName[32], szData[64]
	get_user_name(id, szName, charsmax(szName))
	fvault_get_data(g_fvault, szName, szData, charsmax(szData))		
	
	read_args(g_password[id], 50)
	remove_quotes(g_password[id])
	trim(g_password[id])
	
	g_unlock[id] = true
	
	if(!characters(g_password[id], strlen(g_password[id]))) 
	{	
		client_print_color(id, "!g%s !tDozwolone sa tylko cyfry i litery", prefix);		
		menu_account(id)
	}	
	else if(strlen(g_password[id]) < get_pcvar_num(cvar_mim_characters)) 
	{	
		client_print_color(id, "!g%s !tTwoje haslo musi zawierac minimum %i znakow.", prefix, get_pcvar_num(cvar_mim_characters));		
		menu_account(id)
	}	
	else if(strlen(g_password[id]) > get_pcvar_num(cvar_max_characters)) 
	{	
		client_print_color(id, "!g%s !tTwoje haslo moze zawierac maxymalnie %i znakow.", prefix, get_pcvar_num(cvar_max_characters));		
		menu_account(id)
	}		
	else if(equal(g_password[id], szName[id])) 
	{	
		client_print_color(id, "!g%s !tTwoje haslo nie moze byc takie samo jak nick.", prefix);		
		menu_account(id)
	}
	else if(g_logged[id]) 
	{	
		if(!equal(szData, g_password[id])) 
		{	
			client_print_color(id, "!g%s !tWprowadz swoje haslo ponownie", prefix);		
			client_cmd(id,"messagemode WPROWADZ_PONOWNIE_HASLO")
			g_unlock[id] = false
		}
	}
	else if(!g_registered[id]) 
	{
		client_print_color(id, "!g%s !tWprowadz swoje haslo ponownie", prefix);		
		client_cmd(id,"messagemode WPROWADZ_PONOWNIE_HASLO")
		g_unlock[id] = false
	}
	else 
	{
		if(equal(szData, g_password[id])) 
		{	
			client_print_color(id, "!g%s !tZostales zalogowany.", prefix);		
			remove_task(id)
			client_cmd(id, "jointeam")
			ExecuteForward(g_Forwards[FW_USER_LOGGED], g_ForwardResult, id)
			
			g_logged[id] = true
			g_attempts[id] = 0
		}
		else {
			wrong_password(id)
		}		
	}
	return PLUGIN_HANDLED
}

public cmd_password_again(id) 
{	
	if(g_unlock[id]) 
		return PLUGIN_HANDLED
	
	read_args(g_password_again[id], 50)
	remove_quotes(g_password_again[id])
	trim(g_password_again[id])
	
	g_unlock[id] = true
	
	if(!equal(g_password[id], g_password_again[id])) 
	{
		client_print_color(id, "!g%s !tHasla nie sa takie same.", prefix);		
		menu_account(id)
	}	
	else if(!g_registered[id]) 
	{
		client_print_color(id, "!g%s !tPotwierdzenie danych.", prefix);		
		menu_end_register(id)
	}	
	else {
		client_print_color(id, "!g%s !tPotwierdzenie zmiany hasla.", prefix);		
		menu_change_password(id)
	}	
	return PLUGIN_HANDLED
}

public cmd_password_current(id) 
{	
	if(g_unlock[id]) 
		return PLUGIN_HANDLED
	
	new szName[32], szData[64]
	get_user_name(id, szName, charsmax(szName))
	fvault_get_data(g_fvault, szName, szData, charsmax(szData))		
	
	read_args(g_password[id], 50)
	remove_quotes(g_password[id])
	trim(g_password[id])
	
	g_unlock[id] = true
	
	if(equal(szData, g_password[id])) 
	{	
		if(g_deleted_account[id]) 
		{	
			menu_delete_account(id)
			g_unlock[id] = false
			g_deleted_account[id] = false
		}
		else 
		{
			client_print_color(id, "!g%s !tWprowadz nowe haslo.", prefix);
		
			client_cmd(id,"messagemode WPISZ_SWOJE_HASLO")
			g_unlock[id] = false
		}			
	}
	else {
		wrong_password(id)
	}
	return PLUGIN_HANDLED
}

public menu_end_register(id) 
{	
	new szMenu[128], szName[32]
	get_user_name(id, szName, charsmax(szName))
	
	formatex(szMenu, 127, "\rRejestracja Zakonczona^n^n\yNick: \r[\d%s\r]^n\yTwoje haslo bedzie: \r[\d%s\r]", szName, g_password_again[id]);
	
	
	new Menu = menu_create(szMenu, "handler_menu_end_register")
	
	formatex(szMenu, 63, "Potwierdz Haslo")
	menu_additem(Menu, szMenu, "1", 0)
	
	formatex(szMenu, 63, "\rWprowadz inne haslo^n")
	menu_additem(Menu, szMenu, "2", 0)
	
	formatex(szMenu, 63, "\yAnuluj")
	menu_additem(Menu, szMenu, "3", 0)
	
	menu_setprop(Menu, MPROP_EXIT, MEXIT_NEVER)
	menu_display(id, Menu, 0)
}

public handler_menu_end_register(id, menu, item) 
{	
	if(item == MENU_EXIT) {
		menu_destroy(menu)
		return PLUGIN_HANDLED
	}
	switch(item) 
	{	
		case 0: end_register(id)
		case 1: register(id)
		case 2: menu_account(id)
	}
	menu_destroy(menu)
	return PLUGIN_HANDLED
}

public end_register(id) 
{	
	new szName[32]
	get_user_name(id, szName, charsmax(szName))
	
	fvault_set_data(g_fvault, szName, g_password_again[id])
	

	client_print_color(id, "!g%s !tRejestracja przebiegla pomyslnie", prefix);
	client_print_color(id, "!g%s !tNick: !y[!g %s !y] !tHaslo: !y[!g %s !y]", prefix, szName, g_password_again[id]);	
	remove_task(id)
	
	ExecuteForward(g_Forwards[FW_USER_REGISTER], g_ForwardResult, id)
	g_registered[id] = true
	g_logged[id] = true
	
	if(g_invited[id]) {
		menu_account(id)
		g_invited[id] = false
	}
	else {
		client_cmd(id, "jointeam")
	}
}
public menu_change_password(id) 
{	
	new szMenu[128], szName[32]
	get_user_name(id, szName, charsmax(szName))
		
	formatex(szMenu, 127, "\rZmien Haslo^n^n\yNick: \r[\d%s\r]^n\yHaslo: \r[\d%s\r]", szName, g_password_again[id]);
	
	new Menu = menu_create(szMenu,"handler_menu_change_password")
	
	formatex(szMenu, 63, "Potwierdz Haslo")
	menu_additem(Menu, szMenu, "1", 0)
	
	formatex(szMenu, 63, "\rWprowadz inne Haslo")
	menu_additem(Menu, szMenu, "2", 0)
	
	formatex(szMenu, 63, "\yAnuluj")
	menu_additem(Menu, szMenu, "3", 0)
	
	menu_setprop(Menu, MPROP_EXIT, MEXIT_NEVER)
	menu_display(id, Menu, 0)
}

public handler_menu_change_password(id, menu, item) 
{	
	if(item == MENU_EXIT) {
		menu_destroy(menu)
		return PLUGIN_HANDLED
	}
	switch(item) 
	{
		case 0: replaced_password(id)
		case 1: change(id)
		case 2: menu_account(id)
	}
	menu_destroy(menu)
	return PLUGIN_HANDLED
}

public replaced_password(id) 
{	
	new szName[32]
	get_user_name(id, szName, charsmax(szName))
	fvault_set_data(g_fvault, szName, g_password_again[id])
	

	client_print_color(id, "!g%s !tZmiana Hasla.", prefix);
	client_print_color(id, "!g%s !tNowe Haslo: !y[!g %s !y]", prefix, g_password_again[id]);		
	g_attempts[id] = 0
}

public menu_delete_account(id) 
{	
	new szMenu[128]
		
	formatex(szMenu, 127, "\rCzy na pewno chcesz skasowac swoje konto ?");
	
	new Menu = menu_create(szMenu,"handler_menu_delete_account")
	
	formatex(szMenu, 63, "TAK")
	menu_additem(Menu, szMenu, "1", 0)
	
	formatex(szMenu, 63, "\rNIE")
	menu_additem(Menu, szMenu, "2",0)
	
	menu_setprop(Menu, MPROP_EXIT, MEXIT_NEVER)
	menu_display(id, Menu, 0)
}

public handler_menu_delete_account(id, menu, item) 
{	
	if(item == MENU_EXIT) {
		menu_destroy(menu)
		return PLUGIN_HANDLED
	}
	switch(item) 
	{	
		case 0: account_deleted(id)
		case 1: menu_account(id)
	}
	menu_destroy(menu)
	return PLUGIN_HANDLED
}

public account_deleted(id) 
{	
	new szName[32]
	get_user_name(id, szName, charsmax(szName))
	
	fvault_remove_key(g_fvault, szName)
	
	ExecuteForward(g_Forwards[FW_USER_ACCOUNT_DELETED], g_ForwardResult, id)
	
	client_print(id, print_console, "----------------------------------------")
	client_print(id, print_console, "----- Twoje konto zostalo usuniete -------")
	client_print(id, print_console, "----------------------------------------")	
	client_cmd(id, "disconnect")
	client_cmd(id, "toggleconsole")	
}

public wrong_password(id) 
{	
	g_attempts[id]++
	if(g_attempts[id] >= get_pcvar_num(cvar_attempts)) 
	{	
		server_cmd("amx_banip #%i %i ^"Zostales zbanowany na %d, zbyt wiele razy wprowadziles bledne haslo.^"", get_user_userid(id), get_pcvar_num(cvar_bantime), get_pcvar_num(cvar_bantime))
		remove_task(id)
		g_attempts[id] = 0
	}
	else 
	{
		client_print_color(id, "!g%s !tWprowadzono bledne haslo !y[!g %d !y/!g %d !y]", prefix, g_attempts[id], get_pcvar_num(cvar_attempts));
		menu_account(id)
	}
}

public login_time(id) 
{	
	new szName[32]
	get_user_name(id, szName, charsmax(szName))
	
	if(!g_logged[id] && !g_invited[id] && !(get_user_flags(id) & ADMIN_BAN) && !is_user_hltv(id) && !is_user_bot(id) ) 
	{
		
		client_print(id, print_console, "----------------------------------------")
		client_print(id, print_console, "Nick: *%s *", szName)
		client_print(id, print_console, "Powod: *Nie zalogowano sie na konto *")
		client_print(id, print_console, "Ostrzezenie: *Musisz odczekac %d.0 sekund aby moc ponownie wejsc *", get_pcvar_num(cvar_logintime))
		
		client_print(id, print_console, "----------------------------------------")
		client_print_color(0, "!g%s !y%s !tZostal rozlaczony z powodu dlugotrwalego logowania sie na konto.", prefix, szName)
		client_cmd(id, "disconnect")
		client_cmd(id, "toggleconsole")
	}
}
public PlayerPreThink(id) {
	
	if(!g_logged[id] && !g_invited[id] && !(get_user_flags(id) & ADMIN_BAN) && !is_user_bot(id) && !is_user_hltv(id) && get_pcvar_num(cvar_screenfade)) {
		
		message_begin(MSG_ONE_UNRELIABLE, g_screenfade, {0,0,0}, id)
		write_short(1<<12)
		write_short(1<<12)
		write_short(0x0000)
		write_byte(0)
		write_byte(0)
		write_byte(0)
		write_byte(255)
		message_end()		
	}
}

public commands(id) {
	
	new text[70], arg1[32], arg2[32], arg3[6]
	read_args(text, sizeof(text) - 1)
	remove_quotes(text)
	arg1[0] = '^0'; arg2[0] = '^0'; arg3[0] = '^0'
	parse(text, arg1, sizeof(arg1) - 1, arg2, sizeof(arg2) - 1, arg3, sizeof(arg3) - 1)
	
	if(equali(arg1, "/", 1) || equali(arg1, ".", 1)) format(arg1, 31, arg1[1])
	
	if(arg3[0]) return PLUGIN_CONTINUE
	
	if(equali(arg1, "konto") || equali(arg1, "rejestracja") || equali(arg1, "logowanie")) {
		menu_account(id)
	}
	return PLUGIN_CONTINUE
}

public native_is_user_logged(id) 
{	
	if(!is_user_connected(id))
		return 0
	
	return g_logged[id]
}

public native_is_user_registered(id) 
{	
	if(!is_user_connected(id))
		return 0
	
	return g_registered[id]
}

public native_is_user_invited(id) 
{	
	if(!is_user_connected(id))
		return 0
	
	return g_invited[id]
}

bool:characters(const symbol[], len) 
{	
	new const valid_chars[][] = 
	{	
		"0" ,"1" ,"2" ,"3" ,"4" ,"5" ,"6" ,"7" ,"8" ,"9",
		"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
		"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
		"u", "v", "w", "x", "y", "z"
	}
	static i, a, valids;
	valids = 0
	
	for(i = 0; i < len; i++) 
	{	
		for(a = 0; a < sizeof(valid_chars); a++) 
		{	
			if(symbol[i] == valid_chars[a][0]) 
			{	
				valids++
				break
			}
		}
	}
	if(valids != len)
		return false
		
	return true
}

stock client_print_color(const id, const input[], any:...) 
{	
	new count = 1, players[32]
	static msg[191]
	vformat(msg, 190, input, 3)
	
	replace_all(msg, 190, "!g", "^4")
	replace_all(msg, 190, "!y", "^1")
	replace_all(msg, 190, "!t", "^3")
	replace_all(msg, 190, "!team2", "^0")
	
	if (id)
		players[0] = id;
	else
		get_players(players, count, "ch")
	
	for (new i = 0; i < count; i++) {
		
		if (is_user_connected(players[i])) {
			
			message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("SayText"), _, players[i])
			write_byte(players[i]);
			write_string(msg);
			message_end();
		}
	}
}
