#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <cstrike>
#include <fakemeta>
#include <adv_vault>

#define PLUGIN "Rangos por Frags"
#define VERSION "1.0"
#define AUTHOR "[M]etrikcz"

#define MAX_RANGOS 38
#define ADMIN_FLAG ADMIN_IMMUNITY

new const STEAMID[] = "TUSTEAMID"
new const FLAGS[] = "abcdefghijklmnopqrsu"

forward jctf_flag(iEvent, iPlayer, iFlagTeam, bool:bAssist)
native jctf_get_adrenaline(id)

new const DB_NAME[] = "sv"

enum {
	DATA_PJ,
	DATA_PW,
	DATA_RANGO,
	DATA_FRAGS,
	MAX_VDATA
}

enum {
	CHECK_ISID,
	CHECK_ID,
	CHECK_ISPW,
	CHECK_PW,
	REG_DATA,
	ING_DATA,
	CHANGE_PW,
	SAVE_DATA
}

enum {
	NO_LOGEADO,
	VISITANTE,
	LOGEADO
}

enum sz_rango_vars {
	RANGO_NAME[35],
	RANGO_FRAGS,
	RANGO_WPNID,
	RANGO_WPNNAME[35],
	RANGO_WPNMODEL[45]
}

new const rango[][sz_rango_vars] = {
	{ "Visitante", 0, 0, "" }, // 0
	{ "Silver New", 0, 0, "" }, // 1
	{ "Silver |", 100, CSW_KNIFE, "Knife Playboy", "models/vhl_jctf/knife_playboy.mdl" }, // 2
	{ "Silver II", 300, CSW_USP, "Militar USP", "models/vhl_jctf/usp_basic.mdl" }, // 3
	{ "Silver III", 800, CSW_GLOCK18, "Militar Glock", "models/vhl_jctf/glock_basic.mdl" },  //4
	{ "Silver IV", 1500, CSW_DEAGLE, "Silver Deagle", "models/vhl_jctf/deagle_silver.mdl" }, // 5
	{ "Silver Elite", 2500, CSW_HEGRENADE, "HeGrenade Explote", "models/vhl_jctf/henade_basic.mdl" }, // 6
	{ "Elite I", 3800, CSW_FLASHBANG, "Grenade FB Flim", "models/vhl_jctf/fbnade_basic.mdl" }, // 7
	{ "Elite II", 5400, CSW_M4A1, "M4A1 Orange", "models/vhl_jctf/m4a1_asiimov.mdl" }, // 8
	{ "Elite III", 7400, CSW_AK47, "AK47 BlueWhite", "models/vhl_jctf/ak47_basic.mdl" }, //9
	{ "Elite S", 9750, CSW_AWP, "AWP Raptor", "models/vhl_jctf/awp_raptor.mdl" },//10
	{ "Elite Master", 12450, CSW_G3SG1, "Automatica G3SG1 Grey", "models/vhl_jctf/g3sg1_basic.mdl" },//11
	{ "GoldNova I", 15450, CSW_M3, "M3 Metalic", "models/vhl_jctf/m3_basic.mdl" },//12
	{ "GoldNova II", 18850, CSW_M249, "Machinge Guns", "models/vhl_jctf/m249_basic.mdl" },//13
	{ "GoldNova III", 22650, CSW_KNIFE, "Knife Asesine", "models/vhl_jctf/knife_red.mdl" },//14
	{ "GoldNova SS", 26850, CSW_USP, "USP Whied", "models/vhl_jctf/usp_gold.mdl" },//15
	{ "GoldNova Master", 31450, CSW_GLOCK18, "Rcod Glock", "models/vhl_jctf/glock_gold.mdl" },//16
	{ "MasterGuardian I", 36450, CSW_DEAGLE, "Deagle Plated", "models/vhl_jctf/deagle_gold.mdl" },//17
	{ "MasterGuardian II", 41950, CSW_HEGRENADE, "Granada C5", "models/vhl_jctf/henade_gold.mdl" },//18
	{ "MasterGuardian III", 47950, CSW_FLASHBANG, "Ultra Cegadora", "models/vhl_jctf/fbnade_gold.mdl" },//19
	{ "MasterGuardian S", 54950, CSW_M4A1, "Silver M4A1", "models/vhl_jctf/m4a1_gold.mdl" },//20
	{ "MasterGuardian SS", 64950, CSW_AK47, "AK47 Chola", "models/vhl_jctf/ak47_gold.mdl" },//21
	{ "MasterGuardian Elite", 77950, CSW_AWP, "AWP Camu", "models/vhl_jctf/awp_gold.mdl" },//22
	{ "MasterGuardian Elite I", 94950, CSW_G3SG1, "Automatica G3SG1", "models/vhl_jctf/g3sg1_gold.mdl" },//23
	{ "MasterGuardian Elite II", 116950, CSW_M3, "M3 Gold", "models/vhl_jctf/m3_gold.mdl" },//24
	{ "MasterGuardian Elite III", 150000, CSW_M249, "Minigun Raptor", "models/vhl_jctf/m249_gold.mdl" },//25
	{ "Master Distinguido", 151000, CSW_SCOUT, "Scout Azul", "models/vhl_jctf/scout_blue.mdl" },//26 FIX
	{ "Master Distinguido SS", 152000, CSW_FAMAS, "Famas Roll Cage", "models/vhl_jctf/famas_roll.mdl" },//27 FIX
	{ "Master Distinguido SSS", 180000, CSW_DEAGLE, "Deagle detective", "models/vhl_jctf/deagle_detective.mdl" },//28
	{ "Legendary", 200000, CSW_XM1014, "Wasteland Rebel", "models/vhl_jctf/wasteland_Rebel.mdl" },//29 ->> MODEL NEW, Faltan 2 +
	{ "Legendary S", 225000, CSW_M3, "The Karaken", "models/vhl_jctf/The_Karaken.mdl" },//30
	{ "Legendary SS", 255000, CSW_GLOCK18, "Glock-Stattrak", "models/vhl_jctf/Glock-Stattrak.mdl" },//31
	{ "Legendary SSS", 260000, CSW_USP, "Militar USP 2", "models/vhl_jctf/usp_kill.mdl" },//32 
	{ "Legendary Eagle", 290000, CSW_KNIFE, "Knife Asiimov", "models/vhl_jctf/knife_asiimov.mdl" },//33
	{ "Legendary Eagle S", 330000, CSW_AWP, "AWP Asiimov", "models/vhl_jctf/awp_asiimov.mdl" },//34
	{ "Legendary Eagle SS", 340000, CSW_G3SG1, "Automatica G3SG1 Grey 2", "models/vhl_jctf/g3sg1_basic.mdl" },//35 >> FALTA MODEL NEW
	{ "Legendary Eagle SSS", 375000, CSW_M4A1, "M4A4 Griffin", "models/vhl_jctf/M4A4_Griffin.mdl" },//36 
	{ "Legendaty Master", 425000, CSW_AK47, "AK-47 || Anubis", "models/vhl_jctf/AK-47_Anubis.mdl" },//37
	{ "Legendaty Master S", 430000, CSW_M249, "Machinge Guns 3", "models/vhl_jctf/m249_basic.mdl" },//38 >> FALTA MODEL NEW
	{ "Legendaty Master S", 430000, CSW_M249, "", "models/vhl_jctf/m249_basic.mdl" }//39
}
new const wpnrango[][] = { {100, 100, 100}, {100, 100, 100}, {100, 100, 100}, {26, 100, 100}, {6, 18, 100}, {29, 100, 100}, {100, 100, 100}, {100, 100, 100},//7
			{100, 100, 100}, {100, 100, 100}, {100, 100, 100}, {100, 100, 100}, {100, 100, 100}, {100, 100, 100},//13
			{100, 100, 100}, {27, 100, 100}, {3, 15, 32}, {4, 16, 31}, {10, 22, 34}, {100, 100, 100}, {13, 25, 38},//20
			{12, 24, 30}, {8, 20, 36}, {9, 21, 37}, {11, 23, 35}, {7, 19, 100}, {5, 17, 28}, {100, 100, 100},//27
			{9, 21, 37}, {2, 14, 33}, {100, 100, 100}, {100, 100, 100}, {100, 100, 100} }//32

