Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- VoiceChannels = {};
- VoiceChannels.active = {};
- // Create a new channel.
- function VoiceChannels.Create( bprivate, strname, strpassword, callbackfilter )
- -- If there already is a active channel with this name, put an error and end the function.
- if ( VoiceChannels.IsChannelActive( strname ) ) then
- print( " VoiceChannels : Could not create Voice Channel '" .. strname .. "', channel with same name already exists." );
- return;
- end
- -- Set up the active information of this channel.
- VoiceChannels.active[strname] = {};
- VoiceChannels.active[strname].private = bprivate;
- VoiceChannels.active[strname].players = {};
- if ( bprivate == true ) then
- VoiceChannels.active[strname].password = strpassword;
- end
- local function CBFilter(player)
- return true;
- end;
- VoiceChannels.active[strname].filter = callbackfilter or CBFilter;
- print( " VoiceChannels : Created Voice Channel '" .. strname .. "', Protected: " .. tostring(bprivate) .. "." );
- end
- // See if a channel is active or not.
- function VoiceChannels.IsChannelActive( strChannel )
- if ( VoiceChannels.active[strname] ) then
- return true;
- end
- return false;
- end
- // Add a player to a channel.
- function VoiceChannels.AddPlayer( strChannel, player )
- -- Make sure the channel is active.
- if ( !VoiceChannels.IsChannelActive(strChannel) ) then return; end;
- -- Don't do anything if they are already in the channel.
- if ( VoiceChannels.PlayerIsInChannel(strChannel,player) ) then return; end;
- -- Make sure the player is a real player.
- if ( !player:IsPlayer() ) then return; end;
- -- Make sure they pass the channels custom filter, if they have one.
- if ( !VoiceChannels.active[strChannel].filter(player) ) then
- print("Could not add player to Voice Channel '" .. strChannel .. "'.");
- return;
- end;
- -- Insert the player object into the channels player list.
- table.insert( VoiceChannels.active[strChannel].players, player );
- -- Call the PostPlayerJoinChannel callback.
- VoiceChannels.PostPlayerJoinChannel( player, strChannel );
- print( " VoiceChannels : Added player '" .. player:Nick() .. "(" .. player:SteamID() .. ")' to Voice Channel '" .. strchannel "'." );
- end
- // Check if a player is in a channel.
- function VoiceChannels.PlayerIsInChannel( strchannel, player )
- if ( table.HasValue( VoiceChannels.active[strchannel].players, player ) ) then
- return true;
- end
- return false;
- end
- // A simple function to see if two players are in the same channel.
- function VoiceChannels.PlayerSharesChannelWithPlayer( strchannel, playera, playerb )
- if ( table.HasValue( VoiceChannels.active[strchannel].players, playera ) and table.HasValue( VoiceChannels.active[strchannel].players, playerb ) ) then
- return true;
- end
- return false;
- end
- // The callback that is called after the player joins the channel.
- function VoiceChannels.PostPlayerJoinChannel( player, strChannel )
- end
- // Called after a player leaves a channel.
- function VoiceChannels.PostPlayerLeaveChannel( player, strChannel )
- end
- // Make a player exit a channel properly.
- function VoiceChannels.ExitPlayer( player, strChannel )
- -- Make sure the channel is real.
- if ( !VoiceChannels.IsChannelActive(strChannel) ) then
- return;
- end
- -- If the player isn't in the channel, don't try to exit them.
- if ( !VoiceChannels.PlayerIsInChannel( strChannel, player ) ) then
- return;
- end;
- -- Find the index that is holding the player in this channels player list.
- local key = table.KeyFromValue( VoiceChannels.active[strChannel].players, player );
- -- Terminate that key from the list.
- VoiceChannels.active[strChannel].players[key] = nil;
- -- Call the callback.
- VoiceChannels.PostPlayerLeaveChannel( player, strChannel );
- end
- // Get all active channels.
- function VoiceChannels.GetActiveList()
- return VoiceChannels.active;
- end
- // Make a player tune in on a channel.
- function VoiceChannels.PlayerSelectVoiceChannel( player, strChannel )
- -- If they are in this channel then tune them.
- if ( VoiceChannels.PlayerIsInChannel(strChannel,player) ) then
- player.TunedVoiceChannel = strChannel;
- end
- end
- // See if the supplied password is correct.
- function VoiceChannels.CheckPassword( strChannel, strPassword )
- if ( VoiceChannels.active[strChannel].password == strPassword ) then
- return true;
- end
- return false;
- end
- // Get if a channel is locked/private.
- function VoiceChannels.GetLocked( strChannel )
- return VoiceChannels.active[strChannel].private;
- end
- // Make this all have a purpose.
- hook.Add( "PlayerCanHearPlayersVoice", "VoiceChannelsCHVC", function( listener, speaker )
- return VoiceChannels.PlayerSharesChannelWithPlayer( listener.TunedVoiceChannel or "", listener, speaker );
- end);
- // Make a new channel.
- concommand.Add("VoiceChannels_Create", function( player, command, arguments )
- local name = arguments[1];
- local private = arguments[2];
- local password = arguments[3];
- -- Convert the string argument to a bool.
- if ( string.lower(private) == "true" ) then
- private = true;
- else
- private = false;
- end
- -- Create the channel with the respective information.
- VoiceChannels.Create( private, name, password, function(player)
- return true;
- end);
- end);
- // Join a voice channel.
- concommand.Add( "VoiceChannels_Join", function( player, command, arguments)
- local name = arguments[1];
- local password = arguments[2];
- if ( VoiceChannels.GetLocked(name) ) then
- if ( password and VoiceChannels.CheckPassword(name,password) ) then
- VoiceChannels.AddPlayer(name,player);
- else
- print(" VoiceChannels : Player '" .. player:Nick() .. "' could not join Voice Channel '" .. name .. "' due to invalid password." );
- end
- else
- VoiceChannels.AddPlayer(name,player);
- end
- end);
- // Leave a voice channel.
- concommand.Add("VoiceChannels_Leave", function( player, command, arguments)
- local name = arguments[1] or "";
- VoiceChannels.ExitPlayer(player,name);
- end);
- // Select which voice channel to listen to.
- concommand.Add("VoiceChannels_Select", function( player, command, arguments )
- local name = arguments[1];
- VoiceChannels.PlayerSelectVoiceChannel(player,name);
- end);
- // For admins, to delete voice channels.
- concommand.Add("VoiceChannels_Delete", function( playerr, command, arguments )
- -- This is admin only.
- if ( !playerr:IsAdmin() or !playerr:IsSuperAdmin() ) then return; end;
- -- Just cause.
- local name = arguments[1];
- -- If the channel isn't active, don't bother.
- if ( !VoiceChannels.IsChannelActive(name) ) then return; end;
- -- Do the real removin' shit.
- VoiceChannels.active[name] = nil;
- -- If anyone has that channel selected, un select them.
- for _,p in pairs( player.GetAll() ) do
- if ( string.lower(p.TunedVoiceChannel) == string.lower(name) ) then
- p.TunedVoiceChannel = "";
- end
- end
- end);
- // List all active channels.
- concommand.Add("VoiceChannels_ListAll", function( player, command, arguments )
- PrintTable( VoiceChannels.GetActiveList() );
- end);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement