question

appgame avatar image
appgame asked

server.AddFriend -> Two-way friend confirmation with CloudScript

Receiving status: "OK"-> But Not Addition to Friends Lists.

New Friends is not included in GetFriendsList->ListResults.


CloudScript:


/*
    MakeFriendRequest;
    FunctionParameter:
    { PlayFabId: PlayFabId, FriendPlayFabId: FriendPlayFabId }
*/


handlers.MakeFriendRequest = function (args, context) {

    handlers.GenerateError = () => {
        try {
            var apiResult = server.AddFriend({
                "PlayFabId": args.PlayFabId,
                "FriendPlayFabId":  args.FriendPlayFabId,
            });

            var apiResult = server.SetFriendTags({
                "PlayFabId": args.PlayFabId,
                "FriendPlayFabId": args.FriendPlayFabId,
                "Tags": ["requester"]
            });

            var apiResult = server.AddFriend({
                "PlayFabId": args.FriendPlayFabId,
                "FriendPlayFabId": args.PlayFabId,
            });
            
            var apiResult = server.SetFriendTags({
                "PlayFabId": args.FriendPlayFabId,
                "FriendPlayFabId": args.PlayFabId,
                "Tags": ["requestee"]
            });

        } catch (ex) {
            let error = ex.apiErrorInfo.apiError.error;
            let errorCode = ex.apiErrorInfo.apiError.errorCode;
        }
    }
};



/*
    SearchFriendsRequest;
    FunctionParameter:
    { PlayFabId: PlayFabId, Page: 2, Tag: "requestee" }
*/


handlers.SearchFriendsRequest = function (args, context) {

    handlers.GenerateError = () => {
        try {
            var Friends = new Array();

            var FriendsList = server.GetFriendsList({
                PlayFabId: args.PlayFabId, 
                IncludeFacebookFriends: false, 
                IncludeSteamFriends: false,
                ProfileConstraints: { ShowTags : true }
            }).Friends;
        
            if( args.Page == "All" ){
              var Start = 0; 
              var End = FriendsList.length;
            }else{
                var END = ( parseInt(args.Page) * 20 );
                var START = (END - 20)
            }
        
            for(var i = START; i < END ; i++){
                var F = FriendsList[i];
                if(F == undefined){ break; }
                if(F.Tags.indexOf(args.Tag) > -1 ){Friends.push(F);};
            }

        } catch (ex) {
            let error = ex.apiErrorInfo.apiError.error;
            let errorCode = ex.apiErrorInfo.apiError.errorCode;
        }
        return {
            FriendsList : Friends
        };
    }
}



/*
    FriendsRequestConfirmed;
    FunctionParameter:
    { PlayFabId: PlayFabId, FriendPlayFabId: FriendPlayFabId }
*/


handlers.FriendsRequestConfirmed = function (args, context) {

    handlers.GenerateError = () => {
        try {

        var apiResult = server.SetFriendTags({
            "PlayFabId": args.PlayFabId,
            "FriendPlayFabId": args.FriendPlayFabId,
            "Tags": ["Confirmed"]
        });

        var apiResult = server.SetFriendTags({
            "PlayFabId": args.FriendPlayFabId,
            "FriendPlayFabId": args.PlayFabId,
            "Tags": ["Confirmed"]
        });

        } catch (ex) {
            let error = ex.apiErrorInfo.apiError.error;
            let errorCode = ex.apiErrorInfo.apiError.errorCode;
        }
    }
}



/*
    FriendsRequestDeclined;
    FunctionParameter:
    { PlayFabId: PlayFabId, FriendPlayFabId: FriendPlayFabId }
*/


handlers.FriendsRequestDeclined = function (args, context) {

    handlers.GenerateError = () => {
        try {

        var apiResult = server.RemoveFriend({
            "PlayFabId": args.PlayFabId,
            "FriendPlayFabId": args.FriendPlayFabId,
        });

        var apiResult = server.RemoveFriend({
            "PlayFabId": args.FriendPlayFabId,
            "FriendPlayFabId": args.PlayFabId,
        });

        } catch (ex) {
            let error = ex.apiErrorInfo.apiError.error;
            let errorCode = ex.apiErrorInfo.apiError.errorCode;
        }
    }
}






SCRIPT:

POST: 
URL: https://ABCD.playfabapi.com/Client/ExecuteCloudScript?sdk=JavaScriptSDK-1.63.200402

{
 "FunctionName":"MakeFriendRequest",
 "FunctionParameter":
	{
	 "PlayFabId":"403B08",
	 "FriendPlayFabId":"855E2"
	},
 "RevisionSelection":"Live",
 "GeneratePlayStreamEvent":true,"TitleId":"1234"
}




