private void TidyChannels(int eventChannelId, int eventClientId = -1) { Console.WriteLine("(Thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId + ") " + DateTime.Now + " - TidyChannels: Entering wait queue"); slimtest.Wait(); ChannelInfoResponse _target_channel = queryRunner.GetChannelInfo((uint)eventChannelId); ChannelInfoResponse _parent_channel = queryRunner.GetChannelInfo(_target_channel.ParentChannelId); bool privateChannel = false; slimtest.Release(); Console.WriteLine("(Thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId + ") TidyChannels: Leaving wait queue"); AutoChannelNameType channelNameType = AutoChannelNameType.Numeric; int empty_channel_treshhold = 2; //bool private_channel = false; int parent_channel_id = 0; try { ChannelSettings settings = new ChannelSettings(_parent_channel.Topic); settings.SetActiveSetting("autochannel"); if (settings.ContainsParam("private")) { if (settings.GetParam("private") == "yes") { privateChannel = true; } } if (settings.ContainsParam("empty_channels")) { try { if (Convert.ToInt32(settings.GetParam("empty_channels")) > 0) { empty_channel_treshhold = Convert.ToInt32(settings.GetParam("empty_channels")); } } catch (Exception) { //Value wasn't numeric } } if (settings.ContainsParam("naming_type")) { if (settings.GetParam("naming_type") == "alphabetic") { channelNameType = AutoChannelNameType.Alphabetic; } else if (settings.GetParam("naming_type") == "numeric") { channelNameType = AutoChannelNameType.Numeric; } } VerifyHostPresent(eventChannelId, eventClientId, privateChannel); parent_channel_id = (int)_target_channel.ParentChannelId; } catch (Exception) { //Channel not eligible for tidying return; } //Now we check if the user left any other channel in the auto-section that can now be deleted ListResponse <ChannelListEntry> channels = queryRunner.GetChannelList(false, true, false, false, false); List <ChannelListEntry> empty_channels = new List <ChannelListEntry>(); List <ChannelListEntry> relevant_channels = new List <ChannelListEntry>(); bool channelEmpty = true; //Go through all the channels foreach (ChannelListEntry channel in channels) { channelEmpty = true; //Only match the ones with the same parentid. if (channel.ParentChannelId == parent_channel_id) { foreach (ClientListEntry client in queryRunner.GetClientList()) { if (client.ChannelId == channel.ChannelId) { //There is already someone in the channel. channelEmpty = false; break; } } //If channel is empty, add to list of empty channels for later iteration if (channelEmpty) { empty_channels.Add(channel); //Also remove the password from the empty channel if (channel.IsPasswordProtected == true) { ChannelModification mod = new ChannelModification(); mod.Password = ""; queryRunner.EditChannel(channel.ChannelId, mod); } ListResponse <ChannelGroupClient> channelgroupclientlist = queryRunner.GetChannelGroupClientList(channel.ChannelId, null, 10); foreach (ChannelGroupClient cgclient in channelgroupclientlist) { queryRunner.SetClientChannelGroup(8, channel.ChannelId, cgclient.ClientDatabaseId); } } relevant_channels.Add(channel); } } //Check for excessive amount of empty rooms, delete if necessary if (empty_channels.Count > empty_channel_treshhold) { ChannelListEntry[] sorted_empty_channels = SortChannels_GetArray(empty_channels); for (int i = sorted_empty_channels.Length - 1; i > empty_channel_treshhold - 1; i--) { SimpleResponse resp = queryRunner.DeleteChannel(sorted_empty_channels[i].ChannelId); } } else if (empty_channels.Count < empty_channel_treshhold) { int channels_created = 0; //HostInfoResponse hostinfo = queryRunner.GetHostInfo(); //ClientInfoResponse clientinfo = queryRunner.GetClientInfo(1); do { if (channels_created > 1) { //If more than 2 channels have been created already, we add some delay to make sure not to trigger the flood protection too easily Thread.Sleep(300); } //There are no empty channels left so we need to create a new one. //First we get the lowest available index for a room name. ChannelModification newroom = LowestAvailableRoomName((uint)parent_channel_id, channelNameType); if (newroom.ChannelOrder == null) { newroom.ChannelOrder = SortChannels_FindOrder(relevant_channels, newroom); } string test; //Then we create the channel try { channels_created++; test = queryRunner.CreateChannel(newroom).GetDumpString(); } catch (FormatException) { //Not sure why we're getting an exception. The code is working the way it should. } }while (empty_channels.Count + channels_created < empty_channel_treshhold); //SortChannels(parent_channel_id); } else { //There's still 1 empty channel after user joined the area. We dont have to do anything. } }
/// <summary> /// Gets the next available room name for numeric and alphabetic auto channel areas /// </summary> /// <param name="parent_channel_id">The channel to get list of sub-channels from</param> /// <param name="type">Specify whether the sub-channels are named alphabetically or numerically</param> /// <returns>ChannelModification, containing information about the next available channel</returns> private ChannelModification LowestAvailableRoomName(uint parent_channel_id, AutoChannelNameType type) { List <ChannelListEntry> channels = new List <ChannelListEntry>(); ListResponse <ChannelListEntry> all_channels = queryRunner.GetChannelList(); ChannelModification new_channel = new ChannelModification(); new_channel.IsPermanent = true; new_channel.Description = "Automated channel!"; new_channel.Codec = (TS3QueryLib.Core.Server.Entities.Codec)Codec.OpusVoice; new_channel.CodecQuality = 10; new_channel.Topic = ""; new_channel.ArUnlimitedMaxFamilyClientsInherited = false; //new_channel.HasUnlimitedMaxClients = true; //new_channel.HasUnlimitedMaxFamilyClients = true; new_channel.IsDefault = false; new_channel.IsTemporary = false; new_channel.IsSemiPermanent = false; //new_channel.MaxClients = 0; //new_channel.MaxFamilyClients = 0; //new_channel.NamePhonetic = ""; new_channel.NeededTalkPower = 0; //new_channel.Password = ""; foreach (ChannelListEntry channel in all_channels) { if (channel.ParentChannelId == parent_channel_id) { channels.Add(channel); } } ChannelListEntry[] sorted_channels = SortChannels_GetArray(channels); //Autochannel name type is alphabetic if (type == AutoChannelNameType.Alphabetic) { //Go through each of the current sorted channels for (int i = 0; i < sorted_channels.Length + 1; i++) { //Get name of what channel should appear next in the sort channel list string name = Entities.Alphabet[i % Entities.Alphabet.Length]; //If channel amount exceeds alphabetic letters, add delimiter "Zulu " the amount of times needed to make the name unique for (int i_alphabet = i - Entities.Alphabet.Length; i_alphabet >= 0; i_alphabet = i_alphabet - Entities.Alphabet.Length) { name = Entities.Alphabet_Delimiter + " " + name; } //As long as its not the last loop, carry on with below as normal if (i < sorted_channels.Length) { //If next sorted channel name does not match the expected name, return that channel to be the next available one if (sorted_channels[i].Name != name) { bool exists = false; for (int k = i; k < sorted_channels.Length; k++) { if (sorted_channels[k].Name == name) { exists = true; //if (k > i) // new_channel.ChannelOrder = null; //else if (k == i && i != sorted_channels.Length) // new_channel.ChannelOrder = sorted_channels[i - 1].ChannelId; //else // new_channel.ChannelOrder = null; } } if (!exists) { new_channel.Name = name; //sorted_channels[i - 1].ChannelId; new_channel.ParentChannelId = parent_channel_id; new_channel.ChannelOrder = null; return(new_channel); } } } //Last loop, last procedure! If all channels were already properly sorted, we add one to the end instead else if (i == sorted_channels.Length) { //If all channels were already properly in order, we need to add a new channel at the end instead. new_channel.Name = name; } } new_channel.ChannelOrder = sorted_channels[sorted_channels.Length - 1].ChannelId; new_channel.ParentChannelId = parent_channel_id; //new_channel.Name = "RAWR"; return(new_channel); } else { for (int i = 0; i < sorted_channels.Length; i++) { if (sorted_channels[i].Name.Substring(5) != (i + 1).ToString()) { new_channel.Name = "Room " + (i + 1).ToString(); if (i != 0) { new_channel.ChannelOrder = sorted_channels[i - 1].ChannelId; } else { new_channel.ChannelOrder = 0; } new_channel.ParentChannelId = parent_channel_id; //new_channel.Name = "RAWR"; return(new_channel); } } new_channel.Name = "Room " + (sorted_channels.Length + 1).ToString(); new_channel.ChannelOrder = sorted_channels[sorted_channels.Length - 1].ChannelId; new_channel.ParentChannelId = parent_channel_id; //new_channel.Name = "RAWR"; return(new_channel); } }