/* Includes */

#include < amxmodx >
#include < amxmisc >
#include < sqlvault_ex >
#include < cstrike >
#include < colorchat >
#include < hamsandwich >
#include < fun >
#include < dhudmessage >
#include < codmod >


#define DMG_BULLET (1<<1)


enum _:GildiaInfo
{
	Trie:GraczeGildi,
	NazwaGildi[ 64 ],
	GildiaHP,
	GildiaEXP,
	GildiaREG,
	GildiaODB,
	NumGraczy
};
enum
{
	VALUE_HP,
	VALUE_EXP,
	VALUE_REG,
	VALUE_ODB
}
enum
{
	STATUS_NONE,
	STATUS_MEMBER,
	STATUS_ADMIN,
	STATUS_LEADER
};

new const g_szGangValues[ ][ ] = 
{
	"HP",
	"EXP",
	"REG",
	"ODB"
};

new const gPrefix[ ] = "^04[COD Elite | Gildie]^01";

/* Tries */

new Trie:g_iNazwyGildi;
new Trie:g_iGildiaValues;

/* Vault */

new SQLVault:g_hVault;

/* Arrays */

new Array:g_aGildie;


/* Pcvars */

new g_KosztGildi;
new g_PoziomGildi

new g_pKosztHP;
new g_pKosztEXP;
new g_pKosztREG;
new g_pKosztODB;

new g_pPoziomHP;
new g_pPoziomEXP;
new g_pPoziomREG;
new g_pPoziomODB;

new g_UlepszenieHP;
	
new g_pMaxGraczy;

/* Integers */

new g_iGildia[ 33 ];

new pozostale_strzaly[33];

public plugin_init()
{
	register_plugin( "COD Elite | Gildie", "1.0", "Play" );
	
	g_aGildie 				= ArrayCreate( GildiaInfo );
	
	g_iGildiaValues 			= TrieCreate();
	g_iNazwyGildi 				= TrieCreate();
	
	g_hVault 				= sqlv_open_local( "Gildie", false );
	sqlv_init_ex( g_hVault );
	
	
	g_KosztGildi				= register_cvar( "KosztGildi",		"1000" );	// Koszt Stworzenia Gildi
	g_PoziomGildi				= register_cvar( "PoziomGildi", 		"400" );	// Wymagany poziom do stworzenia Gildi
	g_pMaxGraczy				= register_cvar( "MaxGraczGildi",	"10" );		// Maxymalna liczba graczy w Gildi
	
	
	g_pKosztHP				= register_cvar( "KoszHP", 		"1000" );	// Cena 1 ulepszenia
	g_pKosztEXP				= register_cvar( "KosztEXP",		"500" );	// Cena 1 ulepszenia
	g_pKosztREG				= register_cvar( "KosztReg",		"2000" );	// Cena 1 ulepszenia
	g_pKosztODB				= register_cvar( "KosztODB",		"5000" );	// Cena 1 ulepszenia
	
	g_pPoziomHP				= register_cvar( "MaxPoziomHP", 		"10" );		// Maxymalny poziom umiejetnosci HP
	g_pPoziomEXP				= register_cvar( "MaxPoziomEXP",	"50" );		// Maxymalny poziom EXPA za zabojstwo
	g_pPoziomREG				= register_cvar( "MaxPoziomREG",	"10" );		// Maxymalny poziom Regeneracji HP
	g_pPoziomODB				= register_cvar( "MaxPoziomODB",	"5" );		// Maxumlany poziom Odbicia pociskow
	
	g_UlepszenieHP				= register_cvar( "MaxHP", 		"10" );		//  1 Ulepszenie, ile HP

	
		
	register_menu( "Gildia Menu", 1023, "MenuGildi_Handler" );
	register_menu( "Umiejetnosci", 1023, "Umiejetnosci_Handler" );
	
	for( new i = 0; i < sizeof g_szGangValues; i++ )
	{
		TrieSetCell( g_iGildiaValues, g_szGangValues[ i ], i );
	}
	register_event("DeathMsg", "DeathMsg", "a");
	//RegisterHam(Ham_Spawn, "player", "Spawn", 1);
	RegisterHam(Ham_TakeDamage, "player", "TakeDamage");
	register_logevent("Poczatek_Rundy", 2, "1=Round_Start")
	///register_event("HLTV", "Nowa_Runda", "a", "1=0", "2=0") 
			
	register_clcmd("say /Gildie", "MenuGildi");
	register_clcmd("say /Gildia", "MenuGildi");
		
	register_clcmd( "say /test", "Gracz" );
	register_clcmd( "Wpisz_Nazwe_Gildi", "StworzGildie" );	
	WczytajGildie();
}

public player_count()
{
	new iPlayers[32], iNum, count
	get_players(iPlayers, iNum)
	for(new i=0;i<iNum;i++)
	{
		if(is_user_connected(iPlayers[i]) && !is_user_bot(iPlayers[i]) && !is_user_hltv(iPlayers[i]))
		{
			count++
		}
	}
	return count
}


public client_disconnect( id )
{
	g_iGildia[ id ] = -1;	
}

