コード例 #1
0
        public GameStateProviderTests()
        {
            _dummyPlayer = GetDummyPlayer();
            _dummyMap = GetDummyMap();

            _gameStateRepository = Substitute.For<IGameStateRepository>();
            _playersProvider = Substitute.For<IPlayersProvider>();
            _playersProvider.Get(Guid.Empty).ReturnsForAnyArgs(_dummyPlayer);
            _mapProvider = Substitute.For<IMapProvider>();
            _mapProvider.GetRandomMap().ReturnsForAnyArgs(_dummyMap);
            _gameStateProvider = new GameStateProvider(_gameStateRepository, _playersProvider, _mapProvider);
        }
コード例 #2
0
        public GameStateProviderTests()
        {
            _dummyPlayer = GetDummyPlayer();
            _dummyMap    = GetDummyMap();

            _gameStateRepository = Substitute.For <IGameStateRepository>();
            _playersProvider     = Substitute.For <IPlayersProvider>();
            _playersProvider.Get(Guid.Empty).ReturnsForAnyArgs(_dummyPlayer);
            _mapProvider = Substitute.For <IMapProvider>();
            _mapProvider.GetRandomMap().ReturnsForAnyArgs(_dummyMap);
            _gameStateProvider = new GameStateProvider(_gameStateRepository, _playersProvider, _mapProvider);
        }
コード例 #3
0
        public ValidationResult <GameState> JoinGame(Guid gameId, Guid playerId)
        {
            if (gameId == Guid.Empty)
            {
                return(ValidationResult <GameState> .Failure("gameId not set"));
            }

            if (playerId == Guid.Empty)
            {
                return(ValidationResult <GameState> .Failure("playerId not set"));
            }

            var player = _playersProvider.Get(playerId);
            var game   = _repo.Get(gameId);

            if (player == null)
            {
                return(ValidationResult <GameState> .Failure("Could not find Player"));
            }
            if (game == null)
            {
                return(ValidationResult <GameState> .Failure("Could not find Game"));
            }

            var map = _mapProvider.Get(game.MapId);

            if (game.Players.Count >= map.MaxPlayers)
            {
                return(ValidationResult <GameState> .Failure("Game is already full"));
            }

            if (game.Players.Any(p => p.Player.Id == player.Id))
            {
                return(ValidationResult <GameState> .Failure("Player is already a member of that game"));
            }

            if (game.Started)
            {
                return(ValidationResult <GameState> .Failure("Cannot join a game in progress"));
            }

            game.Players.Add(new GamePlayer {
                Id = Guid.NewGuid(), Player = player, IsAlive = true, Score = 0
            });
            _repo.Save();

            return(ValidationResult <GameState> .Success.WithData(game));
        }