수업 013 (04.22 2020) dictionary, json // Achievement
2020. 4. 22. 12:38
728x90
1. App.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace Study._014
{
class App
{
Dictionary<int, AchievementData> dicAchievementDatas;
Dictionary<int, AchievementInfo> dicAchievementInfos;
public App()
{
string json = File.ReadAllText(path);
AchievementData[] arrAchievementDatas = JsonConvert.DeserializeObject<AchievementData[]>(json); // 역직렬화(deserializeObject)
this.dicAchievementDatas = new Dictionary<int, AchievementData>();
foreach (AchievementData data in arrAchievementDatas)
{
}
AchievementData achievementData = dicAchievementDatas[1000];
Console.WriteLine(desc);
Console.WriteLine();
this.dicAchievementInfos = new Dictionary<int, AchievementInfo>();
// Info 파일 불러 오기
// 해당 파일이 있냐? bool type
if (File.Exists(infoPath))
{
// 기존 유저
Console.WriteLine("*** 기존 유저 ***");
// 파일 불러오기
string infojson = File.ReadAllText(infoPath);
// 문자열 -> 객체 (역직렬화)
AchievementInfo[] arrInfos = JsonConvert.DeserializeObject<AchievementInfo[]>(infojson);
// 배열 -> 사전
foreach (AchievementInfo info in arrInfos)
{
}
}
else
{
// 신규 유저
Console.WriteLine("*** 신규 유저 ***");
// info 객체 생성하고 사전에 추가한다.
foreach (KeyValuePair<int, AchievementData> pair in this.dicAchievementDatas)
{
AchievementData data = pair.Value;
}
this.SaveAchievements();
}
// 데이터 준비 끝
// AchievementInfo 클래스 생성
// Data로 끝나는 클래스는 보통 엑셀 테이블의 값을 가지고 있는
// 매핑 클래스, 값을 변경하면 안됨
// Info로 끝나는 클래스는 보통 저장해야할 값을 가지고 있는
// 클래스, 값이 변경될 수 있음
// (*) 변경될 가능성이 있는것만 멤버로 선언할것
this.PrintAchievement(1000); // 업적보기
// this.DoAchievement(1000); // 업적하기
// this.SaveAchievements(); // 업적 파일로 저장하기
}
public void PrintAchievement(int id)
{
AchievementData data = this.dicAchievementDatas[id];
AchievementInfo info = this.dicAchievementInfos[id];
// id:1000 name:망루 철거자
}
public void DoAchievement(int id) // 업적을 함
{
AchievementData data = this.dicAchievementDatas[id];
// new AchievementInfo(id, 1); // 인스턴스를 계속 생성 (x) 하나의 인스턴스 데이터 만들고 info 만들라!
Console.WriteLine("업적 1개를 했습니다.");
// 1000번에 해당하는 AchievementInfo객체를 찾아서
AchievementInfo info = this.dicAchievementInfos[id];
// 멤버변수 count 값을 +1 한다.
// info 객체의 count값을 출력
}
// 기능이 다르면 따로 써라
// 업적들 저장하기
public void SaveAchievements()
{
// json 형식(배열형식) 의 문자열 -> 객체 (배열) (위에서 한거)
// ** 객체에 있는 것을 문자열로 (위에서 한거의 반대)
// 사전에 있는 AchievementInfo 객체들을 배열에 담는다
AchievementInfo[] arr = new AchievementInfo[length];
int idx = 0;
foreach (var pair in this.dicAchievementInfos)
{
}
foreach(var info in arr)
{
Console.WriteLine(format);
}
Console.WriteLine("\n** 객체 > json 형식의 문자열 **");
// 객체 -> json 형식의 문자열로 변환
string json = JsonConvert.SerializeObject(arr);
Console.WriteLine(json);
// 파일 저장
File.WriteAllText(path, json);
}
}
}
|
2. AchievementData.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._014
{
class AchievementData
{
public int id;
public string name;
public int goal;
public int reward_type;
public int reward_amount;
public string description;
}
}
|
3. AchievementInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._014
{
class AchievementInfo
{
public int id; // id 업적
public int count; // 업적 진행 횟수
// 생성자
public AchievementInfo(int id, int count)
{
this.id = id;
this.count = count;
}
}
}
|
728x90
'C# > Study' 카테고리의 다른 글
수업 014 (04.23 2020) - DateTime (0) | 2020.04.24 |
---|---|
수업 014 (04.23 2020) 복습 - json, dictionary..etc (0) | 2020.04.23 |
수업 013 (04.22 2020) 복습 - dictionary<Tkey, TValue> (0) | 2020.04.22 |
수업 012 (04.21 2020) List<> HerbBag (0) | 2020.04.22 |
수업 012 (04.21 2020) List<> add, remove, enum (0) | 2020.04.21 |