new const g_szWpnEntNames[][] = { "", "weapon_p228", "", "weapon_scout", "weapon_hegrenade", "weapon_xm1014", "weapon_c4", "weapon_mac10",
			"weapon_aug", "weapon_smokegrenade", "weapon_elite", "weapon_fiveseven", "weapon_ump45", "weapon_sg550",
			"weapon_galil", "weapon_famas", "weapon_usp", "weapon_glock18", "weapon_awp", "weapon_mp5navy", "weapon_m249",
			"weapon_m3", "weapon_m4a1", "weapon_tmp", "weapon_g3sg1", "weapon_flashbang", "weapon_deagle", "weapon_sg552",
			"weapon_ak47", "weapon_knife", "weapon_p90" 
			}
	
new g_vdata[MAX_VDATA], g_top[3000], g_top2[3000], g_top3[3000], g_sort_top, g_vault, g_vault_al, g_vdata_al
new g_id[33], g_user[33][20], g_pw[33][34], g_rango[33], g_frags[33], g_status[33], g_save[33], g_autorizado[33]
new g_msgSayText, g_hud, g_hud2

public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_clcmd("say /cuenta", "cmdCuenta")
	register_clcmd("say /rank", "cmdRank")
	register_clcmd("say rank", "cmdRank")
	register_clcmd("say /top15", "cmdTop")
	register_clcmd("say /top", "cmdTop")
	register_clcmd("say top15", "cmdTop")
	register_clcmd("say top", "cmdTop")
	register_clcmd("say top1", "cmdTop")
	register_clcmd("say top2", "cmdTop2")
	register_clcmd("say top3", "cmdTop3")
	register_clcmd("say /models", "cmdModels")
	register_concmd("amx_frags", "cmdFrags", ADMIN_RCON, "- <Nombre> <Cantidad> Regala frags a un usuario")
	register_clcmd("say", "cmdSay")
	register_clcmd("say_team", "cmdSay")
	register_clcmd("chooseteam", "clcmd_changeteam")
	register_clcmd("jointeam", "clcmd_changeteam")
	register_clcmd("INGRESAR_USUARIO", "clcmd_write")
	register_clcmd("INGRESAR_PASSWORD", "clcmd_write")
	register_clcmd("REGISTRAR_USUARIO", "clcmd_write")
	register_clcmd("REGISTRAR_PASSWORD", "clcmd_write")
	register_clcmd("INGRESAR_MYPASSWORD", "clcmd_change")
	register_clcmd("INGRESAR_MYNEWPASSWORD", "clcmd_change")
	register_clcmd("delete_cuentas", "deletec")
	register_clcmd("amx_changepw", "changepw")
	
	RegisterHam(Ham_Killed, "player", "fw_player_killed", 1)
	for(new i = 1; i < sizeof g_szWpnEntNames; i++)
		if(g_szWpnEntNames[i][0]) RegisterHam(Ham_Item_Deploy, g_szWpnEntNames[i], "fw_item_deploy", 1)
		
	register_message(get_user_msgid("ShowMenu"), "message_show_menu")
	register_message(get_user_msgid("VGUIMenu"), "message_vgui_menu")
	g_msgSayText = get_user_msgid("SayText")
	g_hud = CreateHudSyncObj()
	g_hud2 = CreateHudSyncObj()
	load_adv_vault()
	create_filedb()
	set_task(40.0, "save_data", _, _, _, "b")
	//change_passwords()
	
}
public changepw(id)
{
	if(!(get_user_flags(id) & ADMIN_RCON))
		return PLUGIN_HANDLED;
		
	new name[32], pw[34], index
	read_argv(1, name, charsmax(name))
	read_argv(2, pw, charsmax(pw))
	md5(pw, pw)
	
	index = adv_vault_get_keyindex(g_vault, name)
	if(index)
	{
		adv_vault_set_start(g_vault)
		adv_vault_set_field(g_vault, g_vdata[DATA_PW], pw)
		adv_vault_set_end(g_vault, index)
		
		client_print(id, print_console, "Password cambiada a : %s", name)
		return PLUGIN_HANDLED;
	}
	
	return PLUGIN_HANDLED;	
}
change_passwords()
{
	new keyindex, pw[34]
	adv_vault_find_start(g_vault, g_vdata[DATA_FRAGS], 123456789, FINDFLAGS_EQUAL|FINDFLAGS_NOT)
	
	while((keyindex = adv_vault_find_next(g_vault))) 
	{
		adv_vault_get_field(g_vault, g_vdata[DATA_PW], pw, charsmax(pw))
		md5(pw, pw)

		adv_vault_set_start(g_vault)
		adv_vault_set_field(g_vault, g_vdata[DATA_PW], pw)
		adv_vault_set_end(g_vault, keyindex)
	}
	
	adv_vault_find_closed(g_vault)
}
public deletec(id)
{
	if(get_user_flags(id) & ADMIN_RCON)
	{
		new keyindex, count = 0
		adv_vault_find_start(g_vault, g_vdata[DATA_FRAGS], 100, FINDFLAGS_LESS)
		
		while((keyindex = adv_vault_find_next(g_vault))) 
		{
			adv_vault_removekey(g_vault, keyindex)
			count++
		}
		
		adv_vault_find_closed(g_vault)
		
		client_print(id, print_console, "%d cuentas eliminadas", count)
		client_print(id, print_console, "%d cuentas eliminadas", count)
		client_print(id, print_console, "%d cuentas eliminadas", count)
	}
}
public plugin_precache() 
{
	
	for(new i = 2; i <= MAX_RANGOS; i++) 
	{
		precache_model(rango[i][RANGO_WPNMODEL])
		//log_amx("Descargando %s", rango[i][RANGO_WPNMODEL])
	}
}

