Пример #1
0
        private async Task MatchingTask()
        {
            //Wait some time
            DebugLogWriteLine("MatchingTask started, waiting");
            await Task.Delay(30000);

            if (!Active)
            {
                DebugLogWriteLine("MatchingTask canceled. Set Active to false");
                return;
            }
            //Get all users
            DebugLogWriteLine("MatchingTask Matching");
            DateVoiceLobbies.Clear();
            DateVoiceLobbies.AddRange(GetVoiceLobbies(true));
            RefreshUsersInLobbies();
            //Match users
            //Move pairs
            TryMatchUsers();

            //Removeitself or restart
            DateVoiceLobbies.Clear();
            DateVoiceLobbies.AddRange(GetVoiceLobbies(true));
            RefreshUsersInLobbies();
            if (UsersInLobbies.Count > 4)
            {
                //Restart? if not same gender
                CurrentMatchingTask = MatchingTask();
                _ = CurrentMatchingTask.ConfigureAwait(false);
            }
            else
            {
                CurrentMatchingTask = null;
            }
        }
Пример #2
0
        private async Task CombLobbies()
        {
            //Do we really need this?
            //await Task.Delay(1000);
            //Comb lobbies
            DateVoiceLobbies.Clear();
            DateVoiceLobbies.AddRange(GetVoiceLobbies(true));
            DateVoiceLobbies.Where(l => l.Id != DateVoiceLobbies.Last().Id&& l.Users.Count() == 0)
            .ToList()
            .ForEach(async l => {
                DateVoiceLobbies.Remove(l);
                await l.DeleteAsync();
            });
            int i = 0;

            foreach (var l in DateVoiceLobbies.ToArray())
            {
                await l.ModifyAsync(c => c.Name = $"Date Voice Lobby {i++}");
            }
            if (DateVoiceLobbies.Count == 0 || DateVoiceLobbies.Last().Users.Count() > 0)
            {
                //Add empty at the end
                await AddLastEmptyVoiceLobby();
            }

            CurrentCombLobbiesTask = null;
        }
Пример #3
0
        private async Task AddLastEmptyVoiceLobby()
        {
            var c = await Guild.CreateChannelAsync($"Date Voice Lobby {DateVoiceLobbies.Count}", ChannelType.Voice, DateRootCategory,
                                                   overwrites : new List <DiscordOverwriteBuilder>()
            {
                new DiscordOverwriteBuilder()
                {
                    Allowed = Permissions.AccessChannels
                }.For(Guild.EveryoneRole)
            });

            //await c.AddOverwriteAsync(Guild.EveryoneRole, Permissions.AccessChannels);
            DateVoiceLobbies.Add(c);
        }
Пример #4
0
 private void ReturnUserToFirstLobbyAvailable(DiscordMember p)
 {
     _ = DateVoiceLobbies.FirstOrDefault(l => l.UserLimit == 0 || l.UserLimit < l.Users.Count())
         .PlaceMemberAsync(p).ConfigureAwait(false);
 }
Пример #5
0
        /// <summary>
        /// User changed state in voice lobbies (joined/left)
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public async Task VoiceStateUpdated(VoiceStateUpdateEventArgs e)
        {
            InitTask?.Wait();
            //DebugLogWriteLine("VoiceStateUpdated: ");

            if (e.After?.Channel != e.Before?.Channel)               //Channel changed
            {
                var beforeInLobbies     = DateVoiceLobbies.Contains(e.Before?.Channel);
                var afterInLobbies      = DateVoiceLobbies.Contains(e.After?.Channel);
                var beforeInSecretRooms = SecretRooms.Contains(e.Before?.Channel);
                var afterInSecretRooms  = SecretRooms.Contains(e.After?.Channel);

                if (!beforeInLobbies && !beforeInSecretRooms && afterInLobbies)
                {
                    //User connected to lobbies
                    DebugLogWriteLine($"User {e.User} connected to {e.Channel} ");
                    UserConnected_AddToActivity(e.User);
                    //Start matching session
                    TryStartMatchingTask();
                }
                else if ((beforeInLobbies || beforeInSecretRooms) && !afterInLobbies && !afterInSecretRooms)
                {
                    //User left activity
                    DebugLogWriteLine($"User {e.User} left activity ");
                    if (beforeInLobbies)
                    {
                        UsersInLobbies.Remove(e.User);
                        RemoveStateFor(e.User);
                    }
                    //remove disband secret room if one left TODO check logic
                    if (beforeInSecretRooms)
                    {
                        RemoveStateFor(e.User);
                        //DebugLogWrite($"trying to disbnad {e.Before.Channel}... ");
                        await disbandRemoveSecretRoom(e.Before.Channel).ConfigureAwait(false);

                        //DebugLogWrite($"{e.Before.Channel} removed/disbanded");
                    }
                }
                else if (beforeInLobbies && afterInLobbies)
                {
                    DebugLogWriteLine($"User {e.User} switched lobbies ");
                    //User switched Lobbies
                }
                else if (beforeInLobbies && afterInSecretRooms)
                {
                    DebugLogWriteLine($"User {e.User} moved to {e.After.Channel} ");
                    //Moved to secret room
                    UsersInLobbies.Remove(e.User);
                }
                else if (beforeInSecretRooms && afterInLobbies)
                {
                    //Returned from secret room
                    RemoveStateFor(e.User);
                    DebugLogWriteLine($"User {e.User} returned to lobby, trying to disbnad {e.Before.Channel}... ");
                    UserConnected_AddToActivity(e.User);
                    await disbandRemoveSecretRoom(e.Before.Channel).ConfigureAwait(false);

                    //DebugLogWrite($"{e.Before.Channel} removed/disbanded. Starting MatchingTask");
                    //Start matching session
                    TryStartMatchingTask();
                }

                TryCombLobbies();
            }

            async Task disbandRemoveSecretRoom(DiscordChannel channel)
            {
                //Remove empty room, if 1 user left, move him into lobby 0, later room will be removed
                if (channel.Users.Count() == 0)
                {
                    try {                     //usually shouldn't throw, but in debug
                        await channel.DeleteAsync().ConfigureAwait(false);
                    } catch {
                        //DebugLogWrite($"Exception while deleting ");
                    }
                }
                else if (channel.Users.Count() == 1)
                {
                    try {
                        var moveMember = await Guild.GetMemberAsync(channel.Users.First().Id);

                        await DateVoiceLobbies[0].PlaceMemberAsync(moveMember);
                    } catch {
                        //DebugLogWrite($"Exception while moving member ");
                    }
                }
            }
        }
