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

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

new g_level[33]
new g_exp[33] 
new g_vault

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

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 SaveData(id)
{
    new AuthID[35]
    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]
    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))
	{
	}
	else if(zp_get_user_zombie(victim))
	{
		g_exp[attacker]+= 1
		check_level(attacker)
	}	
}

public check_level(id)
{
	if (g_exp[id] > g_level[sizeof g_level - 1] - 1)
	{
		return PLUGIN_HANDLED;
	}
	while (g_exp[id] >= g_level[g_level[id]]) 
	{
		g_level[id]++
		PrintChatColor(id,print_chat, "!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]
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ ansicpg1251\\ deff0\\ deflang1058{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ f0\\ fs16 \n\\ par }
*/
