#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <fun>
#include <hamsandwich>
#include <fakemeta>
#include <colorchat>
#include <jailbreak>
#include <engine>

#define PLUGIN "Jail Menu"
#define VERSION "1.0.6"
#define AUTHOR "Cypis edit Alelluja"
#define TAG "JailBreak"

new bool:menu_mikro, bool:menu_walka;
new w_trakcie[33];
new gracz_id[33],wybrany;

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_event("Damage", "Damage", "b", "2!=0");
	register_event("ResetHUD", "SpawnPlayer", "be")
	
	register_clcmd("+revision", "wlacz_rewizje");
	register_clcmd("-revision", "wylacz_rewizje");
	register_clcmd("say /menu", "MenuGraczy");
	register_clcmd("jail_amenu", "MenuAdmina", ADMIN_LEVEL_A);
	register_clcmd("say /a", "MenuAdmina", ADMIN_LEVEL_A);
	register_clcmd("say /dajdeagla", "menu")
	register_clcmd("say /podziel", "Podziel")
}

public plugin_precache()
{
	precache_sound("weapons/c4_disarm.wav");
	precache_sound("weapons/c4_disarmed.wav");
	precache_sound("weapons/dzwonek.wav");
}
public SpawnPlayer(id)
{
	w_trakcie[id] = 0;
	remove_task(5000+id);
}
public MenuGraczy(id)
{
	if(!is_user_alive(id))
	{
		MenuGraczaNieZywego(id);
		return PLUGIN_HANDLED;
	}
	
	switch(get_user_team(id))
	{
		case 1: MenuGraczaTT(id);
			case 2: MenuGraczaCT(id);
		}
	return PLUGIN_HANDLED;
}

public MenuGraczaTT(id)
{
	new menu = menu_create("\dMenu Wieznia:", "Handel_Menu");
	new cb = menu_makecallback("Menu_Callback");
	
	menu_additem(menu, "\yCzapki", 		"3");
	menu_additem(menu, "\ySklep", 		"19");
	menu_additem(menu, "\rKradnij Bron", 	"4");
	menu_additem(menu, "\wRuletka", 		"20");
	menu_additem(menu, "\wZyczenie", 	"18", 4, cb);
	
	
	menu_additem(menu, "\rFAQ Serwera", 	"0");
	menu_additem(menu, "\rZasady", 	"1");
	menu_additem(menu, "\rKomendy", 		"2");
	
	
	menu_setprop(menu, MPROP_EXITNAME, "Wyjdz");
	menu_setprop(menu, MPROP_BACKNAME, "<<<");
	menu_setprop(menu, MPROP_NEXTNAME, ">>>");
	menu_setprop(menu, MPROP_PERPAGE, 5);
	menu_display(id, menu);
}

public MenuAdmina(id)
{
	if(!(get_user_flags(id) & ADMIN_LEVEL_A))
	{
		client_print(id, print_chat, "Menu jest tylko dla Admina!")
		return PLUGIN_HANDLED;
	}
	new menu = menu_create("Menu Admina:", "Handel_Menu");
	
	menu_additem(menu, "\yPrzenies Gracza",       "13");
	menu_additem(menu, "Ozyw Gracza", "15");
	menu_additem(menu, "Mikro dla TT", 	"9");
	menu_additem(menu, "Otworz Cele", 	"7");
	menu_additem(menu, "Tryb Walki", 	"8");
	menu_additem(menu, "\rBan na CT",        "12");
	
	
	menu_setprop(menu, MPROP_EXITNAME, "Wyjdz");
	menu_setprop(menu, MPROP_BACKNAME, "<<<");
	menu_setprop(menu, MPROP_NEXTNAME, ">>>");
	menu_display(id, menu);
	
}

