Example #1
0
        public void RestartGameState_WhenUserInGame_ShouldRestartAndUpdateGameState()
        {
            Game updatedGame = new Game();
            Guess[] guess = new Guess[]
            {
                new Guess() { Id = 1 },
                new Guess() { Id = 2 },
                new Guess() { Id = 3 },
            };
            mock.Setup(g => g.Users.GetAll()).Returns(new User[]
            {
                new User
                {
                    Id = 2,
                    Nickname = "creatorNickname",
                    Username = "******",
                    SessionKey = "10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef",
                },
            }.AsQueryable());

            mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns(
                new Game()
                {
                    Password = null,
                    GameStatus = "Open",
                    RedUserId = 1,
                    Winner = new User(),
                    BlueUserId = 2,
                    Guesses = guess
                });

            GameService gameSerivce = new GameService(mock.Object);
            mock.Setup(u => u.Games.Update(It.IsAny<Game>())).Callback((Game game) => updatedGame = game);
            mock.Setup(u => u.Guesses.Remove(It.IsAny<Guess[]>()));
            gameSerivce.RestartGameState("10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", 1, "Open");

            Assert.AreEqual("Open", updatedGame.GameStatus);
            Assert.AreEqual(9, updatedGame.MovesLeft);
            Assert.IsNull(updatedGame.Winner);
        }
Example #2
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="game"></param>
 /// <param name="position">The position where to place the marker, should be between 0 and 9</param>
 /// <returns>True if the marker position was not already taken</returns>
 private void PlaceMarker(Game game, User user, int position)
 {
     int count = game.Guesses.Where(g => g.Position == position).Count();
     if (count != 0)
     {
        throw new ServerErrorException("The position is occupied", "INV_POS");
     }
     Guess newGuess = new Guess()
     {
         Position = position,
         User = user,
         Game = game,
     };
     game.Guesses.Add(newGuess);
     game.MovesLeft--;
 }