public client_putinserver( id )
{
	g_iGildia[ id ] = get_user_Gildia( id );
	set_task(5.0, "Regeneracja", id, _, _, "b");
}
public plugin_end()
{
	ZapiszGildie();
	sqlv_close( g_hVault );
}
public TakeDamage(this, idinflictor, idattacker, Float:damage, damagebits, id)
{
	if( g_iGildia[ id ] == -1 )
	{
		return PLUGIN_CONTINUE
	}
	new aData[ GildiaInfo ];
	ArrayGetArray( g_aGildie, g_iGildia[ id ], aData );

	if(pozostale_strzaly[this] > 0 && damagebits & DMG_BULLET)
	{
		pozostale_strzaly[this]--;	
		return HAM_SUPERCEDE;

	}
	return HAM_IGNORED;
}
public Poczatek_Rundy()
{
	for(new i=1; i < 32; i++)	
	{	
		new aData[ GildiaInfo ];
		ArrayGetArray( g_aGildie, g_iGildia[ i ], aData );
		
		pozostale_strzaly[i] = aData[ GildiaODB ]
	}
}
public Regeneracja( id )
{
	if( g_iGildia[ id ] == -1 )
	{
		return PLUGIN_CONTINUE
	}
	new aData[ GildiaInfo ];
	ArrayGetArray( g_aGildie, g_iGildia[ id ], aData );
	
	if( get_user_health(id) < 600 )
	{
		set_user_health(id, get_user_health(id) + aData[ GildiaREG ] );

	}
	return PLUGIN_CONTINUE
}

public DeathMsg( id )
{					
	new killer = read_data(1);
	new aData[ GildiaInfo ];
	ArrayGetArray( g_aGildie, g_iGildia[ id ], aData );
	
	cod_set_user_xp(killer, cod_get_user_xp(killer) + aData[ GildiaEXP ] );
	
	return PLUGIN_CONTINUE
}	
	
public DodatkoweHP(id)
{
	if( !is_user_alive( id ) )
		return 0;

	if( g_iGildia[ id ] == -1 )
	{
		return 0;
	}
	new aData[ GildiaInfo ];
	ArrayGetArray( g_aGildie, g_iGildia[ id ], aData );
	
	set_user_health( id, get_user_health(id) + aData[ GildiaHP ] * get_pcvar_num( g_UlepszenieHP ) )
		
	return 0;
}
public MenuGildi( id )
{	
	static aData[ GildiaInfo ]
	
	new menu, info[124]
	formatex(info,charsmax(info),"\w===|\r Menu Gildi\w |===" );	
	menu = menu_create(info,"MenuGildi_Handler");
		
	new iStatus = getStatus( id, g_iGildia[ id ] );
	
	new stworz[90], zapros[90], usun[90], umiejetnosci[90], odejdz[90], czlonkowie[90], skasuj[90], ustaw[90], zabierz[90], top[90]
	
	format(stworz, charsmax(stworz), "Stworz Gildie \w[ \r%i \yOrderow \w| \r%i \yPoziom \w]^n", get_pcvar_num( g_KosztGildi ), get_pcvar_num( g_PoziomGildi ) );
	format(zapros, charsmax(zapros), "Zapros Gracza do Gildi  |  [ \yWolne miejsca: \w%i ]", get_pcvar_num( g_pMaxGraczy ) - aData[ NumGraczy ] );
	format(usun, charsmax(usun), "Usun czlonka Gildi^n" );
	format(umiejetnosci, charsmax(umiejetnosci), "Umiejetnosci" );
	format(odejdz, charsmax(odejdz), "Opusc Gildie" );
	format(czlonkowie, charsmax(czlonkowie), "Czlonkowie Gildi" );
	format(skasuj, charsmax(skasuj), "Skasuj Swoja Gildie" );
	format(ustaw, charsmax(ustaw), "Ustaw Range" );
	format(zabierz, charsmax(zabierz), "Zabierz Range" );
	format(top, charsmax(top), "Top 5 Gildi" );
	
	if( iStatus == STATUS_NONE )
	{
		menu_additem( menu, stworz, "1");
		
		menu_additem( menu, top, "10");
		
	}
	if( iStatus == STATUS_MEMBER )
	{
		menu_additem( menu, czlonkowie, "6" );	
		menu_additem( menu, umiejetnosci, "3");
		menu_additem( menu, top, "10");
		menu_additem( menu, odejdz, "4" );
	}	
	if( iStatus == STATUS_ADMIN )
	{
		menu_additem( menu, zapros, "2");
		menu_additem( menu, czlonkowie, "6" );
		menu_additem( menu, umiejetnosci, "3");
		menu_additem( menu, top, "10");
	}	
	if( iStatus == STATUS_LEADER )
	{
		menu_additem( menu, zapros, "2");
		menu_additem( menu, usun, "7");
		menu_additem( menu, czlonkowie, "6" );
		menu_additem( menu, umiejetnosci, "3");
		menu_additem( menu, top, "10");
		menu_additem( menu, skasuj, "8");
		menu_additem( menu, ustaw, "5" );
		menu_additem( menu, zabierz, "9");
	}
	menu_display(id, menu)
}

public MenuGildi_Handler(id, menu, item)
{
	if(item == MENU_EXIT) 
	{
		menu_destroy(menu);
		return PLUGIN_HANDLED;
	}
 
	new sKey[6], iKey, access, callback;
	menu_item_getinfo(menu, item, access, sKey, 5,_, _, callback);
	iKey = str_to_num(sKey);
	switch(iKey) 
	{		
		case 1: 
		{
			if( cod_get_user_Ordery( id ) < get_pcvar_num( g_KosztGildi ) || cod_get_user_level( id ) < get_pcvar_num( g_PoziomGildi ) )
			{
				ColorChat( id, NORMAL, "%s Nie spelniasz wszystkich wymagan !", gPrefix );
				return PLUGIN_HANDLED;
			}
			
			client_cmd( id, "messagemode Wpisz_Nazwe_Gildi" );
		}
		
		case 2:
		{
			ZaprosGraczaMenu( id );
			
		}
		
		case 3:
		{
			MenuUmiejetnosci( id );
		}
		
		case 4:
		{
			OpuscGildie( id );
		}
		case 5:
		{
			UstawRangeMenu( id );
		}
		case 6:
		{
			GraczeOnlineMenu( id );
		}
		case 7:
		{
			WyrzucGraczaMenu( id );
		}
		case 8:
		{
			SkasujGildieMenu( id );
		}
		case 9:
		{
			ZabierzRangeMenu( id );
		}
		case 10:
		{
			Top5( id );
		}
			
	}
	menu_destroy(menu);
	return PLUGIN_HANDLED;
}

