Skip to main content
Version: 6.0.0

Full Code

BackndMail

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

// Adds BACKND SDK namespace
using BACKND;

public class Mail {
public bool isCanReceive = false;

public string title; // Mail title
public string content; // Mail content
public string inDate; // Mail inDate

// The string is the name of the mail item and the int is the count
public Dictionary<string, int> postReward = new Dictionary<string, int>();

public override string ToString() {
string result = string.Empty;
result += $"title : {title}\n";
result += $"content : {content}\n";
result += $"inDate : {inDate}\n";

if(isCanReceive) {
result += "Mail Item\n";

foreach(string itemKey in postReward.Keys) {
result += $"| {itemKey} : {postReward[itemKey]}ea\n";
}
} else {
result += "Unsupported mail item.";
}

return result;
}
}

public class BackndMail {
private static BackndMail _instance = null;

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

return _instance;
}
}

private List<Post> _mailList = new List<Post>();

public void SavePostToLocal(BACKND.LitJson.JsonData item) {
foreach(BACKND.LitJson.JsonData itemJson in item) {
if(itemJson["item"].ContainsKey("itemType")) {
int itemId = int.Parse(itemJson["item"]["itemId"].ToString());
string itemType = itemJson["item"]["itemType"].ToString();
string itemName = itemJson["item"]["itemName"].ToString();
int itemCount = int.Parse(itemJson["itemCount"].ToString());

if(BackndGameData.userData.inventory.ContainsKey(itemName)) {
BackndGameData.userData.inventory[itemName] += itemCount;
} else {
BackndGameData.userData.inventory.Add(itemName, itemCount);
}

Debug.Log($"Item received. : {itemName} - {itemCount}ea");
} else {
Debug.LogError("Unsupported item.");
}
}
}

public void MailListGet(MailType mailType) {
var bro = Backnd.Mail.GetMailList(mailType);

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

Debug.Log("Successfully requested mail list to be loaded. : " + bro);

if(bro.GetFlattenJSON()["postList"].Count <= 0) {
Debug.LogWarning("There is no mail to receive.");
return;
}

foreach(BACKND.LitJson.JsonData postListJson in bro.GetFlattenJSON()["postList"]) {
Post post = new Post();

post.title = postListJson["title"].ToString();
post.content = postListJson["content"].ToString();
post.inDate = postListJson["inDate"].ToString();

// Item in the mail
if(mailType == MailType.User) {
if(postListJson["itemLocation"]["tableName"].ToString() == "USER_DATA") {
if(postListJson["itemLocation"]["column"].ToString() == "inventory") {
foreach(string itemKey in postListJson["item"].Keys) post.postReward.Add(itemKey, int.Parse(postListJson["item"][itemKey].ToString()));
} else {
Debug.LogWarning("This column information is not supported yet. : " + postListJson["itemLocation"]["column"].ToString());
}
} else {
Debug.LogWarning("This table information is not supported yet. : " + postListJson["itemLocation"]["tableName"].ToString());
}
} else {
foreach(BACKND.LitJson.JsonData itemJson in postListJson["items"]) {
if(itemJson["chartName"].ToString() == "Item DataTable") {
string itemName = itemJson["item"]["itemName"].ToString();
int itemCount = int.Parse(itemJson["itemCount"].ToString());

if(post.postReward.ContainsKey(itemName)) {
post.postReward[itemName] += itemCount;
} else {
post.postReward.Add(itemName, itemCount);
}

post.isCanReceive = true;
} else {
Debug.LogWarning("This chart information is not supported yet. : " + itemJson["chartName"].ToString());
post.isCanReceive = false;
}
}
}

_mailList.Add(post);
}

for(int i = 0; i < _mailList.Count; i++) {
Debug.Log($"Mail No. {i}\n" + _mailList[i].ToString());
}
}

public void MailReceive(MailType mailType, int index) {
if(_mailList.Count <= 0) {
Debug.LogWarning("There is no mail to receive. Or, call the mail list loading first.");
return;
}

if(index >= _mailList.Count) {
Debug.LogError($"The mail does not exist: request index{index} / maximum mail count : {_mailList.Count}");
return;
}

Debug.Log($"{_mailList[index].inDate} of {mailType.ToString()} Requesting mail claim.");

var bro = Backnd.Mail.ReadMail(mailType, _mailList[index].inDate);

if(bro.IsSuccess() == false) {
Debug.LogError($"{_mailList[index].inDate} of {mailType.ToString()} An error occurred while claiming the mail. : " + bro);
return;
}

Debug.Log($"{_mailList[index].inDate} of {mailType.ToString()} Successfully claimed mail. : " + bro);

_mailList.RemoveAt(index);

if(bro.GetFlattenJSON()["postItems"].Count > 0) {
SavePostToLocal(bro.GetFlattenJSON()["postItems"]);
} else {
Debug.LogWarning("There is no mail item to be claimed.");
}

BackndGameData.Instance.PlayerTableUpdate();
}

public void MaiReceivelAll(MailType mailType) {
if(_mailList.Count <= 0) {
Debug.LogWarning("There is no mail to receive. Or, call the mail list loading first.");
return;
}

Debug.Log($"{mailType.ToString()} Requesting all pieces of mail to be claimed.");

var bro = Backnd.Mail.ReadAllMail(mailType);

if(bro.IsSuccess() == false) {
Debug.LogError($"{mailType.ToString()} An error occurred while claiming all mail : " + bro);
return;
}

Debug.Log("Successfully claimed all mail. : " + bro);

_mailList.Clear();

foreach(BACKND.LitJson.JsonData postItemsJson in bro.GetFlattenJSON()["postItems"]) {
SavePostToLocal(postItemsJson);
}

BackndGameData.Instance.PlayerTableUpdate();
}
}

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(() => {
BackndLogin.Instance.CustomSignIn("user1", "1234");

// Loads game data and saves it locally.(caching)
BackndGameData.Instance.PlayerTableGet();

// Loads the mail list and saves the inDate values locally.
BackndMail.Instance.MailListGet(MailType.Admin);

// Claims the mail by reading the position of the saved piece of mail. Here, the index is the order of the mail; 0 is the topmost and 1 is the next.
BackndMail.Instance.MailReceive(MailType.Admin,0);

// Claims all pieces of mail that have been looked up.
BackndMail.Instance.MaiReceivelAll(MailType.Admin);

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