コード例 #1
0
        public async Task HangmanCommand()
        {
            if (Cache.HangmanInProgress.ContainsKey(Context.Channel.Id))
            {
                await RespondAsync(":game_die: There is a game in progress in this channel!");

                return;
            }

            var logger = _loggerFactory.CreateLogger("Hangman");

            Cache.HangmanInProgress[Context.Channel.Id] = null;

            try
            {
                var card = await YuGiOhDbService.GetRandomCardAsync();

                logger.Info(card.Name);

                var cts            = new CancellationTokenSource();
                var hangmanService = new HangmanService(card.Name);

                var criteria = BaseCriteria
                               .AddCriterion(new NotCommandCriteria(GuildConfig))
                               .AddCriterion(new NotInlineSearchCriteria());

                if (!GuildConfig.HangmanAllowWords)
                {
                    criteria.AddCriterion(new CharacterOnlyCriteria());
                }

                var time = TimeSpan.FromSeconds(GuildConfig.HangmanTime);

                await RespondAsync("You can now type more than a letter for hangman!\n" +
                                   $"As well as change the hangman time ({GuildConfig.Prefix}hangmantime <seconds>)! Ask an admin about it!\n" +
                                   $"You may also disable the ability to input more than one letter! ({GuildConfig.Prefix}hangmanwords <true/false>)\n" +
                                   $"You have **{time.ToPrettyString()}**!\n" +
                                   hangmanService.GetCurrentDisplay());

                var        _    = new Timer((cancelTokenSrc) => (cancelTokenSrc as CancellationTokenSource) !.Cancel(), cts, TimeSpan.FromSeconds(GuildConfig.HangmanTime), Timeout.InfiniteTimeSpan);
                SocketUser user = null;

                do
                {
                    var input = await NextMessageAsync(criteria, token : cts.Token);

                    if (cts.IsCancellationRequested)
                    {
                        break;
                    }

                    user = input.Author;

                    switch (hangmanService.AddGuess(input.Content))
                    {
                    case GuessStatus.Duplicate:
                        await ReplyAsync($"You already guessed `{input}`!\n" +
                                         hangmanService.GetCurrentDisplay());

                        break;

                    case GuessStatus.Nonexistent:
                        await ReplyAsync($"```fix\n{hangmanService.GetHangman()}```\n" +
                                         hangmanService.GetCurrentDisplay());

                        break;

                    case GuessStatus.Accepted:
                        await ReplyAsync(hangmanService.GetCurrentDisplay());

                        break;
                    }
                } while (!cts.IsCancellationRequested && hangmanService.CompletionStatus == CompletionStatus.Incomplete);

                if (cts.IsCancellationRequested)
                {
                    await ReplyAsync($"Time is up! No one won! The card is `{hangmanService.Word}`");
                }
                else
                {
                    switch (hangmanService.CompletionStatus)
                    {
                    case CompletionStatus.Complete:
                        await ReplyAsync($":trophy: The winner is **{(user as SocketGuildUser)!.Nickname ?? user!.Username}**!");

                        break;

                    case CompletionStatus.Hanged:
                        await ReplyAsync($"You have been hanged! The card was `{hangmanService.Word}`.");

                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, "There was a problem with hangman");
            }
            finally
            {
                Cache.HangmanInProgress.TryRemove(Context.Channel.Id, out _);
            }
        }