public StworzGildie( id )
{
	if( cod_get_user_Ordery( id ) < get_pcvar_num( g_KosztGildi ) || cod_get_user_level( id ) < get_pcvar_num( g_PoziomGildi ) )
	{
		ColorChat( id, NORMAL, "%s Nie spelniasz wszystkich wymagan aby, zalozyc gildie !", gPrefix );
		return PLUGIN_HANDLED;
	}
	
	else if( g_iGildia[ id ] > -1 )
	{
		ColorChat( id, NORMAL, "%s Nie mozesz zalozyc gildi, poniewaz nalezysz juz do jakiejs Gildi", gPrefix );
		return PLUGIN_HANDLED;
	}
	
	new szArgs[ 60 ];
	read_args( szArgs, charsmax( szArgs ) );
	
	remove_quotes( szArgs );
	
	if( TrieKeyExists( g_iNazwyGildi, szArgs ) )
	{
		ColorChat( id, NORMAL, "%s Gildia z taka nazwa juz istnieje.", gPrefix );
		MenuGildi( id );
		return PLUGIN_HANDLED;
	}
	
	new aData[ GildiaInfo ];
	
	aData[ NazwaGildi ] 		= szArgs;
	aData[ GildiaHP ] 		= 0;
	aData[ GildiaEXP ]		= 0;
	aData[ GildiaREG ]		= 0;
	aData[ GildiaODB ]		= 0;
	aData[ NumGraczy ] 		= 0;
	aData[ GraczeGildi ] 		= _:TrieCreate();
	
	ArrayPushArray( g_aGildie, aData );
	
	cod_set_user_Ordery( id, cod_get_user_Ordery( id ) - get_pcvar_num( g_KosztGildi ) );
	
	set_user_Gildia( id, ArraySize( g_aGildie ) - 1, STATUS_LEADER );
	
	ColorChat( id, NORMAL, "%s Stworzyles Gildie: '^03%s^01'.", gPrefix, szArgs );
	
	return PLUGIN_HANDLED;
}

public ZaprosGraczaMenu( id )
{	
	new iPlayers[ 32 ], iNum;
	get_players( iPlayers, iNum );
	
	new szInfo[ 6 ], hMenu;
	hMenu = menu_create( "Zapros Gracza:", "ZaprosGracza_Handler" );

	new szName[ 32 ];
	
	for( new i = 0, iPlayer; i < iNum; i++ )
	{
		iPlayer = iPlayers[ i ];
		
		
		if( iPlayer == id || g_iGildia[ iPlayer ] == g_iGildia[ id ] )
			continue;
		
		get_user_name( iPlayer, szName, charsmax( szName ) );
		
		num_to_str( iPlayer, szInfo, charsmax( szInfo ) );
		
		menu_additem( hMenu, szName, szInfo );
		
	}
	
	menu_display( id, hMenu, 0 );
}

public ZaprosGracza_Handler( id, hMenu, iItem )
{
	if( iItem == MENU_EXIT )
	{
		MenuGildi( id );
		return PLUGIN_HANDLED;
	}
	
	new szData[ 6 ], iAccess, hCallback, szName[ 32 ];
	menu_item_getinfo( hMenu, iItem, iAccess, szData, 5, szName, 31, hCallback );
	
	new iPlayer = str_to_num( szData );
	
	if( !is_user_connected( iPlayer ) )
		return PLUGIN_HANDLED;
	
	MenuDolacz( id, iPlayer );

	ColorChat( id, NORMAL, "%s Zaprosiles %s do swojej Gildi", gPrefix, szName );
	
	MenuGildi( id );
	return PLUGIN_HANDLED;
}
public MenuDolacz( id, iPlayer )
{
	new szName[ 32 ];
	get_user_name( id, szName, charsmax( szName ) );
	
	new aData[ GildiaInfo ];
	ArrayGetArray( g_aGildie, g_iGildia[ id ], aData );
	
	new szMenuTitle[ 128 ];
	formatex( szMenuTitle, charsmax( szMenuTitle ), "%s Chcesz Dolaczyc Do ?     %s", szName, aData[ NazwaGildi ] );
	new hMenu = menu_create( szMenuTitle, "MenuPotwierdz_Handler" );
	
	new szInfo[ 6 ];
	num_to_str( g_iGildia[ id ], szInfo, 5 );
	
	menu_additem( hMenu, "Akceptuj", szInfo );
	menu_additem( hMenu, "Odrzuc", "-1" );
	
	menu_display( iPlayer, hMenu, 0 );	
}

