내일배움캠프 18일차 TIL - 사용자 데이터와 테이블
오늘의 학습 키워드
사용자 데이터와 데이터 테이블 나누기
공부한 내용
아이템 클래스 역할 나누기
아래의 상황에서 아이템 클래스 배열을 Json화 하여 변경되는 사용자 데이터도 반영시키는 것이 목표였다.
namespace TextRPG_Team
{
public enum ItemType
{
Equipment,
Consumable,
}
public class Item
{
public int Id { get; set; }
public string Name { get; }
public string Description { get; }
public ItemType Type { get; }
public bool IsHave { get; set; }
public int BuyGold { get; }
public int SellGold { get; }
public class Equipment
{
public int Atk { get; }
public int Def { get; }
public bool IsEquiped { get; set; }
public Equipment(int atk, int def, bool isEquiped)
{
Atk = atk;
Def = def;
IsEquiped = isEquiped;
}
}
public class Consumable
{
public int Count { get; set; }
public int Amount { get; }
public Consumable(int count, int amount)
{
Count = count;
Amount = amount;
}
}
public Equipment EquipmentData { get; set; }
public Consumable ConsumableData { get; set; }
public Item(int id, string name, string description, ItemType type, bool isHave, int buyGold, int sellGold)
{
Id = id;
Name = name;
Description = description;
Type = type;
IsHave = isHave;
BuyGold = buyGold;
SellGold = sellGold;
}
}
}
하지만 Item 배열에 다운캐스팅 한다고 해서 서브 클래스인 Equipment와 Consumable의 데이터가 Item으로 담길리는 없다.
그래서 구조를 바꾸기로 결정했다.
첫 번째 아이디어 : Item을 상속받는 Equipment와 Consumable이 있고 이미 Item배열인 Items가 있는 현재 구조에서 Equipment 배열과 Consumable 배열을 만들어 json으로 따로 관리하려 했으나 메모리 낭비라고 생각이 듬
두 번째 아이디어 : 그래서 둘의 데이터는 같이 관리하되 장착했는지 여부와 몇 개인지는 Item에 통합하여 관리하도록 함 (사용자 데이터 관련한 부분들은 따로 테이블을 만들어 세팅하는 방식)
핵심은 아이템 테이블과 사용자 데이터를 분리하는 것이었다. - 추후 세팅
타입으로 판단하여 Item 안의 해당 타입일때만 이너 클래스를 생성하도록 하였다.
namespace TextRPG_Team
{
public enum ItemType
{
Equipment,
Consumable,
}
public class Item
{
public int Id { get; set; }
public string Name { get; }
public string Description { get; }
public ItemType Type { get; }
public bool IsHave { get; set; }
public int BuyGold { get; }
public int SellGold { get; }
public class Equipment
{
public int Atk { get; }
public int Def { get; }
public bool IsEquiped { get; set; }
public Equipment(int atk, int def, bool isEquiped)
{
Atk = atk;
Def = def;
IsEquiped = isEquiped;
}
}
public class Consumable
{
public int Count { get; set; }
public int Amount { get; }
public Consumable(int count, int amount)
{
Count = count;
Amount = amount;
}
}
public Equipment EquipmentData { get; set; }
public Consumable ConsumableData { get; set; }
public Item(int id, string name, string description, ItemType type, bool isHave, int buyGold, int sellGold)
{
Id = id;
Name = name;
Description = description;
Type = type;
IsHave = isHave;
BuyGold = buyGold;
SellGold = sellGold;
}
}
}
오늘의 회고
오늘은 아이템 사용자 데이터 세팅과 관련해서 작업을 진행했다. 몇 시간 동안 머리를 부여 잡고 고민을 했는데 해결 방법이 잘 떠오르지 않아 힘들었던 것 같다. 하지만 방법을 알게되고 가슴이 막막한 기분을 떨쳐내고 후련하다고 느꼈다. 어려운 과제였다고 느꼈지만 알고 나니까 아무것도 아니었다는 생각도 들고 후련하기도 하다. 이를 동기부여 삼아서 더 열심히 할 수 있을 것 같다.
내일은 이번에 진행한 텍스트 RPG의 제출일이다. 12시에 바로 제출해야 하니 내일은 주석 정리, 버그 체크, Readme 작성에 집중하도록 해야겠다. 과제 잘 마무리 해보자. 내일도 파이팅!
참고 :
https://askforyou.tistory.com/58
[c#] JSON.NET 을 이용한 Json 데이터 파싱 - Json Key Name 가져오기 (JObject, JToken)
[c#] JSON 형변환(Json Convert) with JSON.NET [c#] JSON 형변환(Json Convert) with JSON.NET 어떤 언어든 코딩을 하면서 많이 사용하는것 중의 하나가 JSON 입니다. C# 에서는 JSON 을 사용할때 가장 많이 이용하는 것
askforyou.tistory.com