Ejemplo n.º 1
0
        public virtual async Task MessageReceivedAsync(IDialogContext context,
                                                       IAwaitable <IMessageActivity> argument)
        {
            var message = await argument;

            int answer;

            if (int.TryParse(message.Text, out answer))
            {
                _gameInstance.ApplyAnswer(answer);
            }
            else
            {
                _gameInstance.ApplyAnswer(null);
            }

            if (_gameInstance.HasMore())
            {
                var nextQuestion = _gameInstance.NextQuestion();

                await context.PostAsync(nextQuestion.QuestionText);
            }
            else
            {
                await context.PostAsync("Game is over");

                var summary = _gameInstance.GetSummary();

                _gameMathRankingsRepository = new GameMathRankingsRepository();

                _gameMathRankingsRepository.Save(summary);

                var rankings = _gameMathRankingsRepository.GetAll().ToList();

                context.Done(new GameMathStats((int)summary.GameTime.TotalSeconds,
                                               summary.QuestionsCount,
                                               summary.Score,
                                               rankings
                                               .Select(r => new GameMathRanking()
                {
                    Score    = r.Score,
                    Seconds  = (int)(r.EndDateTime - r.StartDateTime).TotalSeconds,
                    UserName = r.UserName,
                    Date     = r.EndDateTime
                })
                                               .OrderByDescending(r => r.Score)
                                               .ThenBy(r => r.Seconds)
                                               .Take(10)));
            }
        }