#include <amxmodx>
#include <hamsandwich>
#include <nvault>
#include <amxmisc>

#define PLUGIN "Ranks"
#define VERSION "1.1"
#define AUTHOR "underwoker"

new gPlayerRank[ 33 ]
new gPlayerPoints[ 33 ]
new gAuthId[ 33 ][ 40 ]
new gVault
new gpCvarEnable, gpCvarPrivateStart, gpCvarPrivateEnd, gpCvarCorporalStart, gpCvarCorporalEnd, gpCvarSergeantStart, gpCvarSergeantEnd, 
gpCvarSergeantMajorStart, gpCvarSergeantMajorEnd, gpCvarOfficerCadetStart, gpCvarOfficerCadetEnd, 
gpCvar2ndLieutenantStart, gpCvar2ndLieutenantEnd, gpCvarLieutenantStart, gpCvarLieutenantEnd, gpCvarCaptainStart, gpCvarCaptainEnd, 
gpCvarMajorStart, gpCvarMajorEnd, gpCvarLtColonelStart, gpCvarLtColonelEnd, gpCvarColonelStart, gpCvarColonelEnd, 
gpCvarBrigadierStart, gpCvarBrigadierEnd, gpCvarGeneralStart, gpCvarGeneralEnd, gpCvarKillPoints

enum _:Ranks 
{
	RANK_FIELD_MARSHAL,
	RANK_GENERAL,
	RANK_BRIGADIER,
	RANK_COLONEL,
	RANK_LT_COLONEL,
	RANK_MAJOR,
	RANK_CAPTAIN,
	RANK_LIEUTENANT,
	RANK_2ND_LIEUTENANT,
	RANK_OFFICER_CADET,
	RANK_SERGEANT_MAJOR,
	RANK_SERGEANT,
	RANK_CORPORAL,
	RANK_PRIVATE
}

new RankDescription[ Ranks ][] =
{	
	"Дух сервера",
	"Ветеран",
	"Параноик",
	"Четкий",
	"Зомбо-убийца",
	"Мясник",
	"Ниндзя",
	"Псих",
	"Маньяк",
	"Убийца",
	"Сталкер",
	"Головорез",
	"Бандит",
	"Малой"
}

public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	RegisterHam( Ham_Spawn, "player", "fw_spawn" )
	
	register_event( "DeathMsg", "fw_death", "a", "1>0" )
	
	register_cvar( "sv_rank_mod", VERSION, FCVAR_SERVER|FCVAR_SPONLY )
	
	gpCvarEnable = register_cvar( "rank_enable", "1" )
	gpCvarKillPoints = register_cvar( "rank_killpoints", "1" )
	gpCvarPrivateStart = register_cvar( "rank_private_start", "1" )
	gpCvarPrivateEnd = register_cvar( "rank_private_end", "10" )
	gpCvarCorporalStart = register_cvar( "rank_corporal_start", "11" )
	gpCvarCorporalEnd = register_cvar( "rank_corporal_end", "20" )
	gpCvarSergeantStart = register_cvar( "rank_sergeant_start", "21" )
	gpCvarSergeantEnd = register_cvar( "rank_sergeant_end", "33" )
	gpCvarSergeantMajorStart = register_cvar( "rank_sergeantmajor_start", "34" )
	gpCvarSergeantMajorEnd = register_cvar( "rank_sergeantmajor_end", "50" )
	gpCvarOfficerCadetStart = register_cvar( "rank_officercadet_start", "51" )
	gpCvarOfficerCadetEnd = register_cvar( "rank_officercadet_end", "65" )
	gpCvar2ndLieutenantStart = register_cvar( "rank_2ndlieutenant_start", "66" )
	gpCvar2ndLieutenantEnd = register_cvar( "rank_2ndlieutenant_end", "80" )
	gpCvarLieutenantStart = register_cvar( "rank_lieutenant_start", "81" )
	gpCvarLieutenantEnd = register_cvar( "rank_lieutenant_end", "90" )
	gpCvarCaptainStart = register_cvar( "rank_captain_start", "91" )
	gpCvarCaptainEnd = register_cvar( "rank_captain_end", "101" )
	gpCvarMajorStart = register_cvar( "rank_major_start", "102" )
	gpCvarMajorEnd = register_cvar( "rank_major_end", "130" )
	gpCvarLtColonelStart = register_cvar( "rank_ltcolonel_start", "131" )
	gpCvarLtColonelEnd = register_cvar( "rank_ltcolonel_end", "150" )
	gpCvarColonelStart = register_cvar( "rank_colonel_start", "151" )
	gpCvarColonelEnd = register_cvar( "rank_colonel_end", "170" )
	gpCvarBrigadierStart = register_cvar( "rank_brigadier_start", "171" )
	gpCvarBrigadierEnd = register_cvar( "rank_brigadier_end", "190" )
	gpCvarGeneralStart = register_cvar( "rank_general_start", "191" )
	gpCvarGeneralEnd = register_cvar( "rank_general_end", "200" )
	
	gVault = nvault_open( "RANK_MOD" )
}

public plugin_cfg()
{
	if( get_pcvar_num( gpCvarEnable ) )
	{
		new configsDir[ 32 ],file[ 192 ];
		get_configsdir( configsDir, sizeof configsDir - 1 );
		formatex( file, sizeof file - 1,"%s/rank.cfg", configsDir );
		
		if( !file_exists( file) )
		{
			server_print( "* File %s doesn't exist! 		 *", file );
			server_print( "* Creating a default config file named %s *", file )
			
			write_file( file, "//		Rank Mod" )
			write_file( file, "" )
			write_file( file, "// Enables/Disables the plugin" )
			write_file( file, "rank_enable 1" )
			write_file( file, "" )
			write_file( file, "// How many points for a kill" )
			write_file( file, "rank_killerpoints 1" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"Private^" starting and ending." )
			write_file( file, "rank_private_start 1" )
			write_file( file, "rank_private_end 10" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"Corporal^" starting and ending." )
			write_file( file, "rank_corporal_start 11" )
			write_file( file, "rank_corporal_end 20" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"Sergeant^" starting and ending." )
			write_file( file, "rank_sergeant_start 21" )
			write_file( file, "rank_sergeant_end 40" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"Sergeant Major^" starting and ending." )
			write_file( file, "rank_sergeantmajor_start 41" )
			write_file( file, "rank_sergeantmajor_end 70" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"Officer Cadet^" starting and ending." )
			write_file( file, "rank_officercadet_start 71" )
			write_file( file, "rank_officercadet_end 110" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"2nd. Lieutenant^" starting and ending." )
			write_file( file, "rank_2ndlieutenant_start 111" )
			write_file( file, "rank_2ndlieutenant_end 150" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"Lieutenant^" starting and ending." )
			write_file( file, "rank_lieutenant_start 151" )
			write_file( file, "rank_lieutenant_end 200" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"Captain^" starting and ending." )
			write_file( file, "rank_captain_start 201" )
			write_file( file, "rank_captain_end 320" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"Major^" starting and ending." )
			write_file( file, "rank_major_start 321" )
			write_file( file, "rank_major_end 400" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"Lt. Colonel^" starting and ending." )
			write_file( file, "rank_ltcolonel_start 401" )
			write_file( file, "rank_ltcolonel_end 500" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"Colonel^" starting and ending." )
			write_file( file, "rank_colonel_start 501" )
			write_file( file, "rank_colonel_end 650" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"Brigadier^" starting and ending." )
			write_file( file, "rank_brigadier_start 651" )
			write_file( file, "rank_brigadier_end 780" )
			write_file( file, "" )
			write_file( file, "// How many points for ^"General^" starting and ending." )
			write_file( file, "rank_general_start 781" )
			write_file( file, "rank_general_end 1000" )
			write_file( file, "" )
			write_file( file, "// ^"Field Marshal^" Is the best rank, so they is him if they got more than ^"General^"" )
		}
		server_cmd( "exec %s", file )
	}
}

public client_putinserver( id )
{
	if( get_pcvar_num( gpCvarEnable ) )
	{
		get_user_authid ( id, gAuthId[ id ], 39 )
		ps_load_points( id )
	}
}
	
public client_disconnect( id )
{
	if( get_pcvar_num( gpCvarEnable ) )
	{
		remove_task( id )
		
		ps_save_points( id )
	
		gPlayerPoints[ id ] = 0
		gPlayerRank[ id ] = 0
		gAuthId[ id ][ 0 ] = 0
	}
}

public plugin_end ( ) 
{
	if( get_pcvar_num( gpCvarEnable ) )
	{
		nvault_close ( gVault )
	}
}

ps_load_points ( id ) 
{
	if( get_pcvar_num( gpCvarEnable ) )
	{
		static szData[ 7 ], iTimeStamp
	
		// Look up players authid, points and timestamp.
		if ( nvault_lookup ( gVault , gAuthId[ id ] , szData , 6 , iTimeStamp ) ) 
		{
			gPlayerPoints[ id ] = str_to_num ( szData )
		} 
	}
}

ps_save_points ( id ) 
{
	if( get_pcvar_num( gpCvarEnable ) )
	{
		static szData[ 7 ]
	
		// Format the players points in to szData.
		num_to_str( gPlayerPoints[ id ] , szData , 6 )
		
		// Save players authid with the points.
		nvault_set ( gVault, gAuthId[ id ] , szData )
	}
}

public fw_spawn( id )
{
	if( get_pcvar_num( gpCvarEnable ) )
	{
		new iPoints = gPlayerPoints[ id ]
		if( !iPoints )
		{
			client_print( id, print_chat, "You don't have any points to rank up, go get some" )
		} 
		else if ( get_pcvar_num( gpCvarPrivateStart ) <= iPoints  <= get_pcvar_num( gpCvarPrivateEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_PRIVATE  
		} 
		else if ( get_pcvar_num( gpCvarCorporalStart ) <= iPoints <= get_pcvar_num( gpCvarCorporalEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_CORPORAL  
		}
		else if ( get_pcvar_num( gpCvarSergeantStart ) <= iPoints <= get_pcvar_num( gpCvarSergeantEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_SERGEANT  
		}
		else if ( get_pcvar_num( gpCvarSergeantMajorStart ) <= iPoints <= get_pcvar_num( gpCvarSergeantMajorEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_SERGEANT_MAJOR  
		}
		else if ( get_pcvar_num( gpCvarOfficerCadetStart ) <= iPoints <= get_pcvar_num( gpCvarOfficerCadetEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_OFFICER_CADET  
		}
		else if ( get_pcvar_num( gpCvar2ndLieutenantStart ) <= iPoints <= get_pcvar_num( gpCvar2ndLieutenantEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_2ND_LIEUTENANT  
		}
		else if ( get_pcvar_num( gpCvarLieutenantStart ) <= iPoints <= get_pcvar_num( gpCvarLieutenantEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_LIEUTENANT  
		}
		else if ( get_pcvar_num( gpCvarCaptainStart ) <= iPoints <= get_pcvar_num( gpCvarCaptainEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_CAPTAIN  
		}
		else if ( get_pcvar_num( gpCvarMajorStart ) <= iPoints <= get_pcvar_num( gpCvarMajorEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_MAJOR  
		}
		else if ( get_pcvar_num( gpCvarLtColonelStart ) <= iPoints <= get_pcvar_num( gpCvarLtColonelEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_LT_COLONEL  
		}
		else if ( get_pcvar_num( gpCvarColonelStart ) <= iPoints <= get_pcvar_num( gpCvarColonelEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_COLONEL  
		}
		else if ( get_pcvar_num( gpCvarBrigadierStart ) <= iPoints <= get_pcvar_num( gpCvarBrigadierEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_BRIGADIER  
		}
		else if ( get_pcvar_num( gpCvarGeneralStart ) <= iPoints <= get_pcvar_num( gpCvarGeneralEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_GENERAL  
		}
		else if( iPoints >= get_pcvar_num( gpCvarGeneralEnd ) ) 
		{
			gPlayerRank[ id ] = RANK_FIELD_MARSHAL  
		}
		if ( !task_exists( id ) )
		{
			set_task( 2.0 , "PointsRank", id , _, _, "b" )
		}
	}
}

public fw_death( )
{
	if( get_pcvar_num( gpCvarEnable ) )
	{
		gPlayerPoints[ read_data( 1 ) ] += get_pcvar_num( gpCvarKillPoints )
	}
}

public PointsRank( id )
{
	if( get_pcvar_num( gpCvarEnable ) )
	{
		set_hudmessage(255, 0, 0, 0.76, 0.0, 0, 6.0, 3.0)
		show_hudmessage(id, "Звание: %s^nУбийств: %d", RankDescription[ gPlayerRank[ id ] ] , gPlayerPoints[ id ] )
	}
}