Пример #6
0
 private void RefreshUsersInLobbies()
 {
     UsersInLobbies.Clear();
     DateVoiceLobbies.ForEach(l => UsersInLobbies.AddRange(l.Users));
 }
Пример #7
0
        /// <summary>
        /// Gets all channels, members in them and set's up event handlers
        /// </summary>
        /// <param name="guild"></param>
        /// <returns></returns>
        internal async Task Initialize(DiscordGuild guild)
        {
            //DebugLogWriteLine($"Initiating {guild} GuildTask... ");
            //Init guild
            Guild   = guild;
            GuildId = guild.Id;
            SecretRoomOverwriteBuilder = new DiscordOverwriteBuilder();
            SecretRoomOverwriteBuilder.For(Guild.EveryoneRole);
            SecretRoomOverwriteBuilder.Deny(Permissions.AccessChannels);
            //PersonalChannelOverwriteBuilder = new DiscordOverwriteBuilder();
            //PersonalChannelOverwriteBuilder.Allow(Permissions.AccessChannels);

            LogChannel = guild.GetChannel(LogChannelId);
            if (LogChannel != null)
            {
                LogMessage = await LogChannel.SendMessageAsync($"Begin of log {DateTime.Now.ToString()}\n");
            }

            //Init RootCategory
            Guild.Channels.TryGetValue(DateCategoryId, out DiscordChannel cat);
            if (cat == null)
            {
                string m = $"Initialize failed. No Category found on + {DateCategoryId}";
                //DebugLogWriteLine(m);
                throw new Exception(m);
            }
            DateRootCategory = cat;
            Guild.Channels.TryGetValue(DateSecretCategoryId, out DiscordChannel secCat);
            DateSecretCategory = secCat;

            //DebugLogWrite("Initiating voice channels... ");
            //VoiceChannels
            DateVoiceLobbies.Clear();
            SecretRooms.Clear();
            DateVoiceLobbies.AddRange(GetVoiceLobbies());
            SecretRooms.AddRange(GetSecretRooms());
            await VoiceChannelsInitAsync().ConfigureAwait(false);

            if (DateVoiceLobbies.Count == 0 || DateVoiceLobbies.All(c => c.Users.Count() > 0))
            {
                await AddLastEmptyVoiceLobby().ConfigureAwait(false);
            }

            //DebugLogWrite("Initiating Emojis... ");
            //GetEmojis
            MaleEmoji   = DiscordEmoji.FromUnicode(DateBot.Instance.Client, MaleEmojiId);
            FemaleEmoji = DiscordEmoji.FromUnicode(DateBot.Instance.Client, FemaleEmojiId);
            OptionEmojis.Clear();
            OptionEmojis.AddRange(OptionEmojiIds.Select(id => DiscordEmoji.FromUnicode(DateBot.Instance.Client, id)));
            LikeEmoji       = DiscordEmoji.FromUnicode(DateBot.Instance.Client, LikeEmojiId);
            DisLikeEmoji    = DiscordEmoji.FromUnicode(DateBot.Instance.Client, DisLikeEmojiId);
            TimeEmoji       = DiscordEmoji.FromUnicode(DateBot.Instance.Client, TimeEmojiId);
            CancelLikeEmoji = DiscordEmoji.FromUnicode(DateBot.Instance.Client, CancelLikeEmojiId);

            //DebugLogWrite("Initiating Users in lobbies... ");
            //Check and add users in lobbies
            foreach (var u in UsersInLobbies.ToArray())
            {
                AddOrGetUserState(u).LastEnteredLobbyTime = DateTime.Now;
            }

            //DebugLogWrite("Initiating Welcome message... ");
            //Check for welcome message TODO add option emojis
            DateTextChannel = Guild.GetChannel(DateTextChannelId);
            await WelcomeMessageInit().ConfigureAwait(false);

            _ = PrivateControlsMessageInit().ConfigureAwait(false);

            //UpdateTimer = new Timer(TimeSpan.FromSeconds(30).TotalMilliseconds) { AutoReset = true };
            //UpdateTimer.Elapsed += UpdateTimer_Elapsed;
            //UpdateTimer.Start();

            //DebugLogWrite("finished");
            InitTask    = null;
            Initialized = true;

            if (UsersInLobbies.Count > 0)
            {
                TryStartMatchingTask();
            }
        }