//@unloadnpc Match Organizer
//@loadnpc npc/thor/features/bet.txt

prontera,146,92,6	script	Match Organizer	4_F_KHALITZBURG,{
	function _GetPatryMemberAmount;

	if( .game_status > 0 )
	{
		if( strcharinfo(0) == .game_team2$ )
		{
			goto OnTeam2AskInfo;
			end;
		}
		mes "[^0000ffMatch Manager^000000]";
		mes "Sorry, a match is currently ongoing.";
		close;
	}
	if( !getcharid(1) )
	{
		mes "[^0000ffMatch Manager^000000]";
		mes "You need to be in a party in order to join the match.";
		close;
	}
	if( getpartyleader(getcharid(1),2) == getcharid(0) ) {
	} else {
		mes "[^0000ffMatch Manager^000000]";
		mes "You need to be in a party leader in order to organize a match.";
		close;
	}

	mes "[^0000ffMatch Manager^000000]";
	mes "Welcome!!! How can I assist you?";
	@type_bool = select("Organize 1vs1 Match", "Organize a Party Match");
	next;
	if (@type_bool == 2){
		mes "[^0000ffMatch Manager^000000]";
		mes "Map Type: [ ^28bf00GvG^000000 ]";
		mes "Time Limit [ ^28bf002 minutes^000000 ]";
		mes "Type [ ^28bf00"+( @type_bool == 1 ? "1vs1 Match" : "Party Match" )+"^000000 ]";
		mes "^0055FFHow many members?^000000";
		input @sys_member;
		next;
	}
	if( @type_bool == 2 && ( getpartyleader(getcharid(1),2) != getcharid(0) || _GetPatryMemberAmount(getcharid(1)) != @sys_member) )
	{
		mes "[^0000ffMatch Manager^000000]";	
		mes "You don't own party or your team member amount is not "+@sys_member+".";
		close;
	}
	mes "[^0000ffMatch Manager^000000]";
	mes "Map Type: [ ^28bf00GvG^000000 ]";
	mes "Time Limit [ ^28bf002 minutes^000000 ]";
	mes "Type [ ^28bf00"+( @type_bool == 1 ? "1vs1 Match" : "Party Match" )+"^000000 ]";
	if (@type_bool == 2) mes "Party Size [ ^28bf00"+@sys_member+"^000000 ]";
	mes "^0055FFHow much zeny you want to bet for this match?^000000";
	input @bet_zeny;
	if( @bet_zeny > Zeny )
	{
		mes "[^0000ffMatch Manager^000000]";
		mes "You don't have enough zeny.";
		@type_bool = 0;
		@bet_zeny = 0;
		close;
	}
	next;
	mes "[^0000ffMatch Manager^000000]";
	mes "Map Type: [ ^28bf00GvG^000000 ]";
	mes "Time Limit [ ^28bf002 minutes^000000 ]";
	mes "Type [ ^28bf00"+( @type_bool == 1 ? "1vs1 Match" : "Party Match" )+"^000000 ]";
	if (@type_bool == 2) mes "Party Size [ ^28bf00"+@sys_member+"^000000 ]";
	mes "You bet [^28bf00"+@bet_zeny+"^000000]z";
	mes "^0055FFInput your enemy's name^000000";
	input @enemy_name$;
	next;
	if( getcharid(0,@enemy_name$) == 0 )
	{
		@type_bool = 0;
		@bet_zeny = 0;
		@enemy_name$ = "";
		mes "[^0000ffMatch Manager^000000]";
		mes "^0055FFThat player is offline^000000";
		close;
	}
	if( @enemy_name$ == strcharinfo(PC_NAME)){
		mes "[^0000ffMatch Manager^000000]";
		mes "Sorry you can't fight your self";
		@type_bool = 0;
		@bet_zeny = 0;
		@enemy_name$ = "";
		close;		
	}
	if( .game_status > 0 )
	{
		mes "[^0000ffMatch Manager^000000]";
		mes "^0055FFSorry, there is currently ongoing match.  Please try later.^000000";
		close;
	}
	mes "[^0000ffMatch Manager^000000]";
	mes "Map Type: [ ^28bf00GvG^000000 ]";
	mes "Time Limit [ ^28bf002 minutes^000000 ]";
	mes "Type [ ^28bf00"+( @type_bool == 1 ? "1vs1 Match" : "Party Match" )+"^000000 ]";
	if (@type_bool == 2) mes "Party Size [ ^28bf00"+@sys_member+"^000000 ]";
	mes "You bet [ ^28bf00"+@bet_zeny+"^000000 ]z";
	mes "Contender [ ^28bf00"+@enemy_name$+"^000000 ]";
	mes "^0055FFPlease confirm:^000000";
	next;
	if( select("No", "Yes") == 1 )
	{
		@type_bool = 0;
		@bet_zeny = 0;
		close;
	}	
	.game_team1$ = strcharinfo(0);
	.game_team2$ = @enemy_name$;
	.game_status = 1;
	.game_bet = @bet_zeny;
	.game_type = @type_bool;
	.sys_member = @sys_member;

	@type_bool = 0;
	Zeny -= @bet_zeny;
	dispbottom "You spent "+@bet_zeny+" zeny for a bet on a match.  Don't worry we will return the Zeny when your contender will not accept the challenge.";
	@bet_zeny = 0;
	@enemy_name$ = "";
	close2;
	detachrid;
	message .game_team1$, "Complete";
	sleep2 1000;
	announce "[ "+.game_team1$+" ] sent challenge to  [ "+.game_team2$+" ] for a  [ "+( .game_type == 1 ? "1vs1 Match" : "Party Match" )+" ] with a bet of "+.game_bet+" zeny",bc_all;
	message .game_team2$, "Someone invite you for betting, @betarea for more info.";
	.game_sys_wait = gettimetick(2)+60;
	bindatcmd "betarea",strnpcinfo(3)+"::OnTeam2AskInfo";
	freeloop(1);
	while( .game_sys_wait > gettimetick(2) && .game_status == 1 )
		sleep2 500;
	freeloop(0);
	unbindatcmd "betarea";

	if( .game_status == 1 )
	{
		sleep2 1;
		message .game_team1$, "Game End";
		sleep2 1;
		message .game_team2$, "Game End";
		if( attachrid(getcharid(3, .game_team1$)) )
		{
			Zeny = Zeny + .game_bet;
			dispbottom "The challenge has been declined. We are returning the bet of "+.game_bet+" zeny.";
			detachrid;
		}
		callsub OnEndd;
		end;
	}
	message .game_team1$, "Game will start at 10s";
	message .game_team2$, "Game will start at 10s";

	// Side 1
	setcell .arena_map$,56,52,56,47,cell_walkable,0;
	setcell .arena_map$,56,52,59,52,cell_walkable,0;
	setcell .arena_map$,56,47,59,47,cell_walkable,0;

	// Side 2
	setcell .arena_map$,43,52,43,47,cell_walkable,0;
	setcell .arena_map$,43,52,40,52,cell_walkable,0;
	setcell .arena_map$,43,47,40,47,cell_walkable,0;

	gvgoff .arena_map$;
	mapwarp .arena_map$,"prontera",150,180;
	sleep2 10000;
	if( .game_type == 2 && (_GetPatryMemberAmount(getcharid(1, .game_team1$)) != .sys_member ||
		_GetPatryMemberAmount(getcharid(1, .game_team2$)) != .sys_member) )
	{
		message .game_team1$, "One of team have not right member amount";
		message .game_team2$, "One of team have not right member amount";
		callsub OnEndd;
		end;
	}
	if( .game_type == 2 )
	{
		warpparty .arena_map$,41,50, getcharid(1, .game_team1$);
		warpparty .arena_map$,58,50, getcharid(1, .game_team2$);
	}else
	{
		warpchar .arena_map$,41,50, getcharid(0, .game_team1$);
		warpchar .arena_map$,58,50, getcharid(0, .game_team2$);
	}

	for(set .@i,3; .@i > 0; set .@i,.@i-1){
		announce "[Match Organizer] A match between [ "+.game_team1$+" ] and  [ "+.game_team2$+" ] will begin in "+.@i+" min!. Use @match to watch this challenge.", bc_all, C_AQUA, FW_BOLD, 16;
		sleep 60 * 1000;
	}
	sleep2 1000;
	mapannounce .arena_map$,"[Game]: 9s",0;
	sleep2 1000;
	mapannounce .arena_map$,"[Game]: 8s",0;
	sleep2 1000;
	mapannounce .arena_map$,"[Game]: 7s",0;
	sleep2 1000;
	mapannounce .arena_map$,"[Game]: 6s",0;
	sleep2 1000;
	mapannounce .arena_map$,"[Game]: 5s",0;
	sleep2 1000;
	mapannounce .arena_map$,"[Game]: 4s",0;
	sleep2 1000;
	mapannounce .arena_map$,"[Game]: 3s",0;
	sleep2 1000;
	mapannounce .arena_map$,"[Game]: 2s",0;
	sleep2 1000;
	mapannounce .arena_map$,"[Game]: 1s",0;
	gvgon .arena_map$;
	sleep2 1000;
	mapannounce .arena_map$,"[Game]: Go!!",0;
	// Side 1
	setcell .arena_map$,56,52,56,47,cell_walkable,1;
	setcell .arena_map$,56,52,59,52,cell_walkable,1;
	setcell .arena_map$,56,47,59,47,cell_walkable,1;

	// Side 2
	setcell .arena_map$,43,52,43,47,cell_walkable,1;
	setcell .arena_map$,43,52,40,52,cell_walkable,1;
	setcell .arena_map$,43,47,40,47,cell_walkable,1;
	.game_sys_wait = gettimetick(2)+120;
	freeloop(1);
	while( .game_sys_wait > gettimetick(2) &&
		((.@p1 = _GetPatryMemberAmount(getcharid(1, .game_team1$),1)) > 0 &&
		(.@p2 = _GetPatryMemberAmount(getcharid(1, .game_team2$),1)) > 0) )
		sleep2 500;
	freeloop(0);
	if( .@p1 > .@p2 )
	{
		mapannounce .arena_map$,"[Game]: Team 1 Win!!",0;
		announce "[Match Manager] Congratulation to "+.game_team1$+" for winning the match.",bc_all;

		.@aid =	getcharid(3, .game_team1$);
	}else if( .@p1 < .@p2 )
	{
		mapannounce .arena_map$,"[Game]: Team 2 Win!!",0;
		announce "[Match Manager] Congratulation to "+.game_team2$+" for winning the match.",bc_all;
		.@aid =	getcharid(3, .game_team2$);
	}else
		.@aid = 0;
	if( attachrid(.@aid) )
	{
		Zeny = Zeny+(.game_bet*2);
		detachrid;
	}
	sleep2 3000;

	// Terminate QUEUE
	.@it = queueiterator(.match);
	for ( .@aids = qiget(.@it); qicheck(.@it); .@aid = qiget(.@it) )
		.aids[.@c++] = .@aids;
		if(.@it) qiclear .@it;
		queuedel .match;
		sleep 5000;
		for ( .@i = 0; .@i < .@c; ++.@i ) {
			attachrid .aids[.@i];
			set spectator,0;
			set Hp,shp;
			set Sp,ssp;
			setoption Option_Invisible, 0;
			sc_end(SC_XMAS);
			warp slastmap$,slastx,slasty;
		}

	mapwarp .arena_map$,"prontera",150,180;
	callsub OnEndd;
	end;
OnPCDieEvent:
	if( .game_status == 2 && strcharinfo(3) == .arena_map$ && killerrid != getcharid(3) && isloggedin(killerrid) )
	{
		getitem2 7005,1,1,0,0,254,0,getcharid(0)& 65535, getcharid(0) >> 16, killerrid;
	}
	end;

OnPCLoginEvent:
	if (spectator) {
		set spectator,0;
		set Hp,shp;
		set Sp,ssp;
		setoption Option_Invisible, 0;
		sc_end(SC_XMAS);
		warp slastmap$,slastx,slasty;	
	}
	end;

OnEndd:
	.game_team1$ = "";
	.game_team2$ = "";
	.game_status = 0;
	.game_bet = 0;
	.game_type = 0;
	.game_sys_wait = 0;

	return;
OnMatch:
	mes "[^0000ffMatch Manager^000000]";
	getmapxy(.@map$, @x, @y, 0);
	if (etatus) {
		mes "Sorry, you are currently joined an event.  Use @event and leave.";
		close;
	}
	if (strcharinfo(PC_MAP) == .arena_map$) {
	} else {
		
		if (getmapflag(strcharinfo(PC_MAP), mf_pvp) || getmapflag(strcharinfo(PC_MAP), mf_nowarp)) {
			mes "Sorry, you can't spectate on match from this map";
			close;
		}
	}
	if (.deadlock && !Hp) {
		mes "You may not join @match when you are dead.";
		close;
	}
	if (!.game_status) {
		if (!spectator) {	
			mes "Sorry no ongoing match at this time.";
			close;
		}	
	}
	switch(select(
		((getgmlevel() < .GMAccess)?":":" > ^FF0000GM Menu^000000:")+
		(((strcharinfo(PC_NAME) != .game_team1$ && strcharinfo(PC_NAME) != .game_team2$) && (.game_status && !spectator && strcharinfo(PC_MAP) != .arena_map$))?" > ^0055FFWatch Match^000000:":":")+
		((spectator)?" > ^0055FFLeave Match Arena^000000:":":")+
		" > ^777777Close^000000"
	)) {
	case 1:
		close();
	case 2:
		if ( .game_type== 2 ) {
			if (getcharid(CHAR_ID_PARTY) == getcharid(CHAR_ID_PARTY,.game_team1$) || getcharid(CHAR_ID_PARTY) == getcharid(CHAR_ID_PARTY,.game_team2$)) {
				mes "^0055FFSorry, you are a member of the ongoing match.^000000";
				close;				
			}
		}
		queueadd .match,getcharid(3);
		getmapxy(.@map$, @x, @y, 0);
		set slastmap$,.@map$;
		set slastx,@x;
		set slasty,@y;
		set spectator,1;
		set shp,Hp;
		set ssp,Sp;
		setoption Option_Invisible, 1;
		sc_start SC_XMAS, 1600000, 0;
		warp .arena_map$,49,49;
		close;	
	case 3:
		queueremove .match, getcharid(3);
		set spectator,0;
		set Hp,shp;
		set Sp,ssp;
		setoption Option_Invisible, 0;
		sc_end(SC_XMAS);
		warp slastmap$,slastx,slasty;
		close;
	default:
		close;	
	}			
	end;
	
OnMapChange:
OnQuit:
	if (strcharinfo(3) == .arena_map$) end;
	queueremove .match, getcharid(3);
	set spectator,0;
	set Hp,shp;
	set Sp,ssp;
	setoption Option_Invisible, 0;
	sc_end(SC_XMAS);
	warp slastmap$,slastx,slasty;
	end;
OnInit:
	bindatcmd("match","Match Organizer::OnMatch",0,99);
	// Side 1
	setcell .arena_map$,56,52,56,47,cell_walkable,1;
	setcell .arena_map$,56,52,59,52,cell_walkable,1;
	setcell .arena_map$,56,47,59,47,cell_walkable,1;

	// Side 2
	setcell .arena_map$,43,52,43,47,cell_walkable,1;
	setcell .arena_map$,43,52,40,52,cell_walkable,1;
	setcell .arena_map$,43,47,40,47,cell_walkable,1;
	.sys_member = 1;
	.arena_map$ = "ufc_pvp2";
	end;
OnTeam2AskInfo:
	if( strcharinfo(0) != .game_team2$ )
		end;
	mes "[^0000ffMatch Manager^000000]";
	mes "Someone has sent you a GvG challenge.";
	mes "Map Type: [ ^28bf00GvG^000000 ]";
	mes "Time Limit [ ^28bf002 minutes^000000 ]";
	mes "Bet [ ^28bf00"+.game_bet+"^000000 ]z";
	mes "Type [ ^28bf00"+( .game_type == 1 ? "1vs1" : "Party" )+"^000000 ]";
	if (.game_type == 2) mes "Party Size [ ^28bf00"+.sys_member+"^000000 ]";
	mes "Contender [ ^28bf00"+.game_team1$+"^000000 ]";
	switch(select("Decline", "Accept", "Close"))
	{
		case 2:
			if( !getcharid(1) )
			{
				mes "[^0000ffMatch Manager^000000]";
				mes "You need a party.";
				close;
			}
			if( getpartyleader(getcharid(1),2) == getcharid(0) ) {
			} else {
				mes "[^0000ffMatch Manager^000000]";
				mes "You need to be in a party leader in order to accept a match.";
				close;
			}
			if( .game_type == 2 && ( getpartyleader(getcharid(1),2) != getcharid(0) ||
				_GetPatryMemberAmount(getcharid(1)) != .sys_member ) )
			{
				mes "[^0000ffMatch Manager^000000]";
				mes "You don't own party or your team member amount is not "+.sys_member+".";
				close;
			}
			if( .game_bet > Zeny )
			{
				mes "[^0000ffMatch Manager^000000]";
				mes "You don't have enough zeny.";
				close;
			}
			if( .game_sys_wait < gettimetick(2) )
			{
				mes "[^0000ffMatch Manager^000000]";
				mes "Too late";
				close;
			}
			Zeny -= .game_bet;
			dispbottom "You spent "+.game_bet+" zeny for a bet on a match.  Good Luck on the challenge.";

			.game_status = 2;
			.game_sys_wait = 0;
			announce "[ "+.game_team2$+" ] accepted the challenge of [ "+.game_team1$+" ] for [ "+( .game_type == 1 ? "1vs1 Match" : "Party Match" )+" ] with a bet of "+.game_bet+" zeny",bc_all;

			.match = queue();
			queueopt .match, QUEUEOPT_LOGOUT, strnpcinfo(0)+"::OnQuit";
			queueopt .match, QUEUEOPT_MAPCHANGE, strnpcinfo(0)+"::OnMapChange";
			close;
		case 1:
		case 3:
			.@aids = getcharid(3);
			if( attachrid(getcharid(3, .game_team1$)) )
			{
				Zeny = Zeny + .game_bet;
				dispbottom "The challenge has been declined. We are returning the bet of "+.game_bet+" zeny.";
				detachrid;
				attachrid .@aids;
			}
			.game_sys_wait = 0;
			close;
	}
	end;


	function	_GetPatryMemberAmount	{
		deletearray $@partymembername$;
		$@partymembercount = 0;
		getpartymember getarg(0), 0;
		.@result = .@amount = $@partymembercount;
		for( .@i = 0; .@i < .@amount; .@i++ )
			if( getcharid(0, $@partymembername$[.@i]) == 0 )
				.@result--;
			else if( getarg(1,0) == 1 )
			{
				getmapxy(.@m$, .@x, .@y, 0, $@partymembername$[.@i]);
				if( .@m$ != .arena_map$ )
					.@result--;
			}
		return .@result;
	}
}

ufc_pvp2	mapflag	gvg