-------------------------------
-- SSTRP
-- Author: MNWRP
--
-- concmd.lua
-- Contains the concommands and changes the way other concommands work.
-------------------------------

-- Deprecated, PlayerGiveSwep does the same thing.
-- function NoSweps( ply, cmd, args )
-- 
-- 	if( ply:IsSuperAdmin( ) ) then ply:Give( args[ 1 ] ); else return false; end
-- 	
-- end
-- concommand.Add( "gm_giveswep", NoSweps );

function SeeAll( ply, cmd, args )
	if ply:IsAdmin() or ply:IsSuperAdmin() then
		umsg.Start( "ToggleSeeAll", ply ); umsg.End();
	else 
		SendChat( ply, "You're not an Admin" )
	end
end
concommand.Add( "rp_seeall", SeeAll );

function GM:PlayerSpawnNPC( ply )
	if( ply:IsSuperAdmin() ) or ( ply:IsAdmin()) then
		return true
	end
	return false
end

function GM:PlayerSpawnSWEP( ply, class )

	CallTeamHook( "PlayerSpawnSWEP", ply, class ); -- Perhaps allow certain flags to use sweps, eh?
	
	if( ply:IsSuperAdmin( ) ) or ( ply:IsAdmin()) then return true; end
	return false;
	
end

function GM:PlayerGiveSWEP( ply )

	CallTeamHook( "PlayerGiveSWEP", ply, class ); -- Perhaps allow certain flags to use sweps, eh?

	if( ply:IsSuperAdmin( ) ) or ( ply:IsAdmin()) then return true; end
	return false; 
	
end

-- This is the F1 menu
function GM:ShowHelp( ply )

	local PlyCharTable = PlayerData[ FormatSteamID( ply:SteamID() ) ]["characters"]

	for k, v in pairs( PlyCharTable ) do
		
		umsg.Start( "ReceiveChar", ply );
			umsg.Long( k );
			umsg.String( v[ "name" ] );
			umsg.String( v[ "model" ] );
		umsg.End( );
		
	end

	umsg.Start( "playermenu", ply );
	umsg.End( )
	
end

-- NO SENT FOR YOU.
function GM:PlayerSpawnSENT( ply, class )

	CallTeamHook( "PlayerSpawnSWEP", ply, class ); -- Perhaps allow certain flags to use sents, eh?
	
	if( ply:IsSuperAdmin( ) ) or ( ply:IsAdmin()) then return true; end
	return false;
	
end

-- Disallows suicide
function GM:CanPlayerSuicide( ply )

	if( ConVars[ "SuicideEnabled" ] != "1" ) then
	
		ply:ChatPrint( "Suicide is disabled!" )
		return false
		
	end
	
	return true;
	
end

-- Set Title
function ccSetTitle( ply, cmd, args )

	local title = args[ 1 ];
	
	if( string.len( title ) > 33 ) then
	
		SendChat( ply, "Title is too long! Max 32 characters" );
		return;
		
	end
	
	SetCharField( ply, "title", title );
	ply:SetNWString("title", title);
	
	return;
	
end
concommand.Add( "rp_title", ccSetTitle );

-- Change IC Name
function ccChangeName( ply, cmd, args )

	local name = args[ 1 ];
	SetCharField(ply, "name", name );
	ply:SetNWString("name", name);
	
end
concommand.Add( "rp_changename", ccChangeName );

-- Allows a player to skip the respawn timer.
function ccAcceptDeath( ply )
	if( ply:Alive() ) then SendChat( ply, "You're not dead" ) return end
	ply:Spawn()
end
concommand.Add( "rp_acd", ccAcceptDeath );

function ccFlag( ply, cmd, args )
	
	local FlagTo = "";
	
	for k, v in pairs( Teams ) do
	
		if( v[ "flag_key" ] == args[ 1 ] ) then
		
			FlagTo = v;
			FlagTo.n = k;
			break;
			
		end
		
	end
	
	if( FlagTo == "" ) then
	
		SendChat( ply, "Incorrect Flag!" );
		return;
		
	end

	if( ( GetCharField(ply, "flags" ) != nil and table.HasValue( GetCharField( ply, "flags" ), args[ 1 ] ) ) or FlagTo[ "public" ] ) then
		
		ply:SetTeam( FlagTo.n );
		ply:RefreshBusiness();
		ply:Spawn( );
		return;
				
	else
	
		SendChat( ply, "You do not have this flag!" );
		
	end		
	
end
concommand.Add( "rp_flag", ccFlag );

function ccLockDoor( ply, cmd, args )
	
	local entity = ents.GetByIndex( tonumber( args[ 1 ] ) );
	
	if( IsDoor( entity ) ) then
	
		if( entity.owner == ply ) then
		
			entity:Fire( "lock", "", 0 );
			
		else
		
			SendChat( ply, "This is not your door!" );
			
		end
		
	end

end
concommand.Add( "rp_lockdoor", ccLockDoor );

function ccUnLockDoor( ply, cmd, args )
	
	local entity = ents.GetByIndex( tonumber( args[ 1 ] ) );
	
	if( IsDoor( entity ) ) then
	
		if( entity.owner == ply ) then
		
			entity:Fire( "unlock", "", 0 );
			
		else
		
			SendChat( ply, "This is not your door!" );
			
		end
		
	end

end
concommand.Add( "rp_unlockdoor", ccUnLockDoor );