public MenuPotwierdz_Handler( id, hMenu, iItem )
{
	if( iItem == MENU_EXIT )
		return PLUGIN_HANDLED;
	
	new szData[ 6 ], iAccess, hCallback;
	menu_item_getinfo( hMenu, iItem, iAccess, szData, 5, _, _, hCallback );
	
	new iGildia = str_to_num( szData );
	
	if( iGildia == -1 )
		return PLUGIN_HANDLED;
	
	if( getStatus( id, g_iGildia[ id ] ) == STATUS_LEADER )
	{
		ColorChat( id, NORMAL, "%s Nie mozesz odejsc z gildi gdy jestes Liderem", gPrefix );
		return PLUGIN_HANDLED;
	}
	
	set_user_Gildia( id, iGildia );
	
	new aData[ GildiaInfo ];
	ArrayGetArray( g_aGildie, iGildia, aData );
	
	ColorChat( id, NORMAL, "%s Dolaczyles do Gildi ^03%s^01.", gPrefix, aData[ NazwaGildi ] );
	
	return PLUGIN_HANDLED;
}
public MenuUmiejetnosci( id )
{	
	static szMenu[ 512 ], iLen, iKeys, aData[ GildiaInfo ];
	
	if( !iKeys )
	{
		iKeys = MENU_KEY_1 | MENU_KEY_2 | MENU_KEY_3 | MENU_KEY_4 | MENU_KEY_5  | MENU_KEY_0;
	}
	
	ArrayGetArray( g_aGildie, g_iGildia[ id ], aData );
	
	iLen	=	formatex( szMenu, charsmax( szMenu ), "\w===|\r Umiejetnosci Gildi\w |===^n" );
	iLen	+=	formatex( szMenu[ iLen ], charsmax( szMenu ) - iLen, "^n\yTwoje Ordery: \w%i^n^n", cod_get_user_Ordery(id) );
	
	iLen	+=	formatex( szMenu[ iLen ], 511 - iLen, "\r1. \wZdrowie \w[\yPoziom: \w%i | \r%i \w]   [\yKoszt: \r%i Orderow\w]^n"
	, aData[ GildiaHP ], get_pcvar_num( g_pPoziomHP ), get_pcvar_num( g_pKosztHP ) );

	iLen	+=	formatex( szMenu[ iLen ], 511 - iLen, "\r2. \wMadrosc \w[\yPoziom: \w%i | \r%i \w]   [\yKoszt: \r%i Orderow\w]^n"
	, aData[ GildiaEXP ], get_pcvar_num( g_pPoziomEXP ), get_pcvar_num( g_pKosztEXP ) );	
	
	iLen	+=	formatex( szMenu[ iLen ], 511 - iLen, "\r3. \wRegeneracja \w[\yPoziom: \w%i | \r%i \w]   [\yKoszt: \r%i Orderow\w]^n"
	, aData[ GildiaREG ], get_pcvar_num( g_pPoziomREG ), get_pcvar_num( g_pKosztREG ) );
	
	iLen	+=	formatex( szMenu[ iLen ], 511 - iLen, "\r4. \wOdbicie Pocisku \w[\yPoziom: \w%i | \r%i \w]   [\yKoszt: \r%i Orderow\w]^n"
	, aData[ GildiaODB ], get_pcvar_num( g_pPoziomODB ), get_pcvar_num( g_pKosztODB ) );		
	
	
	iLen	+=	formatex( szMenu[ iLen ], 511 - iLen, "^n\r0. \wWyjdz" );
	
	show_menu( id, iKeys, szMenu, -1, "Umiejetnosci" );
}

public Umiejetnosci_Handler( id, iKey )
{
	new aData[ GildiaInfo ];
	ArrayGetArray( g_aGildie, g_iGildia[ id ], aData );
	
	switch( ( iKey + 1 ) % 10 )
	{
		case 0: 
		{
			MenuGildi( id );
			return PLUGIN_HANDLED;
		}
		case 1:
		{
			if( aData[ GildiaHP ] == get_pcvar_num( g_pPoziomHP ) )
			{
				ColorChat( id, NORMAL, "%s Gildia posiada maxymalny poziom umiejetnosci", gPrefix  );
				MenuUmiejetnosci( id );
				return PLUGIN_HANDLED;
			}
			
			new iOrdery = cod_get_user_Ordery(id) - get_pcvar_num( g_pKosztHP );
			
			if( iOrdery < 0 )
			{
				ColorChat( id, NORMAL, "%s Nie masz wystarczajacej ilosci Orderow", gPrefix );
				MenuUmiejetnosci( id );
				return PLUGIN_HANDLED;
			}
			
			aData[ GildiaHP ]++;
			
			cod_set_user_Ordery(id, iOrdery);
		}
		case 2:
		{
			if( aData[ GildiaEXP ] == get_pcvar_num( g_pPoziomEXP ) )
			{
				ColorChat( id, NORMAL, "%s Gildia posiada maxymalny poziom umiejetnosci", gPrefix  );
				MenuUmiejetnosci( id );
				return PLUGIN_HANDLED;
			}
			
			new iOrdery = cod_get_user_Ordery(id) - get_pcvar_num( g_pKosztEXP );
			
			if( iOrdery < 0 )
			{
				ColorChat( id, NORMAL, "%s Nie masz wystarczajacej ilosci Orderow", gPrefix );
				MenuUmiejetnosci( id );
				return PLUGIN_HANDLED;
			}
			
			aData[ GildiaEXP ]++;
			
			cod_set_user_Ordery(id, iOrdery);
		}
		case 3:
		{
			if( aData[ GildiaREG ] == get_pcvar_num( g_pPoziomREG ) )
			{
				ColorChat( id, NORMAL, "%s Gildia posiada maxymalny poziom umiejetnosci", gPrefix  );
				MenuUmiejetnosci( id );
				return PLUGIN_HANDLED;
			}
			
			new iOrdery = cod_get_user_Ordery(id) - get_pcvar_num( g_pKosztREG );
			
			if( iOrdery < 0 )
			{
				ColorChat( id, NORMAL, "%s Nie masz wystarczajacej ilosci Orderow", gPrefix );
				MenuUmiejetnosci( id );
				return PLUGIN_HANDLED;
			}
			
			aData[ GildiaREG ]++;
			
			cod_set_user_Ordery(id, iOrdery);
		}	
		case 4:
		{
			if( aData[ GildiaODB ] == get_pcvar_num( g_pPoziomODB ) )
			{
				ColorChat( id, NORMAL, "%s Gildia posiada maxymalny poziom umiejetnosci", gPrefix  );
				MenuUmiejetnosci( id );
				return PLUGIN_HANDLED;
			}
			
			new iOrdery = cod_get_user_Ordery(id) - get_pcvar_num( g_pKosztODB );
			
			if( iOrdery < 0 )
			{
				ColorChat( id, NORMAL, "%s Nie masz wystarczajacej ilosci Orderow", gPrefix );
				MenuUmiejetnosci( id );
				return PLUGIN_HANDLED;
			}
			
			aData[ GildiaODB ]++;
			
			cod_set_user_Ordery(id, iOrdery);
		}		

	}
	
	ArraySetArray( g_aGildie, g_iGildia[ id ], aData );
	
	new iPlayers[ 32 ], iNum, iPlayer;
	new szName[ 32 ];
	get_players( iPlayers, iNum );
	
	for( new i = 0; i < iNum; i++ )
	{
		iPlayer = iPlayers[ i ];
		
		if( iPlayer == id || g_iGildia[ iPlayer ] != g_iGildia[ id ] )
			continue;
		
		ColorChat( iPlayer, NORMAL, "%s ^03%s ^01Ulepszyl Umiejetnosci Gildi", gPrefix, szName );
	}
	
	ColorChat( id, NORMAL, "%s Ulepszyles Umiejetnosci Gildi", gPrefix );
	
	MenuUmiejetnosci( id );
	
	return PLUGIN_HANDLED;
}

