コード例 #1
0
ファイル: Quartermaster.cs プロジェクト: qvazzler/Lyralei
        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.
            }
        }