using System.Collections.Generic;
using System.Text;
using UnityEngine;
using BackEnd;
public class Post {
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 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();
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();
}
}