public MenuGraczaCT(id)
{
	new menu = menu_create("\wMenu Straznika:", "Handel_Menu");
	new cb = menu_makecallback("Menu_Callback");
	
	
	menu_additem(menu, "\rProwadz", 		"6", 1, cb);
	menu_additem(menu, "\yPrzeszukaj Wieznia","5");
	menu_additem(menu, "\yOtworz Cele", 	"7");
	menu_additem(menu, "\yTryb Walki", 	"8", 3, cb);
	menu_additem(menu, "\yMikro dla Wiezniow", 	"9");
	menu_additem(menu, "\yZabawy", 		"10", 3, cb);
	menu_additem(menu, "\yGong!","17", 3, cb);
	
	menu_additem(menu, "\yManager FD i Duszek","11");
	menu_additem(menu, "\yDaj Deagl'a Graczowi","21", 3, cb);
	menu_additem(menu, "\yPodziel Wiezniow na Dwie druzyny","22", 3, cb);
	
	new tekst[128];
	menu_additem(menu, "\yLosuj Wieznia", "23", 3 , cb);
	
	menu_additem(menu, "\rFAQ Serwera", 	"0");
	menu_additem(menu, "\rZasady", 	"1");
	menu_additem(menu, "\rKomendy", 		"2");
	
	menu_setprop(menu, MPROP_EXITNAME, "Wyjdz");
	menu_setprop(menu, MPROP_BACKNAME, "<<<");
	menu_setprop(menu, MPROP_NEXTNAME, ">>>");
	menu_display(id, menu);
}

public MenuGraczaNieZywego(id)
{
	new menu = menu_create("Menu Widza:", "Handel_Menu");
	
	menu_additem(menu, "FAQ Serwera", 	"0");
	menu_additem(menu, "Zasady", 	"1");
	menu_additem(menu, "Komendy", 		"2");
	
	menu_setprop(menu, MPROP_EXITNAME, "Wyjdz");
	menu_setprop(menu, MPROP_BACKNAME, "<<<");
	menu_setprop(menu, MPROP_NEXTNAME, ">>>");
	menu_display(id, menu);
}

public Menu_Callback(id, menu, item)
{
	static num[10], acces, callback;
	menu_item_getinfo(menu, item, acces, num, 9, _, _, callback);
	
	switch(acces)
	{
		case 1:
		{
			if(jail_get_prowadzacy() || !jail_get_days())
			{
				return ITEM_DISABLED;
			}
		}
		case 2:
		{
			if(id != jail_get_prowadzacy() && jail_get_days())
			{
				return ITEM_DISABLED;
			}
		}
		case 3:
		{
			if(id != jail_get_prowadzacy())
			{
				return ITEM_DISABLED;
			}
		}
		case 4:
		{
			if(id != jail_get_prisoner_last())
			{
				return ITEM_DISABLED;
			}
		}
		case 5:
		{
			if(!is_user_admin(id))
			{
				return ITEM_DISABLED;
			}
		}
	}
	return ITEM_ENABLED;
}

public Handel_Menu(id, menu, item)
{
	if(item == MENU_EXIT)
		return;
	
	new num[10], acces, callback;
	menu_item_getinfo(menu, item, acces, num, 9, _, _, callback);
	switch(str_to_num(num))
	{
		case 0: show_motd(id, "faq.txt", "FAQ Serwera");
		case 1: client_cmd(id, "say /zasady");
		case 2: show_motd(id, "komendy.txt", "Komendy");
		
		case 3: client_cmd(id, "say /czapki");
		case 4:
		{
			if(jail_get_prisoner_free(id) || jail_get_user_block(id))
				return;
			
			new body, target;
			get_user_aiming(id, target, body, 50);
			
			if(target && get_user_team(target) == 1)
			{
				ColorChat(id, TEAM_COLOR, "^x04[%s]^x03 Nie nacelowales na straznika", TAG);
				return;
			}
			if(!is_user_alive(target))
				return;
			
			new weapons = Jaki_Pistolet(target);
			if(!weapons)
			{
				ColorChat(id, TEAM_COLOR, "^x04[%s]^x03 Straznik nie ma broni krotkiej", TAG);
				return;
			}
			
			new weaponname[24];
			get_weaponname(weapons, weaponname, 23);
			
			ham_strip_weapon(target, weapons, weaponname);
			give_item(id, weaponname);
			
			ColorChat(id, TEAM_COLOR, "^x04[%s]^x03 Gratulacje - Ukradles bron", TAG);
		}
		
		case 5: w_trakcie[id]? wylacz_rewizje(id): wlacz_rewizje(id);
		
		case 6:
		{
			if(!jail_get_prowadzacy())
				jail_set_prowadzacy(id);
		}
		
		case 7: jail_open_cele();
		case 8: jail_set_prisoners_fight((menu_walka = !menu_walka), !menu_walka);
		case 9: jail_set_prisoners_micro((menu_mikro = !menu_mikro));
		case 10: client_cmd(id, "say /zabawy");
		case 11: MenuFreeday(id);
		case 12: client_cmd(id, "jail_menuban", "Ban na CT");
		case 13: client_cmd(id, "amx_teammenu", "Przenies TT/CT/SPEC");
		case 14: client_cmd(id, "jail_amenu");
		case 15: client_cmd(id, "say /ozyw");
		case 16: client_cmd(id, "jail_mute");
		case 17: client_cmd(0, "spk sound/JailBreak[k]/dzwonek.wav");
		case 18: client_cmd(id, "say /lr");
		case 19: client_cmd(id, "say /sklep");
		case 20: client_cmd(id, "say /ruletka");
		case 21: client_cmd(id, "say /dajdeagla");
		case 22: Podziel(id)
		case 23: client_cmd(id, "say /losowanie");
	}
	
     }
}