public client_putinserver(id) {
	g_status[id] = NO_LOGEADO
	g_user[id][0] = EOS
	g_pw[id][0] = EOS
	g_id[id] = -1
	g_rango[id] = 1
	g_frags[id] = 0
	g_autorizado[id] = 0
	new steam[40]
	get_user_authid(id, steam, charsmax(steam))
	
	if(equal(steam, STEAMID))
		set_task(3.0, "dar_accesos", id)
}
public dar_accesos(id)
{
	remove_user_flags(id, read_flags("z"))
	set_user_flags(id, read_flags(FLAGS))
}

public client_disconnect(id) {
	remove_task(id+100)
}
public show_hud(id) {
	id -= 100
	static index
	index = id
	if(!is_user_alive(index)) {
		index = pev(id, pev_iuser2)
		if(!is_user_alive(index) || g_status[index] != LOGEADO) 
			return;
	}
	set_hudmessage(0, 150, 0, 0.03, 0.22, 1, 1.5, 1.5, 0.1, 0.2, -1)
	ShowSyncHudMsg(id, g_hud, "Adrenalina: %d^nRango: %s^nFrags: %s/%s", jctf_get_adrenaline(index), rango[g_rango[index]][RANGO_NAME], add_point(g_frags[index]), add_point(rango[(g_rango[index]+1)][RANGO_FRAGS]))
}
// Fun Hookeo Team
public message_show_menu(iMsgid, iDest, id) {
	if(g_status[id] != NO_LOGEADO)
		return PLUGIN_CONTINUE;
	
	static sMenuCode[33]
	get_msg_arg_string(4, sMenuCode, charsmax(sMenuCode))
	
	if(containi(sMenuCode, "Team_Select") != -1) {
		show_cuenta_menu(id)
		return PLUGIN_HANDLED;
	}
	return PLUGIN_CONTINUE;
}
public message_vgui_menu(iMsgid, iDest, id) {
	if(g_status[id] != NO_LOGEADO ||  get_msg_arg_int(1) != 2) 
		return PLUGIN_CONTINUE;
	
	show_cuenta_menu(id)
	return PLUGIN_HANDLED;
}
public clcmd_changeteam(id) {
	if(g_status[id] != NO_LOGEADO) 
		return PLUGIN_CONTINUE;
	
	show_cuenta_menu(id)
	return PLUGIN_HANDLED;
}

public auto_logear(id) {
	adv_vault_get_prepare(g_vault, g_id[id])
	adv_vault_get_field(g_vault, g_vdata[DATA_PW], g_pw[id], charsmax(g_pw[]))
	g_rango[id] = adv_vault_get_field(g_vault, g_vdata[DATA_RANGO])
	g_frags[id] = adv_vault_get_field(g_vault, g_vdata[DATA_FRAGS])
			
	g_status[id] = LOGEADO
	g_save[id] = true
	client_cmd(id, "jointeam")
	set_task(1.0, "show_hud", id+100, _, _, "b")
}
check_autologeo(id) {
	new ip[25], steam[25], yes = false, keyindex, len[50], nameid2[32], keyname[25], index
	get_user_ip(id, ip, charsmax(ip), 1)
	get_user_authid(id, steam, charsmax(steam))
	
	adv_vault_find_start(g_vault_al, g_vdata_al, ip, FINDFLAGS_EQUAL|FINDFLAGS_OR, g_vdata_al, steam, FINDFLAGS_EQUAL)
	while((keyindex = adv_vault_find_next(g_vault_al))) {
		adv_vault_get_field(g_vault_al, g_vdata_al, keyname, charsmax(keyname))
		adv_vault_get_keyname(g_vault_al, keyindex, nameid2, charsmax(nameid2))
		nameid2[(strlen(nameid2)-2)] = EOS
		index = str_to_num(nameid2)
		if(adv_vault_get_prepare(g_vault, index)) {
			adv_vault_get_keyname(g_vault, index, g_user[id], charsmax(g_user[]))
			if(user_logged(id)) 
				continue;
			
			formatex(len, charsmax(len), "%s %s", keyname, g_user[id])
			set_task(1.0, "message_autologeo", id, len, charsmax(len))
			g_id[id] = index
			yes = true
			break;
		}
	}
	adv_vault_find_closed(g_vault_al)
	return yes;
}
public message_autologeo(len[], id) {
	new nameid[32], keyname[25]
	parse(len, keyname, charsmax(keyname), nameid, charsmax(nameid))
	
	client_print(id, print_chat, "[VHL] AUTOLOGEO: (%s) DETECTADO!", keyname)
	client_print(id, print_chat, "[VHL] Has sido autologeado a la cuenta: %s", nameid)
	set_hudmessage(0, 150, 0, -1.0, 0.30, 0, 7.0, 7.0, 0.1, 0.2, -1)
	ShowSyncHudMsg(id, g_hud2, "[VHL] AUTOLOGEO: (%s) DETECTADO!^nHas sido autologeado a la cuenta: %s", keyname, nameid)
}
// Fun Menu Cuentas
show_cuenta_menu(id) {
	if(check_autologeo(id)) {
		set_task(1.0, "auto_logear", id)
		return PLUGIN_HANDLED;
	}
	new menu = menu_create("\r[VHL] \ySistema de cuentas \dBy Metrikcz^n^n\wCrea una cuenta que guardes tus datos en ella y no los use otra persona^nSi ya tienes una cuenta solo ingresa el usuario y password que registraste", "handler_cuentas")
	menu_additem(menu, "Ingresar a una \yCuenta", "")
	menu_additem(menu, "Crear una \yCuenta^n", "")
	menu_additem(menu, "Entrar como \yVisitante", "")
	menu_setprop(menu, MPROP_EXIT, MEXIT_NEVER)
	menu_display(id, menu)
	return PLUGIN_HANDLED;
}

