コード例 #1
0
        public QuestionPageViewModel(QuestionModel question)
        {
            _question = question;

            this.Answers = new List<AnswerViewModel>();
            foreach (var answer in question.Answers)
            {
                this.Answers.Add(new AnswerViewModel(answer, question.Answers.IndexOf(answer)));
            }
        }
コード例 #2
0
        private QuestionPageViewModel SetUpQuestionPageViewModel()
        {
            const int MAX_FOLLOWEES_TO_USE_FOR_ANSWERS = 4;
            const int MAX_TWEETS_TO_RETRIEVE_FOR_FOLLOWEE = 50;
            const int MAX_FOLLOWEES_TO_RETRIEVE_FOR_USER = 100;

            string screenName = UserStateRepository.User.TwitterUserName;
            string friendsListUrlFormat = ConfigurationManager.AppSettings["twitterFriendsListUrlFormat"];

            try {
                var twitterHelper = UserStateRepository.GetTwitterHelper();

                //Get a list of followees (called "friends" in the API) to work with
                var friendsListRequestSettings = new FriendsListRequestSettings {
                    ScreenName = screenName,
                    Count = MAX_FOLLOWEES_TO_RETRIEVE_FOR_USER,
                    IncludeUserEntities = false,
                    SkipStatus = true,
                    RequestUrlFormat = friendsListUrlFormat
                };

                var friendsListJson = twitterHelper.GetFriendsListJson(friendsListRequestSettings);
                var friendsListItems = JsonConvert.DeserializeObject<FriendsList>(friendsListJson);

                //If user does not have at least four followees, he can't play
                if (friendsListItems == null || friendsListItems.Users == null || friendsListItems.Users.Count == 0) {
                    //todo: Handle this exception somewhere up the call stack
                    throw new Exception("You must be following at least four people to play.");
                }

                //Randomly select four distinct followees
                int followeesCount = friendsListItems.Users.Count;
                var randomFollowees = new List<User>();

                Random random = new Random();

                for (int i = 0; i < MAX_FOLLOWEES_TO_USE_FOR_ANSWERS; i++) {
                    bool distinctRandomUserFound = false;
                    while (!distinctRandomUserFound) {
                        int randomIndex = random.Next(0, followeesCount);
                        var followee = friendsListItems.Users[randomIndex];
                        if (!randomFollowees.Contains(followee)) {
                            randomFollowees.Add(followee);
                            distinctRandomUserFound = true;
                        }
                    }
                }

                //Randomly choose one of the random followees to be the one with the correct answer
                int randomIndexForCorrectAnswerFollowee = random.Next(0, MAX_FOLLOWEES_TO_USE_FOR_ANSWERS);
                var correctAnswerFollowee = randomFollowees[randomIndexForCorrectAnswerFollowee];
                var questionModel = new QuestionModel {
                    Answers = new List<AnswerModel>(),
                    QuestionUserName = correctAnswerFollowee.ScreenName
                };

                //Get a random tweet from the "correct" followee and add it to the question model
                string randomTweetForCorrectAnswerFollowee = GetRandomTweetForUser(correctAnswerFollowee.ScreenName, MAX_TWEETS_TO_RETRIEVE_FOR_FOLLOWEE);
                questionModel.Text = randomTweetForCorrectAnswerFollowee;

                //Set up the list of answers (followees' screen names) and randomize their order
                questionModel.Answers = randomFollowees.Select(x => new AnswerModel { UserName = x.ScreenName }).ToList();
                questionModel.Answers.Shuffle();

                //Package it all up in a QuestionPageViewModel
                var questionPageViewModel = new QuestionPageViewModel(questionModel) {
                    Score = UserStateRepository.User.Score
                };

                return questionPageViewModel;
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
                throw ex;
            }
        }