Ejemplo n.º 1
0
        public override void ExecuteCommand(EvtChatCommandArgs args)
        {
            List <string> arguments = args.Command.ArgumentsAsList;

            long       curInputMode = DataHelper.GetSettingInt(SettingsConstants.INPUT_MODE, 0L);
            InputModes inpMode      = (InputModes)curInputMode;

            //Check the number of votes
            if (arguments.Count == 0)
            {
                InputModeVoteRoutine inpModeVoteRoutine = RoutineHandler.FindRoutine <InputModeVoteRoutine>();

                if (inpModeVoteRoutine == null)
                {
                    QueueMessage($"There is no voting in progress for a new input mode. To start one up, pass a mode as an argument: {CachedInputModesStr}");
                }
                else
                {
                    StringBuilder stringBuilder = new StringBuilder(128);

                    //Show the number of votes for each mode
                    Dictionary <InputModes, long> curVotes = inpModeVoteRoutine.GetVotesPerMode();
                    foreach (KeyValuePair <InputModes, long> kvPair in curVotes)
                    {
                        stringBuilder.Append(kvPair.Key.ToString()).Append(' ').Append('=');
                        stringBuilder.Append(' ').Append(kvPair.Value).Append(' ').Append('|').Append(' ');
                    }

                    stringBuilder.Remove(stringBuilder.Length - 3, 3);
                    stringBuilder.Append(". To vote for a new input mode, pass one as an argument: ").Append(CachedInputModesStr);

                    int charLimit = (int)DataHelper.GetSettingInt(SettingsConstants.BOT_MSG_CHAR_LIMIT, 500L);

                    QueueMessageSplit(stringBuilder.ToString(), charLimit, "| ");
                }

                return;
            }

            //Invalid number of arguments
            if (arguments.Count > 1)
            {
                QueueMessage(UsageMessage);
                return;
            }

            using (BotDBContext context = DatabaseManager.OpenContext())
            {
                //Check if the user has the ability to vote
                User user = DataHelper.GetUserNoOpen(args.Command.ChatMessage.Username, context);

                if (user != null && user.HasEnabledAbility(PermissionConstants.VOTE_INPUT_MODE_ABILITY) == false)
                {
                    QueueMessage("You don't have the ability to vote for a new input mode!");
                    return;
                }
            }

            //Check if the cooldown is up
            DateTime nowUTC = DateTime.UtcNow;

            //Check the last completed time
            string lastComplete = DataHelper.GetSettingString(SettingsConstants.INPUT_MODE_NEXT_VOTE_DATE, DataHelper.GetStrFromDateTime(DateTime.UnixEpoch));

            if (DateTime.TryParse(lastComplete, out DateTime lastCompleteDate) == false)
            {
                lastCompleteDate = DateTime.UnixEpoch;
                TRBotLogger.Logger.Warning($"Failed to parse DateTime: {DataHelper.GetStrFromDateTime(lastCompleteDate)}");
            }

            if (nowUTC < lastCompleteDate)
            {
                QueueMessage($"Input mode voting is on cooldown until {lastCompleteDate}");
                return;
            }

            string inputModeStr = arguments[0];

            //Parse
            if (EnumUtility.TryParseEnumValue(inputModeStr, out InputModes parsedInputMode) == false)
            {
                QueueMessage($"Please enter a valid input mode: {CachedInputModesStr}");
                return;
            }

            bool commencedNewVote = false;

            //Get the routine
            InputModeVoteRoutine inputModeVoteRoutine = RoutineHandler.FindRoutine <InputModeVoteRoutine>();

            //Add the routine if it doesn't exist
            if (inputModeVoteRoutine == null)
            {
                long voteDur = DataHelper.GetSettingInt(SettingsConstants.INPUT_MODE_VOTE_TIME, 60000L);
                inputModeVoteRoutine = new InputModeVoteRoutine(voteDur);
                RoutineHandler.AddRoutine(inputModeVoteRoutine);

                commencedNewVote = true;
            }

            //Check for tallying
            if (inputModeVoteRoutine.TallyingCommenced == true)
            {
                QueueMessage("Too late! Voting has ended and tallying has already begun!");
                return;
            }

            string userName = args.Command.ChatMessage.Username.ToLowerInvariant();

            //Add the vote
            inputModeVoteRoutine.AddModeVote(userName, parsedInputMode, out bool voteChanged);

            if (commencedNewVote == false)
            {
                if (voteChanged == true)
                {
                    QueueMessage($"{userName} changed their vote to {parsedInputMode}!");
                }
                else
                {
                    QueueMessage($"{userName} voted for {parsedInputMode}!");
                }
            }
            else
            {
                QueueMessage($"Voting for changing the input mode has begun! {userName} voted for {parsedInputMode}!");
            }
        }
        public override void ExecuteCommand(EvtChatCommandArgs args)
        {
            List <string> arguments = args.Command.ArgumentsAsList;

            //Ignore with too few arguments
            if (arguments.Count > 1)
            {
                QueueMessage(UsageMessage);
                return;
            }

            if (arguments.Count == 0)
            {
                long   periodicInputVal = DataHelper.GetSettingInt(SettingsConstants.PERIODIC_INPUT_ENABLED, 0L);
                string enabledStr       = (periodicInputVal <= 0) ? "disabled" : "enabled";

                QueueMessage($"The periodic input is currently {enabledStr}. To change the periodic input enabled state, pass either \"true\" or \"false\" as an argument.");
                return;
            }

            //Check for sufficient permissions
            using (BotDBContext context = DatabaseManager.OpenContext())
            {
                User user = DataHelper.GetUserNoOpen(args.Command.ChatMessage.Username, context);
                if (user == null || user.HasEnabledAbility(PermissionConstants.SET_PERIODIC_INPUT_ABILITY) == false)
                {
                    QueueMessage("You do not have the ability to change the periodic input state!");
                    return;
                }
            }

            string newStateStr = arguments[0].ToLowerInvariant();

            if (bool.TryParse(newStateStr, out bool newState) == false)
            {
                QueueMessage("Invalid argument. To enable or disable the periodic input, pass either \"true\" or \"false\" as an argument.");
                return;
            }

            long newVal   = (newState == true) ? 1L : 0L;
            bool oldState = false;

            using (BotDBContext context = DatabaseManager.OpenContext())
            {
                Settings periodicInput = DataHelper.GetSettingNoOpen(SettingsConstants.PERIODIC_INPUT_ENABLED, context);
                if (periodicInput == null)
                {
                    periodicInput = new Settings(SettingsConstants.PERIODIC_INPUT_ENABLED, string.Empty, 0L);
                    context.SettingCollection.Add(periodicInput);

                    context.SaveChanges();
                }

                //Same value - don't make changes
                if ((periodicInput.ValueInt > 0 && newVal > 0) || (periodicInput.ValueInt <= 0 && newVal <= 0))
                {
                    QueueMessage("The periodic input state is already this value!");
                    return;
                }

                oldState = (periodicInput.ValueInt > 0) ? true : false;

                periodicInput.ValueInt = newVal;

                context.SaveChanges();
            }

            //Now add or remove the routine if we should
            if (oldState == true && newState == false)
            {
                RoutineHandler.RemoveRoutine(RoutineConstants.PERIODIC_INPUT_ROUTINE_ID);
            }
            //Add the routine
            else if (oldState == false && newState == true)
            {
                BaseRoutine routine = RoutineHandler.FindRoutine(RoutineConstants.PERIODIC_INPUT_ROUTINE_ID, out int indexFound);
                if (routine == null)
                {
                    RoutineHandler.AddRoutine(new PeriodicInputRoutine());
                }
                else
                {
                    QueueMessage("Huh? The periodic input routine is somehow already running even though it was just enabled.");
                }
            }

            if (newState == true)
            {
                QueueMessage("Enabled periodic inputs. Every now and then, an input sequence will be automatically performed.");
            }
            else
            {
                QueueMessage("Disabled periodic inputs.");
            }
        }
        public override void ExecuteCommand(EvtChatCommandArgs args)
        {
            List <string> arguments = args.Command.ArgumentsAsList;

            long       curInputMode = DataHelper.GetSettingInt(SettingsConstants.INPUT_MODE, 0L);
            InputModes inpMode      = (InputModes)curInputMode;

            //See the input mode
            if (arguments.Count == 0)
            {
                QueueMessage($"The current input mode is {inpMode}. To set the input mode, add one as an argument: {CachedInputModesStr}");
                return;
            }

            //Invalid number of arguments
            if (arguments.Count > 1)
            {
                QueueMessage(UsageMessage);
                return;
            }

            using (BotDBContext context = DatabaseManager.OpenContext())
            {
                //Check if the user has the ability to set the mode
                User user = DataHelper.GetUserNoOpen(args.Command.ChatMessage.Username, context);

                if (user != null && user.HasEnabledAbility(PermissionConstants.SET_INPUT_MODE_ABILITY) == false)
                {
                    QueueMessage("You don't have the ability to set the input mode!");
                    return;
                }
            }

            string inputModeStr = arguments[0];

            //Parse
            if (EnumUtility.TryParseEnumValue(inputModeStr, out InputModes parsedInputMode) == false)
            {
                QueueMessage($"Please enter a valid input mode: {CachedInputModesStr}");
                return;
            }

            //Same mode
            if (parsedInputMode == inpMode)
            {
                QueueMessage($"The current input mode is already {inpMode}!");
                return;
            }

            using (BotDBContext context = DatabaseManager.OpenContext())
            {
                //Set the value and save
                Settings inputModeSetting = DataHelper.GetSettingNoOpen(SettingsConstants.INPUT_MODE, context);
                inputModeSetting.ValueInt = (long)parsedInputMode;

                context.SaveChanges();
            }

            QueueMessage($"Changed the input mode from {inpMode} to {parsedInputMode}!");

            //If we set it to Anarchy, check if the Democracy routine is active and remove it if so
            if (parsedInputMode == InputModes.Anarchy)
            {
                BaseRoutine democracyRoutine = RoutineHandler.FindRoutine(RoutineConstants.DEMOCRACY_ROUTINE_ID, out int indexFound);
                if (democracyRoutine != null)
                {
                    RoutineHandler.RemoveRoutine(indexFound);
                }
            }
            //If we set it to Democracy, add the routine if it's not already active
            else if (parsedInputMode == InputModes.Democracy)
            {
                DemocracyRoutine democracyRoutine = RoutineHandler.FindRoutine <DemocracyRoutine>();

                if (democracyRoutine == null)
                {
                    long votingTime = DataHelper.GetSettingInt(SettingsConstants.DEMOCRACY_VOTE_TIME, 10000L);

                    democracyRoutine = new DemocracyRoutine(votingTime);
                    RoutineHandler.AddRoutine(democracyRoutine);
                }
            }
        }
        public override void ExecuteCommand(EvtChatCommandArgs args)
        {
            List <string> arguments = args.Command.ArgumentsAsList;

            if (arguments.Count != 1)
            {
                QueueMessage("Please specify a bet amount!");
                return;
            }

            string creditsName = DataHelper.GetCreditsName();
            string userName    = args.Command.ChatMessage.Username;

            //Check if we can parse the bet amount
            long betAmount = -1L;
            bool success   = long.TryParse(arguments[0], out betAmount);

            if (success == false || betAmount <= 0)
            {
                QueueMessage($"Please specify a positive whole number of {creditsName.Pluralize(false, 0)} greater than 0!");
                return;
            }

            using (BotDBContext context = DatabaseManager.OpenContext())
            {
                //Check if the user exists
                User user = DataHelper.GetUserNoOpen(userName, context);
                if (user == null)
                {
                    QueueMessage("The user calling this does not exist in the database!");
                    return;
                }

                //No ability to enter group bets
                if (user.HasEnabledAbility(PermissionConstants.GROUP_BET_ABILITY) == false)
                {
                    QueueMessage("You do not have the ability to participate in a group bet!");
                    return;
                }

                //Validate credit amount
                if (user.Stats.Credits < betAmount)
                {
                    QueueMessage($"You don't have enough {creditsName.Pluralize(false, 0)} to bet this much!");
                    return;
                }

                if (user.IsOptedOut == true)
                {
                    QueueMessage("You can't participate in the group bet since you opted out of bot stats.");
                    return;
                }
            }

            //Get total time and minimum participants required
            long groupBetTime     = DataHelper.GetSettingInt(SettingsConstants.GROUP_BET_TOTAL_TIME, 120000L);
            int  groupBetMinUsers = (int)DataHelper.GetSettingInt(SettingsConstants.GROUP_BET_MIN_PARTICIPANTS, 3L);

            //Get the routine
            GroupBetRoutine groupBetRoutine = RoutineHandler.FindRoutine <GroupBetRoutine>();

            //We haven't started the group bet, so start it up
            if (groupBetRoutine == null)
            {
                groupBetRoutine = new GroupBetRoutine(groupBetTime, groupBetMinUsers);
                RoutineHandler.AddRoutine(groupBetRoutine);
            }

            //Note: From hereon, use the routine's total time and min participant values
            //The database values could have changed in between calls of this command
            //and thus wouldn't be applicable to the group bet created for the first participant

            //See if the user is already in the group bet
            bool prevParticipant = groupBetRoutine.TryGetParticipant(userName,
                                                                     out GroupBetRoutine.ParticipantData participantData);

            groupBetRoutine.AddOrUpdateParticipant(userName, betAmount);

            string message = string.Empty;

            //Newly added since they were not previously there
            if (prevParticipant == false)
            {
                message = $"{userName} entered the group bet with {betAmount} {creditsName.Pluralize(false, betAmount)}!";

                int participantCount = groupBetRoutine.ParticipantCount;

                if (participantCount < groupBetRoutine.MinParticipants)
                {
                    int diff = groupBetRoutine.MinParticipants - groupBetRoutine.ParticipantCount;
                    message += $" {diff} more user(s) are required to start the group bet!";
                }

                QueueMessage(message);

                //Check if we have enough participants now
                if (participantCount == groupBetRoutine.MinParticipants)
                {
                    TimeSpan timeSpan = TimeSpan.FromMilliseconds(groupBetRoutine.MillisecondsForBet);

                    QueueMessage($"The group bet has enough participants and will start in {timeSpan.Minutes} minute(s) and {timeSpan.Seconds} second(s), so join before then if you want in!");
                }
            }
            else
            {
                QueueMessage($"{userName} adjusted their group bet from {participantData.ParticipantBet} to {betAmount} {creditsName.Pluralize(false, betAmount)}!");
            }
        }