Esempio n. 1
0
        public void JoinGame_MatchListIsEmpty_CreatesNewMatch()
        {
            var playerId = Guid.NewGuid();
            var type     = GameType.TicTacToe;
            var expected = MatchFactory.CreateNewMatch(type, playerId);
            var result   = _service.JoinGame(type, playerId);

            Assert.Equal(expected.gameType, result.gameType);
            Assert.Equal(expected.operationState, result.operationState);
            Assert.Equal(expected.players, result.players);
        }
Esempio n. 2
0
        public void JoinGame_MatchListIsNotEmpty_PlayerJoinsMatch()
        {
            var playerOne     = Guid.NewGuid();
            var playerTwo     = Guid.NewGuid();
            var type          = GameType.TicTacToe;
            var newMatch      = MatchFactory.CreateNewMatch(type, playerOne);
            var expectedMatch = new MatchState
            {
                id            = newMatch.id,
                gameStartTime = newMatch.gameStartTime,
                gameType      = type,
                players       = new List <Guid> {
                    playerOne, playerTwo
                },
                inGameState = new TicTacToeState
                {
                    firstPlayer  = playerOne,
                    secondPlayer = playerTwo,
                    board        = new List <PlayerMark>
                    {
                        PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty,
                        PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty,
                        PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty,
                    },
                },
                operationState = GameOperationState.InProgress,
            };

            _onlineMatchList.Add(newMatch);
            var result = _service.JoinGame(type, playerTwo);

            Assert.Equal(expectedMatch.id, result.id);
            Assert.Equal(expectedMatch.gameStartTime, result.gameStartTime);
            Assert.Equal(expectedMatch.gameType, result.gameType);
            Assert.Equal(expectedMatch.players, result.players);
            Assert.Equal(expectedMatch.inGameState.firstPlayer, result.inGameState.firstPlayer);
            Assert.Equal(expectedMatch.inGameState.secondPlayer, result.inGameState.secondPlayer);
            Assert.Equal(expectedMatch.inGameState.board, result.inGameState.board);
            Assert.Equal(expectedMatch.operationState, result.operationState);
        }
Esempio n. 3
0
        public MatchState JoinGame(GameType matchType, Guid playerId)
        {
            var gameList = _onlineMatches
                           .FindAll(x => x.gameType == matchType)
                           .FindAll(x => x.operationState == GameOperationState.WaitingForPlayers);
            MatchState matchState = null;

            if (gameList.Count == 0)
            {
                matchState = MatchFactory.CreateNewMatch(matchType, playerId);
                _onlineMatches.Add(matchState);
            }
            else
            {
                matchState = gameList.First();
                matchState.players.Add(playerId);
                matchState.inGameState.secondPlayer = playerId;
                matchState.operationState           = GameOperationState.InProgress;
            }

            return(matchState);
        }