private void Answer()
        {
            while (!_cancellationTokenSource.IsCancellationRequested)
            {
                Thread.Sleep(5000);

                if (Questions.Count == 0)
                {
                    continue;
                }

                var message = Questions.Dequeue();
                if (string.IsNullOrEmpty(message))
                {
                    Console.WriteLine($"Шарпик: {_aphorismRepository.GetAphorism()}");
                    continue;
                }

                var answerBuilder = new StringBuilder();

                if (_questionRepository.HelloPhrases.Any(phrase => phrase.IsMatch(message)))
                {
                    answerBuilder.AppendLine($"{_helloRepository.GetHelloPhrase()}");
                }

                if (_questionRepository.MyNamePhrases.Any(phrase => phrase.IsMatch(message)))
                {
                    answerBuilder.AppendLine($"{_myNameRepository.GetMyNamePhrase()}");
                }

                if (_questionRepository.JokePhrases.Any(phrase => phrase.IsMatch(message)))
                {
                    answerBuilder.AppendLine($"{_jokesRepository.GetJoke()}");
                }

                if (_questionRepository.DatePhrases.Any(phrase => phrase.IsMatch(message)))
                {
                    answerBuilder.AppendLine($"{DateTime.Now}");
                }

                if (_questionRepository.ByePhrases.Any(phrase => phrase.IsMatch(message)))
                {
                    answerBuilder.AppendLine($"{_byeRepository.GetByePhrase()}");
                    _cancellationTokenSource.Cancel();
                }

                Console.WriteLine(answerBuilder.Length == 0 ? $"Шарпик: {_aphorismRepository.GetAphorism()}" : $"Шарпик: {answerBuilder}");
            }
        }
Exemple #2
0
        public void ReadEventHandler(object sender, EventArgs args)
        {
            var message = (args as ReadMessageEventArgs)?.Message;

            if (string.IsNullOrEmpty(message))
            {
                Console.WriteLine($"Шарпик: {_aphorismRepository.GetAphorism()}");
                return;
            }

            var answerBuilder = new StringBuilder();

            if (_questionRepository.HelloPhrases.Any(phrase => phrase.IsMatch(message)))
            {
                answerBuilder.AppendLine($"{_helloRepository.GetHelloPhrase()}");
            }

            if (_questionRepository.MyNamePhrases.Any(phrase => phrase.IsMatch(message)))
            {
                answerBuilder.AppendLine($"{_myNameRepository.GetMyNamePhrase()}");
            }

            if (_questionRepository.JokePhrases.Any(phrase => phrase.IsMatch(message)))
            {
                answerBuilder.AppendLine($"{_jokesRepository.GetJoke()}");
            }

            if (_questionRepository.DatePhrases.Any(phrase => phrase.IsMatch(message)))
            {
                answerBuilder.AppendLine($"{DateTime.Now}");
            }

            if (_questionRepository.ByePhrases.Any(phrase => phrase.IsMatch(message)))
            {
                answerBuilder.AppendLine($"{_byeRepository.GetByePhrase()}");
                this.EndListen();
            }

            Thread.Sleep(5000);

            Console.WriteLine(answerBuilder.Length == 0 ? $"Шарпик: {_aphorismRepository.GetAphorism()}" : $"Шарпик: {answerBuilder}");
        }