public OpuscGildie( id )
{
	
	new hMenu = menu_create( "Czy napewno chcesz Odejsc z Gildi?", "OpuscGildie_Handler" );
	menu_additem( hMenu, "Tak", "0" );
	menu_additem( hMenu, "Nie", "1" );
	
	menu_display( id, hMenu, 0 );
}

public OpuscGildie_Handler( id, hMenu, iItem )
{
	if( iItem == MENU_EXIT )
		return PLUGIN_HANDLED;
	
	new szData[ 6 ], iAccess, hCallback;
	menu_item_getinfo( hMenu, iItem, iAccess, szData, 5, _, _, hCallback );
	
	switch( str_to_num( szData ) )
	{
		case 0: 
		{
			if( getStatus( id, g_iGildia[ id ] ) == STATUS_LEADER )
			{
				ColorChat( id, NORMAL, "%s Musisz Zmienic Lidera zanim odejdziesz", gPrefix );
				MenuGildi( id );
				
				return PLUGIN_HANDLED;
			}
			
			ColorChat( id, NORMAL, "%s Opusciles Swoja Gildie", gPrefix );
			set_user_Gildia( id, -1 );
			MenuGildi( id );
		}
		
		case 1: MenuGildi( id );
		}
	
	return PLUGIN_HANDLED;
}
public SkasujGildieMenu( id )
{
	new hMenu = menu_create( "Czy na pewno chcesz skasowac Gildie ?", "SkasujGildie_Handler" );
	menu_additem( hMenu, "Tak", "0" );
	menu_additem( hMenu, "Nie", "1" );
	
	menu_display( id, hMenu, 0 );
}

public SkasujGildie_Handler( id, hMenu, iItem )
{
	if( iItem == MENU_EXIT )
		return PLUGIN_HANDLED;
	
	new szData[ 6 ], iAccess, hCallback;
	menu_item_getinfo( hMenu, iItem, iAccess, szData, 5, _, _, hCallback );
	
	switch( str_to_num( szData ) )
	{
		case 0: 
		{
			
			ColorChat( id, NORMAL, "%s Skasowales swoja Gildie", gPrefix );
			
			new iPlayers[ 32 ], iNum;
			
			get_players( iPlayers, iNum );
			
			new iPlayer;
			
			for( new i = 0; i < iNum; i++ )
			{
				iPlayer = iPlayers[ i ];
				
				if( iPlayer == id )
					continue;
				
				if( g_iGildia[ id ] != g_iGildia[ iPlayer ] )
					continue;
				
				ColorChat( iPlayer, NORMAL, "%s Lider Skasowal Gildie !", gPrefix );
				set_user_Gildia( iPlayer, -1 );
			}
			
			new iGildia = g_iGildia[ id ];
			
			set_user_Gildia( id, -1 );
			
			ArrayDeleteItem( g_aGildie, iGildia );
			
			MenuGildi( id );
		}
		
		case 1: MenuGildi( id );
		}
	
	return PLUGIN_HANDLED;
}
public WyrzucGraczaMenu( id )
{
	new iPlayers[ 32 ], iNum;
	get_players( iPlayers, iNum );
	
	new hMenu = menu_create( "Wyrzuc Gracza z Gildi:", "WyrzucGracza_Handler" );
	new szName[ 32 ], szData[ 6 ];
	
	
	for( new i = 0, iPlayer; i < iNum; i++ )
	{
		iPlayer = iPlayers[ i ];
		
		if( g_iGildia[ iPlayer ] != g_iGildia[ id ] || id == iPlayer )
			continue;
		
		get_user_name( iPlayer, szName, charsmax( szName ) );
		num_to_str( iPlayer, szData, charsmax( szData ) );
		
		menu_additem( hMenu, szName, szData );
	}
	
	menu_display( id, hMenu, 0 );
}

