question

David Dick avatar image
David Dick asked

Where can I find resources for "best practices" using Cloudscript Functions for coding in C#?

I have spent today learning Cloudscript Functions and implementing Playfab Server API calls via Azure Functions but I was hoping to find additional resources other than the quckstart guide to further my understanding on writing clean code and best practices. Are there additional resources someone could provide?

For instance, I specifically wrote an Azure Function in C# for a "Two-way Friend Confirmation" following some of the guide lines found in this post. Sure it works, however, I am curious if I am overlooking anything in terms of a cleaner/more efficient way to write it and if there is any issue (regarding security) getting another player's PlayFabId through the client?

I get another player's Playfab Id through a Client API call in Unity and pass it to this Cloudscript Function:

namespace NetworkSystem.FriendRequest
{
    public static class FriendRequest
    {
        [FunctionName("FriendRequest")]
        public static async Task<dynamic> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] 
	    HttpRequest req, ILogger log)
        {
            var context = JsonConvert.DeserializeObject
            <FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());

            dynamic args = context.FunctionArgument;

            string senderID = context.CallerEntityProfile.Lineage.MasterPlayerAccountId;
            string targetID = args["friendID"];

            PlayFabSettings.staticSettings.DeveloperSecretKey = 
            Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", 
    	    EnvironmentVariableTarget.Process);
            PlayFabSettings.staticSettings.TitleId = 
            Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID", 
	    EnvironmentVariableTarget.Process);

            //Create Step One Request
            var addTargetFriend = new PlayFab.ServerModels.AddFriendRequest
            {
                PlayFabId = senderID,
                FriendPlayFabId = targetID
            };

            //Step One: Add Target As Friend To Sender
            var stepOne = await PlayFabServerAPI.AddFriendAsync(addTargetFriend);   
            if(stepOne.Error != null)
            {
                return stepOne.Error;
            }

            //Create Step Two Request
            var setTargetTag = new PlayFab.ServerModels.SetFriendTagsRequest 
            { 
                PlayFabId = senderID, 
                FriendPlayFabId = targetID,
                Tags = new List<string>()
            };

            //Add Desired Tag For Request
            setTargetTag.Tags.Add("Pending");

            //Step Two: Add Tag To Target
            var stepTwo = await PlayFabServerAPI.SetFriendTagsAsync(setTargetTag);
            if(stepTwo.Error != null)
            {
                return stepTwo.Error;
            }

            //Create Step Three Request
            var addSenderFriend = new PlayFab.ServerModels.AddFriendRequest
            {
                PlayFabId = targetID,
                FriendPlayFabId = senderID
            };

            //Step Three: Add Sender As Friend To Target
            var stepThree = await PlayFabServerAPI.AddFriendAsync(addSenderFriend);
            if (stepThree.Error != null)
            {
                return stepThree.Error;
            }

            //Create Step Four Request
            var setSenderTag = new PlayFab.ServerModels.SetFriendTagsRequest
            {
                PlayFabId = targetID,
                FriendPlayFabId = senderID,
                Tags = new List<string>()
            };

            //Add Desired Tag For Request
            setSenderTag.Tags.Add("Requested");

            //Step Four: Add Tag To Sender
            var stepFour = await PlayFabServerAPI.SetFriendTagsAsync(setSenderTag);
            if (stepFour.Error != null)
            {
                return stepFour.Error;
            }

            //Return a message if the function has executed properly
            return new {message = $"Friend Request Pending For User: {targetID}"};
        }
    }
}
apisCloudScriptFriendsdocumentation
10 |1200

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

1 Answer

·
Made Wang avatar image
Made Wang answered

At present, we have no more official samples about Azure Function Cloud Script.

Regarding the code, what you wrote is correct, and you really need these steps to complete two-way adding friends and two-way adding tags.

Regarding security, if you call GetAccountInfo or other APIs provided by PlayFab, it is feasible for the client to get the PlayFabId of other players. You can also pass in FriendUsername when calling Cloud Script, and when the Add Friend execution is complete, call GetFriendsList to get the friend's PlayFabId.

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.