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]}");
}
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 = "New friends are 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("Requesting 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("Call 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("Requesting my latest game information data to be modified.");
bro = Backend.GameData.Update("USER_DATA", new Where(), param);
}
else
{
Debug.Log($"Requesting the game information data of {gameDataRowInDate} to be modified.");
bro = Backend.GameData.UpdateV2("USER_DATA", gameDataRowInDate, Backend.UserInDate, param);
}
if (bro.IsSuccess())
{
Debug.Log("Successfully modified game information data. : " + bro);
}
else
{
Debug.LogError("Failed to modify game information data. : " + bro);
}
}
}