Esempio n. 1
0
        public void MakeGuess_WhenLastMoveAndNoWinner_ShouldReturnStateGameOver()
        {
            User user=new User()
            {
                SessionKey="10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef",
                Id=1
            };
            Game updatedGame = new Game();
            List<Guess> guesses = new List<Guess>();
            guesses.Add(new Guess() { Position = 1, User=user });
            mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns(
              new Game()
            {
                MovesLeft=1,
                Guesses = guesses,
                GameStatus = "InProgress",
                UserInTurn = 1,
                RedUserId = 1
            });
            mock.Setup(g => g.Users.GetAll()).Returns(new User[]{ user }.AsQueryable());

            mock.Setup(u => u.Games.Update(It.IsAny<Game>())).Callback((Game game) => updatedGame = game);
            GuessModel guessModel = new GuessModel("10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", 1, 2);
            guessService.MakeGuess(guessModel, out gameState);

            Assert.IsTrue(gameState.gameOver);
            Assert.IsNull(gameState.Winner);
            Assert.AreEqual(updatedGame.GameStatus, "Finished");
        }
Esempio n. 2
0
        public Game MakeGuess(GuessModel guessModel, out GameState gameState)
        {
            if(guessModel==null)
            {
                throw new ArgumentNullException("GuessModel is null");
            }
            gameState = new GameState();
            ValidateSessionKey(guessModel.SessionKey);
            User user = GetUserBySessionKey(guessModel.SessionKey);
            Game game = GetGameById(guessModel.GameId);
            if (game.GameStatus != GameStatusType.InProgress)
            {
                throw new ServerErrorException("Game is not in progress", "INV_OP_GAME_STAT");
            }
            ValidateUserInGame(game, user);
            //check userInTurn
            if (game.UserInTurn != user.Id)
            {
                throw new ServerErrorException("Not your turn", "INV_OP_TURN");
            }
            CheckPosition(guessModel.Position);
            PlaceMarker(game, user, guessModel.Position);
              gameState.gameOver= CheckGameOver(game);

                bool isWinner = false;
                int[] arrayOfMarkedPositions = game.Guesses.Where(g => g.User.Id == user.Id).Select(p => p.Position).ToArray();
                isWinner = CheckWinner(arrayOfMarkedPositions);
                if (isWinner)
                {
                    gameState.gameOver = true;
                    game.Winner = user;
                    gameState.Winner = user.Nickname;
                }
                if (gameState.gameOver)
                {

                    game.GameStatus = GameStatusType.Finished;
                    gameState.GameId = (int)game.Id;
                    gameState.Title = game.Title;
                }
                else
                {
                    var otherUser = (game.RedUser == user) ? game.BlueUser : game.RedUser;
                    game.UserInTurn = otherUser.Id;
                }

            this.Data.Games.Update(game);
            this.Data.SaveChanges();
            return game;
        }
Esempio n. 3
0
 public void MakeGuess_WhenGameStatusIsNotInProgress_ShouldThrowException()
 {
     Game game = new Game()
     {
         GameStatus = "Full",
     };
      mock.Setup(g => g.Users.GetAll()).Returns(new User[]
     {new User()
     {
         SessionKey="10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef",
         Id=1
     }}.AsQueryable());
      GuessModel guessModel = new GuessModel("10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", 1, 1);
      mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns(game);
      guessService.MakeGuess(guessModel, out gameState);
 }
Esempio n. 4
0
 public void MakeGuess_WhenPositionIsOccupied_ShouldThrowException()
 {
     List<Guess> guesses = new List<Guess>();
     guesses.Add(new Guess() { Position = 1 });
     Game game = new Game()
     {
         Guesses=guesses,
         GameStatus = "InProgress",
         UserInTurn = 1,
         RedUserId = 1
     };
     mock.Setup(g => g.Users.GetAll()).Returns(new User[]
     {new User()
     {
         SessionKey="10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef",
         Id=1
     }}.AsQueryable());
     GuessModel guessModel = new GuessModel("10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", 1, 1);
     mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns(game);
     guessService.MakeGuess(guessModel, out gameState);
 }
