4. 뒤끝 서버 베이스 공부
using BackEnd;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
using static BackendGameitemData;
public class BackendGameitemData
{
private static BackendGameitemData _instance = null;
public static BackendGameitemData Instance
{
get
{
if (_instance == null)
{
_instance = new BackendGameitemData();
}
return _instance;
}
}
public UseritemData useritemData;
public class UseritemData
{
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($"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 static UseritemData useritemData;
private string gameDataRowInDate = string.Empty;
//게임 정보 삽입
public void GameDataInsert2()
{
if (useritemData == null)
{
useritemData = new UseritemData();
}
Debug.Log("데이터를 초기화합니다.");
useritemData.info = "친추는 언제나 환영입니다.";
useritemData.equipment.Add("전사의 투구");
useritemData.equipment.Add("강철 갑옷");
useritemData.equipment.Add("헤르메스의 군화");
useritemData.inventory.Add("빨간포션", 1);
useritemData.inventory.Add("하얀포션", 1);
useritemData.inventory.Add("파란포션", 1);
Debug.Log("뒤끝 업데이트 목록에 해당 데이터들을 추가합니다.");
Param param = new Param();
param.Add("info", useritemData.info);
param.Add("equipment", useritemData.equipment);
param.Add("inventory", useritemData.inventory);
Debug.Log("게임정보 데이터 삽입을 요청합니다.");
var bro = Backend.GameData.Insert("USER_ITEM", param);
if (bro.IsSuccess())
{
Debug.Log("게임정보 데이터 삽입에 성공했습니다. : " + bro);
//삽입한 게임정보의 고유값입니다.
gameDataRowInDate = bro.GetInDate();
}
else
{
Debug.LogError("게임정보 데이터 삽입에 실패했습니다. : " + bro);
}
}
//게임 정보 불러오기
public void GameDataGet()
{
Debug.Log("게임 정보 조회 함수를 호출합니다.");
var bro = Backend.GameData.GetMyData("USER_ITEM", new Where());
if (bro.IsSuccess())
{
Debug.Log("게임 정보 조회에 성공했습니다. : " + bro);
LitJson.JsonData gameDataJson = bro.FlattenRows(); // Json으로 리턴된 데이터를 받아옵니다.
// 받아온 데이터의 갯수가 0이라면 데이터가 존재하지 않는 것입니다.
if (gameDataJson.Count <= 0)
{
Debug.LogWarning("데이터가 존재하지 않습니다.");
}
else
{
gameDataRowInDate = gameDataJson[0]["inDate"].ToString(); //불러온 게임정보의 고유값입니다.
useritemData = new UseritemData();
useritemData.info = gameDataJson[0]["info"].ToString();
foreach (string itemKey in gameDataJson[0]["inventory"].Keys)
{
useritemData.inventory.Add(itemKey, int.Parse(gameDataJson[0]["inventory"][itemKey].ToString()));
}
foreach (LitJson.JsonData equip in gameDataJson[0]["equipment"])
{
useritemData.equipment.Add(equip.ToString());
}
Debug.Log(useritemData.ToString());
}
}
else
{
Debug.LogError("게임 정보 조회에 실패했습니다. : " + bro);
}
}
//게임 정보 업데이트
public void GameDataUpdate()
{
if (useritemData == null)
{
Debug.LogError("서버에서 다운받거나 새로 삽입한 데이터가 존재하지 않습니다. Insert 혹은 Get을 통해 데이터를 생성해주세요.");
return;
}
Param param = new Param();
param.Add("info", useritemData.info);
param.Add("equipment", useritemData.equipment);
param.Add("inventory", useritemData.inventory);
BackendReturnObject bro = null;
if (string.IsNullOrEmpty(gameDataRowInDate))
{
Debug.Log("내 제일 최신 게임정보 데이터 수정을 요청합니다.");
bro = Backend.GameData.Update("USER_ITEM", new Where(), param);
}
else
{
Debug.Log($"{gameDataRowInDate}의 게임정보 데이터 수정을 요청합니다.");
bro = Backend.GameData.UpdateV2("USER_DATA", gameDataRowInDate, Backend.UserInDate, param);
}
if (bro.IsSuccess())
{
Debug.Log("게임정보 데이터 수정에 성공했습니다. : " + bro);
}
else
{
Debug.LogError("게임정보 데이터 수정에 실패했습니다. : " + bro);
}
}
}
}
뒤끝 베이스에 있는 코드이다. 이걸 기반으로 설명 한다
일단 내 서버에서 아이템을 저장시키기전에
뒤끝 서버 베이스에 있는 것 정도는 완벽하게 해야될거같아서 돌아가기로 했다.
public string info = string.Empty;
info 라는 데이터에 비어있다.
public Dictionary<string, int> inventory = new Dictionary<string, int>();
'inventory'라는 이름의 사전을 선언하는 부분입니다.
Dictionary 키(KEY) 값 (value) 으로 데이터를 저장하는 자료구조 ( C# 기초 문법)
결국 검색이나 대응 관계에 표현이 쉽다
더 쉽게 설명하면 아까 2장에서 만든 코드보다 훨씬
수정이 편하다라고 말할 수있습니다~
public List<string> equipment =new List<string>();
리스트 여러개의 데이터를 저장시킬수있다.
public ?<자료형> 변수 = new ?<자료형>();
이형식을 기억하자
먼저 Tostring() 이부분은
숫자 123을 "123" 문자열로 바꿀때 이용
override이라는 뜻은 재정의하다
즉 이것을 보면 즉 Tostring을 재정의한다는 뜻입니다.
우리가 원하는 정보를 문자열로 쉽게 바꿔서 볼 수 있게 되는것
StringBuilder에 대한 예시이다.
( 알아두면 진짜 좋을 거 같음 )
string str = "Hello";
str = str + ", World!"; // 새로운 문자열을 만들어서 str에 할당합니다.
StringBuilder sb = new StringBuilder();
sb.Append("Hello");
sb.Append(", World!"); // 기존 문자열에 직접 추가합니다. 훨 편하다
StringBuilder 이랑 AppendLine 은 같이씀
가독성도 좋고 효율 적이라서 .
저 코드를 설명하자면 이제 level : 10 이런식으로
문자열로 저장이되는거임 result 값에 저장이되고
result.ToString(); 값을 불러오면 저장 순서대로 나옴
var은 한번 결정되면 변화 x
foreach(var itemKey in inventory.Keys)
라는 코드는
itemKey부터 inventory.key 까지 하나하나 모든것을
꺼내보자 라는 뜻
여기서 모든것이라는 것이 keys에 속함
keys는 Dictionary 속성임
예시로 이코드는 검:5개 를 차례대로 꺼내보는 코드인것
//2023년 11월11일 12:31 기초 공부 중
//2023년 11월 13일 12:23 기초 복습 중