public WyrzucGracza_Handler( id, hMenu, iItem )
{
	if( iItem == MENU_EXIT )
	{
		MenuGildi( id );
		return PLUGIN_HANDLED;
	}
	
	new iAccess, hCallback, szData[ 6 ], szName[ 32 ];
	
	menu_item_getinfo( hMenu, iItem, iAccess, szData, 5, szName, charsmax( szName ), hCallback );
	
	new iPlayer = str_to_num( szData );
	
	if( !is_user_connected( iPlayer ) )
	{
		ColorChat( id, NORMAL, "%s Ten Gracz wyszedl z serwera.", gPrefix );
		MenuGildi( id );
		return PLUGIN_HANDLED;
	}
	
	set_user_Gildia( iPlayer, -1 );
	
	MenuGildi( id );
	
	new iPlayers[ 32 ], iNum, iTemp;
	get_players( iPlayers, iNum );
	
	for( new i = 0; i < iNum; i++ )
	{
		iTemp = iPlayers[ i ];
		
		if( iTemp == iPlayer || g_iGildia[ iTemp ] != g_iGildia[ id ] )
			continue;
		
		ColorChat( iTemp, NORMAL, "%s ^03%s^01 Zostal Wyrzucony z Gildi", gPrefix, szName );
	}
	
	ColorChat( iPlayer, NORMAL, "%s Zostales wyrzucony z Gildi", gPrefix, szName );
	
	return PLUGIN_HANDLED;
}
public UstawRangeMenu( id )
{
	new iPlayers[ 32 ], iNum;
	new szName[ 32 ], szData[ 6 ];
	new hMenu = menu_create( "Wybierz gracza do awansu:", "UstawRange_Handler" );

	
	get_players( iPlayers, iNum );


	for( new i = 0, iPlayer; i < iNum; i++ )
	{
		iPlayer = iPlayers[ i ];
		
		if( g_iGildia[ id ] != g_iGildia[ iPlayer ] || getStatus( iPlayer, g_iGildia[ iPlayer ] ) > STATUS_MEMBER )
			continue;

		get_user_name( iPlayer, szName, charsmax( szName ) );

		num_to_str( iPlayer, szData, charsmax( szData ) );

		menu_additem( hMenu, szName, szData );
	}
	menu_display( id, hMenu, 0 );
}
public UstawRange_Handler( id, hMenu, iItem )
{
	if( iItem == MENU_EXIT )
	{
		menu_destroy( hMenu );
		MenuGildi( id );
		return PLUGIN_HANDLED;
	}

	new iAccess, hCallback, szData[ 6 ], szName[ 32 ];

	menu_item_getinfo( hMenu, iItem, iAccess, szData, charsmax( szData ), szName, charsmax( szName ), hCallback );


	new iChosen = str_to_num( szData );

	
	if( !is_user_connected( iChosen ) )
	{
		menu_destroy( hMenu );
		MenuGildi( id );
		return PLUGIN_HANDLED;
	}

	set_user_Gildia( iChosen, g_iGildia[ id], STATUS_ADMIN );

	new iPlayers[ 32 ], iNum;
	get_players( iPlayers, iNum );

	for( new i = 0, iPlayer; i < iNum; i++ )
	{
		iPlayer = iPlayers[ i ];


		if( g_iGildia[ iPlayer ] != g_iGildia[ id ] || iPlayer == iChosen )
			continue;

		ColorChat( iPlayer, NORMAL, "%s ^03%s ^01dostal awans na Zastepce Gildi", gPrefix, szName );
	}

	ColorChat( iChosen, NORMAL, "%s ^01Dostales awans w Gildi", gPrefix );

	menu_destroy( hMenu );
	return PLUGIN_HANDLED;
}
public ZabierzRangeMenu( id )
{
	new iPlayers[ 32 ], iNum;
	new szName[ 32 ], szData[ 6 ];

	new hMenu = menu_create( "Wybierz gracza do degradacji:", "ZabierzRange_Handler" );

	get_players( iPlayers, iNum );

	for( new i = 0, iPlayer; i < iNum; i++ )
	{
		iPlayer = iPlayers[ i ];
		
		if( g_iGildia[ id ] != g_iGildia[ iPlayer ] || getStatus( iPlayer, g_iGildia[ iPlayer ] ) != STATUS_ADMIN )
			continue;

		get_user_name( iPlayer, szName, charsmax( szName ) );
		num_to_str( iPlayer, szData, charsmax( szData ) );
		menu_additem( hMenu, szName, szData );
	}
	menu_display( id, hMenu, 0 );

}	
public ZabierzRange_Handler( id, hMenu, iItem )
{
	if( iItem == MENU_EXIT )
	{
		menu_destroy( hMenu );
		MenuGildi( id );
		return PLUGIN_HANDLED;
	}
	new iAccess, hCallback, szData[ 6 ], szName[ 32 ];

	menu_item_getinfo( hMenu, iItem, iAccess, szData, charsmax( szData ), szName, charsmax( szName ), hCallback );

	new iChosen = str_to_num( szData );

	if( !is_user_connected( iChosen ) )
	{
		menu_destroy( hMenu );
		MenuGildi( id );
		return PLUGIN_HANDLED;
	}

	set_user_Gildia( iChosen, g_iGildia[ id ], STATUS_MEMBER );

	
	new iPlayers[ 32 ], iNum;
	get_players( iPlayers, iNum );

	for( new i = 0, iPlayer; i < iNum; i++ )
	{
		iPlayer = iPlayers[ i ];
		
		if( g_iGildia[ iPlayer ] != g_iGildia[ id ] || iPlayer == iChosen )
			continue;

		
		ColorChat( iPlayer, NORMAL, "%s ^03%s ^01zostal zdegradowany z rangi Zastepcy Gildi", gPrefix, szName );
	}

	ColorChat( iChosen, NORMAL, "%s ^01Zostales zdegradowany z rangi Zastepcy Gildi", gPrefix );

	menu_destroy( hMenu );
	return PLUGIN_HANDLED;
}