code: 200
data:
 APIRequestsIssued: 0
 ExecutionTimeSeconds: 0.000203
 FunctionName: "MakeFriendRequest
 "HttpRequestsIssued: 0
 Logs: []
 MemoryConsumedBytes: 4744
 ProcessorTimeSeconds: 0
 Revision: 17
 __proto__: Object
status: "OK"



10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sarah Zhang avatar image
Sarah Zhang answered

The “‘code’: 200, ‘status’:’OK’” means the API ExecuteCloudScript was called successfully. You can refer to the following code to let the function work and catch the error using try/catch block.

handlers.MakeFriendRequest = (args, context) => {
    try {

        var apiResult = server.AddFriend({
            "PlayFabId": args.PlayFabId,
            "FriendPlayFabId": args.FriendPlayFabId,
        });

        var apiResult = server.SetFriendTags({
            "PlayFabId": args.PlayFabId,
            "FriendPlayFabId": args.FriendPlayFabId,
            "Tags": ["requester"]
        });

        var apiResult = server.AddFriend({
            "PlayFabId": args.FriendPlayFabId,
            "FriendPlayFabId": args.PlayFabId,
        });

        var apiResult = server.SetFriendTags({
            "PlayFabId": args.FriendPlayFabId,
            "FriendPlayFabId": args.PlayFabId,
            "Tags": ["requestee"]
        });


    } catch (ex) {
        let error = ex.apiErrorInfo.apiError.error;
        let errorCode = ex.apiErrorInfo.apiError.errorCode;
    }

};
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

appgame avatar image appgame commented ·
Testing was at ZERO Friends in FriendsList.

"Busy Wait" could be higher depending on FriendsList.length.
Above 500 to 5,000 Friends.


YOUR MAX Execution Time Seconds is bellow 9.99.

2 Sec.AddFriend()
2 Sec.SetFriendTags();
2 Sec.AddFriend()
2 Sec.SetFriendTags();
= 8 Seconds Execution Time.

0 Likes 0 ·
appgame avatar image appgame commented ·
Hello,


"Best Answer" without the use of handlers.GenerateError =()=>{}
ExecutionTimeSeconds: 0.1268618


If this Fails with higher number of Friends in the FriendList?
500 to 5,000 Friends. Go to Option #2.


Thanks you for the help, Sarah Zhang.....
0 Likes 0 ·
appgame avatar image appgame commented ·
If Both Attempts Fail.
POST them individually.

1,
POST:	Server/AddFriend
	Requester Wait: Response Status "OK"
2,
POST:	Server/SetFriendTags
	Requester Wait: Response Status "OK"

3,
POST:	Server/AddFriend
	Requestee Wait: Response Status "OK"
4,
POST:	Server/SetFriendTags
	Requestee Wait: Response Status "OK"

0 Likes 0 ·
appgame avatar image
appgame answered
Known as "Busy Wait"


?(IF)
AddFriend() is using Loop & Busy when SetFriendTags() is called.
There will not be Friend to update SetFriendTags().


AddFriend() & SetFriendTags() have "Empty Response".
AddFriend() Error "UsersAlreadyFriends".


/**
   2 Sec. Wait.
   MAX  Execution Time Seconds: 9.99
**/


handlers.MakeFriendRequest = (args, context) => { 
   try {
	AB = [ args.PlayFabId, args.FriendPlayFabId ];
	var exitTime = 0;
	function SetInterval(){
		var now = new Date();
		exitTime = now.getTime() + 2000;
	}
	SetInterval();
	i = 1;
	Busy = 0;
	while(true) {
		A = 0; B = 1; if( i > 2 ){ A = 1; B = 0; };
		C = {"PlayFabId": AB[A],"FriendPlayFabId": AB[B]};
		if( (i % 2) == 1 && Busy == 0 ){
			var apiResult = server.AddFriend(C)
		};
		if( (i % 2) == 0 && Busy == 0 ){
			C["Tags"] = ( i == 2 ) ? "requester" : "requestee"; 
			var apiResult = server.SetFriendTags(C)
		};
		now = new Date();
		if( now.getTime() > exitTime ){
			Busy = 0; i++; SetInterval();}
		else{
			Busy = 1;
		};
		if(i==4){ return {status : "OK"};};
	};
   
   } catch (ex) {
	let error=ex.apiErrorInfo.apiError.error;
	let errorCode=ex.apiErrorInfo.apiError.errorCode;
   } ;
};
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

appgame avatar image
appgame answered

RESULT SUCCESS {GetFriendsList}

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.