using System.Collections.Generic;
using System.Text;
using UnityEngine;
using BackEnd;
public class UserData {
public int level = 1;
public float atk = 3.5f;
public string info = string.Empty;
public Dictionary<string, int> inventory = new Dictionary<string, int>();
public List<string> equipment = new List<string>();
public override string ToString() {
StringBuilder result = new StringBuilder();
result.AppendLine($"level : {level}");
result.AppendLine($"atk : {atk}");
result.AppendLine($"info : {info}");
result.AppendLine($"inventory");
foreach(var itemKey in inventory.Keys) {
result.AppendLine($"| {itemKey} : {inventory[itemKey]}ea");
}
result.AppendLine($"equipment");
foreach(var equip in equipment) {
result.AppendLine($"| {equip}");
}
return result.ToString();
}
}
public class BackendGameData {
private static BackendGameData _instance = null;
public static BackendGameData Instance {
get {
if(_instance == null) {
_instance = new BackendGameData();
}
return _instance;
}
}
public static UserData userData;
private string gameDataRowInDate = string.Empty;
public void GameDataInsert() {
if(userData == null) {
userData = new UserData();
}
Debug.Log("Resets data.");
userData.level = 1;
userData.atk = 3.5f;
userData.info = "Adding friends is always welcome.";
userData.equipment.Add("Warrior's Helm");
userData.equipment.Add("Steel Armor");
userData.equipment.Add("Hermes Military Boots");
userData.inventory.Add("Red Potion", 1);
userData.inventory.Add("White Potion", 1);
userData.inventory.Add("Blue Potion", 1);
Debug.Log("Adds these pieces of data to the BACKND update list.");
Param param = new Param();
param.Add("level", userData.level);
param.Add("atk", userData.atk);
param.Add("info", userData.info);
param.Add("equipment", userData.equipment);
param.Add("inventory", userData.inventory);
Debug.Log("Requests the insertion of game information data.");
var bro = Backend.GameData.Insert("USER_DATA", param);
if(bro.IsSuccess()) {
Debug.Log("Successfully inserted game information data. : " + bro);
gameDataRowInDate = bro.GetInDate();
} else {
Debug.LogError("Failed to insert game information data. : " + bro);
}
}
public void GameDataGet() {
Debug.Log("Calls the game information lookup method.");
var bro = Backend.GameData.GetMyData("USER_DATA", new Where());
if(bro.IsSuccess()) {
Debug.Log("Game information lookup successful. : " + bro);
LitJson.JsonData gameDataJson = bro.FlattenRows();
if(gameDataJson.Count <= 0) {
Debug.LogWarning("The data does not exist.");
} else {
gameDataRowInDate = gameDataJson[0]["inDate"].ToString();
userData = new UserData();
userData.level = int.Parse(gameDataJson[0]["level"].ToString());
userData.atk = float.Parse(gameDataJson[0]["atk"].ToString());
userData.info = gameDataJson[0]["info"].ToString();
foreach(string itemKey in gameDataJson[0]["inventory"].Keys) {
userData.inventory.Add(itemKey, int.Parse(gameDataJson[0]["inventory"][itemKey].ToString()));
}
foreach(LitJson.JsonData equip in gameDataJson[0]["equipment"]) {
userData.equipment.Add(equip.ToString());
}
Debug.Log(userData.ToString());
}
} else {
Debug.LogError("Failed to look up game information. : " + bro);
}
}
public void LevelUp() {
Debug.Log("Increases the level by 1.");
userData.level += 1;
userData.atk += 3.5f;
userData.info = "Changes details.";
}
public void GameDataUpdate() {
if(userData == null) {
Debug.LogError("Data downloaded from the server or newly inserted data does not exist. Use "Insert" or "Get" to create data.");
return;
}
Param param = new Param();
param.Add("level", userData.level);
param.Add("atk", userData.atk);
param.Add("info", userData.info);
param.Add("equipment", userData.equipment);
param.Add("inventory", userData.inventory);
BackendReturnObject bro = null;
if(string.IsNullOrEmpty(gameDataRowInDate)) {
Debug.Log("Requests my latest game information data to be edited.");
bro = Backend.GameData.Update("USER_DATA", new Where(), param);
} else {
Debug.Log($"Requests {gameDataRowInDate} to be edited.");
bro = Backend.GameData.UpdateV2("USER_DATA", gameDataRowInDate, Backend.UserInDate, param);
}
if(bro.IsSuccess()) {
Debug.Log("Successfully edited game information data. : " + bro);
} else {
Debug.LogError("Failed to edit game information data. : " + bro);
}
}
}