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

#define PLUGIN "[ZP]Level System"
#define VERSION "2.2"
#define AUTHOR "heka" 

new const Level_Exp[33] = {0,10,25,50,65,80,100,125,150,180,200,230,250,280,320,350,380,420,450,480,520,550,580,620,660,700,730,760,800,850,900,999,1000}	

new g_level[33], g_exp[33], 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", "0") //Save by  STEAM_ID = 0; NICK = 1
}

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

public client_connect(id)
{
	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))
	{
		if(g_level[attacker] < sizeof Level_Exp - 1)g_exp[attacker]+= 1
		check_level(attacker)
	}	
}

public check_level(id)
{
	if(g_exp[id] > Level_Exp[sizeof Level_Exp - 1] - 1)
		return PLUGIN_HANDLED
	
	while(g_exp[id] >= Level_Exp[g_level[id]]) 
	{
		g_level[id]++
		ChatColor(id, "^4[Zombies & Humans]^1You got ^4%d ^1LVL!!!", g_level[id]) 
	} 
	return PLUGIN_HANDLED
}

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

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

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()
			}
		}
	}
}