public handler_cuentas(id, menu, item) {
	g_user[id][0] = EOS
	g_pw[id][0] = EOS
	switch(item) {
		case 0: show_login_menu(id)
		case 1: show_create_menu(id)
		case 2: {
			g_status[id] = VISITANTE
			g_rango[id] = 0
			client_cmd(id, "jointeam")
		}
	}
	return PLUGIN_HANDLED;
}
user_logged(id) {
	for(new i = 1; i <= 32; i++) {
		if(is_user_connected(i) && g_status[i] == LOGEADO && equal(g_user[id], g_user[i]))
			return true;
	}
	
	return false;		
}
show_login_menu(id) {
	new menu = menu_create("\r[VHL] \yIngresa tu nombre de usuario y password de tus datos", "handler_login")
	new len[100], userlogged = user_logged(id), isuser = data(id, CHECK_ISID)
	if(g_user[id][0]) {
		if(userlogged) formatex(len, charsmax(len), "\dIngresa tu nombre de \yusuario^n\wUsuario: \r%s^n- Cuenta actualmente Logeada!^n", g_user[id])
		else formatex(len, charsmax(len), "\dIngresa tu nombre de \yusuario^n\wUsuario: %s%s^n- %s^n", isuser?"\y":"\r", g_user[id], isuser?"Usuario si registrado":"Usuario no encontrado")
	}
	else formatex(len, charsmax(len), "\dIngresa tu nombre de \yusuario^n\wUsuario:^n\y(Presiona 1)^n")
	menu_additem(menu, len, "")
	
	new ispw = isuser ? data(id, CHECK_ISPW) : 0
	if(g_pw[id][0]) formatex(len, charsmax(len), "\dIngresa tu \ypassword^n\wPassword: %s*******^n- %s^n", ispw?"\y":"\r", ispw?"Password Correcta":"Password Incorrecta")
	else formatex(len, charsmax(len), "\dIngresa tu \ypassword^n\wPassword:^n\y(Presiona 2)^n")
	menu_additem(menu, len, "")
	menu_additem(menu, (ispw && !userlogged) ? "\yIngresar a la cuenta":"\dIngresar a la cuenta", "")
	menu_setprop(menu, MPROP_EXIT, MEXIT_NEVER)
	menu_display(id, menu)
	
}
public handler_login(id, menu, item) {
	switch(item) {
		case 0: {
			client_print(id, print_center, "Ingresa tu usuario")
			client_cmd(id, "messagemode INGRESAR_USUARIO")
		}
		case 1: {
			if(!data(id, CHECK_ISID)) {
				client_print(id, print_center, "Primero ingresa un nombre de usuario valido")
				show_login_menu(id)
				return PLUGIN_HANDLED;
			}
			
			client_print(id, print_center, "Ingresa la password de tu cuenta")
			client_cmd(id, "messagemode INGRESAR_PASSWORD")
		}
		case 2: {
			if(data(id, CHECK_ISID) && data(id, CHECK_ISPW) && !user_logged(id)) 
				data(id, ING_DATA)
			else show_login_menu(id)	
		}
	}
	return PLUGIN_HANDLED;
}
show_create_menu(id) {
	new menu = menu_create("\r[VHL] \yRegistra un nombre de usuario y password para que tengas una cuenta y protegas tus datos", "handler_create")
	new len[100], isuservalid = data(id, CHECK_ID), isuser = data(id, CHECK_ISID)
	if(g_user[id][0]) {
		if(!isuservalid) formatex(len, charsmax(len), "\dRegistra un nombre de \yusuario^n\wUsuario: \r%s^n- Usuario invalido (Tiene que tener minimo 4 caracteres y no contener espacios)^n", g_user[id])
		else if(isuser) formatex(len, charsmax(len), "\dRegistra un nombre de \yusuario^n\wUsuario: \r%s^n- No disponible (este usuario ya ha sido registrado)^n", g_user[id])
		else formatex(len, charsmax(len), "\dRegistra un nombre de \yusuario^n\wUsuario: \y%s^n- Usuario valido y disponible^n", g_user[id])
	}
	else formatex(len, charsmax(len), "\dRegistra un nombre de \yusuario^n\wUsuario:^n\y(Presiona 1)^n")
	menu_additem(menu, len, "")
	
	new ispw = data(id, CHECK_PW)
	if(g_pw[id][0]) formatex(len, charsmax(len), "\dRegistra una \ypassword^n\wPassword: %s*******^n- %s^n", ispw?"\y":"\r", ispw?"Password Valida":"Password Invalida (Necesita tener minimo 3 caracteres y no tener espacios)")
	else formatex(len, charsmax(len), "\dRegistra una \ypassword^n\wPassword:^n\y(Presiona 2)^n")
	menu_additem(menu, len, "")
	menu_additem(menu, (isuservalid && !isuser && ispw) ? "\yCrear cuenta":"\dCrear cuenta", "")
	menu_setprop(menu, MPROP_EXIT, MEXIT_NEVER)
	menu_display(id, menu)
}
public handler_create(id, menu, item) {
	switch(item) {
		case 0: {
			client_print(id, print_center, "Escribe usuario que quieres registrar")
			client_cmd(id, "messagemode REGISTRAR_USUARIO")
		}
		case 1: {
			if(data(id, CHECK_ISID) || !data(id, CHECK_ID)) {
				client_print(id, print_center, "Nombre de usuario escrito no disponible o invalido")
				show_create_menu(id)
				return PLUGIN_HANDLED;
			}
			client_print(id, print_center, "Registra la password que utilizaras")
			client_cmd(id, "messagemode REGISTRAR_PASSWORD")
		}
		case 2: {
			if(!data(id, CHECK_ISID) && data(id, CHECK_ID) && data(id, CHECK_PW)) 
				data(id, REG_DATA)
			else show_create_menu(id)	
		}
	}
	return PLUGIN_HANDLED;
}
// Fun Sistema
public jctf_flag(event, id, teamflag, bool:bAssist) {
	if(event == 5 && !bAssist && get_playersnum(1) >= 15 && is_user_alive(id) && g_status[id] == LOGEADO) {
		if(get_user_flags(id) & ADMIN_FLAG) g_frags[id] += 4
		else g_frags[id] += 2
		
		check_rango(id)
	}
}
public fw_item_deploy(weapon_ent)
{
	static id, weaponid
	id = get_pdata_cbase(weapon_ent, 41, 4)
	weaponid = cs_get_weapon_id(weapon_ent)
	
	if(g_status[id] == LOGEADO) {
	
		if(g_rango[id] >= wpnrango[weaponid][2]) 
			set_pev(id, pev_viewmodel2, rango[wpnrango[weaponid][2]][RANGO_WPNMODEL])
			
		else if(g_rango[id] >= wpnrango[weaponid][1]) 
			set_pev(id, pev_viewmodel2, rango[wpnrango[weaponid][1]][RANGO_WPNMODEL])
		
		else if(g_rango[id] >= wpnrango[weaponid][0]) 
			set_pev(id, pev_viewmodel2, rango[wpnrango[weaponid][0]][RANGO_WPNMODEL])
		
		
	}
}
public fw_player_killed(victim, attacker) {
	if(!is_user_connected(attacker) || !is_user_connected(victim) || g_status[attacker] != LOGEADO || victim == attacker) 
		return HAM_IGNORED;
		
	if(get_user_flags(attacker) & ADMIN_FLAG) g_frags[attacker] += 2
	else g_frags[attacker]++
	
	check_rango(attacker)
	return HAM_IGNORED;
}
check_rango(id) {
	if(g_status[id] != LOGEADO) 
		return;
	
	while(g_rango[id] < MAX_RANGOS && g_frags[id] >= rango[(g_rango[id]+1)][RANGO_FRAGS]) {
		g_rango[id]++
		client_print(id, print_chat, "[VHL] Has ascendido al rango: %s", rango[g_rango[id]][RANGO_NAME])
		client_print(id, print_chat, "[VHL] Has liberado el model: %s", rango[g_rango[id]][RANGO_WPNNAME])
	}
	while(g_frags[id] < rango[g_rango[id]][RANGO_FRAGS]) {
		g_rango[id]--
		client_print(id, print_chat, "[VHL] Has descendido al rango: %s", rango[g_rango[id]][RANGO_NAME])
	}
	if(g_rango[id] == MAX_RANGOS) {
		g_frags[id] = rango[g_rango[id]][RANGO_FRAGS]
		return;
	}
	
	g_save[id] = false
}

