Exemplo n.º 1
0
        private async Task StartGameInternal(string strMaxUsers, string strMaxMinutesToWait, string strSecondsDelayBetweenDays, string strNumWinners, string displayMessage, int testUsers)
        {
            bool cleanupCommandInstance = false;

            try
            {
                Logger.LogInternal("Command " + (testUsers > 0 ? "T" : "") + $"'startGame' executed by '{Context.Message.Author.Username}'");

                if (CheckAccess())
                {
                    BotGameInstance    gameInstance = new BotGameInstance();
                    RunningCommandInfo commandInfo;
                    if (CreateChannelCommandInstance("StartGame", Context.User.Id, Context.Channel.Id, Context.Guild.Id, gameInstance, out commandInfo))
                    {
                        cleanupCommandInstance = true;
                        int maxUsers;
                        int maxMinutesToWait;
                        int secondsDelayBetweenDays;
                        int numWinners;

                        if (Int32.TryParse(strMaxUsers, out maxUsers) == false)
                        {
                            maxUsers = 100;
                        }
                        if (Int32.TryParse(strMaxMinutesToWait, out maxMinutesToWait) == false)
                        {
                            maxMinutesToWait = 5;
                        }
                        if (Int32.TryParse(strSecondsDelayBetweenDays, out secondsDelayBetweenDays) == false)
                        {
                            secondsDelayBetweenDays = 10;
                        }
                        if (Int32.TryParse(strNumWinners, out numWinners) == false)
                        {
                            numWinners = 1;
                        }
                        if (numWinners <= 0)
                        {
                            numWinners = 1;
                        }
                        if (maxMinutesToWait <= 0)
                        {
                            maxMinutesToWait = 1;
                        }
                        if (secondsDelayBetweenDays <= 0)
                        {
                            secondsDelayBetweenDays = 5;
                        }
                        if (maxUsers <= 0)
                        {
                            maxUsers = 1;
                        }


                        SocketGuildUser user = Context.Message.Author as SocketGuildUser;
                        string          userThatStartedGame = user?.Nickname ?? Context.Message.Author.Username;
                        gameInstance.StartGame(numWinners, maxUsers, maxMinutesToWait, secondsDelayBetweenDays, Context.Channel, userThatStartedGame, testUsers);
                        cleanupCommandInstance = false;
                        //await Context.Channel.SendMessageAsync($"MaxUsers: {maxUsers}  MaxMinutesToWait: {maxMinutesToWait} SecondsDelayBetweenDays: {secondsDelayBetweenDays} NumWinners: {numWinners}");
                    }
                    else
                    {
                        try
                        {
                            await LogAndReplyAsync($"The '{commandInfo.CommandName}' command is currently running!.  Can't run this command until that finishes");
                        }
                        catch (Exception ex)
                        {
                            await Logger.Log(new LogMessage(LogSeverity.Error, "StartGameInternal", "Unexpected Exception", ex));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                await Logger.Log(new LogMessage(LogSeverity.Error, "StartGame", "Unexpected Exception", ex));
            }
            finally
            {
                try
                {
                    if (cleanupCommandInstance)
                    {
                        RemoveChannelCommandInstance(Context.Channel.Id);
                    }
                }
                catch (Exception ex)
                {
                    await Logger.Log(new LogMessage(LogSeverity.Error, "StartGame", "Unexpected Exception in Finally", ex));
                }
            }
        }
Exemplo n.º 2
0
 public static bool CreateChannelCommandInstance(string commandName, ulong userId, ulong channelId, ulong guildId, BotGameInstance gameInstance, out RunningCommandInfo instance)
 {
     lock (SyncObj)
     {
         ChannelCommandInstances.TryGetValue(channelId, out instance);
         if (instance == null)
         {
             instance = new RunningCommandInfo(commandName, userId, channelId, guildId, gameInstance);
             ChannelCommandInstances[channelId] = instance;
             return(true);
         }
     }
     return(false);
 }