public void AddOrUpdateUserSession(UserSession userSession)
        {
            if (string.IsNullOrWhiteSpace(userSession.UserId))
            {
                throw new ArgumentException("User Id cannot be empty");
            }

            this.userSessionContainer.Save(userSession.UserId, userSession);
        }
        public void AddOrUpdateUserSessionTest()
        {
            var users = new AzureBlobContainer<UserProfile>(CloudStorageAccount.DevelopmentStorageAccount);
            var sessions = new AzureBlobContainer<UserSession>(CloudStorageAccount.DevelopmentStorageAccount);
            var friends = new AzureBlobContainer<Friends>(CloudStorageAccount.DevelopmentStorageAccount);
            var target = new UserRepository(users, sessions, friends);
            IAzureBlobContainer<UserSession> userSessionContainer = (IAzureBlobContainer<UserSession>)
                target.GetType().GetField("userSessionContainer", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(target);

            UserSession userSessionFirstVersion = new UserSession() { UserId = "johnny", ActiveGameQueueId = Guid.NewGuid() };
            target.AddOrUpdateUserSession(userSessionFirstVersion);
            var result = userSessionContainer.Get(userSessionFirstVersion.UserId);
            Assert.AreEqual(userSessionFirstVersion, result);

            UserSession userSessionSecondVersion = new UserSession() { UserId = userSessionFirstVersion.UserId, ActiveGameQueueId = Guid.NewGuid() };
            target.AddOrUpdateUserSession(userSessionSecondVersion);
            result = userSessionContainer.Get(userSessionFirstVersion.UserId);
            Assert.AreEqual(userSessionSecondVersion, result);
            Assert.AreNotEqual(userSessionFirstVersion, result);
        }
Exemplo n.º 3
0
        public void Do(IDictionary<string, object> context)
        {
            var userId = context["userId"].ToString();
            var gameId = new Guid(context["gameId"].ToString());

            if (!string.IsNullOrWhiteSpace(userId))
            {
                var userSession = new UserSession
                {
                    UserId = userId,
                    ActiveGameQueueId = Guid.Empty
                };

                this.userRepository.AddOrUpdateUserSession(userSession);
            }

            var game = this.gameRepository.GetGame(gameId);

            if (game != null)
            {
                if (game.Users.RemoveAll(u => u.UserId == userId) > 0)
                {
                    this.gameRepository.AddOrUpdateGame(game);
                }
            }
            else
            {
                // gameId parameter is the gameQueueId
                var gameQueue = this.gameRepository.GetGameQueue(gameId);

                if (gameQueue != null)
                {
                    if (gameQueue.Users.RemoveAll(u => u.UserId == userId) > 0)
                    {
                        this.gameRepository.AddOrUpdateGameQueue(gameQueue);
                    }
                }
            }
        }
        public void FailedAddOrUpdateUserSessionTest()
        {
            var users = new AzureBlobContainer<UserProfile>(CloudStorageAccount.DevelopmentStorageAccount);
            var sessions = new AzureBlobContainer<UserSession>(CloudStorageAccount.DevelopmentStorageAccount);
            var friends = new AzureBlobContainer<Friends>(CloudStorageAccount.DevelopmentStorageAccount);
            var target = new UserRepository(users, sessions, friends);

            UserSession userSessionFirstVersion = new UserSession() { UserId = null, ActiveGameQueueId = Guid.NewGuid() };
            ExceptionAssert.ShouldThrow<ArgumentException>(() => target.AddOrUpdateUserSession(userSessionFirstVersion));

            UserSession userSessionSecondVersion = new UserSession() { UserId = string.Empty, ActiveGameQueueId = Guid.NewGuid() };
            ExceptionAssert.ShouldThrow<ArgumentException>(() => target.AddOrUpdateUserSession(userSessionSecondVersion));
        }
 public bool Equals(UserSession userSession)
 {
     return this.UserId == userSession.UserId && this.ActiveGameQueueId == userSession.ActiveGameQueueId;
 }
Exemplo n.º 6
0
        public HttpResponseMessage Queue(HttpRequestMessage request)
        {
            dynamic formContent = request.Content.ReadAsAsync<JsonValue>().Result;
            GameType gameType;

            if (!Enum.TryParse<GameType>(formContent.gameType.Value, true, out gameType))
            {
                return BadRequest("Invalid gameType parameter");
            }

            try
            {
                // Update userSession blob
                var userSession = new UserSession
                {
                    UserId = CurrentUserId,
                    ActiveGameQueueId = Guid.Empty
                };

                this.userRepository.AddOrUpdateUserSession(userSession);
                this.gameRepository.AddUserToGameQueue(CurrentUserId, gameType);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }

            return SuccessResponse;
        }