#include <amxmodx>
#include <dhudmessage>
#include <hamsandwich>
#include <cstrike>
#include <chatcolor>
#include <engine>
#include <fun>
#include <sqlx>
#include <fakemeta>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"

new const Crprefixx[] = "[InformA?ciAl]";

new const SqlN[][] = {  "localhost", "root", "", "teszt" };

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

new valtozouj[33];

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
new gFelhasznalo[33][100], gJelszo[33][100], gMenu[33], gId[33], gFolyamatban[33], bool:gBejelentkezes[33];

new Handle:SqlTuple;

public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	register_impulse(201, "RegisztMenu");
	
	register_clcmd("Jelszo", "cmdJelszo");
	register_clcmd("Felhasznalo", "cmdFelhasznalo");
	// Add your code here...
}
public RegisztMenu(id) {
	new menu = menu_create("RegisztrA?ciAls MenAL^n A mAldot A­rta A©s fejleszti: Toretto", "Regi_h");
	
	menu_additem(menu, "RegisztrA?lA?s", "0", 0);
	menu_additem(menu, "BejelentkezA©s", "1", 0);
	
	menu_display(id, menu, 0);
}
public Regi_h(id, menu, item) {
	switch(item) {
		case MENU_EXIT: {
			menu_destroy(menu);
			return;
		}
		case 0: {
			RegFomenu(id);
			gMenu[id] = 0;
		}
		case 1: {
			RegFomenu(id);
			gMenu[id] = 1;
		}
	}
}
public RegFomenu(id) {
	new cim[121];
	new menu = menu_create(gMenu[id] == 0? "ReigsztrA?ciAls MenAL^n A mAldot A­rta A©s fejleszti: Toretto ":"BejelentkezA©s MenAL^n A mAldot A­rta A©s fejleszti: Toretto", "RegFo_h");
	
	format(cim, charsmax(cim), "FelhasznA?lAlnA©v: %s", gFelhasznalo[id]);
	menu_additem(menu, cim, "0", 0);
	
	format(cim, charsmax(cim), "Jelszo: %s", gJelszo[id]);
	menu_additem(menu, cim, "1", 0);
	
	menu_additem(menu, gMenu[id] == 0 ? "RegisztrA?lA?s!":"BejelentkezA©s!", "2",0);
	
	menu_display(id, menu, 0);
}
public RegFo_h(id, menu, item) {
	if(item == MENU_EXIT)
	{
		menu_destroy(menu);
		return;
	}
	
	new data[9], szName[64];
	new access, callback;
	menu_item_getinfo(menu, item, access, data,charsmax(data), szName,charsmax(szName), callback);
	new key = str_to_num(data);
	
	switch(key)
	{
		case 0: client_cmd(id, "messagemode Felhasznalo");
			case 1: client_cmd(id, "messagemode Jelszo");
			case 2: cmdBejelentkezes(id);
		}
}
public cmdBejelentkezes(id) {
	if(gBejelentkezes[id] == true)
		return PLUGIN_HANDLED;
	
	
	if((strlen(gFelhasznalo[id]) == 0)) {
		client_printcolor(id, "^4[%s] ^1Nem adtA?l meg felhaszA?lAlnevet!", Crprefixx);
		RegFomenu(id)
		return PLUGIN_HANDLED;
	}
	if((strlen(gJelszo[id]) == 0)) {
		client_printcolor(id, "^4[%s] ^1Nem adtA?l meg jelszAlt!", Crprefixx);
		RegFomenu(id)
		return PLUGIN_HANDLED;
	}
	
	if(gMenu[id] ==  0) {
		if(gFolyamatban[id] == 0) {
			client_printcolor(id, "^4[%s] ^1RegisztrA?lA?s folyamatban van.... KA©rlek vA?rj!", Crprefixx);
			SqlAccBetolt(id);
			RegFomenu(id);
			gFolyamatban[id] = 1;
			return PLUGIN_HANDLED;
		}
		else RegFomenu(id);
	}
	if(gMenu[id] ==  1) {
		if(gFolyamatban[id] == 0) {
			client_printcolor(id, "^4[%s] ^1BejelentkezA©s folyamatban van.... KA©rlek vA?rj!", Crprefixx);
			SqlAccBetolt(id);
			RegFomenu(id);
			gFolyamatban[id] = 1;
			return PLUGIN_HANDLED;
		}
		else RegFomenu(id);
	}
	
	return PLUGIN_CONTINUE;
}
public cmdJelszo(id) {
	if(gBejelentkezes[id] == true)
		return PLUGIN_HANDLED;
	
	gJelszo[id][0] = EOS;
	read_args(gJelszo[id], 99);
	remove_quotes(gJelszo[id]);
	
	if((strlen(gJelszo[id]) < 4 || strlen(gJelszo[id]) > 20)) {
		client_printcolor(id, "^4[%s] ^1A jelszavad nem lehet rA¶videbb mint 4, illetve hosszabb 20 karakternA©l!.", Crprefixx);
		gJelszo[id][0] = EOS;
	}
	
	RegFomenu(id);
	return PLUGIN_HANDLED;
}
public cmdFelhasznalo(id) {
	if(gBejelentkezes[id] == true)
		return PLUGIN_HANDLED;
	
	gFelhasznalo[id][0] = EOS;
	read_args(gFelhasznalo[id], 99);
	remove_quotes(gFelhasznalo[id]);
	
	if((strlen(gFelhasznalo[id]) < 4 || strlen(gFelhasznalo[id]) > 20)) {
		client_printcolor(id, "^4[%s] ^1A felhasznA?lAlneved nem lehet rA¶videbb mint 4, illetve hosszabb 20 karakternA©l!.", Crprefixx);
		gFelhasznalo[id][0] = EOS;
	}
	
	RegFomenu(id);
	return PLUGIN_HANDLED;
}
public client_putinserver(id)
{
	gFolyamatban[id] = 0
	gBejelentkezes[id] = false
	copy(gJelszo[id], 31, "")
	copy(gFelhasznalo[id], 31, "")
	
	
}



