class 1 custom_pvp_matchmaker : teleporter
{
parameter:
	string	fnLowLevel = "custom_pvp_master_ll.htm";
	string	fnCongrats = "custom_pvp_master_c.htm";
	string	fnInstructions = "custom_pvp_master_in.htm";
	string	fnReady = "custom_pvp_master_par.htm";
	string	fnParty = "custom_pvp_party.htm";
	string	fn5v5 = "custom_pvp_5v5.htm";
	int	TelPosX1 = 151484;
	int	TelPosY1 = 46731;
	int	TelPosZ1 = -3400;
	int	TelPosX2 = 147567;
	int	TelPosY2 = 46711;
	int	TelPosZ2 = -3400;
	int	SpawnPosX = 82904;
	int	SpawnPosY = 149399;
	int	SpawnPosZ = -3464;
	int	TownPosX = 83396;
	int	TownPosY = 147980;
	int	TownPosZ = -3400;
	string	DoorName1 = "aden_colosseum_001_002";
	string	DoorName2 = "aden_colosseum_002_001";
	int	buffuptime = 30;
	int	doorclosetime = 15;
	int	matchtime = 360;
	int	regist_announce = 10000;
	int	participant_announce = 60000;
	int	reward_item_id = 57;
	int	reward_item_id_2 = 100000;
	int	reward_item_amount = 0;
	int	reward_item_amount_2 = 0;
	int	min_level = 81;

handler:
	EventHandler CREATED()
	{
		myself.i_ai0 = 0;
		myself.i_ai4 = 0;
		myself::AddTimerEx(2001,5000);
		myself::AddTimerEx(2002,10000);
		super;
	}

	EventHandler TALKED(talker,i0,i1,i2,c0,c1,party0,fhtml0)
	{
		if( myself::Party_GetCount(talker) > 1 )
		{
			party0 = myself::GetParty(talker);
			c1 = myself::GetLeaderOfParty(party0);
			i0 = myself::Party_GetCount(c1);
			for(i1 = 0; i1 < i0; i1 = ( i1 + 1 ))
			{
				c0 = myself::Party_GetCreature(c1,i1);
				if( myself::GetMemoState(c0,999) == myself.i_ai2 || myself::GetMemoState(c0,999) == myself.i_ai3 )
				{
					i2 = 1;
				}
			}
			if( i2 == 1 )
			{
				myself::FHTML_SetFileName(fhtml0,fnInstructions);
				myself::FHTML_SetInt(fhtml0,"matchtime",matchtime);
				myself::ShowFHTML(talker,fhtml0);
			}
			else
			{
				myself::FHTML_SetFileName(fhtml0,fnHi);
				myself::FHTML_SetInt(fhtml0,"waiting",myself.i_ai4);
				myself::ShowFHTML(talker,fhtml0);
			}
		}
		else if( myself::GetMemoState(talker,999) == myself.i_ai2 || myself::GetMemoState(talker,999) == myself.i_ai3 )
		{
			myself::FHTML_SetFileName(fhtml0,fnInstructions);
			myself::FHTML_SetInt(fhtml0,"matchtime",matchtime);
			myself::ShowFHTML(talker,fhtml0);
		}
		else
		{
			myself::FHTML_SetFileName(fhtml0,fnHi);
			myself::ShowFHTML(talker,fhtml0);
		}
	}

