示例#1
0
        public async Task HandleMessage(TelegramBotClient bot, Message message)
        {
            //Ensure only one message can be handled at a time
            using (var disposableSemaphore = await _semaphoreSlim.DisposableWaitAsync())
            {
                var msg = message.Text;

                if (!BotActive)
                {
                    switch (msg)
                    {
                    case "/start":
                        BotActive = true;
                        await bot.SendTextMessageAsync(ChatId, "Bot is now active");

                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    if (msg.Equals("/stop"))
                    {
                        BotActive   = false;
                        CurrentGame = null;
                        await bot.SendTextMessageAsync(ChatId, "Bot is now inactive");
                    }
                    else if (msg.Equals("/stopgame"))
                    {
                        await bot.SendTextMessageAsync(ChatId, "Stopping game");

                        CurrentGame = null;
                    }
                    else if (msg.StartsWith("/play"))
                    {
                        var chosenWordLists = msg.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).Skip(1);

                        var wordLists = WordListInitiator.GetThese(chosenWordLists);
                        var words     = wordLists.SelectMany(t => t.Words).ToList();

                        if (words.Count == 0)
                        {
                            await DisplayHelp(bot);
                        }
                        else
                        {
                            CurrentGame = new HangmanGameState(_imageObtainer, _logger, this, words);
                            await CurrentGame.PrintHang(bot);
                        }
                    }
                    else if (msg.Equals("/words"))
                    {
                        await DisplayWordLists(bot);
                    }
                    else if (msg.Equals("/points"))
                    {
                        var sb = new StringBuilder();
                        sb.AppendLine("Points:");
                        var pointThings = Points.OrderByDescending(t => t.Value);
                        foreach (var point in pointThings)
                        {
                            sb.AppendLine($"{GetName(point.Key)}: {point.Value}");
                        }

                        await bot.SendTextMessageAsync(ChatId, sb.ToString());
                    }
                    else if (msg.Equals("/hint") && CurrentGame != null)
                    {
                        var isNoob = await CurrentGame.GiveHint(bot, msg);

                        if (isNoob)
                        {
                            AddPoints(message.From.Id, -1000);
                        }
                    }
                    else if (msg.Equals("/cheat") && CurrentGame != null && message.From.Id == BotConstants.DevedseId)
                    {
                        await CurrentGame.Cheat(bot);
                    }
                    else if (CurrentGame != null)
                    {
                        var correct = await CurrentGame.HandleGuess(bot, msg);

                        if (correct)
                        {
                            await bot.SendTextMessageAsync(ChatId, $"You f*****g did it {GetName(message.From.Id)}, 10 points to gryffindor");

                            AddPoints(message.From.Id, 10);

                            CurrentGame = null;
                        }
                    }
                }
            }
        }