#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <fun>
#include <hamsandwich>
#include <fakemeta_util>
#include <WPMGPrintChatColor>
#include <next21_DR_core>
#include <next21_DR_secondary>

#define PLUGIN "n21_Deathrun_vipmenu"
#define VERSION "1.0"
#define AUTHOR "Psycrow"

#if cellbits == 32
	#define OFFSET_CSMONEY 115
#else
	#define OFFSET_CSMONEY 140
#endif

new bral[33][9], g_msgMoney, g_maxplayers, g_player_id[33][33]

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
					
	register_logevent("NewRound", 2, "1=Round_Start")
					
	register_clcmd("say /vip", "Vip_Menu" )
	register_clcmd("say_team /vip", "Vip_Menu" )
	register_clcmd("vip", "Vip_Menu" )
	
	g_msgMoney = get_user_msgid("Money")
	g_maxplayers = get_maxplayers()
}

public NewRound()
{
	for(new id=1;id<=g_maxplayers;id++)
		bral[id]={1,1,1,1,1,1,3,3,3}
}
	
public Vip_Menu(id)
{		
	new menu_name[160], status[24]
	
	if(get_user_flags(id) & ADMIN_LEVEL_H)
		status = "VIP"
	else
		status = "Игрок"
		
	format(menu_name, charsmax(menu_name), "\rВип меню^n\dСтатус: \r%s, \dЦена: \r150 рублей", status)
	new dr_vipmenu = menu_create(menu_name, "Vip_Handler")
	
	if(get_user_flags(id) & ADMIN_LEVEL_H)
	{
		if(is_user_alive(id) && get_user_gravity(id) > 0.5 && bral[id][0])
			menu_additem(dr_vipmenu, "\wГравитация", "1", 0)
		else
			menu_additem(dr_vipmenu, "\dГравитация", "1", 0)
			
		if(is_user_alive(id) && get_user_maxspeed(id) < 400.0 && !(cs_get_user_team(id) == CS_TEAM_T && n21_dr_get_mode_st_id() == 4) && bral[id][1])
			menu_additem(dr_vipmenu, "\wСкорость", "2", 0)
		else
			menu_additem(dr_vipmenu, "\dСкорость", "2", 0)
			
		if(get_pdata_int(id, OFFSET_CSMONEY) < 50000 && bral[id][2])
			menu_additem(dr_vipmenu, "\wДеньги (6000$)^n", "3", 0)
		else
			menu_additem(dr_vipmenu, "\dДеньги (6000$)^n", "3", 0)
			
		if(!is_user_alive(id) && !n21_in_duel() && bral[id][3])
			menu_additem(dr_vipmenu, "\wВозродиться", "4", 0)
		else
			menu_additem(dr_vipmenu, "\dВозродиться", "4", 0)
			
		if(is_user_alive(id) && get_user_health(id) != 255 && !n21_in_duel() && !(cs_get_user_team(id) == CS_TEAM_T && n21_dr_get_mode_st_id() == 2) && bral[id][4])
			menu_additem(dr_vipmenu, "\w255 HP", "5", 0)
		else
			menu_additem(dr_vipmenu, "\d255 HP", "5", 0)
			
		if(is_user_alive(id) && get_user_armor(id) != 255 && !n21_in_duel() && !(cs_get_user_team(id) == CS_TEAM_T && n21_dr_get_mode_st_id() == 2) && bral[id][5])
			menu_additem(dr_vipmenu, "\w255 Armor^n", "6", 0)
		else
			menu_additem(dr_vipmenu, "\d255 Armor^n", "6", 0)
			
		if(!n21_in_duel() && bral[id][6])
		{
			new lol[64]
			format(lol, 64, "\wВозродить игрока (%d)", bral[id][6])
			menu_additem(dr_vipmenu, lol, "7", 0)
		}
		else
		{
			new lol[64]
			format(lol, 64, "\dВозродить игрока (%d)", bral[id][6])
			menu_additem(dr_vipmenu, lol, "7", 0)
		}
			
		if(bral[id][7])
		{
			new lol[64]
			format(lol, 64, "\wПодарить гравитацию (%d)", bral[id][7])
			menu_additem(dr_vipmenu, lol, "8", 0)
		}
		else
		{
			new lol[64]
			format(lol, 64, "\dПодарить гравитацию (%d)", bral[id][7])
			menu_additem(dr_vipmenu, lol, "8", 0)
		}
			
		if(bral[id][8])
		{
			new lol[64]
			format(lol, 64, "\wПодарить скорость (%d)", bral[id][8])
			menu_additem(dr_vipmenu, lol, "9", 0)
		}
		else
		{
			new lol[64]
			format(lol, 64, "\dПодарить скорость (%d)", bral[id][8])
			menu_additem(dr_vipmenu, lol, "9", 0)
		}
	}
	else
	{
		menu_additem(dr_vipmenu, "\dГравитация", "1", 0)
		menu_additem(dr_vipmenu, "\dСкорость", "2", 0)
		menu_additem(dr_vipmenu, "\dДеньги (6000$)", "3", 0)
		menu_additem(dr_vipmenu, "\dВозродиться", "4", 0)
		menu_additem(dr_vipmenu, "\d255 HP", "5", 0)
		menu_additem(dr_vipmenu, "\d255 Armor", "6", 0)
		menu_additem(dr_vipmenu, "\dВозродить игрока (2)", "7", 0)
		menu_additem(dr_vipmenu, "\dПодарить гравитацию (2)", "8", 0)
		menu_additem(dr_vipmenu, "\dПодарить скорость (2)", "9", 0)
	}
		
	menu_setprop(dr_vipmenu, MPROP_EXIT, MEXIT_ALL)
	menu_setprop(dr_vipmenu, MPROP_EXITNAME, "\yВыход")
	menu_setprop(dr_vipmenu, MPROP_NEXTNAME, "\yВперед")
	menu_setprop(dr_vipmenu, MPROP_BACKNAME, "\yНазад")
	
	menu_display(id, dr_vipmenu, 0)
}