	EventHandler MENU_SELECTED(talker,ask,reply,i3,i4,i5,c0,c1,c2,c3,party0,party1)
	{
		if( ask == -23 )
		{
			if( reply == 1 )
			{
				if( talker.level >= min_level )
				{
					if( myself.i_ai0 == 0 )
					{
						if( myself::Party_GetCount(talker) == 3 || myself::Party_GetCount(talker) == 5 )
						{
							party0 = myself::GetParty(talker);
							c3 = myself::GetLeaderOfParty(party0);
							i4 = myself::Party_GetCount(c3);
							i3 = ( 2 + gg::Rand(100) );
							myself.i_ai0 = 1;
							myself.i_ai2 = i3;
							myself.i_ai4 = i4;
							myself.i_ai1 = party0;
							for(i5 = 0; i5 < i4; i5 = ( i5 + 1 ))
							{
								c2 = myself::Party_GetCreature(c3,i5);
								if( c2.level >= min_level )
								{
									myself::SetMemoState(c2,999,i3);
								}
								else
								{
									myself::ShowPage(talker,fnLowLevel);
								}
							}
						}
						else
						{
							myself::ShowPage(talker,fn5v5);
						}
					}
					else if( myself.i_ai0 == 1 )
					{
						if( myself::Party_GetCount(talker) == myself.i_ai4 )
						{
							party0 = myself::GetParty(talker);
							party1 = myself.i_ai1;
							c1 = myself::GetLeaderOfParty(party0);
							c3 = myself::GetLeaderOfParty(party1);
							i4 = myself::Party_GetCount(c1);
							i3 = ( 2 + gg::Rand(100) );
							myself.i_ai0 = 2;
							myself.i_ai3 = i3;
							myself.i_ai1 = buffuptime;
							for(i5 = 0; i5 < i4; i5 = ( i5 + 1 ))
							{
								c0 = myself::Party_GetCreature(c1,i5);
								c2 = myself::Party_GetCreature(c3,i5);
								if( c2.level >= min_level )
								{
									myself::SetMemoState(c0,999,i3);
									myself::InstantTeleport(c0,TelPosX1,TelPosY1,TelPosZ1);
									myself::InstantTeleport(c2,TelPosX2,TelPosY2,TelPosZ2);
								}
								else
								{
									myself::ShowPage(talker,fnLowLevel);
								}
							}
							myself::InstantTeleport(myself.sm,149467,46780,-3408);
							myself::AddTimerEx(1001,5000);
						}
						else
						{
							myself::Say("You have to match the registered party of " + myself::IntToStr(myself.i_ai4) + " participant for the match to begin");
						}
					}
				}
				else
				{
					myself::ShowPage(talker,fnLowLevel);
				}
			}
			else if( reply == 2 )
			{
				myself::ShowPage(talker,fnReady);
			}
		}
	}

