using System.Collections.Generic;
using System.Text;
using UnityEngine;
using BACKND;
public class Mail {
public bool isCanReceive = false;
public string title;
public string content;
public string inDate;
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();
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();
}
}