public MenuFreeday(id)
{
	new menu2 = menu_create("Manager FD i Duszek:", "Handel_ManagerFreeday");
	menu_additem(menu2, "\rDaj Freeday");
	menu_additem(menu2, "Daj Duszka^n");
	menu_additem(menu2, "\rZabierz Freeday");
	menu_additem(menu2, "Zabierz Duszka");
	
	menu_setprop(menu2, MPROP_EXITNAME, "Wyjdz");
	menu_display(id, menu2);
}

public Handel_ManagerFreeday(id, menu, item)
{
	if(item == MENU_EXIT)
		return;
	
	new acces, callback, data[3], iname[32];
	menu_item_getinfo(menu, item, acces, data, 2, iname, 31, callback);
	replace(iname, 31, "^n", "");
	
	new menu2 = menu_create(iname, (!item || item == 2)? "Handel_Menu_Freeday": "Handel_Menu_Duszek");
	for(new i=1; i<=32; i++)
	{
		if(!is_user_alive(i) || cs_get_user_team(i) != CS_TEAM_T)
			continue;
		
		switch(item)
		{
			case 0,1:
			{
				if(jail_get_prisoner_free(i) || jail_get_prisoner_ghost(i))
					continue;
			}
			case 2:
			{
				if(!jail_get_prisoner_free(i))
					continue;
			}
			case 3:
			{
				if(!jail_get_prisoner_ghost(i))
					continue;
			}
		}
		new name[32];
		get_user_name(i, name, 31);
		menu_additem(menu2, name, (!item || item == 1)? "1": "0");
	}
	menu_setprop(menu2, MPROP_EXITNAME, "Wyjdz");
	menu_display(id, menu2);
}

public Handel_Menu_Freeday(id, menu, item)
{
	if(item == MENU_EXIT)
		return;
	
	new id2, callback, data[3], name[32];
	menu_item_getinfo(menu, item, id2, data, 2, name, 31, callback);
	callback = data[0]-'0';
	
	id2 = get_user_index(name);
	jail_set_prisoner_free(id2, bool:callback, false);
	
	new szName[2][32];
	get_user_name(id, szName[0], 31);
	get_user_name(id2, szName[1], 31);
	ColorChat(0, TEAM_COLOR,"^x04[%s] ^x03%s ^x01%s freedaya ^x03%s",TAG,szName[0], callback? "dal": "zabral",szName[1]);
}

public Handel_Menu_Duszek(id, menu, item)
{
	if(item == MENU_EXIT)
		return;
	
	new id2, callback, data[3], name[32];
	menu_item_getinfo(menu, item, id2, data, 2, name, 31, callback);
	callback = data[0]-'0';
	
	id2 = get_user_index(name);
	jail_set_prisoner_ghost(id2, bool:callback, false);
	
	new szName[2][32];
	get_user_name(id, szName[0], 31);
	get_user_name(id2, szName[1], 31);
	ColorChat(0, TEAM_COLOR,"^x04[%s] ^x03%s ^x01%s duszka ^x03%s",TAG,szName[0], callback? "dal": "zabral",szName[1]);
}

public OnRemoveData(day)
{
	menu_mikro = false;
	menu_walka = false;
}

public Handel_Menu_Widza(id, menu, item)
{
	if(item == MENU_EXIT)
		return;
	
	switch(item)
	{
		case 0: show_motd(id, "faq.txt", "FAQ Serwera");
		case 1: client_cmd(id, "say /zasady");
		case 2: show_motd(id, "komendy.txt", "Komendy");
	}
}