Esempio n. 5
0
        public void MakeGuess_WhenPlaceThirdRow_ShouldWinning()
        {
            User redUser = new User()
             {
                 SessionKey = "10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef",
                 Id = 1
             };
             Game updatedGame = new Game();
             List<Guess> guesses = new List<Guess>();
             guesses.Add(new Guess() { Position = 7, User = redUser });
             guesses.Add(new Guess() { Position = 8, User = redUser });

             mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns(
               new Game()
               {
                   MovesLeft = 9,
                   Guesses = guesses,
                   GameStatus = "InProgress",
                   UserInTurn = 1,
                   RedUserId = 1
               });
             mock.Setup(g => g.Users.GetAll()).Returns(new User[] { redUser }.AsQueryable());
             mock.Setup(u => u.Games.Update(It.IsAny<Game>())).Callback((Game game) => updatedGame = game);
             GuessModel guessModel = new GuessModel("10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", 1, 9);
             guessService.MakeGuess(guessModel, out gameState);
             Assert.AreEqual(1, updatedGame.Winner.Id);
             Assert.IsTrue(gameState.gameOver);
        }
Esempio n. 6
0
        public void MakeGuess_WhenPlacedMaerkerAndGameIsNotOver_ShouldUpdateGame()
        {
            User redUser = new User()
            {
                SessionKey = "10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef",
                Id = 1
            };
               User blueUser = new User()
            {
                SessionKey = "20043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef",
                Id = 2
            };
            Game updatedGame = new Game();
            List<Guess> guesses = new List<Guess>();
            guesses.Add(new Guess() { Position = 1, User = blueUser });
            mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns(
              new Game()
              {
                  MovesLeft = 8,
                  Guesses = guesses,
                  GameStatus = "InProgress",
                  UserInTurn = 1,
                  RedUser=redUser,
                  RedUserId = 1,
                  BlueUser=blueUser,
                  BlueUserId=2
              });
            mock.Setup(g => g.Users.GetAll()).Returns(new User[] { redUser }.AsQueryable());

            mock.Setup(u => u.Games.Update(It.IsAny<Game>())).Callback((Game game) => updatedGame = game);
            GuessModel guessModel = new GuessModel("10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", 1, 2);
            guessService.MakeGuess(guessModel, out gameState);

            Assert.AreEqual(2, updatedGame.UserInTurn);
            Assert.IsFalse(gameState.gameOver);
            Assert.IsNull(gameState.Winner);
            Assert.AreEqual(updatedGame.GameStatus, "InProgress");
        }
Esempio n. 7
0
        public void MakeGuess(GuessModel guessModel)
        {
            User user;
            User opponent;
            try
            {
                user = UserServise.GetUserBySessionKey(guessModel.SessionKey);
                opponent = GameServise.GetAnotherOpponentInGame(guessModel.GameId, guessModel.SessionKey);

                Game game;
                GameState gameState;

                game = GuessService.MakeGuess(guessModel, out gameState);

                string symbol;
                if (game.RedUser.Id == user.Id)
                {
                    symbol = "O";
                }
                else
                {
                    symbol = "X";
                }
                Clients.Caller.addMarkerPlacement(guessModel.GameId, symbol, guessModel.Position);
                Clients.Client(opponent.ConnectionId).addMarkerPlacement(guessModel.GameId, symbol, guessModel.Position);
                if (gameState.gameOver)
                {
                    Clients.Caller.gameOver(gameState);
                    Clients.Client(opponent.ConnectionId).gameOver(gameState);
                    if (game.RedUser.Id == user.Id)
                    {
                        Clients.Client(user.ConnectionId).restartOption(guessModel.GameId);
                        Clients.Client(opponent.ConnectionId).waitingForOpponent(guessModel.GameId, "restart");
                    }
                    else
                    {
                        Clients.Client(opponent.ConnectionId).restartOption(guessModel.GameId);
                        Clients.Client(user.ConnectionId).waitingForOpponent(guessModel.GameId, "restart");
                    }
                }
                else
                {
                    UpdateUserInTurn(game, user, opponent);
                }
            }
            catch (ServerErrorException ex)
            {
                Clients.Caller.serverErrorException(ex.Message);
            }
        }