コード例 #1
0
        public void Assert_ViewCountResult_ctor_is_correct()
        {
            ViewCountResult viewCountResult = new ViewCountResult();

            Assert.AreEqual(viewCountResult.Correct, false);
            Assert.AreEqual(viewCountResult.CorrectGuesses, 0);
            Assert.AreEqual(viewCountResult.VideoGuessed, null);
            Assert.AreEqual(viewCountResult.VideoNotGuessed, null);
        }
コード例 #2
0
        public void Assert_ViewCountResult_ctor_with_values_is_correct()
        {
            bool      correct        = true;
            int       correctGuesses = 152;
            VideoView videoView      = new VideoView("videoId", 125639876);

            ViewCountResult viewCountResult = new ViewCountResult(correct, correctGuesses, videoView, videoView);

            Assert.AreEqual(viewCountResult.Correct, correct);
            Assert.AreEqual(viewCountResult.CorrectGuesses, correctGuesses);
            Assert.AreEqual(viewCountResult.VideoGuessed.VideoId, videoView.VideoId);
            Assert.AreEqual(viewCountResult.VideoGuessed.ViewCount, videoView.ViewCount);
            Assert.AreEqual(viewCountResult.VideoNotGuessed.VideoId, videoView.VideoId);
            Assert.AreEqual(viewCountResult.VideoNotGuessed.ViewCount, videoView.ViewCount);
        }
コード例 #3
0
        public async Task <ActionResult <ViewCountResult> > GetViewCountAsync(string videoIdGuessed, string videoIdNotGuessed)
        {
            // Make sure they are not empty
            if (string.IsNullOrEmpty(videoIdGuessed) || string.IsNullOrEmpty(videoIdNotGuessed))
            {
                return(BadRequest());
            }

            // Make sure that the videoIds passed, are in their session
            string sessionLastYoutubeVideos = HttpContext.Session.GetString(Constants.SESSION_LAST_YOUTUBEVIDEOS);

            if (sessionLastYoutubeVideos == null)
            {
                return(BadRequest());
            }
            string[] sessionVideos = sessionLastYoutubeVideos.Split(',');
            if (!sessionVideos.Any(videoIdGuessed.Contains) || !sessionVideos.Any(videoIdNotGuessed.Contains))
            {
                return(BadRequest());
            }

            // Resets the videos that are linked to your session
            HttpContext.Session.SetString(Constants.SESSION_LAST_YOUTUBEVIDEOS, "");

            ViewCountResult  viewCountResult = new ViewCountResult();
            List <VideoView> videoViews      = await ygApi.GetVideosViewCountAsync(videoIdGuessed, videoIdNotGuessed);

            foreach (VideoView videoView in videoViews)
            {
                if (videoView.VideoId == videoIdGuessed)
                {
                    viewCountResult.VideoGuessed = videoView;
                }
                if (videoView.VideoId == videoIdNotGuessed)
                {
                    viewCountResult.VideoNotGuessed = videoView;
                }
            }

            int?correctGuesses = HttpContext.Session.GetInt32(Constants.SESSION_CORRECT_GUESSES_KEY);

            if (viewCountResult.VideoGuessed == null || viewCountResult.VideoNotGuessed == null)
            {
                return(Ok(viewCountResult));
            }


            if (viewCountResult.VideoGuessed.ViewCount >= viewCountResult.VideoNotGuessed.ViewCount)
            {
                viewCountResult.Correct = true;
                if (correctGuesses == null)
                {
                    viewCountResult.CorrectGuesses = 1;
                    HttpContext.Session.SetInt32(Constants.SESSION_CORRECT_GUESSES_KEY, 1);
                }
                else
                {
                    correctGuesses++;
                    viewCountResult.CorrectGuesses = (int)correctGuesses;
                    HttpContext.Session.SetInt32(Constants.SESSION_CORRECT_GUESSES_KEY, (int)correctGuesses);
                }
            }
            else
            {
                HttpContext.Session.SetInt32(Constants.SESSION_GAME_OVER_KEY, 1);
                return(Ok(viewCountResult));
            }

            // Check if they are trying to "cheat" the system, by not being redirected to the GameOver screen.
            int?gameOver = HttpContext.Session.GetInt32(Constants.SESSION_GAME_OVER_KEY);

            if (gameOver == 1)
            {
                viewCountResult.Correct = false;
                return(Ok(viewCountResult));
            }
            return(Ok(viewCountResult));
        }