Пример #1
0
        public static void EnsureCategoryCorrespondance(Q20GameState state, string messageText)
        {
            var commands = Correspondance.CorrespondingCommands[state];

            if (!commands.Contains(messageText))
            {
                throw new ArgumentException($"Invalid command {messageText} for category {state}");
            }
        }
Пример #2
0
        private async Task ResolveAndExecute(Message message)
        {
            //A-z
            var messageText = new string(message.Text.Where(x => x <= 122 && x >= 32).ToArray()).Trim();

            var currentSession = context.Sessions.FirstOrDefault(x => x.ChatId == message.Chat.Id);

            if (currentSession == null)
            {
                if (Q20GameBotKeywords.StartKeywords.Contains(messageText))
                {
                    context.Sessions.Add(new Session()
                    {
                        ChatId          = message.Chat.Id,
                        LastRequestMade = DateTime.Now,
                    });

                    CurrentPages[message.Chat.Id] = await Q20GameBotTools.GetNewGamePageAsync();

                    await context.SaveChangesAsync();

                    await BotClient.SendTextMessageAsync(message.Chat.Id, Q20GameBotMessages.GameStartMessage);

                    await BotClient.SendTextMessageAsync(message.Chat.Id, "**Q1.  Is it classified as Animal, Vegetable or Mineral?**",
                                                         replyMarkup : Q20GameBotKeyboards.ChooseCategoryKeyboard, parseMode : ParseMode.Markdown);
                }
                else
                {
                    await BotClient.SendTextMessageAsync(message.Chat.Id, "Wrong message - shoul've been Start");
                }


                return;
            }

            if (string.Equals(messageText, Q20GameBotKeywords.ExitKeywoard, StringComparison.InvariantCultureIgnoreCase))
            {
                context.Sessions.Remove(currentSession);
                await context.SaveChangesAsync();

                await BotClient.SendTextMessageAsync(message.Chat.Id,
                                                     Q20GameBotMessages.ExitMessage);

                await BotClient.SendTextMessageAsync(message.Chat.Id,
                                                     Q20GameBotMessages.MainMenuMessage,
                                                     replyMarkup : Q20GameBotKeyboards.MainMenuKeyboard);

                return;
            }

            if (DateTime.Now - currentSession.LastRequestMade > TimeSpan.FromMinutes(5))
            {
                context.Sessions.Remove(currentSession);
                await BotClient.SendTextMessageAsync(message.Chat.Id,
                                                     Q20GameBotMessages.SessionExpiredMessage);

                await BotClient.SendTextMessageAsync(message.Chat.Id,
                                                     Q20GameBotMessages.MainMenuMessage,
                                                     replyMarkup : Q20GameBotKeyboards.MainMenuKeyboard);

                await context.SaveChangesAsync();

                return;
            }

            HtmlDocument currentPage  = CurrentPages[currentSession.ChatId];
            Q20GameState currentState = Q20GameBotTools.ResolveGameState(currentPage);

            try
            {
                Q20GameBotTools.EnsureCategoryCorrespondance(currentState, messageText);
            }
            catch (ArgumentException)
            {
                await BotClient.SendTextMessageAsync(message.Chat.Id, Q20GameBotMessages.NotUnderstoodMessage);

                return;
            }

            string nextUri      = Q20GameBotTools.GetNextUri(currentPage, messageText);
            var    nextGamePage = await Q20GameBotTools.GetGamePageAsync(nextUri);

            var nextGamePageState = Q20GameBotTools.ResolveGameState(nextGamePage);

            CurrentPages[currentSession.ChatId] = nextGamePage;

            string question = HtmlEntity.DeEntitize(Q20GameBotTools.GetQuestion(nextGamePage)).Trim();

            await BotClient.SendTextMessageAsync(message.Chat.Id, question,
                                                 replyMarkup : Correspondance.CorrespondingKeyboards[nextGamePageState]);

            if (nextGamePageState == Q20GameState.GameFinish)
            {
                context.Sessions.Remove(currentSession);
            }
            else
            {
                currentSession.LastRequestMade = DateTime.Now;
            }

            await context.SaveChangesAsync();
        }