Advertisement
Guest User

Untitled

a guest
Feb 6th, 2012
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.02 KB | None | 0 0
  1. VoiceChannels = {};
  2. VoiceChannels.active = {};
  3.  
  4.  
  5. // Create a new channel.
  6. function VoiceChannels.Create( bprivate, strname, strpassword, callbackfilter )
  7.  
  8.     -- If there already is a active channel with this name, put an error and end the function.
  9.     if ( VoiceChannels.IsChannelActive( strname ) ) then
  10.         print( " VoiceChannels : Could not create Voice Channel '" .. strname .. "', channel with same name already exists." );
  11.         return;
  12.     end
  13.    
  14.     -- Set up the active information of this channel.
  15.     VoiceChannels.active[strname] = {};
  16.     VoiceChannels.active[strname].private = bprivate;
  17.     VoiceChannels.active[strname].players = {};
  18.    
  19.     if ( bprivate == true ) then
  20.         VoiceChannels.active[strname].password = strpassword;
  21.     end
  22.    
  23.     local function CBFilter(player)
  24.         return true;
  25.     end;
  26.    
  27.     VoiceChannels.active[strname].filter = callbackfilter or CBFilter;
  28.    
  29.     print( " VoiceChannels : Created Voice Channel '" .. strname .. "', Protected: " .. tostring(bprivate) .. "." );
  30. end
  31.  
  32.  
  33. // See if a channel is active or not.
  34. function VoiceChannels.IsChannelActive( strChannel )
  35.     if ( VoiceChannels.active[strname] ) then
  36.         return true;
  37.     end
  38.    
  39.     return false;
  40. end
  41.  
  42.  
  43. // Add a player to a channel.
  44. function VoiceChannels.AddPlayer( strChannel, player )
  45.  
  46.  
  47.     -- Make sure the channel is active.
  48.     if ( !VoiceChannels.IsChannelActive(strChannel) ) then return; end;
  49.    
  50.    
  51.     -- Don't do anything if they are already in the channel.
  52.     if ( VoiceChannels.PlayerIsInChannel(strChannel,player) ) then return; end;
  53.    
  54.    
  55.     -- Make sure the player is a real player.
  56.     if ( !player:IsPlayer() ) then return; end;
  57.    
  58.    
  59.     -- Make sure they pass the channels custom filter, if they have one.
  60.     if ( !VoiceChannels.active[strChannel].filter(player) ) then
  61.         print("Could not add player to Voice Channel '" .. strChannel .. "'.");
  62.         return;
  63.     end;
  64.    
  65.    
  66.     -- Insert the player object into the channels player list.
  67.     table.insert( VoiceChannels.active[strChannel].players, player );
  68.    
  69.    
  70.     -- Call the PostPlayerJoinChannel callback.
  71.     VoiceChannels.PostPlayerJoinChannel( player, strChannel );
  72.    
  73.    
  74.     print( " VoiceChannels : Added player '" .. player:Nick() .. "(" .. player:SteamID() .. ")' to Voice Channel '" .. strchannel "'." );
  75. end
  76.  
  77.  
  78. // Check if a player is in a channel.
  79. function VoiceChannels.PlayerIsInChannel( strchannel, player )
  80.     if ( table.HasValue( VoiceChannels.active[strchannel].players, player ) ) then
  81.         return true;
  82.     end
  83.    
  84.     return false;
  85. end
  86.  
  87.  
  88. // A simple function to see if two players are in the same channel.
  89. function VoiceChannels.PlayerSharesChannelWithPlayer( strchannel, playera, playerb )
  90.     if ( table.HasValue( VoiceChannels.active[strchannel].players, playera ) and table.HasValue( VoiceChannels.active[strchannel].players, playerb ) ) then
  91.         return true;
  92.     end
  93.    
  94.     return false;
  95. end
  96.  
  97.  
  98. // The callback that is called after the player joins the channel.
  99. function VoiceChannels.PostPlayerJoinChannel( player, strChannel )
  100. end
  101.  
  102.  
  103. // Called after a player leaves a channel.
  104. function VoiceChannels.PostPlayerLeaveChannel( player, strChannel )
  105. end
  106.  
  107.  
  108. // Make a player exit a channel properly.
  109. function VoiceChannels.ExitPlayer( player, strChannel )
  110.  
  111.     -- Make sure the channel is real.
  112.     if ( !VoiceChannels.IsChannelActive(strChannel) ) then
  113.         return;
  114.     end
  115.    
  116.    
  117.     -- If the player isn't in the channel, don't try to exit them.
  118.     if ( !VoiceChannels.PlayerIsInChannel( strChannel, player ) ) then
  119.         return;
  120.     end;
  121.    
  122.    
  123.     -- Find the index that is holding the player in this channels player list.
  124.     local key = table.KeyFromValue( VoiceChannels.active[strChannel].players, player );
  125.    
  126.     -- Terminate that key from the list.
  127.     VoiceChannels.active[strChannel].players[key] = nil;
  128.    
  129.     -- Call the callback.
  130.     VoiceChannels.PostPlayerLeaveChannel( player, strChannel );
  131. end
  132.  
  133.  
  134. // Get all active channels.
  135. function VoiceChannels.GetActiveList()
  136.     return VoiceChannels.active;
  137. end
  138.  
  139.  
  140. // Make a player tune in on a channel.
  141. function VoiceChannels.PlayerSelectVoiceChannel( player, strChannel )
  142.    
  143.     -- If they are in this channel then tune them.
  144.     if ( VoiceChannels.PlayerIsInChannel(strChannel,player) ) then
  145.         player.TunedVoiceChannel = strChannel;
  146.     end
  147. end
  148.  
  149.  
  150. // See if the supplied password is correct.
  151. function VoiceChannels.CheckPassword( strChannel, strPassword )
  152.     if ( VoiceChannels.active[strChannel].password == strPassword ) then
  153.         return true;
  154.     end
  155.    
  156.     return false;
  157. end
  158.  
  159.  
  160. // Get if a channel is locked/private.
  161. function VoiceChannels.GetLocked( strChannel )
  162.     return VoiceChannels.active[strChannel].private;
  163. end
  164.  
  165.  
  166. // Make this all have a purpose.
  167. hook.Add( "PlayerCanHearPlayersVoice", "VoiceChannelsCHVC", function( listener, speaker )
  168.     return VoiceChannels.PlayerSharesChannelWithPlayer( listener.TunedVoiceChannel or "", listener, speaker );
  169. end);
  170.  
  171.  
  172. // Make a new channel.
  173. concommand.Add("VoiceChannels_Create", function( player, command, arguments )
  174.  
  175.     local name = arguments[1];
  176.     local private = arguments[2];
  177.     local password = arguments[3];
  178.    
  179.     -- Convert the string argument to a bool.
  180.     if ( string.lower(private) == "true" ) then
  181.         private = true;
  182.     else
  183.         private = false;
  184.     end
  185.    
  186.     -- Create the channel with the respective information.
  187.     VoiceChannels.Create( private, name, password, function(player)
  188.         return true;
  189.     end);
  190. end);
  191.  
  192.  
  193. // Join a voice channel.
  194. concommand.Add( "VoiceChannels_Join", function( player, command, arguments)
  195.     local name = arguments[1];
  196.     local password = arguments[2];
  197.    
  198.     if ( VoiceChannels.GetLocked(name) ) then
  199.    
  200.         if ( password and VoiceChannels.CheckPassword(name,password) ) then
  201.             VoiceChannels.AddPlayer(name,player);
  202.         else
  203.             print(" VoiceChannels : Player '" .. player:Nick() .. "' could not join Voice Channel '" .. name .. "' due to invalid password." );
  204.         end
  205.     else
  206.         VoiceChannels.AddPlayer(name,player);
  207.     end
  208. end);
  209.  
  210.  
  211. // Leave a voice channel.
  212. concommand.Add("VoiceChannels_Leave", function( player, command, arguments)
  213.     local name = arguments[1] or "";
  214.    
  215.     VoiceChannels.ExitPlayer(player,name);
  216. end);
  217.  
  218.  
  219. // Select which voice channel to listen to.
  220. concommand.Add("VoiceChannels_Select", function( player, command, arguments )
  221.     local name = arguments[1];
  222.    
  223.     VoiceChannels.PlayerSelectVoiceChannel(player,name);
  224. end);
  225.  
  226.  
  227. // For admins, to delete voice channels.
  228. concommand.Add("VoiceChannels_Delete", function( playerr, command, arguments )
  229.  
  230.     -- This is admin only.
  231.     if ( !playerr:IsAdmin() or !playerr:IsSuperAdmin() ) then return; end;
  232.    
  233.    
  234.     -- Just cause.
  235.     local name = arguments[1];
  236.    
  237.    
  238.     -- If the channel isn't active, don't bother.
  239.     if ( !VoiceChannels.IsChannelActive(name) ) then return; end;
  240.    
  241.    
  242.     -- Do the real removin' shit.
  243.     VoiceChannels.active[name] = nil;
  244.    
  245.    
  246.     -- If anyone has that channel selected, un select them.
  247.     for _,p in pairs( player.GetAll() ) do
  248.         if ( string.lower(p.TunedVoiceChannel) == string.lower(name) ) then
  249.             p.TunedVoiceChannel = "";
  250.         end
  251.     end
  252.    
  253. end);
  254.  
  255.  
  256. // List all active channels.
  257. concommand.Add("VoiceChannels_ListAll", function( player, command, arguments )
  258.     PrintTable( VoiceChannels.GetActiveList() );
  259. end);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement