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


#define PLUGIN "[ZP]Level System"
#define VERSION "2.1"
#define AUTHOR "heka" // fixed by Good_Hash

// Version 2.1b :
// Added:
// zp_level_save_by 1/0 - Save by STEAM_ID ( 0 ) ::: Save by NICK ( 1 )
// default : 1.


new const Level_Exp[24] = {0, 8, 16, 32, 40, 52, 62,76,90,115,128,136,147,169,190,230,264,292,326,364,395,426,464,500}
new g_level[33]
new g_exp[33] 
new g_vault, g_save_type

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	RegisterHam(Ham_Killed, "player", "fw_PlayerKilled")
    	g_vault = nvault_open("zp_level_system")
	g_save_type = register_cvar("zp_level_save_by", "1")
}

public plugin_natives()
{
	register_native("zp_level", "native_level", 1)
	register_native("zp_exp", "native_exp", 1)
	register_native("zp_next_level_exp", "native_zp_next_level_exp", 1)
}

public client_connect(id)
{
	if ( is_user_bot(id) )
		return
		
	g_level[id] = 1

	LoadData(id)
}

public client_disconnect(id)
{
	SaveData(id)

	g_exp[id] = 0
	g_level[id] = 0
}

public plugin_end()
{
	nvault_close(g_vault)
}

public SaveData(id)
{
	new AuthID[35]
	if ( get_pcvar_num(g_save_type) )
	{
		get_user_name(id, AuthID, 32)
		replace_all(AuthID, 255, "#", "[r]")
	}
	else
		get_user_authid(id,AuthID,34)
 
	new vaultkey[64],vaultdata[256]
	format(vaultkey,63,"%s",AuthID)
	format(vaultdata,255,"%i#%i#",g_exp[id],g_level[id])
	nvault_set(g_vault,vaultkey,vaultdata)
	return PLUGIN_CONTINUE
}
public LoadData(id)
{
	new AuthID[35]
	if ( get_pcvar_num(g_save_type) )
	{
		get_user_name(id, AuthID, 32)
		replace_all(AuthID, 255, "#", "[r]")
	}
	else
		get_user_authid(id,AuthID,34)
 
	new vaultkey[64],vaultdata[256]
	format(vaultkey,63,"%s",AuthID)
	format(vaultdata,255,"%i#%i#",g_exp[id],g_level[id])
	nvault_get(g_vault,vaultkey,vaultdata,255)
 
	replace_all(vaultdata, 255, "#", " ")
 
	new playerxp[32], playerlevel[32]
 
	parse(vaultdata, playerxp, 31, playerlevel, 31)
 
	g_exp[id] = str_to_num(playerxp)
	g_level[id] = str_to_num(playerlevel)
 
	return PLUGIN_CONTINUE
}

public fw_PlayerKilled(victim, attacker)
{
	if(zp_get_user_nemesis(attacker) || zp_get_user_zombie(attacker))
	{
		return
	}
	
	if(zp_get_user_zombie(victim))
	{
		g_exp[attacker]+= 1
		check_level(attacker)
	}	
}

public check_level(id)
{
	new MAX_LVL = sizeof Level_Exp - 1;
	
	if (g_exp[id] > Level_Exp[MAX_LVL] - 1 )
		return PLUGIN_HANDLED;
	
	
	if( g_exp[id] >= Level_Exp[ g_level[id]])
	{
		if ( g_level[id] < MAX_LVL ) g_level[id]++
		g_exp[id] = 0
	}
	/*while (g_exp[id] >= Level_Exp[g_level[id]]) 
	{
		g_level[id]++
		ChatColor(id, "!g[Zombies & Humans]!yТы качнул !g%d !yлвл!!!", g_level[id]) 
	} */
	return PLUGIN_HANDLED;
}

public native_level(id)
{
	return g_level[id]
}

public native_exp(id)
{
	return g_exp[id]
}

public native_zp_next_level_exp(id)
{
	new progress = 100
	
	if (g_level[id] == (sizeof Level_Exp - 1))
		return progress
	progress = (Level_Exp[g_level[id]+1]/100)
	progress = progress > 0 ? (g_exp[id]/progress) : 0
	return progress
}

stock ChatColor(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, "!r", "^3") 
	replace_all(msg, 190, "!b", "^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"), {0,0,0}, players[i])
				write_byte(players[i]);
				write_string(msg);
				message_end();
			}
		}
	}
}