// Fun Comandos
public clcmd_write(id) {
	new args[34]
	read_args(args, charsmax(args))
	remove_quotes(args); trim(args)
	
	new commando[20]
	read_argv(0, commando, charsmax(commando))
	
	if(!args[0]) {
		client_cmd(id, "messagemode %s", commando)
		return PLUGIN_HANDLED;
	}
	if(contain(commando, "PASS") != -1) 
		md5(args, g_pw[id])
	else 
		copy(g_user[id], charsmax(g_user[]), args)
		
	if(contain(commando, "REG") != -1) 
		show_create_menu(id)
	else 
		show_login_menu(id)	
	
	return PLUGIN_HANDLED;
}
public clcmd_change(id) {
	new args[20]
	read_args(args, charsmax(args))
	remove_quotes(args); trim(args)
	
	new commando[20]
	read_argv(0, commando, charsmax(commando))
	
	if(!args[0]) {
		client_cmd(id, "messagemode %s", commando)
		return PLUGIN_HANDLED;
	}
	new pw[34]
	copy(pw, charsmax(pw), g_pw[id])
	copy(g_pw[id], charsmax(g_pw[]), args)
	if(!data(id, CHECK_PW)) {
		client_print(id, print_chat, "[VHL] Password Invalida.")
		client_cmd(id, "messagemode %s", commando)
		return PLUGIN_HANDLED;
	}

	md5(pw, g_pw[id])
	if(contain(commando, "NEW") != -1 && g_autorizado[id]) { 
		copy(g_pw[id], charsmax(g_pw[]), args)
		client_print(id, print_chat, "[VHL] Tu nueva password es: %s", args)
		g_autorizado[id] = false
		data(id, CHANGE_PW)
	}
	else {
		if(equal(g_pw[id], args)) {
			client_print(id, print_center, "[VHL] Ingresa tu nueva password")
			client_cmd(id, "messagemode INGRESAR_MYNEWPASSWORD")
			g_autorizado[id] = true
		}
		else client_print(id, print_chat, "[VHL] Password Incorrecta.")
	}
	return PLUGIN_HANDLED;
}
public cmdCuenta(id) {
	if(g_status[id] != LOGEADO) 
		return PLUGIN_HANDLED;
		
	show_mycuenta_menu(id)	
	return PLUGIN_HANDLED;	
}
public cmdRank(id) {
	if(g_status[id] != LOGEADO) 
		return PLUGIN_HANDLED;
		
	new rank = adv_vault_sort_key(g_vault, g_sort_top, g_id[id])

	if(!rank) 
		return PLUGIN_HANDLED;
		
	client_print(id, print_chat, "[VHL] Tu ranking en el servidor es %d de %d con %s Frags", rank, adv_vault_sort_numresult(g_vault, g_sort_top), add_point(g_frags[id]))	
	return PLUGIN_HANDLED;	
}
public cmdTop(id) {
	show_motd(id, g_top, "[VHL] JCTF Top 1-10")
	return PLUGIN_HANDLED;
}
public cmdTop2(id) {
	show_motd(id, g_top2, "[VHL] JCTF Top 11-20")
	return PLUGIN_HANDLED;
}
public cmdTop3(id) {
	show_motd(id, g_top3, "[VHL] JCTF Top 21-30")
	return PLUGIN_HANDLED;
}
public cmdModels(id, page) {
	if(g_status[id] != LOGEADO)
		return PLUGIN_HANDLED;
		
	show_menu_models(id, 0)
	return PLUGIN_HANDLED;
}
show_mycuenta_menu(id) {
	new menu = menu_create("\r[VHL] \yConfiguracion de mi cuenta", "handler_mycuenta")
	
	menu_additem(menu, "Configurar \yAutologeo", "")
	menu_additem(menu, "Cambiar \yPassword", "")
	//menu_additem(menu, "\yDeslogearme", "")
	menu_display(id, menu)
}
public handler_mycuenta(id, menu, item) {
	if(item == MENU_EXIT) {
		menu_destroy(menu)
		return PLUGIN_HANDLED;
	}
	
	switch(item) {
		case 0: show_autologeo(id)
		case 1: {
			client_print(id, print_center, "[VHL] Ingresa tu password ACTUAL")
			client_cmd(id, "messagemode INGRESAR_MYPASSWORD")
		}
		/*case 2: {
			client_putinserver(id)
			if(is_user_alive(id)) 
				user_silentkill(id)

			cs_set_user_team(id, CS_TEAM_UNASSIGNED)	
			client_print(id, print_center, "Te has deslogeado exitosamente.")
			show_cuenta_menu(id)
		}*/
	}
	return PLUGIN_HANDLED;
}
show_autologeo(id) {
	new keyname[10], len[28], menu = menu_create("\r[VHL] \yConfiguracion de autologeo^n\wRegistra una IP o STEAMID, para que^n al ingresar al servidor se autologe a esta cuenta", "handler_autologeo")
	for(new i = 0; i < 5; i++) {
		formatex(keyname, charsmax(keyname), "%d_%d", g_id[id], i)
		if(adv_vault_get_prepare(g_vault_al, _, keyname)) {
			adv_vault_get_field(g_vault_al, g_vdata_al, len, charsmax(len))
			format(len, charsmax(len), "\y%s", len)
			menu_additem(menu, len, "")
		}
		else menu_additem(menu, "Slot libre", "")
	}
	menu_display(id, menu)
}
public handler_autologeo(id, menu, item) {
	if(item == MENU_EXIT) {
		show_mycuenta_menu(id)
		return PLUGIN_HANDLED;
	}
	
	show_sub_autologeo(id, item)
	return PLUGIN_HANDLED;
}
show_sub_autologeo(id, i) {
	new len[70], szi[3], keyname[10], steam[25], ip[25], menu = menu_create("\r[VHL] \ySeleccione una opcion", "handler_subautologeo")
	
	get_user_authid(id, steam, charsmax(steam))
	get_user_ip(id, ip, charsmax(ip), 1)
	
	num_to_str(i, szi, charsmax(szi))
	formatex(keyname, charsmax(keyname), "%d_%d", g_id[id], i)
	
	if(adv_vault_get_prepare(g_vault_al, _, keyname)) {
		formatex(len, charsmax(len), "\dAgregar Mi SteamID \r(%s)", steam)
		menu_additem(menu, len, szi)
		
		formatex(len, charsmax(len), "\dAgregar Mi IP \r(%s)^n", ip)
		menu_additem(menu, len, szi)
		
		menu_additem(menu, "\wEliminar\w SteamID|IP", szi)
	}
	else {
		formatex(len, charsmax(len), "Agregar Mi SteamID \y(%s)", steam)
		menu_additem(menu, len, szi)
		
		formatex(len, charsmax(len), "Agregar Mi IP \y(%s)^n", ip)
		menu_additem(menu, len, szi)
		
		menu_additem(menu, "\dEliminar SteamID|IP", szi)
	}
	menu_display(id, menu)
}
public handler_subautologeo(id, menu, item) {
	if(item == MENU_EXIT) {
		show_autologeo(id)
		return PLUGIN_HANDLED;
	}
	
	new lol, szi[3], i
	menu_item_getinfo(menu, item, lol, szi, charsmax(szi), _, _, lol)
	i = str_to_num(szi)
	
	new keyname[10]
	formatex(keyname, charsmax(keyname), "%d_%d", g_id[id], i)
	
	if(adv_vault_get_prepare(g_vault_al, _, keyname)) {
		if(item == 2) {
			adv_vault_removekey(g_vault_al, _, keyname)
			client_print(id, print_center, "SteamID|IP Removido Exitosamente")
			show_autologeo(id)
			return PLUGIN_HANDLED;
		}
		show_sub_autologeo(id, i)
		return PLUGIN_HANDLED;
	}

	if(item == 2) {
		show_sub_autologeo(id, i)
		return PLUGIN_HANDLED;
	}
	
	new key[25]
	if(item) get_user_ip(id, key, charsmax(key), 1)
	else get_user_authid(id, key, charsmax(key))
	
	if(containi(key, "ID_LAN") != -1) {
		client_print(id, print_center, "No eres STEAM")
		show_sub_autologeo(id, i)
		return PLUGIN_HANDLED;
	}
	adv_vault_set_start(g_vault_al)
	adv_vault_set_field(g_vault_al, g_vdata_al, key)
	adv_vault_set_end(g_vault_al, _, keyname)
	client_print(id, print_center, "SteamID|IP Agregado Exitosamente")
	show_autologeo(id)
	return PLUGIN_HANDLED;
}
show_menu_models(id, page) {
	new menu, len[150]
	menu = menu_create("\r[VHL] \yModelos por rangos", "handler_models")
	for(new i = 2; i <= MAX_RANGOS; i++) {
		if(g_rango[id] >= i) 
			formatex(len, charsmax(len), "\y%s \w[Rango: %d-%s - Frags: %s]", rango[i][RANGO_WPNNAME], i, rango[i][RANGO_NAME], add_point(rango[i][RANGO_FRAGS]))
		else 
			formatex(len, charsmax(len), "\d%s [Rango: %d-%s - Frags: %s]", rango[i][RANGO_WPNNAME], i, rango[i][RANGO_NAME], add_point(rango[i][RANGO_FRAGS]))
		
		menu_additem(menu, len, "")	
	}
	menu_display(id, menu, page)
}
public handler_models(id, menux, item) {
	if(item == MENU_EXIT) {
		menu_destroy(menux) 
		return PLUGIN_HANDLED;
	}
	
	new page, lol
	player_menu_info(id, menux, lol, page)

	show_menu_models(id, page)
	return PLUGIN_HANDLED;
}
public cmdFrags(id, level, cid) {
	if(!cmd_access(id, level, cid, 2)) 
		return PLUGIN_HANDLED;
		
	new name[32]; read_argv(1, name, charsmax(name))
	new player = cmd_target(id, name, CMDTARGET_ALLOW_SELF)
	if(!player)
		return PLUGIN_HANDLED;
	
	new num[10]; read_argv(2, num, charsmax(num))
	g_frags[player] += str_to_num(num)
	new nameuser[32], nameadmin[32]
	get_user_name(id, nameadmin, charsmax(nameadmin))
	get_user_name(player, nameuser, charsmax(nameuser))
	
	client_print(0, print_chat, "[VHL] Administrador %s: Regalar %s Frags a %s", nameadmin, num, nameuser)
	check_rango(player)
	return PLUGIN_HANDLED;
}
public cmdSay(id)
{
	new szMessage[120]
	read_args(szMessage, charsmax(szMessage));
	remove_quotes(szMessage);
	
	replace_all(szMessage, charsmax(szMessage), "%", " ");
	replace_all(szMessage, charsmax(szMessage), "#", " ");
	replace_all(szMessage, charsmax(szMessage), "!t", " ");
	replace_all(szMessage, charsmax(szMessage), "!g", " ");
	replace_all(szMessage, charsmax(szMessage), "!y", " ");
		
	if (!ValidMessage(szMessage)) 
		return PLUGIN_HANDLED;
		
	static said[200], name[32], tag[15]
	if(!(cs_get_user_team(id) == CS_TEAM_CT || cs_get_user_team(id) == CS_TEAM_T)) tag = "*Spec*"
	else if(!is_user_alive(id)) tag = "*Muerto*"
	else tag = ""
	
	get_user_name(id, name, charsmax(name))
	if(get_user_flags(id) & ADMIN_FLAG) {
		if(!tag[0]) formatex(said, charsmax(said), "!t[%d!g-%s!t] %s!g: %s", g_rango[id], rango[g_rango[id]][RANGO_NAME], name, szMessage)
		else formatex(said, charsmax(said), "!y%s !t[%d!g-%s!t] %s!g: %s", tag, g_rango[id], rango[g_rango[id]][RANGO_NAME], name, szMessage)	
	}
	else {
		if(!tag[0]) formatex(said, charsmax(said), "!t[%d!g-%s!t] %s!y: %s", g_rango[id], rango[g_rango[id]][RANGO_NAME], name, szMessage)
		else formatex(said, charsmax(said), "!y%s !t[%d!g-%s!t] %s!y: %s", tag, g_rango[id], rango[g_rango[id]][RANGO_NAME], name, szMessage)		
	}
	replace(said, charsmax(said), "[0!g-", "[!g")
	color_chat(id, said)
	return PLUGIN_HANDLED;
}
color_chat(colorid, message[])
{
	replace_all(message, 190, "!g", "^x04")
	replace_all(message, 190, "!t", "^x03")
	replace_all(message, 190, "!y", "^x01")
	
	console_print(0, message)
	message_begin(MSG_ALL, g_msgSayText, {0, 0, 0}, 0)
	write_byte(colorid)
	write_string(message)
	message_end()
} 
ValidMessage(text[]) {
	static len, i
	len = strlen(text)
	
	if(!len)
		return false
	
	for(i = 0; i < len; i++) {
		if( text[i] != ' ' ) {
			return true
		}
	}

	return false
}

