예제 #1
0
        public static async Task <MatchLobby> SetMatchLobbyLockState(string matchLobbyId, bool locked, string creatorId, DocumentClient documentClient)
        {
            var matchLobby = await GetMatchLobbyAsyncById(documentClient, matchLobbyId);

            if (matchLobby == null)
            {
                throw TicTacToeExceptionUtil.NotFoundException(Constants.ExceptionLobbyNotFound);
            }

            if (matchLobby.CreatorId != creatorId)
            {
                throw TicTacToeExceptionUtil.OnlyLobbyCreatorCanLockException(Constants.ExceptionOnlyCreatorCanLockLobby);
            }

            if (matchLobby.Locked == locked)
            {
                return(matchLobby);
            }

            matchLobby.Locked = locked;

            await DocumentClientManager.ReplaceDocument(documentClient, matchLobby, Constants.DatabaseName, Constants.MatchLobbyTableName);

            return(matchLobby);
        }
예제 #2
0
        public static async Task <MatchLobby> LeaveMatchLobby(MatchLobby matchLobby, DocumentClient documentClient)
        {
            matchLobby.CurrentAvailability++;

            // This method will update the Cosmos DB document with the newest availability.
            // If there was an outside-modification by other player (race condition),
            // this will throw an error and the player won't be able to join this Lobby.
            await DocumentClientManager.ReplaceDocument(documentClient, matchLobby, Constants.DatabaseName, Constants.MatchLobbyTableName);

            return(matchLobby);
        }
예제 #3
0
        public static async Task <TicTacToeSharedGroupData> JoinMatchLobby(PlayFabAuthenticationContext authenticationContext, string matchLobbyId, string playerTwo, DocumentClient documentClient)
        {
            var tttShareGroupData = await SharedGroupDataUtil.GetAsync(authenticationContext, matchLobbyId);

            // we need the document reference for the Cosmos DB for using its "etag",
            // which avoids having race-conditions when writing in DDBB.
            var documentLobby = await GetMatchLobbyFromClient(documentClient, matchLobbyId);

            if (tttShareGroupData?.Match == null)
            {
                throw new Exception("Match not exists");
            }

            if (documentLobby == null)
            {
                throw new Exception("Match Lobby does not exist in Database");
            }

            if (!string.IsNullOrWhiteSpace(tttShareGroupData.Match.PlayerOneId) && !string.IsNullOrWhiteSpace(tttShareGroupData.Match.PlayerTwoId))
            {
                throw new Exception("Match is full");
            }

            if (string.Compare(tttShareGroupData.Match.PlayerOneId, playerTwo, true) == 0)
            {
                throw new Exception("This player is already the player one");
            }

            tttShareGroupData.Match.PlayerTwoId = playerTwo;
            tttShareGroupData.MatchLobby.CurrentAvailability--;

            // update the current availability in Cosmos DB
            documentLobby.CurrentAvailability = tttShareGroupData.MatchLobby.CurrentAvailability;

            // This method will update the Cosmos DB document with the newest availability.
            // If there was an outside-modification by other player (race condition),
            // this will throw an error and the player won't be able to join this Lobby.
            await DocumentClientManager.ReplaceDocument(documentClient, documentLobby, Constants.DATABASE_NAME, Constants.MATCH_LOBBY_TABLE_NAME);

            await SharedGroupDataUtil.AddMembersAsync(
                authenticationContext,
                matchLobbyId,
                new List <string> {
                tttShareGroupData.Match.PlayerTwoId
            }
                );

            await SharedGroupDataUtil.UpdateAsync(authenticationContext, tttShareGroupData);

            return(tttShareGroupData);
        }
예제 #4
0
        public static async Task <MatchLobby> JoinMatchLobby(string matchLobbyId, string playerTwo, DocumentClient documentClient, string invitationCode = null)
        {
            var matchLobby = await GetMatchLobbyAsyncById(documentClient, matchLobbyId);

            if (matchLobby == null)
            {
                throw TicTacToeExceptionUtil.NotFoundException(Constants.ExceptionLobbyNotFound);
            }

            if (matchLobby.CurrentAvailability == 0)
            {
                throw TicTacToeExceptionUtil.LobbyFullException(Constants.ExceptionLobbyIsFull);
            }

            if (matchLobby.CreatorId == playerTwo)
            {
                throw TicTacToeExceptionUtil.RequesterIsLobbyCreatorException(Constants.ExceptionRequesterIsCreator);
            }

            if (matchLobby.Locked)
            {
                if (string.IsNullOrEmpty(invitationCode))
                {
                    throw TicTacToeExceptionUtil.NotInvitationCodeIncludedException(Constants.ExceptionMissingInvitationCode);
                }

                if (GetInvitationIdFromNetworkId(matchLobby.NetworkId) != invitationCode)
                {
                    throw TicTacToeExceptionUtil.InvalidInvitationCodeException(Constants.ExceptionInvalidInvitationCode);
                }
            }

            matchLobby.CurrentAvailability--;

            // This method will update the Cosmos DB document with the newest availability.
            // If there was an outside-modification by other player (race condition),
            // this will throw an error and the player won't be able to join this Lobby.
            await DocumentClientManager.ReplaceDocument(documentClient, matchLobby, Constants.DatabaseName, Constants.MatchLobbyTableName);

            return(matchLobby);
        }