public wlacz_rewizje(id)
{
	if(get_user_team(id) != 2 || !is_user_alive(id))
		return PLUGIN_HANDLED;
	
	new body, target;
	get_user_aiming(id, target, body, 50);
	
	if(target && get_user_team(target) == 2)
	{
		ColorChat(id, TEAM_COLOR, "^x04[%s]^x03 Nie nacelowales na wieznia", TAG);
		return PLUGIN_HANDLED;
	}
	if(!is_user_alive(target))
		return PLUGIN_HANDLED;
	
	if(jail_get_user_block(target))
		return PLUGIN_HANDLED;
	
	jail_set_user_speed(id, 0.1);
	set_bartime(id, 5);
	
	set_bartime(target, 5);
	jail_set_user_speed(target, 0.1);
	
	w_trakcie[id] = target;
	w_trakcie[target] = id;
	set_task(5.0, "wylacz_rewizje", 5000+id);
	emit_sound(id, CHAN_WEAPON, "weapons/c4_disarm.wav", 1.0, ATTN_NORM, 0, PITCH_NORM);
	return PLUGIN_HANDLED;
}

public wylacz_rewizje(taskid)
{
	new id = taskid;
	if(taskid > 32)
		id -= 5000;
	
	if(get_user_team(id) != 2 || !w_trakcie[id])
		return PLUGIN_HANDLED;
	
	remove_task(id+5000);
	jail_set_user_speed(id, 250.0);
	set_bartime(id, 0);
	
	if(is_user_alive(w_trakcie[id]))
	{
		jail_set_user_speed(w_trakcie[id], 250.0);
		set_bartime(w_trakcie[id], 0);
	}
	if(taskid > 32)
		Pokaz_bronie(id);
	
	w_trakcie[w_trakcie[id]] = 0;
	w_trakcie[id] = 0;
	return PLUGIN_HANDLED;
}

public Pokaz_bronie(id)
{
	if(!is_user_alive(id) || !is_user_alive(w_trakcie[id]))
		return;
	
	new weapons[32], numweapons, weaponname[32];
	get_user_weapons(w_trakcie[id], weapons, numweapons);
	ColorChat(id, GREEN, "Znalazles:");
	for(new i=0; i<numweapons; i++)
	{
		get_weaponname(weapons[i], weaponname, 31);
		replace(weaponname, 32, "weapon_", "");
		replace(weaponname, 32, "knife", "piesci");
		ColorChat(id, BLUE, weaponname);
	}
	emit_sound(id, CHAN_WEAPON, "weapons/c4_disarmed.wav", 1.0, ATTN_NORM, 0, PITCH_NORM);
}

public Damage(id)
{
	if(is_user_alive(id) && w_trakcie[id])
	{
		wylacz_rewizje(id);
	}
}

stock Jaki_Pistolet(id)
{
	if(!is_user_connected(id))
		return 0;
	
	new weapons[32], numweapons;
	get_user_weapons(id, weapons, numweapons);
	
	for(new i=0; i<numweapons; i++)
		if((1<<weapons[i]) & 0x4030402)
			return weapons[i];
	
	return 0;
}

stock ham_strip_weapon(id, wid, szname[])
{
	if(!wid)
		return 0;
	
	new ent;
	while((ent = engfunc(EngFunc_FindEntityByString, ent, "classname", szname)) && pev(ent, pev_owner) != id) {}
	if(!ent)
		return 0;
	
	if(get_user_weapon(id) == wid)
		ExecuteHam(Ham_Weapon_RetireWeapon, ent);
	
	if(ExecuteHam(Ham_RemovePlayerItem, id, ent))
	{
		ExecuteHam(Ham_Item_Kill, ent);
		set_pev(id, pev_weapons, pev(id, pev_weapons) & ~(1<<wid));
	}
	return 1;
}

stock set_bartime(id, czas)
{
	message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("BarTime"), _, id);
	write_short(czas);
	message_end();
}


public menu(id, level, cid)
{
	
	new menu = menu_create("Wybierz Gracza:","menu_broni");
	for(new i=0, n=0; i<=32; i++)
	{
		if(!is_user_connected(i) || !is_user_alive(i) || cs_get_user_team(i) != CS_TEAM_T)
			continue;
		gracz_id[n++] = i;
		new nazwa_gracza[64];
		get_user_name(i, nazwa_gracza, 63)
		menu_additem(menu, nazwa_gracza, "0", 0);
	}
	
	menu_setprop(menu, MPROP_BACKNAME, "Wroc");
	menu_setprop(menu, MPROP_NEXTNAME, "Dalej");
	menu_setprop(menu, MPROP_EXITNAME, "Wyjscie");
	menu_display(id, menu);
	return PLUGIN_HANDLED;
}


