Skip to main content
Version: 5.15.0

Full Game Information Code

BackendGameData.cs

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

// Add BACKND SDK namespace
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>();

// This is a method for data debugging. (Debug.Log(UserData);)
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);

// Unique value of the inserted game information.
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(); // Receive data returned in JSON format.

// If the number of returned data cases is 0, the data does not exist.
if (gameDataJson.Count <= 0)
{
Debug.LogWarning("The data does not exist.");
}
else
{
gameDataRowInDate = gameDataJson[0]["inDate"].ToString(); //Unique value of the loaded game information.

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.";
}

// Modify game information
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);
}
}
}

BackendManager.cs

using UnityEngine;


// Add BACKND SDK namespace
using BackEnd;

public class BackendManager : MonoBehaviour
{
void Start()
{
var bro = Backend.Initialize(); // 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)
void Test()
{
BackendLogin.Instance.CustomLogin("user1", "1234"); // Log in to BACKND

BackendGameData.Instance.GameDataGet(); // Data insertion method

// [Addition] When the loaded data from the server does not exist, new data is created and inserted
if (BackendGameData.userData == null)
{
BackendGameData.Instance.GameDataInsert();
}

BackendGameData.Instance.LevelUp(); // [Addition] Change locally saved data

BackendGameData.Instance.GameDataUpdate(); // [Addition] Overwrite data saved in the server (only parts that have changed)

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