I have implemented a system where a player can send a friend request to another player by writing to their read-only data (using a cloud script) and waiting for that player to accept, and returning an "accept" message - as described in some of the answers on the forums. Works great!
This is fine in a game when I know the playfabID of the friend I want to add.
But if I only know the player's display name (and not their playfabID), it's hard to write to that player's read-only data. Here's a cloud-script solution that I came up with, but it seems really messy. Is there a better way to do this?
handlers.AddFriendByName = function (args, context) { var myID = currentPlayerId; var friendName = args.friendName; var myName = args.myName; //Try to add friend for player ID var result = server.AddFriend({ "PlayFabId": myID, "FriendTitleDisplayName": friendName }); //If fail, return {} meaning friend name was not found if (result.apiError != null) { return {}; } //Hack to find friend ID: //List all friends and search for the one we just added var friendsresult = server.GetFriendsList({ "PlayFabId": myID }); var friendID = ""; if ("Friends" in friendsresult) { var friends = friendsresult.Friends; for (var i = 0; i < friends.length ; i++) { if (friends[i].TitleDisplayName == friendName) { friendID = friends[i].FriendPlayFabId; break; } } } if (friendID == "") return {}; //Remove friend from playfab (so that we can send a request instead) server.RemoveFriend({ "PlayFabId": myID, "FriendPlayFabId": friendID }); //Call handler function to create a new friend request using the friend ID var requestArgs = { "friendID": friendID, "myName": myName }; handlers.addFriendRequest(requestArgs, null); //return {ID} return {ID : friendID} }
Note: I haven't tested this code, so I'm sure there's sytax errors.
Answer by Brendan · Apr 14, 2017 at 07:49 AM
Actually, I would advise against this design pattern, as it has a few problems. Most critically, this design will fail at scale - take, for example, the scenario where you have someone playing in your game who is well-known, such as a popular YouTube streamer. For any game with a non-trivial player population, you'll likely have hundreds or even thousands of people hitting that person with a friend request. If you're writing all requests to the same key, you'll encounter throttling on writes to the key, since only one player can write to it at a time. But also, since multiple players will be reading and then writing it, they'll be stomping each others' data constantly. Alternately, if you have each player write to a distinct key using their PlayFab ID, the player's data space will rapidly run out of keys.
For a way to implement a two-way confirmed friends system that will work using existing components, have a look at this thread, where we describe an implementation that makes use of the friend tag system: https://community.playfab.com/questions/370/206712537-Two-way-friend-confirmation-with-Cloud-Script.html
Thanks Brendan! That does sound like a better solution, and cleaner too! As someone on the other link said, it would be really useful to add this to the recipes/tutorials section as there were many solutions on the answer forums, and I wasn't sure which was the right one.
Yes, we're in the process of a fairly significant documentation update. Once we have that complete, we'll be looking at our samples repo, to see about getting more examples in there.
Would I still need to call server.GetFriendsList and parse through the results to find this ID (assuming I only knew the name to begin with)
1) Yes, but you can get a PlayFab ID from any of the other unique identifiers (https://api.playfab.com/documentation/client/method/GetAccountInfo).
2) You would get the friends list to check if you have any outstanding friend requests and process them.
Ah I didn't know I could use GetAccountInfo with the other identifiers, thanks! That'll work perfect.
C#/UWP: AddFriend per Cloud Script fails 5 Answers
Cloud script Friends GetPlayerStatistics vs GetFriendLeaderboard considering request limit 1 Answer
"Internal Server Error" in GetFriendLeaderboard API 0 Answers
How to manage Friend list manually without errors and API request limit 1 Answer
Playfab action-event server system 1 Answer