이게대체 왜 클래스 하나밖에 안늘었는데, 관리하는 클래스가 달라진다고 이렇게 어려울것인가...
너무나도 어려워서 몸둘바를 모를정도로 어렵다.
수업당시에는 이해가 가는 부분도 있고 안가는 부분도 있는데, 강사님이 설명을 정말 잘해주셔서
말로는 이해가 다되지만, 머리로 생각해서 다시 만들라고만 하면 모르겠다.
특히 GameInfo가 AchievementInfo를 관리해서 저장을 하는데,
id가 어디로가고 Value값이 어디로갔다가 저기로 가고
저기있는줄 알았는데 사실 여기있고, 점점 꼬이는거 같아서 내머리도 같이 꼬인다.
이번에도 복습하면서 이해한거 같으면서도, 생각하려고 하면 바로 뇌정지가 온다.
언제쯤 시원하게 이해할 수 있을까..
-----------------------------------------------------------------------------------------------------------------------------------
예제 문제
-----------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------------
Json file
-----------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------------
신규유저 출력값
-----------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------------
기존유저 출력값
-----------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------------
App Class
-----------------------------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace Repeat_015
{
class App
{
private const string DATA_PATH = "./data";
Dictionary<int, AchievementData> dicAchievementData; //업적 데이터 사전
GameInfo gameInfo; //게임 인포 파일
//생성자
public App()
{
this.Init(); //초기화 함수
string json = this.LoadData(DATA_PATH, ACHIEVEMENT_DATA_FILENAME); //로드 데이터 메서드 호출
var arrAchievementData = JsonConvert.DeserializeObject<AchievementData[]>(json); //역직렬화
this.dicAchievementData = arrAchievementData.ToDictionary(x => x.id, x => x); //딕셔너리에 저장
if (this.CheckNewbie())
{
Console.WriteLine("신규 유저 입니다.");
foreach (var pair in this.dicAchievementData)
{
var data = pair.Value;
}
//신규유저면 한번 저장
var gameInfoJson = JsonConvert.SerializeObject(this.gameInfo);
var fullPath = string.Format("{0}/{1}", DATA_PATH, GAME_INFO_FILENAME);
File.WriteAllText(fullPath, gameInfoJson);
if (File.Exists(fullPath))
{
Console.WriteLine("파일이 저장되었습니다.");
}
}
else
{
string infojson = this.LoadData(DATA_PATH, GAME_INFO_FILENAME);
this.gameInfo = JsonConvert.DeserializeObject<GameInfo>(infojson);
}
}
//메서드
public void Init()
{
this.dicAchievementData = new Dictionary<int, AchievementData>();
this.gameInfo = new GameInfo();
}
public string LoadData(string path, string filename) //로드데이터 메서드
{
string fullpath = string.Format("{0}/{1}", path, filename);
return File.ReadAllText(fullpath);
}
public bool CheckNewbie()
{
bool isNewbie = true;
if (File.Exists(string.Format("{0}/{1}", DATA_PATH, GAME_INFO_FILENAME)))
{
Console.WriteLine("기존 유저 입니다.");
isNewbie = false;
}
return isNewbie;
}
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
AchievementData Class
-----------------------------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Repeat_015
{
class AchievementData
{
public int id;
public string name;
public string desc;
public int goal;
public int reward_type;
public int reward_amount;
public string icon_reward;
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
AchievementInfo Class
-----------------------------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Repeat_015
{
class AchievementInfo
{
public int id;
public int count;
public AchievementInfo(int id, int count)
{
this.id = id;
this.count = count;
}
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
GameInfo Class
-----------------------------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Repeat_015
{
class GameInfo
{
public Dictionary<int, AchievementInfo> dicAchievementInfo;
public GameInfo()
{
this.Init(); //초기화 메서드 호출
}
public void Init() //초기화 메서드 정의
{
this.dicAchievementInfo = new Dictionary<int, AchievementInfo>();
}
}
}
|
'C# > 복습' 카테고리의 다른 글
2020-04-30 삼항연산자, Null병합연산자, 연산자 우선순위 (0) | 2020.04.30 |
---|---|
2020-04-28 저장 예제만들어 복습 (0) | 2020.04.29 |
2020-04-25 업적시스템 복습 & 생각정리 1 (0) | 2020.04.25 |
메서드반환 문제2 (0) | 2020.04.21 |
메서드반환 문제 1 (0) | 2020.04.21 |