public GraczeOnlineMenu( id )
{
	new szName[ 64 ], iPlayers[ 32 ], iNum;
	get_players( iPlayers, iNum );
	
	new hMenu = menu_create( "Gracze Online Gildi", "GraczeMenu_Handler" );
	
	for( new i = 0, iPlayer; i < iNum; i++ )
	{
		iPlayer = iPlayers[ i ];
		
		if( g_iGildia[ id ] != g_iGildia[ iPlayer ] )
			continue;
		
		get_user_name( iPlayer, szName, charsmax( szName ) );
		
		switch( getStatus( iPlayer, g_iGildia[ id ] ) )
		{
			case STATUS_MEMBER:
			{
				add( szName, charsmax( szName ), " \w[Czlonek]" );
			}
			
			case STATUS_ADMIN:
			{
				add( szName, charsmax( szName ), " \y[Zastpeca]" );
			}
			
			case STATUS_LEADER:
			{
				add( szName, charsmax( szName ), " \r[Lider]" );
			}
		}
		
		menu_additem( hMenu, szName );
	}
	
	menu_display( id, hMenu, 0 );
}

public GraczeMenu_Handler( id, hMenu, iItem )
{
	if( iItem == MENU_EXIT )
	{
		menu_destroy( hMenu );
		MenuGildi( id );
		return PLUGIN_HANDLED;
	}
	
	menu_destroy( hMenu );
	
	GraczeOnlineMenu( id )
	return PLUGIN_HANDLED;
}
public Top5( id )
{
	new iSize = ArraySize( g_aGildie );
	
	new iOrder[ 100 ][ 2 ];
	
	new aData[ GildiaInfo ];
	
	for( new i = 0; i < iSize; i++ )
	{
		ArrayGetArray( g_aGildie, i, aData );
		
		iOrder[ i ][ 0 ] = i;
	}
	
	SortCustom2D( iOrder, iSize, "Top5_Handler" );
	
	new szMessage[ 2048 ];
	formatex( szMessage, charsmax( szMessage ), "<body bgcolor=#000000><font color=#FFB000><pre>" );
	format( szMessage, charsmax( szMessage ), "%s%2s %-22.22s %7s %4s %10s %9s^n", szMessage, "#", "Nazwa", "Zdrowie", "Madrosc", "Regeneracja", "Odbicie Pocisku" );
		
	for( new i = 0; i < min( 5, iSize ); i++ )
	{
		ArrayGetArray( g_aGildie, iOrder[ i ][ 0 ], aData );
		
		format( szMessage, charsmax( szMessage ), "%s%-2d %22.22s %7d %4d %10d %9d^n" 
		, szMessage, i + 1, aData[ NazwaGildi ], aData[ GildiaHP ], aData[ GildiaEXP ], aData[ GildiaREG ], aData[ GildiaODB ] );
	}
	
	show_motd( id, szMessage, "Top 15 Gildi Serwera" );
}
public Top5_Handler( const iElement1[ ], const iElement2[ ], const iArray[ ], szData[], iSize ) 
{
	if( iElement1[ 1 ] > iElement2[ 1 ] )
		return -1;
	
	else if( iElement1[ 1 ] < iElement2[ 1 ] )
		return 1;
	
	return 0;
}
public WczytajGildie()
{
	new szConfigsDir[ 60 ];
	get_configsdir( szConfigsDir, charsmax( szConfigsDir ) );
	
	add( szConfigsDir, charsmax( szConfigsDir ), "/Gildie.ini" );
	
	new iFile = fopen( szConfigsDir, "rt" );
	
	new aData[ GildiaInfo ];
	
	new szBuffer[ 512 ], szData[ 6 ], szValue[ 6 ], i, iCurGang;
	
	while( !feof( iFile ) )
	{
		fgets( iFile, szBuffer, charsmax( szBuffer ) );
		
		trim( szBuffer );
		remove_quotes( szBuffer );
		
		if( !szBuffer[ 0 ] || szBuffer[ 0 ] == ';' ) 
		{
			continue;
		}
		
		if( szBuffer[ 0 ] == '[' && szBuffer[ strlen( szBuffer ) - 1 ] == ']' )
		{
			copy( aData[ NazwaGildi ], strlen( szBuffer ) - 2, szBuffer[ 1 ] );
			aData[ GildiaHP ] = 0;
			aData[ GildiaEXP ] = 0;
			aData[ GildiaREG ] = 0;
			aData[ GildiaODB ] = 0;
			aData[ NumGraczy ] = 0;
			aData[ GraczeGildi ] = _:TrieCreate();
			
			if( TrieKeyExists( g_iNazwyGildi, aData[ NazwaGildi ] ) )
			{
				new szError[ 256 ];
				formatex( szError, charsmax( szError ), "Gildia juz istnieje: %s", aData[ NazwaGildi ] );
				set_fail_state( szError );
			}
			
			ArrayPushArray( g_aGildie, aData );
			
			TrieSetCell( g_iNazwyGildi, aData[ NazwaGildi ], iCurGang );
			
			log_amx( "Gang Created: %s", aData[ NazwaGildi ] );
			
			iCurGang++;
			
			continue;
		}
		
		strtok( szBuffer, szData, 31, szValue, 511, '=' );
		trim( szData );
		trim( szValue );
		
		if( TrieGetCell( g_iGildiaValues, szData, i ) )
		{
			ArrayGetArray( g_aGildie, iCurGang - 1, aData );
			
			switch( i )
			{					
				case VALUE_HP:
					aData[ GildiaHP ] = str_to_num( szValue );
					
				case VALUE_EXP:
					aData[ GildiaEXP ] = str_to_num( szValue );
		
				case VALUE_REG:
					aData[ GildiaREG ] = str_to_num( szValue );	
					
				case VALUE_ODB:
					aData[ GildiaODB ] = str_to_num( szValue );						
			}
			
			ArraySetArray( g_aGildie, iCurGang - 1, aData );
		}
	}
	
	new Array:aSQL;
	sqlv_read_all_ex( g_hVault, aSQL );
	
	new aVaultData[ SQLVaultEntryEx ];
	
	new iGildia;
	
	for( i = 0; i < ArraySize( aSQL ); i++ )
	{
		ArrayGetArray( aSQL, i, aVaultData );
		
		if( TrieGetCell( g_iNazwyGildi, aVaultData[ SQLVEx_Key2 ], iGildia ) )
		{
			ArrayGetArray( g_aGildie, iGildia, aData );
			
			TrieSetCell( aData[ GraczeGildi ], aVaultData[ SQLVEx_Key1 ], str_to_num( aVaultData[ SQLVEx_Data ] ) );
			
			aData[ NumGraczy ]++;
			
			ArraySetArray( g_aGildie, iGildia, aData );
		}
	}
	
	fclose( iFile );
}

