Skip to main content
Version: 5.9.6

all-code

BACKND Guidelines > 8. Implementing Friend Functions > Full Code

Full Code

BackendFriend.cs

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

// Adds BACKND SDK namespace
using BackEnd;

public class BackendFriend {
private static BackendFriend _instance = null;

public static BackendFriend Instance {
get {
if(_instance == null) {
_instance = new BackendFriend();
}

return _instance;
}
}

private List<Tuple<string, string>> _requestFriendList = new List<Tuple<string, string>>();

public void SendFriendRequest(string nickName) {
var inDateBro = Backend.Social.GetUserInfoByNickName(nickName);

if(inDateBro.IsSuccess() == false) {
Debug.LogError("An error occurred while searching the name of the user. : " + inDateBro);
return;
}

string inDate = inDateBro.GetReturnValuetoJSON()["row"]["inDate"].ToString();

Debug.Log($"inDate value of {nickName} is {inDate}.");

var friendBro = Backend.Friend.RequestFriend(inDate);

if(friendBro.IsSuccess() == false) {
Debug.LogError($"{inDate} An error occurred while sending a friend request. : " + friendBro);
return;
}

Debug.Log("Friend request sent successfully." + friendBro);
}

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);
}

public void GetFriendList() {
var bro = Backend.Friend.GetFriendList();

if(bro.IsSuccess() == false) {
Debug.LogError("An error occurred while loading the friends list. : " + bro);
return;
}

Debug.Log("Successfully loaded the friends list. : " + bro);

if(bro.FlattenRows().Count <= 0) {
Debug.Log("There are no friends.");
return;
}

int index = 0;

string friendListString = "Friends List\n";

foreach(LitJson.JsonData friendJson in bro.FlattenRows()) {
string nickName = friendJson["nickname"]?.ToString();
string inDate = friendJson["inDate"].ToString();

friendListString += $"{index}. {nickName} - {inDate}\n";
index++;
}

Debug.Log(friendListString);
}
}

BackendManager.cs

using UnityEngine;
using System.Threading.Tasks;

// Adds BACKND SDK namespace
using BackEnd;

public class BackendManager : MonoBehaviour {
void Start() {
var bro = Backend.Initialize(true); // Initialize BACKND

// Response value for BACKND initialization
if(bro.IsSuccess()) {
Debug.Log("Initialization successful : " + bro); // If successful, 'statusCode 204 Success'
} else {
Debug.LogError("Initialization failed : " + bro); // If failed, a 4xx statusCode error occurs
}

Test();
}

// A method that allows synchronous methods to be called from asynchronous methods(cannot be accessed by the Unity UI)
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


// Must be logged in as the user to which the friend request was sent in Step 1.
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

BackendFriend.Instance.GetFriendList(); // Loads friends list

Debug.Log("Test complete.");
});
}
}