public Vip_Handler(id, menu, item)
{
	if(item == MENU_EXIT)
	{
		menu_destroy(menu)
		client_cmd(id,"menu2")
		return PLUGIN_HANDLED
	}
			
	if(!(get_user_flags(id) & ADMIN_LEVEL_H))
	{
		PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tУ вас нету доступа к VIP меню. Skype: !gpsycrow0. !tЦена: !g150 рублей") 	
		menu_destroy(menu)
		return PLUGIN_HANDLED
	}
	
	if(!bral[id][item])
	{
		PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tЭтот пункт будет доступен со следующего раунда") 
		menu_destroy(menu)
		Vip_Menu(id)
		return PLUGIN_HANDLED	
	}	
	
	switch(item)
	{
		case 0:
		{
			if(!is_user_alive(id))
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tВы мертвы") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED	
			}
			
			if(get_user_gravity(id) <= 0.5)
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tЭто у вас уже есть") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED	
			}
	
			set_user_gravity(id, 0.5)
			bral[id][0]--
		}
		case 1:
		{
			if(!is_user_alive(id))
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tВы мертвы") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED	
			}
			
			if(get_user_maxspeed(id) >= 400.0 || (cs_get_user_team(id) == CS_TEAM_T && n21_dr_get_mode_st_id() == 4))
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tЭто у вас уже есть") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED
			}
		
			n21_dr_set_maxspeed(id, 400.0)
			bral[id][1]--
		}
		case 2:
		{
			if(get_pdata_int(id, OFFSET_CSMONEY) >= 50000)
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tУ вас макимум денег") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED	
			}
			
			set_fix_money(id,6000)
			bral[id][2]--
		}
		case 3:
		{
			if(is_user_alive(id))
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tВы живы") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED	
			}
			
			if(n21_in_duel())
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tСейчас дуэль") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED		
			}
			
			ExecuteHam(Ham_CS_RoundRespawn, id)
			bral[id][3]--
		}
		case 4:
		{
			if(!is_user_alive(id))
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tВы мертвы") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED	
			}
			
			if(get_user_health(id) == 255)
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tМаксимум хп") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED		
			}
			
			if(n21_in_duel())
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tСейчас дуэль") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED		
			}
			
			if(cs_get_user_team(id) == CS_TEAM_T && n21_dr_get_mode_st_id() == 2)
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tРежим призрак") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED		
			}
			
			fm_set_user_health(id, 250)
			bral[id][4]--
		}
		case 5:
		{
			if(!is_user_alive(id))
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tВы мертвы") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED	
			}
			
			if(get_user_armor(id) == 255)
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tМаксимум брони") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED		
			}
			
			if(n21_in_duel())
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tСейчас дуэль") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED		
			}
			
			if(cs_get_user_team(id) == CS_TEAM_T && n21_dr_get_mode_st_id() == 2)
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tРежим призрак") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED		
			}
			
			fm_set_user_armor(id, 250)
			bral[id][5]--
		}
		case 6:
		{
			if(n21_in_duel())
			{
				PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tСейчас дуэль") 
				menu_destroy(menu)
				Vip_Menu(id)
				return PLUGIN_HANDLED		
			}
						
			new dr_plrsmenu = menu_create("\rВозродить игрока:", "Players_Handler_pt1")
			
			new Players[32], Count
			get_players(Players, Count, "h") 
			
			for(new i=0; i<Count; i++)
			{
				new pl_name[32], number[3]
				num_to_str(i, number, 3)
				get_user_name(Players[i], pl_name, 32)
				if(!is_user_alive(Players[i]) && id != Players[i])
					format(pl_name, 32, "\w%s", pl_name)
				else
					format(pl_name, 32, "\d%s", pl_name)
					
				menu_additem(dr_plrsmenu, pl_name, number, 0)	
				g_player_id[id][i] = Players[i]
			}
			
			menu_setprop(dr_plrsmenu, MPROP_EXIT, MEXIT_ALL)
			menu_setprop(dr_plrsmenu, MPROP_EXITNAME, "\yВыход")
			menu_setprop(dr_plrsmenu, MPROP_NEXTNAME, "\yВперед")
			menu_setprop(dr_plrsmenu, MPROP_BACKNAME, "\yНазад")
	
			menu_display(id, dr_plrsmenu, 0)
			return PLUGIN_HANDLED	
		}
		case 7:
		{			
			new dr_plrsmenu = menu_create("\rВыдать скорость:", "Players_Handler_pt2")
			
			new Players[32], Count
			get_players(Players, Count, "h") 
			
			for(new i=0; i<Count; i++)
			{
				new pl_name[32], number[3]
				num_to_str(i, number, 3)
				get_user_name(Players[i], pl_name, 32)
				if(is_user_alive(Players[i]) && id != Players[i] && get_user_gravity(Players[i]) > 0.5)
					format(pl_name, 32, "\w%s", pl_name)
				else
					format(pl_name, 32, "\d%s", pl_name)
					
				menu_additem(dr_plrsmenu, pl_name, number, 0)	
				g_player_id[id][i] = Players[i]
			}
			
			menu_setprop(dr_plrsmenu, MPROP_EXIT, MEXIT_ALL)
			menu_setprop(dr_plrsmenu, MPROP_EXITNAME, "\yВыход")
			menu_setprop(dr_plrsmenu, MPROP_NEXTNAME, "\yВперед")
			menu_setprop(dr_plrsmenu, MPROP_BACKNAME, "\yНазад")
	
			menu_display(id, dr_plrsmenu, 0)
			return PLUGIN_HANDLED	
		}
		case 8:
		{			
			new dr_plrsmenu = menu_create("\rВыдать гравитацию:", "Players_Handler_pt3")
			
			new Players[32], Count
			get_players(Players, Count, "h") 
			
			for(new i=0; i<Count; i++)
			{
				new pl_name[32], number[3]
				num_to_str(i, number, 3)
				get_user_name(Players[i], pl_name, 32)
				if(is_user_alive(Players[i]) && id != Players[i] && get_user_maxspeed(Players[i]) < 400.0 && !(cs_get_user_team(Players[i]) == CS_TEAM_T && n21_dr_get_mode_st_id() == 4))
					format(pl_name, 32, "\w%s", pl_name)
				else
					format(pl_name, 32, "\d%s", pl_name)
					
				menu_additem(dr_plrsmenu, pl_name, number, 0)	
				g_player_id[id][i] = Players[i]
			}
			
			menu_setprop(dr_plrsmenu, MPROP_EXIT, MEXIT_ALL)
			menu_setprop(dr_plrsmenu, MPROP_EXITNAME, "\yВыход")
			menu_setprop(dr_plrsmenu, MPROP_NEXTNAME, "\yВперед")
			menu_setprop(dr_plrsmenu, MPROP_BACKNAME, "\yНазад")
	
			menu_display(id, dr_plrsmenu, 0)
			return PLUGIN_HANDLED	
		}
	}
	
	menu_destroy(menu)
	Vip_Menu(id)
	return PLUGIN_HANDLED		
}

