Skip to main content
Version: 5.9.6

all-code

BACKND Guidelines > 3. Implementing Ranking Functions > Full Code

Full Code

BackendRank.cs

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

// Adds BACKND SDK namespace
using BackEnd;

public class BackendRank {
private static BackendRank _instance = null;

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

return _instance;
}
}

public void RankInsert(int score) {
// [Change required] Change the 'copied UUID value' to the UUID value of the ranking created in 'BACKND Console > Ranking Management.'
string rankUUID = "Copied UUID value"; // Example: "4088f640-693e-11ed-ad29-ad8f0c3d4c70"

string tableName = "USER_DATA";
string rowInDate = string.Empty;

// The inDate value of the data used in the game data is required to insert a ranking.
// Therefore, you must load the data and extract its inDate value.
Debug.Log("Attempts data lookup.");
var bro = Backend.GameData.GetMyData(tableName, new Where());

if(bro.IsSuccess() == false) {
Debug.LogError("An error has occurred while looking up data : " + bro);
return;
}

Debug.Log("Data lookup successful : " + bro);

if(bro.FlattenRows().Count > 0) {
rowInDate = bro.FlattenRows()[0]["inDate"].ToString();
} else {
Debug.Log("The data does not exist. Attempting data insertion.");
var bro2 = Backend.GameData.Insert(tableName);

if(bro2.IsSuccess() == false) {
Debug.LogError("An error occurred while inserting data : " + bro2);
return;
}

Debug.Log("Data insertion successful : " + bro2);

rowInDate = bro2.GetInDate();
}

Debug.Log("rowInDate of my game information : " + rowInDate); // The extracted rowIndate value is as follows:

Param param = new Param();
param.Add("level", score);

// Edits the data with the extracted rowIndate to the param value and updates the data for the ranking.
Debug.Log("Attempting ranking insertion.");
var rankBro = Backend.URank.User.UpdateUserScore(rankUUID, tableName, rowInDate, param);

if(rankBro.IsSuccess() == false) {
Debug.LogError("An error has occurred while registering the ranking. : " + rankBro);
return;
}

Debug.Log("Successfully inserted the ranking. : " + rankBro);
}

public void RankGet() {
string rankUUID = "Copied UUID value"; // Example: "4088f640-693e-11ed-ad29-ad8f0c3d4c70"
var bro = Backend.URank.User.GetRankList(rankUUID);

if(bro.IsSuccess() == false) {
Debug.LogError("An error occurred while looking up rankings. : " + bro);
return;
}

Debug.Log("Ranking lookup successful. : " + bro);

Debug.Log("Total number of users registered to the ranking : " + bro.GetFlattenJSON()["totalCount"].ToString());

foreach(LitJson.JsonData jsonData in bro.FlattenRows()) {
StringBuilder info = new StringBuilder();

info.AppendLine("Ranking : " + jsonData["rank"].ToString());
info.AppendLine("Nickname : " + jsonData["nickname"].ToString());
info.AppendLine("Score : " + jsonData["score"].ToString());
info.AppendLine("gamerInDate : " + jsonData["gamerInDate"].ToString());
info.AppendLine("Sort code : " + jsonData["index"].ToString());
info.AppendLine();
Debug.Log(info);
}
}
}

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(() => {
BackendLogin.Instance.CustomLogin("user1", "1234"); // BACKND login method

BackendRank.Instance.RankInsert(100); // [Addition] Ranking registration method
BackendRank.Instance.RankGet(); // [Addition] Ranking loading method

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