Skip to main content
Version: 5.11.6

Full Code

BackendPost

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

// Adds BACKND SDK namespace
using BackEnd;

public class Post {
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 BackendPost {
private static BackendPost _instance = null;

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

return _instance;
}
}

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

public void SavePostToLocal(LitJson.JsonData item) {
foreach(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(BackendGameData.userData.inventory.ContainsKey(itemName)) {
BackendGameData.userData.inventory[itemName] += itemCount;
} else {
BackendGameData.userData.inventory.Add(itemName, itemCount);
}

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

public void PostListGet(PostType postType) {
var bro = Backend.UPost.GetPostList(postType);

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(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(postType == PostType.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(LitJson.JsonData itemJson in postListJson["items"]) {
if(itemJson["chartName"].ToString() == "Item Chart") {
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;
}
}
}

_postList.Add(post);
}

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

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

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

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

var bro = Backend.UPost.ReceivePostItem(postType, _postList[index].inDate);

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

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

_postList.RemoveAt(index);

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

BackendGameData.Instance.GameDataUpdate();
}

public void PostReceiveAll(PostType postType) {
if(_postList.Count <= 0) {
Debug.LogWarning("There is no mail to receive. Or, call the mail list loading first.");
return;
}

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

var bro = Backend.UPost.ReceivePostItemAll(postType);

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

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

_postList.Clear();

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

BackendGameData.Instance.GameDataUpdate();
}
}

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

// Loads game data and saves it locally.(caching)
BackendGameData.Instance.GameDataGet();

// Loads the mail list and saves the inDate values locally.
BackendPost.Instance.PostListGet(PostType.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.
BackendPost.Instance.PostReceive(PostType.Admin,0);

// Claims all pieces of mail that have been looked up.
BackendPost.Instance.PostReceiveAll(PostType.Admin);

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