Example #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);
        }
Example #2
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);
        }