예제 #1
0
        public async Task <ExamResult> Pass(ChatIO chatIo, UsersWordsService service, UserWordModel word,
                                            UserWordModel[] examList)
        {
            var variants = examList.Randomize().SelectMany(e => e.GetTranslations()).ToArray();

            var msg = $"=====>   {word.Word}    <=====\r\n" +
                      $"Choose the translation";
            await chatIo.SendMessageAsync(msg, InlineButtons.CreateVariants(variants));

            var choice = await chatIo.TryWaitInlineIntKeyboardInput();

            if (choice == null)
            {
                return(ExamResult.Retry);
            }

            if (word.GetTranslations().Contains(variants[choice.Value]))
            {
                await service.RegisterSuccess(word);

                return(ExamResult.Passed);
            }

            await service.RegisterFailure(word);

            return(ExamResult.Failed);
        }
예제 #2
0
        public async Task <ExamResult> Pass(
            ChatIO chatIo,
            UsersWordsService service,
            UserWordModel word,
            UserWordModel[] examList)
        {
            if (!word.HasAnyPhrases)
            {
                return(ExamResult.Impossible);
            }

            var targetPhrase = word.GetRandomExample();

            var otherExamples = examList
                                .SelectMany(e => e.Phrases)
                                .Where(p => p != targetPhrase)
                                .Take(8).ToArray();

            if (!otherExamples.Any())
            {
                return(ExamResult.Impossible);
            }

            var variants = otherExamples
                           .Append(targetPhrase)
                           .Randomize()
                           .Select(e => e.TranslatedPhrase)
                           .ToArray();

            var msg = $"=====>   {targetPhrase.OriginPhrase}    <=====\r\n" +
                      $"Choose the translation";
            await chatIo.SendMessageAsync(msg, InlineButtons.CreateVariants(variants));

            var choice = await chatIo.TryWaitInlineIntKeyboardInput();

            if (choice == null)
            {
                return(ExamResult.Retry);
            }

            if (variants[choice.Value] == targetPhrase.TranslatedPhrase)
            {
                await service.RegisterSuccess(word);

                return(ExamResult.Passed);
            }
            await service.RegisterFailure(word);

            return(ExamResult.Failed);
        }
예제 #3
0
        public async Task <ExamResult> Pass(ChatIO chatIo, UsersWordsService service, UserWordModel word, UserWordModel[] examList)
        {
            if (!word.Phrases.Any())
            {
                return(ExamResult.Impossible);
            }

            var phrase = word.GetRandomExample();

            var replaced = phrase.OriginPhrase.Replace(phrase.OriginWord, "...");

            if (replaced == phrase.OriginPhrase)
            {
                return(ExamResult.Impossible);
            }

            var sb = new StringBuilder();

            sb.AppendLine($"\"{phrase.TranslatedPhrase}\"");
            sb.AppendLine();
            sb.AppendLine($" translated as ");
            sb.AppendLine();
            sb.AppendLine($"\"{replaced}\"");
            sb.AppendLine($"Choose missing word...");

            var variants = examList.Randomize().Select(e => e.Word).ToArray();
            var _        = chatIo.SendMessageAsync(sb.ToString(), InlineButtons.CreateVariants(variants));

            var choice = await chatIo.TryWaitInlineIntKeyboardInput();

            if (choice == null)
            {
                return(ExamResult.Retry);
            }

            if (variants[choice.Value] == word.Word)
            {
                await service.RegisterSuccess(word);

                return(ExamResult.Passed);
            }

            await chatIo.SendMessageAsync($"Origin was: \"{phrase.OriginPhrase}\"");

            await service.RegisterFailure(word);

            return(ExamResult.Failed);
        }
예제 #4
0
        public async Task<ExamResult> Pass(ChatIO chatIo, UsersWordsService service, UserWordModel word, UserWordModel[] examList)
        {
            if (!word.Phrases.Any())
                return ExamResult.Impossible;
            
            var targetPhrase = word.GetRandomExample();

            var other = examList.SelectMany(e => e.Phrases)
                .Where(p => !string.IsNullOrWhiteSpace(p?.OriginPhrase) && p!= targetPhrase)
                .Take(8).ToArray();

            if(!other.Any())
                return ExamResult.Impossible;

            var variants = other
                .Append(targetPhrase)
                .Randomize()
                .Select(e => e.OriginPhrase)
                .ToArray();
            
            var msg = $"=====>   {targetPhrase.TranslatedPhrase}    <=====\r\n" +
                      $"Choose the translation";
            await chatIo.SendMessageAsync(msg,
                variants.Select((v, i) => new InlineKeyboardButton
                {
                    CallbackData = i.ToString(),
                    Text = v
                }).ToArray());
            
            var choice = await chatIo.TryWaitInlineIntKeyboardInput();
            if (choice == null)
                return ExamResult.Retry;
            
            if (variants[choice.Value] == targetPhrase.OriginPhrase)
            {
                await service.RegisterSuccess(word);
                return ExamResult.Passed;
            }
            await service.RegisterFailure(word);
            return ExamResult.Failed;
        }
예제 #5
0
        private async Task <bool> EnterSingleWordAsync(User user, string?word = null)
        {
            if (word == null)
            {
                await _chatIo.SendMessageAsync("Enter english word", new InlineKeyboardButton
                {
                    CallbackData = "/start",
                    Text         = "Cancel"
                });

                while (true)
                {
                    var input = await _chatIo.WaitUserInputAsync();

                    if (input.CallbackQuery != null && input.CallbackQuery.Data == "/start")
                    {
                        throw new ProcessInterruptedWithMenuCommand("/start");
                    }

                    if (!string.IsNullOrEmpty(input.Message?.Text))
                    {
                        word = input.Message.Text;
                        break;
                    }
                }
            }

            //find word in local dictionary(if not, find it in Ya dictionary)
            var translations = await _addWordService.FindInDictionaryWithoutExamples(word);

            if (!translations.Any())
            {
                translations = await _addWordService.TranslateAndAddToDictionary(word);
            }
            if (translations?.Any() != true)
            {
                await _chatIo.SendMessageAsync("No translations found. Check the word and try again");

                return(true);
            }

            await _chatIo.SendMessageAsync($"Choose translation for '{word}'",
                                           InlineButtons.CreateVariantsWithCancel(translations.Select(t => t.RuWord)));

            await _addWordService.RegistrateTranslationRequest(user);

            while (true)
            {
                var input = await _chatIo.TryWaitInlineIntKeyboardInput();

                if (!input.HasValue)
                {
                    return(false);
                }
                if (input !.Value < 0 || input.Value >= translations.Count)
                {
                    continue;
                }

                var selected = translations[input.Value];
                await _addWordService.AddWordsToUser(user, new[] { selected });

                if (selected.Examples.Count > 0)
                {
                    await _chatIo.SendMessageAsync($"Saved. Examples: {selected.Examples.Count}");
                }
                else
                {
                    await _chatIo.SendMessageAsync($"Saved.");
                }
                return(true);
            }
        }