Skip to main content
Version: 5.9.6

all-code

BACKND Guidelines > 2. Implementing Game Information Functions > Full Code

Game Information Full Code

BackendGameData.cs

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

// Adds 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]}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);

//This is the unique value for the inserted game information.
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(); // Receives 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(); //This is the 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.";
}

// Edit 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("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);
}
}
}

BackendManager.cs

using UnityEngine;
using System.Threading.Tasks;

// Adds BACKND SDK namespace
using BackEnd;

public class BackendManager : MonoBehaviour {
void Start() {
var bro = Backend.Initialize(true); // 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)
async void Test() {
await Task.Run(() => {
BackendLogin.Instance.CustomLogin("user1", "1234"); // BACKND login

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] Locally saved data is changed

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

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