public ZapiszGildie()
{
	new szConfigsDir[ 64 ];
	get_configsdir( szConfigsDir, charsmax( szConfigsDir ) );
	
	add( szConfigsDir, charsmax( szConfigsDir ), "/Gildie.ini" );
	
	if( file_exists( szConfigsDir ) )
		delete_file( szConfigsDir );
	
	new iFile = fopen( szConfigsDir, "wt" );
	
	new aData[ GildiaInfo ];
	
	new szBuffer[ 256 ];
	
	for( new i = 0; i < ArraySize( g_aGildie ); i++ )
	{
		ArrayGetArray( g_aGildie, i, aData );
		
		formatex( szBuffer, charsmax( szBuffer ), "[%s]^n", aData[ NazwaGildi ] );
		fputs( iFile, szBuffer );
		
		formatex( szBuffer, charsmax( szBuffer ), "HP=%i^n", aData[ GildiaHP ] );
		fputs( iFile, szBuffer );
		
		formatex( szBuffer, charsmax( szBuffer ), "EXP=%i^n", aData[ GildiaEXP ] );
		fputs( iFile, szBuffer );
		
		formatex( szBuffer, charsmax( szBuffer ), "REG=%i^n", aData[ GildiaREG ] );
		fputs( iFile, szBuffer );	
		
		formatex( szBuffer, charsmax( szBuffer ), "ODB=%i^n", aData[ GildiaODB ] );
		fputs( iFile, szBuffer );		

	}
	
	fclose( iFile );
}
	
set_user_Gildia( id, iGildia, iStatus=STATUS_MEMBER )
{
	new szAuthID[ 35 ];
	
	get_user_name( id, szAuthID, charsmax( szAuthID ) );

	new aData[ GildiaInfo ];
	
	if( g_iGildia[ id ] > -1 )
	{
		ArrayGetArray( g_aGildie, g_iGildia[ id ], aData );
		TrieDeleteKey( aData[ GraczeGildi ], szAuthID );
		aData[ NumGraczy ]--;
		ArraySetArray( g_aGildie, g_iGildia[ id ], aData );
		
		sqlv_remove_ex( g_hVault, szAuthID, aData[ NazwaGildi ] );
	}
	if( iGildia > -1 )
	{
		ArrayGetArray( g_aGildie, iGildia, aData );
		TrieSetCell( aData[ GraczeGildi ], szAuthID, iStatus );
		aData[ NumGraczy ]++;
		ArraySetArray( g_aGildie, iGildia, aData );
		
		sqlv_set_num_ex( g_hVault, szAuthID, aData[ NazwaGildi ], iStatus );		
	}
	g_iGildia[ id ] = iGildia;
	
	return 1;
}
get_user_Gildia( id )
{
	new szAuthID[ 35 ];
	get_user_name( id, szAuthID, charsmax( szAuthID ) );
	
	new aData[ GildiaInfo ];
	
	for( new i = 0; i < ArraySize( g_aGildie ); i++ )
	{
		ArrayGetArray( g_aGildie, i, aData );
	
		if( TrieKeyExists( aData[ GraczeGildi ], szAuthID ) )
		return i;
	}
	
	return -1;
}

getStatus( id, iGildia )
{
	if( !is_user_connected( id ) || iGildia == -1 )
	return STATUS_NONE;

	new aData[ GildiaInfo ];
	ArrayGetArray( g_aGildie, iGildia, aData );
	
	new szAuthID[ 35 ];
	get_user_name( id, szAuthID, charsmax( szAuthID ) );
	
	new iStatus;
	TrieGetCell( aData[ GraczeGildi ], szAuthID, iStatus );
	
	return iStatus;
}

public Gracz(id)
{
	
	new menu = menu_create("Wybierz Gracza:", "Gracz_handler");
	
	new aData[ GildiaInfo ];
	for( new i = 0; i < ArraySize( g_aGildie ); i++ )
	{
		ArrayGetArray( g_aGildie, i, aData );
		menu_additem(menu, aData[ NazwaGildi ], "0", 0);
		
	}
	menu_display(id, menu);
}
public Gracz_handler(id, menu, item)
{
	if(item == MENU_EXIT)
	{
		menu_destroy(menu);
		return PLUGIN_CONTINUE;
	}
	
	
	return PLUGIN_CONTINUE;
	
}

/*public Nowa_Runda(id)
{


}*/