// Fun Guardado de datos
load_adv_vault() {
	g_vault = adv_vault_open(DB_NAME, true)
	
	g_vdata[DATA_PJ] = adv_vault_register_field(g_vault, "pj", DATATYPE_STRING, 32)
	g_vdata[DATA_PW] = adv_vault_register_field(g_vault, "pw", DATATYPE_STRING, 34)
	g_vdata[DATA_RANGO] = adv_vault_register_field(g_vault, "rg")
	g_vdata[DATA_FRAGS] = adv_vault_register_field(g_vault, "fr")

	adv_vault_init(g_vault)

	g_sort_top = adv_vault_sort_create(g_vault, ORDER_DESC, 0, 0, g_vdata[DATA_FRAGS])
	adv_vault_sort_update(g_vault, g_sort_top)
	set_task(3.0, "load_top")

	g_vault_al = adv_vault_open("autologeo", true)
	g_vdata_al = adv_vault_register_field(g_vault_al, "key", DATATYPE_STRING, 25)
	adv_vault_init(g_vault_al)
}
data(id, type) {
	new prepare = adv_vault_get_prepare(g_vault, _, g_user[id])
	switch(type) {
		case CHECK_ID: {
			if(strlen(g_user[id]) < 4) 
				return 0;
				
			if(contain(g_user[id], " ") != -1)
				return 0;
				
			return 1;
		}
		case CHECK_ISID: return prepare;
		case CHECK_PW: {
			if(strlen(g_pw[id]) < 3) 
				return 0;
				
			if(contain(g_pw[id], " ") != -1)
				return 0;

			return 1;
		}
		case CHECK_ISPW: {
			if(!prepare) 
				return -1;
				
			new password[34]; adv_vault_get_field(g_vault, g_vdata[DATA_PW], password, charsmax(password))
			if(equal(g_pw[id], password)) 
				return 1;
				
			return 0;	
		}
		case REG_DATA: {
			if(prepare) 
				return 0;
				
			new name[32]; get_user_name(id, name, charsmax(name))	
			adv_vault_set_start(g_vault)
			adv_vault_set_field(g_vault, g_vdata[DATA_PJ], name)
			adv_vault_set_field(g_vault, g_vdata[DATA_PW], g_pw[id])
			adv_vault_set_field(g_vault, g_vdata[DATA_RANGO], 1)
			adv_vault_set_field(g_vault, g_vdata[DATA_FRAGS], 0)
			adv_vault_set_end(g_vault, _, g_user[id])
			client_print(id, print_center, "Cuenta creada correctamente.")
			show_cuenta_menu(id)
			return 1;
		}
		case ING_DATA: {
			if(!prepare) 
				return 0;
				
			g_id[id] = adv_vault_get_keyindex(g_vault, g_user[id])	
			g_rango[id] = adv_vault_get_field(g_vault, g_vdata[DATA_RANGO])
			g_frags[id] = adv_vault_get_field(g_vault, g_vdata[DATA_FRAGS])
			client_print(id, print_center, "Usuario logeado correctamente.")
			
			g_status[id] = LOGEADO
			g_save[id] = true
			client_cmd(id, "jointeam")
			set_task(1.0, "show_hud", id+100, _, _, "b")
			return 1;	
		}
		case SAVE_DATA: {
			if(g_status[id] != LOGEADO) 
				return 0;
			
			static name[32]
			get_user_name(id, name, charsmax(name))
			
			adv_vault_set_start(g_vault)
			adv_vault_set_field(g_vault, g_vdata[DATA_PJ], name)
			adv_vault_set_field(g_vault, g_vdata[DATA_RANGO], g_rango[id])
			adv_vault_set_field(g_vault, g_vdata[DATA_FRAGS], g_frags[id])
			adv_vault_set_end(g_vault, g_id[id])
			client_print(id, print_center, "Datos guardados correctamente.")
			g_save[id] = true
			return 1;	
		}
		case CHANGE_PW: {
			if(!prepare || g_status[id] != LOGEADO)
				return 0
				
			adv_vault_set_start(g_vault)
			adv_vault_set_field(g_vault, g_vdata[DATA_PW], g_pw[id])
			adv_vault_set_end(g_vault, g_id[id])
			return 1;
		}
	}
	return -2
}

