본문 바로가기

C#/Problems

2020-04-25 업적시스템 복습(2) 에러코드

-----------------------------------------------------------------------------------------------------------------------------------

예제 문제.

처음에 업적이랑 리워드랑 데이터를 나눠서 할까하다가 더 헷갈릴거 같아서 그냥 업적데이터만 만들었다.

-----------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------------

뜻밖의 에러...

업적 정보 인포파일까지는 괜찮은데, 여기서 클래스 하나만 더 생겨도 왜이리 헷갈리는지 모르겠다.

-----------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------------

키 값이 없이 나온 문제의 Json 파일

왜요? 키를 왜 안주시죠?

-----------------------------------------------------------------------------------------------------------------------------------

 

-----------------------------------------------------------------------------------------------------------------------------------

키를 주지 않은 문제의 코드

문제는 AchievementInfo에 너무 적응이 되버려서, 그냥 생각도안하고 코딩한 결과 였다.

저기서 배열을 만들어 뜬금없는 AchievementInfo를 모아다가 이름만 game_info.json으로 저장했다.

GameInfo를 만들어놓고 쓰질못하니.. Data Manager라는 개념과 Game Launcher 개념이 들어오면 얼마나

헤매일지 눈에 선하다...

결론: AchievementInfo로 초장부터 장난질했다...

-----------------------------------------------------------------------------------------------------------------------------------

 

-----------------------------------------------------------------------------------------------------------------------------------

잘될거라 생각한 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
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
 
namespace Repeat_015
{
    class App
    {
        private const string DATA_PATH = "./data";
 
        private const string GAME_INFO_FILENAME = "game_info.json";
        private const string ACHIEVEMENT_DATA_FILENAME = "achievement_data.json";
        
 
       
        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;
                    AchievementInfo info = new AchievementInfo(data.id,0);
                    this.gameInfo.dicAchievementInfo.Add(info.id, info);
                }
 
                //신규유저면 한번 저장
                //-------------------------------------------------------
                int length = this.gameInfo.dicAchievementInfo.Count;
                var arrInfo = new AchievementInfo[length];
 
                int inx = 0;
 
                foreach (var pair in this.gameInfo.dicAchievementInfo)
                {
                    arrInfo[inx++= pair.Value;
                }
 
                string infojson = JsonConvert.SerializeObject(arrInfo);
                string fullpath = string.Format("{0}/{1}", DATA_PATH, GAME_INFO_FILENAME);
                File.WriteAllText(fullpath, infojson);
 
                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;
using System.Threading.Tasks;
 
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;
using System.Threading.Tasks;
 
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;
using System.Threading.Tasks;
 
namespace Repeat_015
{
    class GameInfo
    {
        public Dictionary<int, AchievementInfo> dicAchievementInfo;
        public GameInfo()
        {
            this.Init();    //초기화 메서드 호출
        }
 
        public void Init() //초기화 메서드 정의
        {
            this.dicAchievementInfo = new Dictionary<int, AchievementInfo>();
        }
 
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------