// PVP ROYALE, Comprobacion de ganador del mapa shitty pubg.
// Yo tampoco jugue pubg. Giselachar20. 


array<int> userIDs;
//array<Vector> spawnpoints;
uint timer = 35;
bool g_active = true;
string g_winsound = "tu3sday/wingame.wav";
CScheduledFunction@ g_hpr = null;
CScheduledFunction@ g_pc = null;


void MapInit()
{
		g_SoundSystem.PrecacheSound( g_winsound );

	g_Hooks.RegisterHook( Hooks::Player::ClientDisconnect, @ClientDisconnect );
	g_Hooks.RegisterHook( Hooks::Player::PlayerSpawn, @PlayerSpawn );
	g_Hooks.RegisterHook( Hooks::Player::PlayerKilled, @PlayerKilled );

}


HookReturnCode PlayerKilled( CBasePlayer@ pPlayer, CBaseEntity@ pAttacker, int iGib )
{
	const int uid = g_EngineFuncs.GetPlayerUserId( pPlayer.edict() );

	int id = userIDs.find( uid );
	
	if( id >= 0 )
		userIDs.removeAt( id );

	string originStr = "" + pPlayer.pev.origin.x + " " + pPlayer.pev.origin.y + " " + pPlayer.pev.origin.z;
	

	WinnerCheck();

	return HOOK_HANDLED;
}

HookReturnCode PlayerSpawn( CBasePlayer@ pPlayer )
{
	if ( g_active )
		return HOOK_CONTINUE;

	const int uid = g_EngineFuncs.GetPlayerUserId( pPlayer.edict() );

	userIDs.insertLast( uid );

	return HOOK_HANDLED;
}

HookReturnCode ClientDisconnect( CBasePlayer@ pPlayer )
{
	const int uid = g_EngineFuncs.GetPlayerUserId( pPlayer.edict() );

	int id = userIDs.find( uid );

	if( id >= 0 )	
		userIDs.removeAt( id );
		
	WinnerCheck();

	return HOOK_HANDLED;
}


void Winner( CBasePlayer@ pPlayer )
{
	pPlayer.AddPoints( 1000, false );

	g_SoundSystem.PlaySound( g_EntityFuncs.IndexEnt(0), CHAN_STATIC, g_winsound, 1.0f, ATTN_NONE, 0, 100 );


g_PlayerFuncs.ClientPrintAll( HUD_PRINTCENTER, "Gano el battle royale" + pPlayer.pev.netname + " !!\n" );
	WinnerVote( pPlayer.pev.netname );

}
void WinnerVote( string pname )
{
	Vote@ EndVote = Vote( 'Ganador!', "[Un kpo el] " + pname + " les gano a todos!", 10.0f, 51.0f );

	EndVote.SetYesText( 'Jugar de nuevo' );
	EndVote.SetNoText( 'Otro mapa' );
	EndVote.SetVoteBlockedCallback( @EndVoteBlocked );
	EndVote.SetVoteEndCallback( @EndVoteEnd );
	EndVote.Start();
}

void EndVoteEnd( Vote@ pVote, bool fResult, int iVoters )
{
    if ( fResult )
    {
		Intermission();

		g_Scheduler.SetTimeout( "RestartMap", 6 );
    }
    else
    {
		Intermission();
	
		g_Scheduler.SetTimeout( "GameEnd", 6 );
    }
}

void GameEnd()
{
	g_EntityFuncs.FireTargets( "gameend", g_EntityFuncs.Instance(0), g_EntityFuncs.Instance(0), USE_ON, 0.0f, 0.0f );
}

void RestartMap()
{
	g_EngineFuncs.ChangeLevel( g_Engine.mapname );
}

void WinnerCheck()
{
	if( g_active && userIDs.length() == 1 )
	{
	
		g_Scheduler.RemoveTimer( g_hpr );
		g_Scheduler.RemoveTimer( g_pc );
	
		for ( int i = 1; i <= g_Engine.maxClients; ++i )
		{
			CBasePlayer@ pPlayer = g_PlayerFuncs.FindPlayerByIndex( i );

			if ( pPlayer is null )
				continue;
			
			const int uid = g_EngineFuncs.GetPlayerUserId( pPlayer.edict() );

			if ( uid == userIDs[0] )
			{
				Winner( pPlayer );
				break;
			}
		}
	}
}


void Intermission()
{
	NetworkMessage message( MSG_ALL, NetworkMessages::SVC_INTERMISSION, null );
	message.End();
}