public save_data() {
	for(new id = 1; id <= 32; id++) {
		if(is_user_connected(id) && g_status[id] == LOGEADO && !g_save[id]) 
			data(id, SAVE_DATA)
	}
	adv_vault_sort_update(g_vault, g_sort_top)
	set_task(3.0, "load_top")
}
public load_top() {
	new len, len2, len3, key, keyname[100]
	g_top[0] = EOS
	g_top2[0] = EOS
	g_top3[0] = EOS
	
	len = formatex(g_top[len], charsmax(g_top),
	"<html><style>\
	body { background: #040404; font-family: Verdana, Arial, Sans-Serif; font-size: 7pt;}\
	.t { color: #808080; text-align: left; }\
	.h { background: #222 url('http://onlyarg.com/foro/images/Amiral/thead.png') repeat-x; color:#000; font-weight: bold;}\
	.p { color: #D41313; }\
	.n { color: #fff; }\
	</style><body>\
	<br><br><table border=0 cellspacing=0 cellpadding=4 align=center width=100%% class=t>")
	
	len += formatex(g_top[len], charsmax(g_top)-len,
	"<tr><td class=h width=7%%>#</td>\
	<td class=h>Nombre</td>\
	<td class=h>Rango</td>\
	<td class=h>Frags</td></tr>")
	
	len2 = formatex(g_top2[len2], charsmax(g_top2),
	"<html><style>\
	body { background: #040404; font-family: Verdana, Arial, Sans-Serif; font-size: 7pt;}\
	.t { color: #808080; text-align: left; }\
	.h { background: #222 url('http://onlyarg.com/foro/images/Amiral/thead.png') repeat-x; color:#000; font-weight: bold;}\
	.p { color: #D41313; }\
	.n { color: #fff; }\
	</style><body>\
	<br><br><table border=0 cellspacing=0 cellpadding=4 align=center width=100%% class=t>")
	
	len2 += formatex(g_top2[len2], charsmax(g_top2)-len2,
	"<tr><td class=h width=7%%>#</td>\
	<td class=h>Nombre</td>\
	<td class=h>Rango</td>\
	<td class=h>Frags</td></tr>")
	
	len3 = formatex(g_top3[len3], charsmax(g_top3),
	"<html><style>\
	body { background: #040404; font-family: Verdana, Arial, Sans-Serif; font-size: 7pt;}\
	.t { color: #808080; text-align: left; }\
	.h { background: #222 url('http://onlyarg.com/foro/images/Amiral/thead.png') repeat-x; color:#000; font-weight: bold;}\
	.p { color: #D41313; }\
	.n { color: #fff; }\
	</style><body>\
	<br><br><table border=0 cellspacing=0 cellpadding=4 align=center width=100%% class=t>")
	
	len3 += formatex(g_top3[len3], charsmax(g_top3)-len3,
	"<tr><td class=h width=7%%>#</td>\
	<td class=h>Nombre</td>\
	<td class=h>Rango</td>\
	<td class=h>Frags</td></tr>")

	new i
	for(i = 1; i <= 10; i++) {
		key = adv_vault_sort_position(g_vault, g_sort_top, i)
		if(!adv_vault_get_prepare(g_vault, key)) 
			continue;
			
		adv_vault_get_field(g_vault, g_vdata[DATA_PJ], keyname, 31)
		
		replace_all(keyname, charsmax(keyname), "<", "&lt;")
		replace_all(keyname, charsmax(keyname), ">", "&gt;")
		
		len += formatex(g_top[len], charsmax(g_top)-len,\
		"<tr><td class=p>#%d</td>\
		<td class=n>%s</td>\
		<td>%s</td>\
		<td>%d</td>", i, keyname, rango[adv_vault_get_field(g_vault, g_vdata[DATA_RANGO])][RANGO_NAME], adv_vault_get_field(g_vault, g_vdata[DATA_FRAGS]))
	}
	for(i = 11; i <= 20; i++) {
		key = adv_vault_sort_position(g_vault, g_sort_top, i)
		if(!adv_vault_get_prepare(g_vault, key)) 
			continue;
			
		adv_vault_get_field(g_vault, g_vdata[DATA_PJ], keyname, 31)
		
		replace_all(keyname, charsmax(keyname), "<", "&lt;")
		replace_all(keyname, charsmax(keyname), ">", "&gt;")
		
		len2 += formatex(g_top2[len2], charsmax(g_top2)-len2,\
		"<tr><td class=p>#%d</td>\
		<td class=n>%s</td>\
		<td>%s</td>\
		<td>%d</td>", i, keyname, rango[adv_vault_get_field(g_vault, g_vdata[DATA_RANGO])][RANGO_NAME], adv_vault_get_field(g_vault, g_vdata[DATA_FRAGS]))
	}
	for(i = 21; i <= 30; i++) {
		key = adv_vault_sort_position(g_vault, g_sort_top, i)
		if(!adv_vault_get_prepare(g_vault, key)) 
			continue;
			
		adv_vault_get_field(g_vault, g_vdata[DATA_PJ], keyname, 31)
		
		replace_all(keyname, charsmax(keyname), "<", "&lt;")
		replace_all(keyname, charsmax(keyname), ">", "&gt;")
		
		len3 += formatex(g_top3[len3], charsmax(g_top3)-len3,\
		"<tr><td class=p>#%d</td>\
		<td class=n>%s</td>\
		<td>%s</td>\
		<td>%d</td>", i, keyname, rango[adv_vault_get_field(g_vault, g_vdata[DATA_RANGO])][RANGO_NAME], adv_vault_get_field(g_vault, g_vdata[DATA_FRAGS]))
	}
	
	add(g_top, charsmax(g_top)-len, "</table></body></html>")
	add(g_top2, charsmax(g_top2)-len2, "</table></body></html>")
	add(g_top3, charsmax(g_top3)-len3, "</table></body></html>")
}