public Players_Handler_pt1(id, menu, item)
{
	if(item == MENU_EXIT)
	{
		menu_destroy(menu)
		Vip_Menu(id)
		return PLUGIN_HANDLED
	}
	
	if(!bral[id][6])
	{
		PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tЭтот пункт будет доступен со следующего раунда") 
		menu_destroy(menu)
		Vip_Menu(id)
		return PLUGIN_HANDLED
	}
	
	if(n21_in_duel())
	{
		PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tСейчас дуэль")
		menu_destroy(menu)
		Vip_Menu(id)
		return PLUGIN_HANDLED
	}
	
	new page_number = floatround(item/7.0,floatround_floor)
	
	if(is_user_connected(g_player_id[id][item]))
	{
		if(is_user_alive(g_player_id[id][item]))
		{
			PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tЭтот игрок жив") 
			menu_display(id, menu, page_number)
			return PLUGIN_HANDLED
		}
		else if(id == g_player_id[id][item])
		{
			PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tНельзя использовать на самого себя") 
			menu_display(id, menu, page_number)
			return PLUGIN_HANDLED		
		}
		else
		{
			new pl_name[32], vip_name[32]
			get_user_name(g_player_id[id][item], pl_name, 32)
			get_user_name(id, vip_name, 32)
			PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tВы возродили игрока !g%s", pl_name) 
			PrintChatColor(g_player_id[id][item], PRINT_COLOR_PLAYERTEAM, "!g[VIP] %s !tвозродил вас", vip_name) 
			ExecuteHam(Ham_CS_RoundRespawn, g_player_id[id][item])
			bral[id][6]--
			menu_display(id, menu, page_number)
			return PLUGIN_HANDLED
		}
	}
	
	menu_display(id, menu, page_number)
	return PLUGIN_HANDLED	
}

