////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Handle BSR command
        /// </summary>
        /// <param name="p_Service">Chat service</param>
        /// <param name="p_Message">Chat message</param>
        /// <param name="p_ID">ID of the BSR</param>
        private void Command_BSR(IChatService p_Service, IChatMessage p_Message, string p_ID, bool p_ModeratorAddCommand, bool p_ModeratorAddToTop)
        {
            bool l_IsModerator = HasPower(p_Message.Sender);

            if (p_ModeratorAddCommand && !l_IsModerator)
            {
                SendChatMessage($"@{p_Message.Sender.UserName} You have no power here!");
                return;
            }

            if (!QueueOpen && !(l_IsModerator && p_ModeratorAddCommand))
            {
                SendChatMessage($"@{p_Message.Sender.UserName} the queue is closed!");
                return;
            }

            string l_Key = p_ID.ToLower();

            if (!OnlyHexInString(l_Key))
            {
                _ = m_BeatSaver.Search(l_Key).ContinueWith((p_SearchTaskResult) =>
                {
                    if (p_SearchTaskResult.Result == null || p_SearchTaskResult.Result.Docs.Count == 0)
                    {
                        SendChatMessage($"@{p_Message.Sender.UserName} your search \"{l_Key}\" produced 0 results!");
                        return;
                    }

                    var l_Docs     = p_SearchTaskResult.Result.Docs;
                    string l_Reply = $"@{p_Message.Sender.UserName} your search \"{l_Key}\" produced {l_Docs.Count} results : ";

                    int l_I = 0;
                    for (; l_I < l_Docs.Count && l_I < 4; ++l_I)
                    {
                        if (l_I != 0)
                        {
                            l_Reply += ", ";
                        }

                        l_Reply += " (!bsr " + l_Docs[l_I].Key + ") " + l_Docs[l_I].Name;
                    }

                    if (l_I < l_Docs.Count)
                    {
                        l_Reply += "...";
                    }

                    SendChatMessage(l_Reply);
                });

                return;
            }

            var l_UserRequestCount = 0;

            /// Check if blacklisted
            if (!(l_IsModerator && p_ModeratorAddCommand))
            {
                lock (SongBlackList)
                {
                    var l_BeatMap = SongBlackList.Where(x => x.BeatMap.Key.ToLower() == l_Key).FirstOrDefault();
                    if (l_BeatMap != null)
                    {
                        SendChatMessage($"@{p_Message.Sender.UserName} (bsr {l_BeatMap.BeatMap.Key}) {l_BeatMap.BeatMap.Metadata.SongName} / {l_BeatMap.BeatMap.Metadata.LevelAuthorName} is blacklisted!");
                        return;
                    }
                }
            }

            /// Check if already in queue
            lock (SongQueue)
            {
                var l_BeatMap = SongQueue.Where(x => x.BeatMap.Key.ToLower() == l_Key).FirstOrDefault();
                if (l_BeatMap != null)
                {
                    SendChatMessage($"@{p_Message.Sender.UserName} (bsr {l_BeatMap.BeatMap.Key}) {l_BeatMap.BeatMap.Metadata.SongName} / {l_BeatMap.BeatMap.Metadata.LevelAuthorName} is already in queue!");
                    return;
                }

                l_UserRequestCount = SongQueue.Where(x => x.RequesterName == p_Message.Sender.UserName).Count();
            }

            string l_NamePrefix = "";

            /// Handle request limits
            if (p_Service is BeatSaberPlusChatCore.Services.Twitch.TwitchService)
            {
                var l_TwitchUser = p_Message.Sender as BeatSaberPlusChatCore.Models.Twitch.TwitchUser;

                (int, string)l_Limit = (Config.ChatRequest.UserMaxRequest, "Users");

                if (l_TwitchUser.IsVip && !l_TwitchUser.IsSubscriber)
                {
                    l_Limit = (l_Limit.Item1 + Config.ChatRequest.VIPBonusRequest, "VIPs");
                }
                if (l_TwitchUser.IsSubscriber && !l_TwitchUser.IsVip)
                {
                    l_Limit = (l_Limit.Item1 + Config.ChatRequest.SubscriberBonusRequest, "Subscribers");
                }
                if (l_TwitchUser.IsSubscriber && l_TwitchUser.IsVip)
                {
                    l_Limit = (l_Limit.Item1 + Config.ChatRequest.VIPBonusRequest + Config.ChatRequest.SubscriberBonusRequest, "VIP Subscribers");
                }
                if (l_TwitchUser.IsModerator || l_TwitchUser.IsBroadcaster)
                {
                    l_Limit = (1000, "Moderators");
                }

                if (l_TwitchUser.IsModerator || l_TwitchUser.IsBroadcaster)
                {
                    l_NamePrefix = "🗡";
                }
                else if (l_TwitchUser.IsVip)
                {
                    l_NamePrefix = "💎";
                }
                else if (l_TwitchUser.IsSubscriber)
                {
                    l_NamePrefix = "👑";
                }

                if (l_UserRequestCount >= l_Limit.Item1)
                {
                    SendChatMessage($"@{p_Message.Sender.UserName} You already have {l_UserRequestCount} on the queue. {l_Limit.Item2} are limited to {l_Limit.Item1} request(s).");
                    return;
                }
            }

            /// Check if already requested
            if (!(l_IsModerator && p_ModeratorAddCommand) && m_RequestedThisSession.Contains(l_Key))
            {
                SendChatMessage($"@{p_Message.Sender.UserName} this song was already requested this session!");
                return;
            }

            /// Fetch beatmap
            _ = m_BeatSaver.Key(l_Key).ContinueWith(p_SongTaskResult =>
            {
                try
                {
                    string l_Reply = "@" + p_Message.Sender.UserName + " map " + l_Key + " not found.";
                    if (p_SongTaskResult.Result != null && ((l_IsModerator && p_ModeratorAddCommand) || FilterBeatMap(p_SongTaskResult.Result, p_Message.Sender.UserName, out l_Reply)))
                    {
                        var l_BeatMap = p_SongTaskResult.Result;
                        float l_Vote  = (float)Math.Round((double)l_BeatMap.Stats.Rating * 100f, 0);

                        if ((l_IsModerator && p_ModeratorAddCommand) || !m_RequestedThisSession.Contains(l_Key))
                        {
                            m_RequestedThisSession.Add(l_Key.ToLower());

                            var l_Entry = new SongEntry()
                            {
                                BeatMap       = l_BeatMap,
                                RequesterName = p_Message.Sender.UserName,
                                RequestTime   = DateTime.Now,
                                NamePrefix    = l_NamePrefix
                            };

                            lock (SongQueue)
                            {
                                if (l_IsModerator && p_ModeratorAddToTop)
                                {
                                    SongQueue.Insert(0, l_Entry);
                                }
                                else
                                {
                                    SongQueue.Add(l_Entry);
                                }
                            }

                            /// Update request manager
                            OnQueueChanged();

                            l_Reply = $"(bsr {l_BeatMap.Key}) {l_BeatMap.Metadata.SongName} / {l_BeatMap.Metadata.LevelAuthorName} {l_Vote}% requested by @{p_Message.Sender.UserName} added to queue.";
                        }
                        else
                        {
                            l_Reply = $"@{p_Message.Sender.UserName} this song was already requested this session!";
                        }
                    }

                    SendChatMessage(l_Reply);
                }
                catch (System.Exception p_Exception)
                {
                    Logger.Instance.Error("Command_BSR");
                    Logger.Instance.Error(p_Exception);
                }
            });
        }