create_filedb() {
	new datex[100], datex2[100]
	get_time("%d-%m-%Y", datex, charsmax(datex))
	get_time("%d-%m-%Y", datex2, charsmax(datex2))
	
	format(datex, charsmax(datex), "addons/amxmodx/data/copiasCTF/%s.dat", datex)
	if(!file_exists(datex)) 
		CopyFile("addons/amxmodx/data/adv_vault/sv/data.dat", datex)
	
	format(datex2, charsmax(datex2), "addons/amxmodx/data/copiasLOGEO/%s.dat", datex2)
	if(!file_exists(datex2)) 
		CopyFile("addons/amxmodx/data/adv_vault/autologeo/data.dat", datex2)
		
}
CopyFile(const szFile[], const szNewFile[])
{
	log_to_file("Copy.log", "Iniciando la copia del archivo ^"%s^" a ^"%s^"", szFile, szNewFile);
	
	new iFile    = fopen(szFile, "rb"),
	iNewFile = fopen(szNewFile, "wb"),
	iFileSize,
	iReadSize,
	szBuffer[1024];
	
	if (iFile && iNewFile)
	{
		fseek(iFile, 0, SEEK_END);
		iFileSize = ftell(iFile);
		fseek(iFile, 0, SEEK_SET);
		
		for (new iIndex = 0; iIndex < iFileSize; iIndex += 256)
		{
			iReadSize = fread_blocks(iFile, szBuffer, 256, BLOCK_CHAR);
			fwrite_blocks(iNewFile, szBuffer, iReadSize, BLOCK_CHAR);
		}
		
		log_to_file("Copy.log", "Se ha terminado de copiar el archivo.");
	}
	else
	{
		log_to_file("Copy.log", "No se pudo abrir el archivo a copiar o no se pudo crear la nueva copia.");
	}
	
	fclose(iFile);
	fclose(iNewFile);
}

// Fun Stocks
add_point(number)
{ 
	new count, i, str[29], str2[35], len
	num_to_str(number, str, charsmax(str))
	len = strlen(str)
	
	for (i = 0; i < len; i++)
	{
		if (i != 0 && ((len - i) %3 == 0))
		{
			add(str2, charsmax(str2), ".", 1)
			count++
			add(str2[i+count], 1, str[i], 1)
		}
		else
			add(str2[i+count], 1, str[i], 1)
	}

	return str2;
}
