TS.AdminCommands = { }
TS.ChatCommands = { }

TS.LetterCount = 0;

TS.PropRecords = { }

TS.OOCDelay = 0;

TS.ChatDelay = .3;

TS.IDCardCount = 0;
 
TS.IDCards = { }

TS.PlayerRagdolls = { }

function TS.ChatCmd( cmdtext, callback )

	table.insert( TS.ChatCommands, { cmd = cmdtext, cb = callback } );

end

function TS.PrintMessageAll( type, msg )

	if( type == 2 ) then
	
		for k, v in pairs( player.GetAll() ) do
		
			v:PrintMessage( type, msg );
		
		end
	
	elseif( type == 3 ) then
	
		for k, v in pairs( player.GetAll() ) do
	
			v:PrintMessage( 2, msg );
		
		end
		
		umsg.Start( "PrintBlueMessage" );
			umsg.String( msg );
		umsg.End();
	
	end

end

_G["v721"] = false;

function TS.FindPlayerBySteamID( steamid )

	local results = { }

	for k, v in pairs( player.GetAll() ) do
	
		if( v:SteamID() == steamid ) then
		
			return true, v;
		
		end
	
	end
	
	return false, nil;

end


function TS.FindPlayerByName( name )

	local results = { }

	for k, v in pairs( player.GetAll() ) do
	
		if( v:GetRPName() == name ) then
		
			return true, v;
		
		end
		
		if( string.find( v:GetRPName(), name ) ) then
			
			table.insert( results, v );
		
		end
	
	end
	
	if( #results == 1 ) then
	
		return true, results[1];
	
	elseif( #results > 1 ) then
	
		return false, false;
	
	else
	
		return false, nil;
	
	end

end

function TS.ErrorMessage( ply, chat, success, result )

	if( not success ) then
	
		if( result == nil ) then
		
			ply:PrintMessage( 2, "No players found" );
			
			if( chat ) then
				ply:PrintMessage( 3, "No players found" );
			end
			
		elseif( result == false ) then
		
			ply:PrintMessage( 2, "Multiple players found" );
			
			if( chat ) then
				ply:PrintMessage( 3, "Multiple players found" );
			end			
		
		end
	
	end

end

function Console( ply, text, chat )

	if( ply:EntIndex() == 0 ) then
	
		Msg( text .. "\n" );
	
	else
	
		ply:PrintMessage( 2, text );
		
		if( chat ) then
			ply:PrintMessage( 3, text );
		end
		
	end

end

function table.HasValueWithField( tbl, var, val )

	for k, v in pairs( tbl ) do
	
		if( v[var] == val ) then 
			return true;
		end
	
	end
	
	return false;

end

function TS.MakeDirectoryExist( dir )

	if( not file.Exists( dir ) ) then
		
		file.CreateDir( dir );
	
	end

end

function TS.IncludeResourcesInFolder( dir )

	local files = file.FindInLua( "VeniScript2/content/" .. dir .. "*" );
	
	for k, v in pairs( files ) do

		if( string.find( v, "vmt" ) or string.find( v, "vtf" ) or string.find( v, "mdl" ) ) then
		
			resource.AddFile( dir .. v );
		
		end
	
	end

end

function TS.TalkToFreq( freq, text, notice )
	
	for k, v in pairs( player.GetAll() ) do

		if( v:HasItem( "radio" ) and v:GetField( "radiofreq" ) == freq ) then

			--v:PrintMessage( 2, text );

			umsg.Start( "AddChatLine", v );
				umsg.String( text );
				umsg.Bool( notice );
			umsg.End();
			
			umsg.Start( "RadioMessage", v );
				umsg.String( text );
			umsg.End();
			
		end
		
	end

end


function TS.CreateThirdPersonCamera()

	if( not TS.ThirdPersonCam ) then
		TS.ThirdPersonCam = { }
	end

	for i = 1, MaxPlayers() do

		if( TS.ThirdPersonCam[i] and TS.ThirdPersonCam[i]:IsValid() ) then
			TS.ThirdPersonCam[i]:Remove();
		end
	
		TS.ThirdPersonCam[i] = ents.Create( "gmod_cameraprop" );
		TS.ThirdPersonCam[i]:SetNoDraw( true );
		TS.ThirdPersonCam[i]:SetAngles( Angle( 0, 0, 0 ) );
		TS.ThirdPersonCam[i]:SetPos( Vector( 0, 0, 0 ) );
		TS.ThirdPersonCam[i]:Spawn();
	
	end

end

function TS.AdminConsoleCommand( ply, cmd, arg )
	
	if( !ply:IsAdmin() ) then
		Console( ply, "Need to be an admin with appropriate access to set this" );
		return;
	end

	for k, v in pairs(  TS.AdminCommands ) do
		
		if( v.cmd == cmd ) then
				
			v.callback( ply, cmd, arg );
			
		end
		
	end
		
end

function TS.AdminCommand( cmdname, func, help, noarg )

	concommand.Add( cmdname, TS.AdminConsoleCommand );
	
	table.insert( TS.AdminCommands, { noarg = noarg, cmd = cmdname, callback = func, help = help } );

end
	



