Ejemplo n.º 1
0
        public void Choose_WithInvalidPlayerId_ThrowsEntityNotFoundException()
        {
            // Arrange
            const int playerId = 12;
            const int gameId   = 84;
            const int xPlayer  = 33;

            var httpContext = new DefaultHttpContext();

            httpContext.Request.Headers["X-Player"] = xPlayer.ToString();
            var controllerContext = new ControllerContext
            {
                HttpContext = httpContext
            };

            sut.ControllerContext = controllerContext;

            var game = new Game();

            _gameDataService.GetGame(gameId).Returns(game);

            // Act
            Action action = () => sut.Choose(gameId, playerId);

            // Assert
            action.Should().Throw <EntityNotFoundException <Player> >();
        }
Ejemplo n.º 2
0
        public IActionResult GetGame([FromHeader] int gameId)
        {
            var game = _gameDataService.GetGame(gameId);

            _gameHub.Send("GameInfo", JsonConvert.SerializeObject(game, Formatting.Indented, new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            }));
            return(Ok(game));
        }
Ejemplo n.º 3
0
        public Player JoinGame(string joinKey, string userName)
        {
            var game = _gameDataService.GetGame(joinKey);

            _playerRepository.Add(new Player
            {
                UserName = userName,
                GameId   = game.Id
            });
            return(_playerRepository.GetPlayerByName(userName, game.Id));
        }
Ejemplo n.º 4
0
        public void JoinGameWithValidUserNameAndValidJoinKey_JoinsGameAndBroadcasts()
        {
            // Arrange
            const string joinKey  = "somestring";
            const string userName = "******";
            const int    gameId   = 12;

            var player = new Player {
                UserName = userName, GameId = gameId
            };
            var game = new Game();

            game.Id = gameId;
            _gameDataService.GetGame(joinKey).Returns(game);
            _playerRepository.GetPlayerByName(userName, gameId).Returns(player);

            // Act
            var result = sut.JoinGame(joinKey, userName);

            // Assert
            _gameDataService.Received().GetGame(joinKey);
            _playerRepository.Received().Add(Arg.Is <Player>(x => x.UserName == userName && x.GameId == gameId));
            _playerRepository.Received().GetPlayerByName(userName, gameId);
            result.Should().Be(player);
        }
Ejemplo n.º 5
0
        public IActionResult Vote(int gameId, bool inFavor)
        {
            if (!int.TryParse(Request.Headers["X-Player"], out var playerId))
            {
                throw new BadRequestException("X-Player request header is missing");
            }

            var game  = _gameDataService.GetGame(gameId);
            var state = _gameStateProvider.Get(game.GameStateId);

            state.Vote(game, playerId, inFavor);
            return(Ok(Mapper.Map <GameStatusDto>(game)));
        }
Ejemplo n.º 6
0
        public IActionResult Choose(int gameId, int chosenPlayerId)
        {
            if (!int.TryParse(Request.Headers["X-Player"], out var playerId))
            {
                throw new BadRequestException("X-Player request header is missing");
            }

            var game = _gameDataService.GetGame(gameId);

            if (!game.HasPlayer(chosenPlayerId))
            {
                throw new EntityNotFoundException <Player>(chosenPlayerId);
            }

            var state = _gameStateProvider.Get(game.GameStateId);

            state.Choose(game, playerId, chosenPlayerId);
            return(Ok(Mapper.Map <GameStatusDto>(game)));
        }
Ejemplo n.º 7
0
        private void AddGame()
        {
            var game = new GameModel()
            {
                GameId = this.GameId
            };

            game = _gameData.GetGame(game);

            if (game != null)
            {
                foreach (var item in GamesList)
                {
                    if (item.GameId == game.GameId)
                    {
                        return;
                    }
                }
                GamesList.Add(game);
                _cache.CachedUser.AvailableGames.Add(game);
            }
        }
Ejemplo n.º 8
0
        public void Vote_WithValidParameters_CallsStateVote()
        {
            // Arrange
            const int  gameId    = 84;
            const int  xPlayerId = 12;
            const bool inFavor   = true;

            var httpContext = new DefaultHttpContext();

            httpContext.Request.Headers["X-Player"] = xPlayerId.ToString();
            var controllerContext = new ControllerContext
            {
                HttpContext = httpContext
            };

            sut.ControllerContext = controllerContext;

            var game = new Game
            {
                GameStateId = GameState.VoteForGovernment
            };

            var gameState = Substitute.For <IGameState>();

            _gameStateProvider.Get(Arg.Any <GameState>()).Returns(gameState);
            _gameDataService.GetGame(gameId).Returns(game);

            // Act
            var result = sut.Vote(gameId, inFavor) as OkObjectResult;

            // Assert
            _gameStateProvider.Received().Get(GameState.VoteForGovernment);
            _gameDataService.Received().GetGame(gameId);
            gameState.Received().Vote(game, xPlayerId, inFavor);
            result.Should().NotBeNull();
            result.Should().BeOfType <OkObjectResult>();
            result.Value.Should().Be(game);
        }
Ejemplo n.º 9
0
        public IActionResult GetGame([FromHeader] int gameId)
        {
            var game = _gameDataService.GetGame(gameId);

            return(Ok(Mapper.Map <GameStatusDto>(game)));
        }