function ccOpenDoor( ply, cmd, args )

	local entity = ply:GetEyeTrace( ).Entity;
	
	if( entity != nil and entity:IsValid( ) and IsDoor( entity ) and ply:GetPos( ):Distance( entity:GetPos( ) ) < 200 ) then
	
		local pos = entity:GetPos( );
		
		for k, v in pairs( Doors ) do
		
			if( tonumber( v[ "x" ] ) == math.ceil( tonumber( pos.x ) ) and tonumber( v[ "y" ] ) == math.ceil( tonumber( pos.y ) ) and tonumber( v[ "z" ] ) == math.ceil( tonumber( pos.z ) ) ) then
			
				for k2, v2 in pairs( Teams[ ply:Team( ) ][ "door_groups" ] ) do
				
					if( tonumber( v[ "group" ] ) == tonumber( v2 ) ) then
					
						entity:Fire( "toggle", "", 0 );
						
					end
					
				end
				
			end
			
		end
		
	end
	
end
concommand.Add( "rp_opendoor", ccOpenDoor );

function ccPurchaseDoor( ply, cmd, args )
	local door = ents.GetByIndex( tonumber( args[ 1 ] ) );
	
	local pos = door:GetPos( );
	
	for k, v in pairs( Doors ) do
		
		if( tonumber( v[ "x" ] ) == math.ceil( tonumber( pos.x ) ) and tonumber( v[ "y" ] ) == math.ceil( tonumber( pos.y ) ) and tonumber( v[ "z" ] ) == math.ceil( tonumber( pos.z ) ) ) then
		
			SendChat( ply, "This is not a purchaseable door!" );
			return;
			
		end
		
	end
	
	if( IsDoor( door ) ) then

		if( door.owner == nil ) then
		
			if( tonumber( GetCharField( ply, "money" ) ) >= 50 ) then
				
				-- Enough money to start off, let's start the rental.
				ChangeMoney( ply, -50 );
				door.owner = ply;
				
				local function Rental( ply, doornum )
				
					local door = ents.GetByIndex( tonumber( doornum ) );
					
					if( door.owner == ply ) then
					
						if( tonumber( GetCharField( ply, "money" ) ) >= 50 ) then
						
							ChangeMoney( ply, 0 - 50 );
							SendChat( ply, "You have been charged 50 credits for a door!" );
							-- Start the timer again
							timer.Simple( 900, Rental, ply, doornum ); -- 15 minutes hoo rah
							
						else
						
							SendChat( ply, "You have lost a door due to insufficient funds." );
							door.owner = nil;
							
						end
						
					end
				
				end
				
				timer.Simple( 900, Rental, ply, tonumber( args[ 1 ] ) );
				
			end
			
		elseif( door.owner == ply ) then
		
			door.owner = nil;
			SendChat( ply, "Door Unowned" );
			
		else
		
			SendChat( ply, "This door is already rented by someone else!" );
			
		end
	
	end
	
end
concommand.Add( "rp_purchasedoor", ccPurchaseDoor );

function ccDropWeapon( ply, cmd, args )
	
	local wep = ply:GetActiveWeapon( )
	
	if( ItemData[ wep:GetClass( ) ] == nil ) then SendChat( ply, "This weapon cannot be dropped!" ); return; end
	
	ply:StripWeapon( wep:GetClass( ) );
	
	CreateItem( wep:GetClass( ), ply:CalcDrop( ), Angle( 0,0,0 ) );
	
end
concommand.Add( "rp_dropweapon", ccDropWeapon );

function ccOverwatchSound( ply, cmd, args )
if(ply:Team() > 1) then
	ply:EmitSound(tostring(args[1]), 100, 100)
else
end
end
concommand.Add( "rp_owsound", ccOverwatchSound );

function ccPickupItem( ply, cmd, args )
	
	local item = ents.GetByIndex( tonumber( args[ 1 ] ) );
	
	if( item != nil and item:IsValid( ) and item:GetClass( ) == "item_prop" and item:GetPos( ):Distance( ply:GetShootPos( ) ) < 100 ) then
		
		item:Pickup( ply );
		ply:GiveItem( item.Class );
		
	end

end
concommand.Add( "rp_pickup", ccPickupItem );

function ccUseItem( ply, cmd, args )
	
	local item = ents.GetByIndex( tonumber( args[ 1 ] ) );
	
	if( item != nil and item:IsValid( ) and item:GetClass( ) == "item_prop" and item:GetPos( ):Distance( ply:GetShootPos( ) ) < 100 ) then
		
		item:UseItem( ply );
		
	end

end
concommand.Add( "rp_useitem", ccUseItem );

function ccGiveMoney( ply, cmd, args )
	
	if( player.GetByID( args[ 1 ] ) != nil ) then
	
		local target = player.GetByID( args[ 1 ] );
		
		if( tonumber( args[ 2 ] ) > 0 ) then
		
			if( tonumber( GetCharField( ply, "money" ) ) >= tonumber( args[ 2 ] ) ) then
			
				ChangeMoney( target, args[ 2 ] );
				ChangeMoney( ply, 0 - args[ 2 ] );
				SendChat( ply, "You gave " .. target:Nick( ) .. " " .. args[ 2 ] .. " credits!" );
				SendChat( target, ply:Nick( ) .. " gave you " .. args[ 2 ] .. " credits!" );
				
			else
			
				SendChat( ply, "You do not have that many tokens!" );
				
			end
			
		else
		
			SendChat( ply, "Invalid amount of money!" );
			
		end
		
	else
	
		SendChat( ply, "Target not found!" );
		
	end
	
end
concommand.Add( "rp_givemoney", ccGiveMoney );	

function ccOpenChat( ply, cmd, args )

	ply:SetNWInt( "chatopen", 1 )
	
end
concommand.Add( "rp_openedchat", ccOpenChat );

function ccCloseChat( ply, cmd, args )

	ply:SetNWInt( "chatopen", 0 )
	
end
concommand.Add( "rp_closedchat", ccCloseChat );