Skip to main content
Version: 6.0.0

Full Code

BackndFriend.cs

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

// Adds BACKND SDK namespace
using BACKND;

public class BackndFriend {
private static BackndFriend _instance = null;

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

return _instance;
}
}

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

public void SendFriendRequest(string nickName) {
var inDateBro = Backnd.Player.GetPlayerInfoByDisplayName(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 = Backnd.Friend.SendFriendRequest(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 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);
}

public void GetFriendList() {
var bro = Backnd.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(BACKND.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);
}
}

BackndManager.cs

using UnityEngine;
using System.Threading.Tasks;

// Adds BACKND SDK namespace
using BACKND;

public class BackndManager : MonoBehaviour {
void Start() {
var bro = Backnd.Initialize(); // 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 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


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

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

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