Skip to main content
Version: 5.9.6

get-and-apply

BACKND Guidelines > 8. Implementing Friend Functions > Step 3. Loading and Accepting Friend Requests

Loading and Accepting Friend Requests

1. Load and accept friend requests sent to me

Add content to the GetReceivedRequestFriend and ApplyFriend methods in BackendFriend.cs written in the Pre-arrangements stage.

BackendFriend.cs

Before editing

    public void GetReceivedRequestFriend() {
// 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 GetReceivedRequestFriend() {
var bro = Backend.Friend.GetReceivedRequestList();

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(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 = Backend.Friend.AcceptFriend(_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 BackendManager.cs

BackendManager, 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.

BackendManager.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 CustomLogin because an ID has already been created with user2Id)
BackendLogin.Instance.CustomSignUp(user2Id, "1234");
BackendLogin.Instance.UpdateNickname(user2Id); // Change the nickname to be same as the ID

string user1Nickname = "Desired Name"; // Nickname of user1(may differ depending on the user)
BackendFriend.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.
BackendLogin.Instance.CustomLogin("user1", "1234");

BackendFriend.Instance.GetReceivedRequestFriend(); // Loads friend requests list
BackendFriend.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 GetReceivedRequestList error case and AcceptFriend error case.