Пример #1
0
        /// <summary>
        /// Gets User from chat message
        /// </summary>
        /// <param name="chatLine">Full chat line</param>
        /// <returns>Sender user</returns>
        private User GetUserFromChat(string chatLine)
        {
            User sender = _usersManager.JoinUser(chatLine);

            _experienceManager.CheckUserRankUp(sender);
            sender.ApplyBadges(Parsing.ParseBadges(chatLine));
            return(sender);
        }
Пример #2
0
        private string ExperienceHandler(User sender, Command command, List <string> args)
        {
            switch (command.CommandHandlerType)
            {
            // !rank    | 0 input args
            case CommandHandlerType.Info:
                try
                {
                    if (sender == null)
                    {
                        throw new NullReferenceException("Something went wrong, sender is null");
                    }

                    // Success: 5 params: {0:User} {1:rank level} {2: rank name} {3: user's XP} {4: XP needed to next level}
                    var xpToNextRank = _experienceManager.NextUserRank(sender)?.ExpRequired.ToString() ?? "??";
                    return(string.Format(command.SuccessMessage,
                                         sender.DisplayName,
                                         sender.UserRank.RankLevel,
                                         sender.UserRank.RankName,
                                         sender.Experience,
                                         xpToNextRank));
                }
                catch (Exception ex)
                {
                    // Fail: 4 params: {0:User} {1:rank level} {2: rank name} {3: user's XP}
                    _logger.LogError("Critical error occurred during rank command.\n{ex}", ex);

                    if (sender == null)
                    {
                        return(String.Format(command.FailMessage, "N/A", "N/A", "N/A", "N/A"));
                    }

                    return(String.Format(command.FailMessage, sender.DisplayName, sender.UserRank.RankLevel, sender.UserRank.RankName, sender.Experience));
                }

            case CommandHandlerType.InfoSecond:
                // !nextrank   | 0 input args
                try
                {
                    if (sender == null)
                    {
                        throw new NullReferenceException("Something went wrong, sender is null");
                    }

                    Rank nextRank = _experienceManager.NextUserRank(sender);
                    if (nextRank == null)
                    {
                        throw new RanksException("User's next rank is null");
                    }

                    // Success: 5 params: {0:User} {1:next rank level} {2:next rank name} {3: XP needed to next level} {4: time to next rank}
                    return(String.Format(command.SuccessMessage, sender.DisplayName,
                                         nextRank.RankLevel,
                                         nextRank.RankName,
                                         _experienceManager.ToNextUserRank(sender),
                                         _experienceManager.TimeToNextUserRank(sender, _experienceManager.ActiveExperienceReward, _experienceManager.ExperienceTickInterval)));
                }
                catch (RanksException ex)
                {
                    // Fail: 4 params: {0:User} {1:next rank level} {2:next rank name} {3: user's XP}
                    _logger.LogError("Error occurred during rank information command.\n{ex}", ex);

                    if (sender == null)
                    {
                        return(String.Format(command.FailMessage, "N/A", "N/A", "N/A", "N/A"));
                    }

                    Rank nextRank = _experienceManager.NextUserRank(sender);
                    return(String.Format(command.FailMessage, sender.DisplayName,
                                         nextRank != null ? nextRank.RankLevel.ToString() : "N/A",
                                         nextRank != null ? nextRank.RankName : "N/A", sender.Experience));
                }
                catch (Exception ex)
                {
                    // Fail: 4 params: {0:User} {1:next rank level} {2:next rank name} {3: user's XP}
                    _logger.LogError("Critical error occurred during rank information command.\n{ex}", ex);

                    if (sender == null)
                    {
                        return(String.Format(command.FailMessage, "N/A", "N/A", "N/A", "N/A"));
                    }

                    Rank nextRank = _experienceManager.NextUserRank(sender);
                    return(String.Format(command.ErrorMessage, sender.DisplayName,
                                         nextRank != null ? nextRank.RankLevel.ToString() : "N/A",
                                         nextRank != null ? nextRank.RankName : "N/A", sender.Experience));
                }

            case CommandHandlerType.Add:
                // Add experience,  !addexp 500 Bukk94 |  2 input args {0:Number of points} {1:User to which add points}
                User receiver = null;
                try
                {
                    if (args.Count < 2)
                    {
                        throw new IndexOutOfRangeException($"Command {command.CommandFormat} should contain some arguments. Found {args.Count}.");
                    }

                    if (long.Parse(args[0]) <= 0)
                    {     // Can't add 0 or negative amount
                        command.ResetCommandCooldown();
                        return("");
                    }

                    receiver = _usersManager.FindOnlineUser(args[1]);
                    if (receiver == null)
                    {     // user is not online
                        _botDataManager.AddUserExperienceToFile(args[1], long.Parse(args[0]));
                    }
                    else
                    {
                        receiver.AddExperience(long.Parse(args[0]));
                        bool newRank = _experienceManager.CheckUserRankUp(receiver);
                        if (newRank && !string.IsNullOrEmpty(_botDataManager.BotDictionary.NewRankMessage))
                        {
                            OnCommandResponse?.Invoke(this, new OnCommandResponseArgs
                            {
                                Message = string.Format(_botDataManager.BotDictionary.NewRankMessage,
                                                        receiver.DisplayName,
                                                        receiver.UserRank.RankLevel,
                                                        receiver.UserRank.RankName)
                            });
                        }

                        _usersManager.SaveData();
                    }

                    _logger.LogInformation("Sucessfully added {total} experience points to {sender}.", args[0], args[1]);

                    // Success: 2 params: {0:User} {1:Number of points}
                    return(String.Format(command.SuccessMessage, args[1], args[0]));
                }
                catch (Exception ex)
                {
                    _logger.LogError("Critical error occurred during experienece addition command.\n{ex}", ex);

                    // Fail: 1 param: {:User to which add points}
                    return(String.Format(receiver != null ? receiver.DisplayName : args[1]));
                }
            }


            return("Unknown experience handler");
        }