	EventHandler TIMER_FIRED_EX(timer_id)
	{
		if( timer_id == 1001 )
		{
			if( myself.i_ai1 > 0 )
			{
				if( myself.i_ai1 == buffuptime )
				{
					myself::Shout("You have " + myself::IntToStr(myself.i_ai1) + " seconds to get ready");
				}
				else if( myself.i_ai1 == 1 )
				{
					myself::Shout("Coliseum gates will be opening in " + myself::IntToStr(myself.i_ai1) + " second!");
				}
				else if( myself.i_ai1 < 11 )
				{
					myself::Shout("Coliseum gates will be opening in " + myself::IntToStr(myself.i_ai1) + " seconds!");
				}
				myself.i_ai1 = ( myself.i_ai1 - 1 );
				myself::AddTimerEx(1001,1000);
			}
			else
			{
				myself.i_ai1 = doorclosetime;
				myself::Castle_GateOpenClose2(DoorName1,0);
				myself::Castle_GateOpenClose2(DoorName2,0);
				myself::AddTimerEx(1002,100);
			}
		}
		else if( timer_id == 1002 )
		{
			if( myself.i_ai1 > 0 )
			{
				if( myself.i_ai1 == doorclosetime )
				{
					myself::Shout("Let the fight begin!");
				}
				else if( myself.i_ai1 == 1 )
				{
					myself::Shout("Coliseum gates will be closing in " + myself::IntToStr(myself.i_ai1) + " second!");
				}
				else if( myself.i_ai1 < 11 )
				{
					myself::Shout("Coliseum gates will be closing in " + myself::IntToStr(myself.i_ai1) + " seconds!");
				}
				myself.i_ai1 = ( myself.i_ai1 - 1 );
				myself::AddTimerEx(1002,1000);
			}
			else
			{
				myself.i_ai1 = matchtime;
				myself::Castle_GateOpenClose2(DoorName1,1);
				myself::Castle_GateOpenClose2(DoorName2,1);
				myself::AddTimerEx(1003,100);
			}
		}
		else if( timer_id == 1003 )
		{
			if( myself.i_ai1 > 0 && myself.i_ai0 > 0 )
			{
				if( myself.i_ai1 == matchtime )
				{
					myself::Shout("Max duration is " + myself::IntToStr(myself.i_ai1) + " seconds");
					myself::Shout("Team that successfully casts 'Incantation of Valor' wins the match");
				}
				else if( myself.i_ai1 == 1 )
				{
					myself::Shout("This match will be ending in " + myself::IntToStr(myself.i_ai1) + " second!");
				}
				else if( myself.i_ai1 < 21 )
				{
					myself::Shout("This match will be ending in " + myself::IntToStr(myself.i_ai1) + " seconds!");
				}
				myself.i_ai1 = ( myself.i_ai1 - 1 );
				myself::AddTimerEx(1003,1000);
			}
			else
			{
				myself.i_ai0 = 0;
				myself.i_ai2 = 1;
				myself.i_ai3 = 1;
				myself.i_ai4 = 0;
				myself::InstantTeleportInMyTerritory(TownPosX,TownPosY,TownPosZ,100);
				myself::InstantTeleport(myself.sm,SpawnPosX,SpawnPosY,SpawnPosZ);
				myself::AddTimerEx(2002,10000);
			}
		}
		else if( timer_id == 2001 )
		{
			myself::InstantTeleport(myself.sm,SpawnPosX,SpawnPosY,SpawnPosZ);
			myself::InstantTeleportInMyTerritory(TownPosX,TownPosY,TownPosZ,100);
		}
		else if( timer_id == 2002 )
		{
			if( myself.i_ai0 == 0 )
			{
				myself::AddTimerEx(2002,regist_announce);
			}
			else if( myself.i_ai0 == 1 && myself.i_ai3 != 6 )
			{
				myself::Say("A party of " + myself::IntToStr(myself.i_ai4) + "  is needed! or registration will be cancelled");
				myself.i_ai3 = ( myself.i_ai3 + 1 );
				myself::AddTimerEx(2002,participant_announce);
			}
			else if( myself.i_ai0 == 1 && myself.i_ai3 == 6 )
			{
				myself.i_ai0 = 0;
				myself.i_ai2 = 1;
				myself.i_ai3 = 1;
				myself.i_ai4 = 0;
				myself::InstantTeleportInMyTerritory(TownPosX,TownPosY,TownPosZ,100);
				myself::InstantTeleport(myself.sm,SpawnPosX,SpawnPosY,SpawnPosZ);
				myself::AddTimerEx(2002,10000);
			}
		}
	}

	EventHandler SEE_CREATURE(creature,c0)
	{
		c0 = creature;
		if( creature.master )
		{
			c0 = creature.master;
		}
		if( myself::GetMemoState(c0,999) != myself.i_ai2 && myself::GetMemoState(c0,999) != myself.i_ai3 )
		{
			if( myself::InMyTerritory(myself.sm) )
			{
				myself::Say(myself::MakeFString(1039009,creature.name,"","","",""));
				myself::InstantTeleport(creature,TownPosX,TownPosY,TownPosZ);
			}
		}
	}

	EventHandler ATTACKED(attacker,skill_name_id,i1,i2,i3,c0,c1,party0)
	{
		if( myself::InMyTerritory(myself.sm) )
		{
			if( myself::GetMemoState(attacker,999) == myself.i_ai2 || myself::GetMemoState(attacker,999) == myself.i_ai3 )
			{
				if( skill_name_id == 1040909 )
				{
					if( myself::Party_GetCount(attacker) > 1 )
					{
						party0 = myself::GetParty(attacker);
						c1 = myself::GetLeaderOfParty(party0);
						i1 = myself::Party_GetCount(c1);
						i2 = myself::GetMemoState(attacker,999);
						for(i3 = 0; i3 < i1; i3 = ( i3 + 1 ))
						{
							c0 = myself::Party_GetCreature(c1,i3);
							if( myself::GetMemoState(c0,999) == i2 )
							{
								if( reward_item_id != 0 )
								{
									myself::GiveItem1(c0,reward_item_id,reward_item_amount);
								}
								if( reward_item_id_2 != 0 )
								{
									myself::GiveItem1(c0,reward_item_id_2,reward_item_amount_2);
								}
								myself::ShowPage(c0,fnCongrats);
							}
						}
						myself.i_ai0 = 0;
					}
				}
			}
		}
	}

}