public Players_Handler_pt2(id, menu, item)
{
	if(item == MENU_EXIT)
	{
		menu_destroy(menu)
		Vip_Menu(id)
		return PLUGIN_HANDLED
	}
	
	if(!bral[id][7])
	{
		PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tЭтот пункт будет доступен со следующего раунда") 
		menu_destroy(menu)
		Vip_Menu(id)
		return PLUGIN_HANDLED
	}
	
	new page_number = floatround(item/7.0,floatround_floor)
	
	if(is_user_connected(g_player_id[id][item]))
	{
		if(!is_user_alive(g_player_id[id][item]))
		{
			PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tЭтот игрок мертв") 
			menu_display(id, menu, page_number)
			return PLUGIN_HANDLED
		}
		if(get_user_gravity(g_player_id[id][item]) <= 0.5)
		{
			PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tУ игрока уже есть гравитация") 
			menu_display(id, menu, page_number)
			return PLUGIN_HANDLED	
		}
		if(id == g_player_id[id][item])
		{
			PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tНельзя использовать на самого себя") 
			menu_display(id, menu, page_number)
			return PLUGIN_HANDLED
		}
		else
		{
			new pl_name[32], vip_name[32]
			get_user_name(g_player_id[id][item], pl_name, 32)
			get_user_name(id, vip_name, 32)
			PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tВы выдали гравитацию игроку !g%s", pl_name) 
			PrintChatColor(g_player_id[id][item], PRINT_COLOR_PLAYERTEAM, "!g[VIP] %s !tвыдал вам гравитацию", vip_name) 
			set_user_gravity(g_player_id[id][item], 0.5)
			bral[id][8]--
			menu_display(id, menu, page_number)
			return PLUGIN_HANDLED
		}
	}
	
	menu_display(id, menu, page_number)
	return PLUGIN_HANDLED	
}

