Beispiel #1
0
 public void JoinGame_WhenUserIsCreator_ShouldThrowException()
 {
     JoinGameModel joinGameModel = new JoinGameModel()
     {
         GameId = 1,
         Password = null,
         SessionKey = "10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef",
     };
     mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns(
         new Game()
         {
             Password = null,
             GameStatus = "Open",
             RedUserId = 1
         });
     mock.Setup(g => g.Users.GetAll()).Returns(new User[]
     {
         new User
         {
             Id = 1,
             Nickname = "creatorNickname",
             Username = "******",
             SessionKey = "10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef",
         },
     }.AsQueryable());
     GameService gameService = new GameService(mock.Object);
     gameService.JoinGame(joinGameModel);
 }
Beispiel #2
0
 public void JoinGame_WhenPasswordIsNotSame_ShouldThrowException()
 {
     JoinGameModel joinGameModel = new JoinGameModel()
     {
         GameId = 1,
         Password = "******",
         SessionKey = "100448Sitruv4IfYOGRSp6PqmxNFYr11vvZKQpCcLzmWThbTI3"
     };
     mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns(
         new Game()
         {
             Password = "******",
             GameStatus = "Open",
         });
     GameService gameService = new GameService(mock.Object);
     gameService.JoinGame(joinGameModel);
 }
Beispiel #3
0
 public void JoinGame_WhenGameIsNotOpen_ShouldThrowException()
 {
     JoinGameModel joinGameModel = new JoinGameModel()
     {
         GameId = 1,
     };
     mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns(
         new Game()
         {
             GameStatus = "InProgress",
         });
     GameService gameService = new GameService(mock.Object);
     gameService.JoinGame(joinGameModel);
 }
Beispiel #4
0
 public void JoinGame_WhenDataIsValid_ShouldUpdateGameState()
 {
     Game updatedGame = new Game();
     JoinGameModel joinGameModel = new JoinGameModel()
     {
         GameId = 1,
         Password = null,
         SessionKey = "10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef",
     };
     mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns(
         new Game()
         {
             Password = null,
             GameStatus = "Open",
             RedUserId = 1
         });
     mock.Setup(g => g.Users.GetAll()).Returns(new User[]
     {
         new User
         {
             Id = 2,
             Nickname = "creatorNickname",
             Username = "******",
             SessionKey = "10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef",
         },
     }.AsQueryable());
     mock.Setup(u => u.Games.Update(It.IsAny<Game>())).Callback((Game game) => updatedGame = game);
     GameService gameService = new GameService(mock.Object);
     gameService.JoinGame(joinGameModel);
     Assert.AreEqual("Full", updatedGame.GameStatus);
     Assert.AreEqual(2, updatedGame.BlueUser.Id);
 }
Beispiel #5
0
        public void JoinGame(JoinGameModel joinGameModel)
        {
            if (joinGameModel==null)
            {
                throw new ArgumentNullException("JoinGameModel is null");
            }
            ValidateGamePassword(joinGameModel.Password);
            Game game = GetGameById(joinGameModel.GameId);
            if (game.Password != null && game.Password != joinGameModel.Password)
            {
                throw new ServerErrorException("Incorrect game password", "INV_GAME_AUTH");
            }
            if (game.GameStatus != GameStatusType.Open)
            {
                throw new ServerErrorException("Тhe game is not open", "ERR_GAME_STAT_NOT_OPEN");
            }

            User blueUser = GetUserBySessionKey(joinGameModel.SessionKey);
            if(game.RedUserId==blueUser.Id)
            {
                throw new ServerErrorException("You are the creator of the game", "INV_GAME_USR");
            }
            game.BlueUser = blueUser;
            game.GameStatus = GameStatusType.Full;
            this.Data.Games.Update(game);
            this.Data.SaveChanges();
        }
Beispiel #6
0
        public void JoinGameAndNotifyMyOpponent(JoinGameModel joinGameModel)
        {
            try{
            User opponent;

            GameServise.JoinGame(joinGameModel);
            opponent = GameServise.GetAnotherOpponentInGame(joinGameModel.GameId, joinGameModel.SessionKey);

            Clients.All.nothifyAllClientForOpenGames();
            Clients.Client(opponent.ConnectionId).joinedGame();
              }
            catch (ServerErrorException ex)
            {
                Clients.Caller.serverErrorException(ex.Message);
            }
        }