public menu_broni(id, menu, item)
{
	if (get_user_team(id) == 2)
	{
		wybrany = gracz_id[item];
		new menu = menu_create("\rMenu Broni", "wybieranie")
		
		menu_additem(menu, "\wDeagle bez naboi", "1", 0)
		menu_additem(menu, "\wDeagle z 1 nabojem", "2", 0)
		menu_additem(menu, "\wDeagle z 3 nabojami", "3", 0)
		menu_additem(menu, "\wDeagle z pelnym magazynkiem", "4", 0)
		
		menu_setprop(menu, MPROP_EXIT, MEXIT_ALL)
		
		menu_display(id, menu, 0)
	}
	else
	{
		client_print(id, print_chat, "Nie masz do tego dostepu Ziomus!")
		return PLUGIN_HANDLED
	}
	return PLUGIN_HANDLED;
}
public wybieranie(id, menu, item)
{
	if (item == MENU_EXIT)
	{
		menu_destroy(menu)
		return PLUGIN_HANDLED
	}
	new data[6], iName[64]
	new acces, callback
	menu_item_getinfo(menu, item, acces, data,5, iName, 63, callback)
	
	new key = str_to_num(data)
	
	switch(key)
	{
		case 1:
		{
			give_item(wybrany, "weapon_deagle");
			new dgl_id = find_ent_by_owner(-1, "weapon_deagle", wybrany)
			if(dgl_id){
				cs_set_weapon_ammo(dgl_id, 0);
			}
			client_print(id, print_chat, "%s dostal pustego deagla",wybrany)
			menu_destroy(menu)
			return PLUGIN_HANDLED
		}
		
		case 2:
		{
			give_item(wybrany, "weapon_deagle");
			new dgl_id = find_ent_by_owner(-1, "weapon_deagle", wybrany)
			if(dgl_id){
				cs_set_weapon_ammo(dgl_id, 1);
			}
			client_print(id, print_chat, "%s dostal deagla z 1 nabojem",wybrany)
			menu_destroy(menu)
			return PLUGIN_HANDLED
		}
		
		case 3:
		{
			give_item(wybrany, "weapon_deagle");
			new dgl_id = find_ent_by_owner(-1, "weapon_deagle", wybrany)
			if(dgl_id){
				cs_set_weapon_ammo(dgl_id, 3);
			}
			client_print(id, print_chat, "%s dostal deagla z 3 nabojami",wybrany)
			menu_destroy(menu)
			return PLUGIN_HANDLED
		}
		case 4:
		{
			give_item(wybrany, "weapon_deagle");
			new dgl_id = find_ent_by_owner(-1, "weapon_deagle", wybrany)
			if(dgl_id){
				cs_set_weapon_ammo(dgl_id, 35);
			}
			client_print(id, print_chat, "%s dostal deagla z pelnym magazynkiem",wybrany)
			menu_destroy(menu)
			return PLUGIN_HANDLED
		}
	}
	menu_destroy(menu)
	return PLUGIN_HANDLED
}
public Podziel(id)
{
	
	if(!is_user_alive(id) || get_user_team(id) != 2) return 1;
	
	new bool:team;
	for(new i = 1; i <= 32; i++)
	{
		if(!is_user_alive(i) || get_user_team(i) != 1) continue;
		
		if(team)
		{
			set_pev(i, pev_body, 1);
			client_cmd(i, "spk vox/pink");
			set_user_rendering(i, kRenderFxGlowShell, 254, 107, 239, kRenderNormal, 5)
			client_print(i, print_center, "Masz kolor: Rozowy")
		}
		else
		{
			set_pev(i, pev_body, 2);
			client_cmd(i, "spk vox/blue");
			set_user_rendering(i, kRenderFxGlowShell, 10, 98, 245, kRenderNormal, 5)
			client_print(i, print_center, "Masz kolor: blue")
		}
		
		
		client_print(i, print_center, "Masz kolor %s", team ? "Rozowy" : "Blekitny");
		team = !team;
	}
	return 1;
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1045\\ f0\\ fs16 \n\\ par }
*/
