Exemple #1
0
        private LearningModeOperator GetLearningModeOperator()
        {
            var fileValidator = new FileValidator(new ValidationData());
            var setService    = new SetService(fileValidator);
            var fileSources   = new List <ISource>();

            for (int i = 0; i < FilePaths.Count; i++)
            {
                string absolutePath = AppSettings.Words != null
                    ? AppSettings.Words.SetsDirectoryPath
                    : AppDomain.CurrentDomain.BaseDirectory;
                if (FilePaths[i].StartsWith('.'))
                {
                    absolutePath = AppDomain.CurrentDomain.BaseDirectory;
                }
                string filePath = Path.Combine(absolutePath, FilePaths[i].TrimStart('.', '\\', '/'));
                SetFileExists(filePath);
                fileSources.Add(new FileSource(filePath));
            }
            var wordsSetOperator     = new WordsSetOperator(setService, fileSources);
            var learningModeOperator = new LearningModeOperator(wordsSetOperator, AppSettings.Learning, Mode);

            return(learningModeOperator);
        }
Exemple #2
0
        public async Task ExecuteAsync(List <string>?args = null)
        {
            VerifyParameters(args);
            VerifyConfiguration();
            ConsoleService.SetVersionInWindowTitle();
            ConsoleService.ClearConsole();
            LearningModeOperator    modeOperator        = GetLearningModeOperator();
            IEnumerable <Question?> questionsEnumerable = modeOperator.GetQuestions(
                randomizeEachIteration: true,
                pieceSize: Mode == ModeEnum.Full ? GetPieceSize() : -1
                );

            foreach (Question?question in questionsEnumerable)
            {
                if (question == null || modeOperator.CurrentQuestionInfo == null)
                {
                    break;
                }
                if (Mode == ModeEnum.Spelling &&
                    !await PronunciationAudioExists(modeOperator.CurrentQuestionInfo.Entry.Words))
                {
                    modeOperator.UpdateAnswersRegister(AppSettings.Learning?.NumOfOpenQuestions ?? 0);
                    continue;
                }
                PlayPronunciationAudio(
                    modeOperator.CurrentQuestionInfo.Entry.Words,
                    WhenEnum.BeforeAnswer,
                    modeOperator.CurrentQuestionInfo.AnswerType
                    );
                ConsoleService.PresentQuestion(
                    Mode,
                    question,
                    new ResultStatus(
                        modeOperator.GetResult(QuestionTypeEnum.Closed),
                        modeOperator.GetNumberOfQuestions(QuestionTypeEnum.Closed)
                        ),
                    new ResultStatus(
                        modeOperator.GetResult(QuestionTypeEnum.Open),
                        modeOperator.GetNumberOfQuestions(QuestionTypeEnum.Open)
                        ),
                    new ResultStatus(
                        modeOperator.GetCurrentQuestionResult(),
                        modeOperator.GetNumberOfCurrentQuestions()
                        )
                    );
                string       answer          = ConsoleService.ReadAnswer();
                AnswerResult?answerResult    = modeOperator.VerifyAnswer(answer);
                bool         isAnswerCorrect = answerResult?.Result ?? false;
                string       givenAnswer     = answerResult?.GivenAnswers == null
                    ? answer
                    : string.Join(", ", answerResult.GivenAnswers);
                string correctAnswer = string.Join(", ", answerResult?.CorrectAnswers ?? new List <string>());
                ConsoleService.PresentResult(
                    Mode,
                    question,
                    new ResultStatus(
                        modeOperator.GetResult(QuestionTypeEnum.Closed),
                        modeOperator.GetNumberOfQuestions(QuestionTypeEnum.Closed)
                        ),
                    new ResultStatus(
                        modeOperator.GetResult(QuestionTypeEnum.Open),
                        modeOperator.GetNumberOfQuestions(QuestionTypeEnum.Open)
                        ),
                    new ResultStatus(
                        modeOperator.GetCurrentQuestionResult(),
                        modeOperator.GetNumberOfCurrentQuestions()
                        ),
                    answer,
                    isAnswerCorrect,
                    correctAnswer,
                    modeOperator.CurrentQuestionInfo.Entry.ToString(Words.Models.EntryPartEnum.Translations)
                    );
                PlayPronunciationAudio(
                    modeOperator.CurrentQuestionInfo.Entry.Words,
                    WhenEnum.AfterAnswer,
                    modeOperator.CurrentQuestionInfo.AnswerType
                    );
                CommandEnum command = ConsoleService.HandleCommand();
                if (command == CommandEnum.Close)
                {
                    break;
                }
                else if (command == CommandEnum.Restart)
                {
                    await ExecuteAsync(args);

                    return;
                }
                else if (command == CommandEnum.Override && !isAnswerCorrect)
                {
                    modeOperator.OverridePreviousMistake();
                }
                await LearningHistoryService.SaveRecordToDbAsync(
                    modeOperator.CurrentQuestionInfo.Entry.SetPath.Namespace,
                    modeOperator.CurrentQuestionInfo.Entry.SetPath.Name,
                    Mode.ToString().ToLower(),
                    question.Content,
                    modeOperator.CurrentQuestionInfo.QuestionType.ToString().ToLower(),
                    givenAnswer,
                    correctAnswer,
                    isAnswerCorrect
                    );

                WriteLog(
                    isAnswerCorrect,
                    question.Content,
                    correctAnswer,
                    new ResultStatus(
                        modeOperator.GetResult(QuestionTypeEnum.Closed),
                        modeOperator.GetNumberOfQuestions(QuestionTypeEnum.Closed)
                        ),
                    new ResultStatus(
                        modeOperator.GetResult(QuestionTypeEnum.Open),
                        modeOperator.GetNumberOfQuestions(QuestionTypeEnum.Open)
                        ),
                    modeOperator.AnswersRegister
                    );
                if (ShouldResetMode(isAnswerCorrect, command))
                {
                    await ExecuteAsync(args);

                    return;
                }
            }

            ConsoleService.ShowSummary();
        }