Example #2
0
        ////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Load database
        /// </summary>
        private void LoadDatabase()
        {
            try
            {
                string l_FilePath = m_DBFilePath;
                if (!System.IO.File.Exists(l_FilePath))
                {
                    Logger.Instance.Error("File not found " + m_DBFilePath);
                    return;
                }

                var l_JSON = JObject.Parse(System.IO.File.ReadAllText(l_FilePath, UTF8Encoding.UTF8));
                if (l_JSON["queue"] != null)
                {
                    JArray l_JSONSongs = (JArray)l_JSON["queue"];
                    for (int l_SongIt = 0; l_SongIt < l_JSONSongs.Count; l_SongIt++)
                    {
                        SongEntry l_Entry = new SongEntry();
                        if ((l_JSONSongs[l_SongIt] as JObject).ContainsKey("key"))
                            l_Entry.BeatMap = new BeatSaverSharp.Beatmap(m_BeatSaver, (string)l_JSONSongs[l_SongIt]["key"]);
                        else
                        {
                            l_Entry.BeatMap = new BeatSaverSharp.Beatmap(m_BeatSaver, "", "", "");
                            JsonConvert.PopulateObject((string)l_JSONSongs[l_SongIt]["btm"], l_Entry.BeatMap);
                        }

                        if ((l_JSONSongs[l_SongIt] as JObject).ContainsKey("rqt"))
                            l_Entry.RequestTime = SDK.Misc.Time.FromUnixTime(l_JSONSongs[l_SongIt]["rqt"].Value<long>());

                        l_Entry.RequesterName   = (string)l_JSONSongs[l_SongIt]["rqn"];

                        if ((l_JSONSongs[l_SongIt] as JObject).ContainsKey("npr"))
                            l_Entry.NamePrefix = (string)l_JSONSongs[l_SongIt]["npr"];

                        SongQueue.Add(l_Entry);

                        /// Start populate
                        l_Entry.BeatMap.Populate().ContinueWith(x => OnBeatmapPopulated(x, l_Entry));
                    }
                }
                if (l_JSON["history"] != null)
                {
                    JArray l_JSONSongs = (JArray)l_JSON["history"];
                    for (int l_SongIt = 0; l_SongIt < l_JSONSongs.Count; l_SongIt++)
                    {
                        SongEntry l_Entry = new SongEntry();
                        if ((l_JSONSongs[l_SongIt] as JObject).ContainsKey("key"))
                            l_Entry.BeatMap = new BeatSaverSharp.Beatmap(m_BeatSaver, (string)l_JSONSongs[l_SongIt]["key"]);
                        else
                        {
                            l_Entry.BeatMap = new BeatSaverSharp.Beatmap(m_BeatSaver, "", "", "");
                            JsonConvert.PopulateObject((string)l_JSONSongs[l_SongIt]["btm"], l_Entry.BeatMap);
                        }

                        if ((l_JSONSongs[l_SongIt] as JObject).ContainsKey("rqt"))
                            l_Entry.RequestTime = SDK.Misc.Time.FromUnixTime(l_JSONSongs[l_SongIt]["rqt"].Value<long>());

                        l_Entry.RequesterName   = (string)l_JSONSongs[l_SongIt]["rqn"];

                        if ((l_JSONSongs[l_SongIt] as JObject).ContainsKey("npr"))
                            l_Entry.NamePrefix = (string)l_JSONSongs[l_SongIt]["npr"];

                        SongHistory.Add(l_Entry);

                        /// Start populate
                        l_Entry.BeatMap.Populate().ContinueWith(x => OnBeatmapPopulated(x, l_Entry));
                    }
                }
                if (l_JSON["blacklist"] != null)
                {
                    JArray l_JSONSongs = (JArray)l_JSON["blacklist"];
                    for (int l_SongIt = 0; l_SongIt < l_JSONSongs.Count; l_SongIt++)
                    {
                        SongEntry l_Entry = new SongEntry();
                        if ((l_JSONSongs[l_SongIt] as JObject).ContainsKey("key"))
                            l_Entry.BeatMap = new BeatSaverSharp.Beatmap(m_BeatSaver, (string)l_JSONSongs[l_SongIt]["key"]);
                        else
                        {
                            l_Entry.BeatMap = new BeatSaverSharp.Beatmap(m_BeatSaver, "", "", "");
                            JsonConvert.PopulateObject((string)l_JSONSongs[l_SongIt]["btm"], l_Entry.BeatMap);
                        }

                        if ((l_JSONSongs[l_SongIt] as JObject).ContainsKey("rqt"))
                            l_Entry.RequestTime = SDK.Misc.Time.FromUnixTime(l_JSONSongs[l_SongIt]["rqt"].Value<long>());

                        l_Entry.RequesterName   = (string)l_JSONSongs[l_SongIt]["rqn"];

                        if ((l_JSONSongs[l_SongIt] as JObject).ContainsKey("npr"))
                            l_Entry.NamePrefix = (string)l_JSONSongs[l_SongIt]["npr"];

                        SongBlackList.Add(l_Entry);

                        /// Start populate
                        l_Entry.BeatMap.Populate().ContinueWith(x => OnBeatmapPopulated(x, l_Entry));
                    }
                }
            }
            catch (System.Exception p_Exception)
            {
                Logger.Instance.Critical("LoadDataBase");
                Logger.Instance.Critical(p_Exception);
            }
        }
        /// <summary>
        /// Block a song
        /// </summary>
        /// <param name="p_Service">Chat service</param>
        /// <param name="p_Message">Chat message</param>
        /// <param name="p_ID">ID of the BSR</param>
        private void Command_Block(IChatService p_Service, IChatMessage p_Message, string p_ID)
        {
            if (!HasPower(p_Message.Sender))
            {
                SendChatMessage($"@{p_Message.Sender.UserName} You have no power here!");
                return;
            }

            string l_Key = p_ID.ToLower();

            if (!OnlyHexInString(l_Key))
            {
                SendChatMessage($"@{p_Message.Sender.UserName} Invalid key!");
                return;
            }

            lock (SongBlackList)
            {
                var l_BeatMap = SongBlackList.Where(x => x.BeatMap.Key.ToLower() == l_Key).FirstOrDefault();
                if (l_BeatMap != null)
                {
                    SendChatMessage($"@{p_Message.Sender.UserName} (bsr {l_BeatMap.BeatMap.Key}) {l_BeatMap.BeatMap.Metadata.SongName} / {l_BeatMap.BeatMap.Metadata.LevelAuthorName} is already blacklisted!");
                    return;
                }
            }

            /// Fetch beatmap
            _ = m_BeatSaver.Key(l_Key).ContinueWith(p_SongTaskResult =>
            {
                try
                {
                    string l_Reply = "@" + p_Message.Sender.UserName + " map " + l_Key + " not found.";
                    if (p_SongTaskResult.Result != null)
                    {
                        var l_BeatMap = p_SongTaskResult.Result;
                        l_Reply       = $"@{p_Message.Sender.UserName} (bsr {l_BeatMap.Key}) {l_BeatMap.Metadata.SongName} / {l_BeatMap.Metadata.LevelAuthorName} is now blacklisted!";

                        lock (SongQueue) { lock (SongHistory) { lock (SongBlackList) {
                                                                    SongQueue.RemoveAll(x => x.BeatMap.Key == l_BeatMap.Key);
                                                                    SongHistory.RemoveAll(x => x.BeatMap.Key == l_BeatMap.Key);

                                                                    /// Add to blacklist
                                                                    SongBlackList.RemoveAll(x => x.BeatMap.Hash == l_BeatMap.Hash);
                                                                    SongBlackList.Insert(0, new SongEntry()
                                    {
                                        BeatMap = l_BeatMap, NamePrefix = "🗡", RequesterName = p_Message.Sender.DisplayName
                                    });
                                                                } } }

                        /// Update request manager
                        OnQueueChanged();
                    }

                    SendChatMessage(l_Reply);
                }
                catch (System.Exception p_Exception)
                {
                    Logger.Instance.Error("Command_Block");
                    Logger.Instance.Error(p_Exception);
                }
            });
        }