public client_disconnect(id)
{  
	gFolyamatban[id] = 0;
	
	if(gBejelentkezes[id]) {
		SqlAccFissites(id)
	}
	
	
	gBejelentkezes[id] = false;
	
}


public plugin_cfg()
{
	SqlTuple = SQL_MakeDbTuple(SqlN[0], SqlN[1], SqlN[2], SqlN[3]);
	
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public SqlvaltozoBetolt(id) {
	new Query[2048], Len = 0;
	
	new a[191];
	
	format(a, 190, "%s", gFelhasznalo[id]);
	
	replace_all(a, 190, "\", "\\")
	replace_all(a, 190, "'", "\'")
	
	Len += format(Query[Len], charsmax(Query), "SELECT * FROM valtozok ");
	Len += format(Query[Len], charsmax(Query)-Len, "WHERE Felhasznalo = '%s'", a);
	
	new Data[2]
	Data[0] = id;
	Data[1] = get_user_userid(id);
	
	SQL_ThreadQuery(SqlTuple, "SqlvaltozoBetoltQwe", Query, Data, 2);
}
public SqlvaltozoBetoltQwe(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) {
	if(FailState == TQUERY_CONNECT_FAILED)
	{
		set_fail_state("[ *HIBA* ] NEM LEHET KAPCSOLODNI AZ ADATBAZISHOZ!")
		return
	}
	else if(FailState == TQUERY_QUERY_FAILED)
	{
		set_fail_state("[ *HIBA* ] A LEKERDEZES MEGSZAKADT!")
		return
	}
	
	if(Errcode)
	{
		log_amx("[ *HIBA* ] PROBLEMA A LEKERDEZESNEL! ( %s )",Error)
		return
	}
	
	new id = Data[0];
	
	SqlvaltozoLoad(id);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public SqlAccBetolt(id) {
	new Query[2048], Len = 0;
	
	new a[191];
	
	format(a, 190, "%s", gFelhasznalo[id]);
	
	replace_all(a, 190, "\", "\\")
	replace_all(a, 190, "'", "\'")
	
	Len += format(Query[Len], charsmax(Query), "SELECT * FROM regi_sql ");
	Len += format(Query[Len], charsmax(Query)-Len, "WHERE Felhasznalo = '%s'", a);
	
	new Data[2]
	Data[0] = id;
	Data[1] = get_user_userid(id);
	
	SQL_ThreadQuery(SqlTuple, "SqlAccBetoltQwe", Query, Data, 2);
}
public SqlAccBetoltQwe(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) {
	if(FailState == TQUERY_CONNECT_FAILED)
	{
		set_fail_state("[ *HIBA* ] NEM LEHET KAPCSOLODNI AZ ADATBAZISHOZ!")
		return
	}
	else if(FailState == TQUERY_QUERY_FAILED)
	{
		set_fail_state("[ *HIBA* ] A LEKERDEZES MEGSZAKADT!")
		return
	}
	
	if(Errcode)
	{
		log_amx("[ *HIBA* ] PROBLEMA A LEKERDEZESNEL! ( %s )",Error)
		return
	}
	
	new id = Data[0];
	
	if(Data[1] != get_user_userid(id))
		return;
	
	new Found = SQL_NumRows(Query);
	
	if(gMenu[id] == 0) {
		if(Found > 0) {
			client_printcolor(id, "^4[%s] ^1Ez a felhasznA?lAlnA©v mA?r foglalt!", Crprefixx);
			gFolyamatban[id] = 0;
			RegFomenu(id);
		}
		else SqlAccKeszit(id);
	}
	else if(gMenu[id] == 1) {
		if(Found == 0) {
			client_printcolor(id, "^4[%s] ^1HibA?s jelszAl vagy felhasznA?lAlnA©v!", Crprefixx);
			gFolyamatban[id] = 0;
			RegFomenu(id);
		}
		else SqlAccLoad(id);
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public SqlvaltozoKeszit(id) {
	new Query[2048], Len = 0;
	
	new a[191];
	
	format(a, 190, "%s", gFelhasznalo[id]);
	
	replace_all(a, 190, "\", "\\")
	
	new Nev[32]; get_user_name(id, Nev, 31);
	
	Len += format(Query[Len], charsmax(Query), "INSERT INTO valtozok ");
	Len += format(Query[Len], charsmax(Query)-Len, "(Felhasznalo) VALUES ('%s')", a);
	
	new Data[2]
	Data[0] = id;
	Data[1] = get_user_userid(id);
	
	SQL_ThreadQuery(SqlTuple,"SqlvaltozoKeszitQwe", Query, Data, 2)
}
public SqlvaltozoKeszitQwe(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) {
	if(FailState == TQUERY_CONNECT_FAILED)
	{
		set_fail_state("[ *HIBA* ] NEM LEHET KAPCSOLODNI AZ ADATBAZISHOZ!")
		return
	}
	else if(FailState == TQUERY_QUERY_FAILED)
	{
		set_fail_state("[ *HIBA* ] A LEKERDEZES MEGSZAKADT!")
		return
	}
	
	if(Errcode)
	{
		log_amx("[ *HIBA* ] PROBLEMA A LEKERDEZESNEL! ( %s )",Error)
		return
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public SqlAccKeszit(id) {
	new Query[2048], Len = 0;
	
	new a[191], b[191], c[191];
	
	format(a, 190, "%s", gFelhasznalo[id]);
	format(b, 190, "%s", gJelszo[id]);
	
	replace_all(a, 190, "\", "\\")
	replace_all(a, 190, "'", "\'")
	replace_all(b, 190, "\", "\\")
	replace_all(b, 190, "'", "\'")
	
	new Nev[32]; get_user_name(id, Nev, 31);
	
	format(c, 190, "%s", Nev);
	
	replace_all(c, 190, "\", "\\")
	replace_all(c, 190, "'", "\'")
	
	Len += format(Query[Len], charsmax(Query), "INSERT INTO regi_sql ");
	Len += format(Query[Len], charsmax(Query)-Len, "(Felhasznalo, Jelszo,Jatekosnev) VALUES ('%s', '%s', '%s')", a, b, c);
	
	new Data[2]
	Data[0] = id;
	Data[1] = get_user_userid(id);
	
	SQL_ThreadQuery(SqlTuple,"SqlAccKeszitQwe", Query, Data, 2)
}
public SqlAccKeszitQwe(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) {
	if(FailState == TQUERY_CONNECT_FAILED)
	{
		set_fail_state("[ *HIBA* ] NEM LEHET KAPCSOLODNI AZ ADATBAZISHOZ!")
		return
	}
	else if(FailState == TQUERY_QUERY_FAILED)
	{
		set_fail_state("[ *HIBA* ] A LEKERDEZES MEGSZAKADT!")
		return
	}
	
	if(Errcode)
	{
		log_amx("[ *HIBA* ] PROBLEMA A LEKERDEZESNEL! ( %s )",Error)
		return
	}
	
	new id = Data[0];
	
	if(Data[1] != get_user_userid(id))
		return;
	
	client_printcolor(id, "^4[%s] ^1Sikeresen RegisztrA?ltA?l! FelhasznA?lAlnA©v: ^4%s ^1| JelszAl: ^4%s", Crprefixx, gFelhasznalo[id], gJelszo[id]);
	gFolyamatban[id] = 0;
	gMenu[id] = 1;
	RegFomenu(id);
	SqlvaltozoKeszit(id);
	return;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public SqlvaltozoLoad(id) {
	new Query[2048], Len = 0;
	
	Len += format(Query[Len], charsmax(Query), "SELECT * FROM valtozok ");
	Len += format(Query[Len], charsmax(Query)-Len, "WHERE Felhasznalo = '%s'", gFelhasznalo[id]);
	
	new Data[2];
	Data[0] = id;
	Data[1] = get_user_userid(id);
	
	SQL_ThreadQuery(SqlTuple,"SqlvaltozoLoadQwe", Query, Data, 2);
}
public SqlvaltozoLoadQwe(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) {
	if(FailState == TQUERY_CONNECT_FAILED || FailState == TQUERY_QUERY_FAILED)
	{
		log_amx("%s", Error)
		return
	}
	else
	{
		new id = Data[0];
		
		if (Data[1] != get_user_userid(id)) 
			return ;
		
		valtozouj[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "Valtozouj"));
		
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public SqlAccLoad(id) {
	new Query[2048], Len = 0;
	
	Len += format(Query[Len], charsmax(Query), "SELECT * FROM regi_sql ");
	Len += format(Query[Len], charsmax(Query)-Len, "WHERE Felhasznalo = '%s'", gFelhasznalo[id]);
	
	new Data[2];
	Data[0] = id;
	Data[1] = get_user_userid(id);
	
	SQL_ThreadQuery(SqlTuple,"SqlAccLoadQwe", Query, Data, 2);
}
public SqlAccLoadQwe(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) {
	if(FailState == TQUERY_CONNECT_FAILED || FailState == TQUERY_QUERY_FAILED)
	{
		log_amx("%s", Error)
		return
	}
	else
	{
		new id = Data[0];
		
		if (Data[1] != get_user_userid(id))
			return ;
		
		new Password[100];
		SQL_ReadResult(Query, 2, Password, 99);
		
		if(equal(gJelszo[id], Password)) {
			if ( is_online( id ) )
			{
				client_printcolor(id, "^4[%s] ^3%s ^1mA?r be van jelentkezve!", Crprefixx, gFelhasznalo[id]);
				RegFomenu(id);
				return;
			}
			gId[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "Id"));
			
			
			
			client_printcolor(id, "!g[%s] !nSzia,!t %s!n (#!g%d!n) Sikeresen BejelenetkeztA©l!", Crprefixx, gFelhasznalo[id], gId[id]);
			gFolyamatban[id] = 0
			gBejelentkezes[id] = true;
			SqlvaltozoBetolt(id);
		}
		else  {
			client_printcolor(id, "^4[%s] ^1HibA?s jelszAl vagy felhasznA?lAlnA©v!", Crprefixx);
			gFolyamatban[id] = 0;
			RegFomenu(id);
		}
	}
}

public SqlAccFissites(id) {
	new Query[2048], Len = 0;
	
	new c[191];
	
	new Nev[32]; get_user_name(id, Nev, 31);
	
	format(c, 190, "%s", Nev);
	
	replace_all(c, 190, "\", "\\")
	replace_all(c, 190, "'", "\'")
	
	Len += format(Query[Len], charsmax(Query), "UPDATE regi_sql SET ");
	
	
	Len += format(Query[Len], charsmax(Query)-Len, "Jatekosnev = '%s' ", c);
	
	Len += format(Query[Len], charsmax(Query)-Len,"WHERE Id = '%d'", gId[id]);
	
	SQL_ThreadQuery(SqlTuple,"SqlUpdateQwe", Query);
}
public SqlUpdateQwe(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) {
	if(FailState == TQUERY_CONNECT_FAILED){
		return set_fail_state("[ *HIBA* ] NEM LEHET KAPCSOLODNI AZ ADATBAZISHOZ!")
	}else if(FailState == TQUERY_QUERY_FAILED){
		return set_fail_state("[ *HIBA* ] A LEKERDEZES MEGSZAKADT!")
	}
	if(Errcode){ 
		return log_amx("[ *HIBA* ] PROBLEMA A LEKERDEZESNEL! ( %s )",Error)
	}
	return PLUGIN_CONTINUE
}

stock is_online( iPlayerId )
{
	for ( new i = 1; i < 33; ++i )
		if ( gBejelentkezes[ i ] )
		if ( equal( gFelhasznalo[ iPlayerId ], gFelhasznalo[ i ] ) )
		return i;
	return 0;
}
stock client_printcolor(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, "!n", "^1")
	replace_all(msg, 190, "!t", "^3")    
	
	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()
			}
		}
	}return PLUGIN_HANDLED
}