Loading and Accepting Friend Requests
1. Load and accept friend requests sent to me
Add content to the GetReceivedSendFriendRequest and ApplyFriend methods in BackndFriend.cs written in the Pre-arrangements stage.
BackndFriend.cs
Before editing
public void GetReceivedSendFriendRequest() {
// Adds logic of Step 3. Loading and Accepting Friend Requests(part on loading)
}
public void ApplyFriend(int index) {
// Adds logic of Step 3. Loading and Accepting Friend Requests(part on accepting)
}
After editing
public void GetReceivedSendFriendRequest() {
var bro = Backnd.Friend.GetUnresolvedFriendRequestList();
if(bro.IsSuccess() == false) {
Debug.Log("An error occurred while loading the received friend requests list. : " + bro);
return;
}
if(bro.FlattenRows().Count <= 0) {
Debug.LogError("There are no received friend requests.");
return;
}
Debug.Log("Successfully loaded the received friend requests list. : " + bro);
int index = 0;
foreach(BACKND.LitJson.JsonData friendJson in bro.FlattenRows()) {
string nickName = friendJson["nickname"]?.ToString();
string inDate = friendJson["inDate"].ToString();
_requestFriendList.Add(new Tuple<string, string>(nickName, inDate));
Debug.Log($"{index}. {nickName} - {inDate}");
index++;
}
}
public void ApplyFriend(int index) {
if(_requestFriendList.Count <= 0) {
Debug.LogError("There are no friend requests.");
return;
}
if(index >= _requestFriendList.Count) {
Debug.LogError($"Exceeded the limit of the received friend request list. Select : {index} / List maximum : {_requestFriendList.Count}");
return;
}
var bro = Backnd.Friend.AcceptFriendRequest(_requestFriendList[index].Item2);
if(bro.IsSuccess() == false) {
Debug.LogError("An error occurred while accepting the friend request. : " + bro);
return;
}
Debug.Log($"{_requestFriendList[index].Item1} has become a friend. : " + bro);
}
2. Add method call to BackndManager.cs
BackndManager, which is automatically called when the game is executed, must be used to call this method.
Add it so that the method may be called after BACKND initialization and BACKND login.
The user to log in must be the one that sent the friend request, not the user who logged in(signed up) in Step 2.
BackndManager.cs
Before editing(Logic for Step 2. Sending Friend Requests)
async void Test() {
await Task.Run(() => {
// Signed up as user2 because the friend request will be sent to user1
string user2Id = "user2";
// Sign up as user2Id(When 409 error occurs, change CustomSignUp to CustomSignIn because an ID has already been created with user2Id)
BackndLogin.Instance.CustomSignUp(user2Id, "1234");
BackndLogin.Instance.ChangeDisplayName(user2Id); // Change the nickname to be same as the ID
string user1Nickname = "Desired Name"; // Nickname of user1(may differ depending on the user)
BackndFriend.Instance.SendFriendRequest(user1Nickname); // Method for sending friend requests
Debug.Log("Test complete.");
});
}
After editing
async void Test() {
await Task.Run(() => {
// Must be logged in as the user to which user2 sent the friend request in Step 2.
BackndLogin.Instance.CustomSignIn("user1", "1234");
BackndFriend.Instance.GetReceivedSendFriendRequest(); // Loads friend requests list
BackndFriend.Instance.ApplyFriend(0); // Accepts the latest friend request on the friend requests list
Debug.Log("Test complete.");
});
}
3. Test in Unity
After editing the script, execute Unity debugging and check the console log of Unity.
The method has been successfully called when the log displays 'OO has become a friend. : statusCode : 204'.
When errors, such as statusCode : 400, 404, and 409, occur instead of the above log, you can check which errors caused the failure in GetUnresolvedFriendRequestList error case and AcceptFriendRequest error case.