public Players_Handler_pt3(id, menu, item)
{
	if(item == MENU_EXIT)
	{
		menu_destroy(menu)
		Vip_Menu(id)
		return PLUGIN_HANDLED
	}
	
	if(!bral[id][8])
	{
		PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tЭтот пункт будет доступен со следующего раунда") 
		menu_destroy(menu)
		Vip_Menu(id)
		return PLUGIN_HANDLED
	}
	
	new page_number = floatround(item/7.0,floatround_floor)
	
	if(is_user_connected(g_player_id[id][item]))
	{
		if(!is_user_alive(g_player_id[id][item]))
		{
			PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tЭтот игрок мертв") 
			menu_display(id, menu, page_number)
			return PLUGIN_HANDLED
		}
		if(get_user_maxspeed(g_player_id[id][item]) >= 400.0 || (cs_get_user_team(g_player_id[id][item]) == CS_TEAM_T && n21_dr_get_mode_st_id() == 4))
		{
			PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tУ игрока уже повышена скорость") 
			menu_display(id, menu, page_number)
			return PLUGIN_HANDLED	
		}
		if(id == g_player_id[id][item])
		{
			PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tНельзя использовать на самого себя") 
			menu_display(id, menu, page_number)
			return PLUGIN_HANDLED
		}
		else
		{
			new pl_name[32], vip_name[32]
			get_user_name(g_player_id[id][item], pl_name, 32)
			get_user_name(id, vip_name, 32)
			PrintChatColor(id, PRINT_COLOR_PLAYERTEAM, "!g[VIP] !tВы выдали скорость игроку !g%s", pl_name) 
			PrintChatColor(g_player_id[id][item], PRINT_COLOR_PLAYERTEAM, "!g[VIP] %s !tвыдал вам скорость", vip_name) 
			n21_dr_set_maxspeed(g_player_id[id][item], 400.0)
			bral[id][7]--
			menu_display(id, menu, page_number)
			return PLUGIN_HANDLED
		}
	}
	
	menu_display(id, menu, page_number)
	return PLUGIN_HANDLED	
}

stock set_fix_money(id,money)
{
	new MAX_MONEY = n21_dr_get_maxmoney()
	new curr_money = get_pdata_int(id, OFFSET_CSMONEY)
	if(curr_money+money > MAX_MONEY)
		money = MAX_MONEY-curr_money
			
	set_pdata_int(id, OFFSET_CSMONEY, curr_money+money)
			
	message_begin(MSG_ONE, g_msgMoney, _, id)
	write_long(curr_money+money)
	write_byte(1)
	message_end()
	
	return money
}
