C#/수업내용

2020.04.23. 수업내용 - json 파일 읽고 쓰기(불러오기, 저장하기) 3, 파일 2개 연습

dev_sr 2020. 4. 23. 23:52

 

 

오늘 배운 테이블 2개를 불러오는 방법과 선생님께서 카페에 올려주신 코드를 참고했습니다.

 

 

엑셀데이터

 

1) achievement_data

 

2) reward_data

 

1. 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_015
{
    class App
    {
 
        private AchievementData[] arrAchievementDatas;
        private RewardData[] arrRewardData;
        private Dictionary<int, AchievementData> dicAchievementDatas;
        private Dictionary<int, RewardData> dicRewardData;
        private GameInfo gameInfo;
 
        public App()
        {
           #region 테이블 2개 가져오고 저장하고 다시 불러오기
 
            this.Init();
 
            //json 파일 불러오기
            string jsonData = File.ReadAllText("./achievement_data.json");
            string jsonReward = File.ReadAllText("./reward_data.json");
 
            this.arrAchievementDatas = JsonConvert.DeserializeObject<AchievementData[]>(jsonData);
            this.arrRewardData = JsonConvert.DeserializeObject<RewardData[]>(jsonReward);
 
            //사전에 값 넣기
            foreach (AchievementData data in arrAchievementDatas)
            {
                this.dicAchievementDatas.Add(data.id, data);
            }
 
            foreach (RewardData reward in arrRewardData)
            {
                this.dicRewardData.Add(reward.reward_id, reward);
            }
 
 
            //기존유저 가려내기
            if (File.Exists("./achievement_info.json"))
            {
                string jsonInfo = File.ReadAllText("./achievement_info.json");
 
                AchievementInfo[] newArrInfo = JsonConvert.DeserializeObject<AchievementInfo[]>(jsonInfo);
                //this.gameInfo = JsonConvert.DeserializeObject<GameInfo>(jsonInfo);
 
                foreach(AchievementInfo info in newArrInfo)
                {
                    this.gameInfo.dicAcievementInfo.Add(info.id, info);
                }
                
 
                //foreach(KeyValuePair<int, AchievementInfo> pair in this.gameInfo.dicAcievementInfo)
                //{
                //    Console.WriteLine("{0},{1}",pair.Key,pair.Value.count);
                //}
            }
 
            //신규유저
            else
            {
                foreach (KeyValuePair<int, AchievementData> pair in this.dicAchievementDatas)
                {
                    AchievementData data = pair.Value;
                    AchievementInfo info = new AchievementInfo(data.id, 0);
                    this.gameInfo.dicAcievementInfo.Add(info.id, info);
                }
                this.SaveData();
            }
 
 
            //출력하기
            this.PrintAchievement(1000);
 
            //수행하기
            //this.DoAchieve(1000);
 
            //저장하기
            //this.SaveData();
        }
 
        public void Init()
        {
          
            this.dicAchievementDatas = new Dictionary<int, AchievementData>();
            this.dicRewardData = new Dictionary<int, RewardData>();
 
 
            //Info (id, count)값을 넣을 dictionary를 생성할 객체생성
            gameInfo = new GameInfo();
 
        }
 
 
        public void PrintAchievement(int id)
        {
            AchievementData data = this.dicAchievementDatas[id];
            RewardData reward = this.dicRewardData[data.reward_id];
            AchievementInfo info = this.gameInfo.dicAcievementInfo[id];
 
            foreach (KeyValuePair<int, AchievementData> pair in this.dicAchievementDatas)
            {
                if (pair.Key == id)
                {
                    Console.WriteLine("id: {0}\t\t 이름: {1}\t 설명: {2}\t "pair.Key, data.name, data.desc);
                    Console.WriteLine("보상종류: {0}\t 보상양: {1}\t 보상아이콘: {2}\t 목표: {3}/{4}"
                        , reward.reward_name, data.reward_amount, reward.reward_icon, info.count, data.goal);
                }
            }
        }
 
        public void DoAchieve(int id)
        {
            AchievementData data = this.dicAchievementDatas[id];
            RewardData reward = this.dicRewardData[data.reward_id];
            AchievementInfo info = this.gameInfo.dicAcievementInfo[id];
 
            foreach (KeyValuePair<int, AchievementData> pair in this.dicAchievementDatas)
            {
                if (pair.Key == id)
                {
                    Console.WriteLine("\n업적을 1개 수행했습니다.\n");
                    info.count++;
 
                    Console.WriteLine("id: {0}\t\t 이름: {1}\t 설명: {2}\t "pair.Key, data.name, data.desc);
                    Console.WriteLine("보상종류: {0}\t 보상양: {1}\t 보상아이콘: {2}\t 목표: {3}/{4}"
                        , reward.reward_name, data.reward_amount, reward.reward_icon, info.count, data.goal);
                }
            }
        }
 
        public void SaveData()
        {
            //딕셔너리->배열->문자열->저장
 
            AchievementInfo[] arrInfo = new AchievementInfo[this.gameInfo.dicAcievementInfo.Count];
 
            int index = 0;
 
            foreach (KeyValuePair<int, AchievementInfo> pair in this.gameInfo.dicAcievementInfo)
            {
                arrInfo[index++= pair.Value;
            }
 
            string json = JsonConvert.SerializeObject(arrInfo);
 
            string path = "./achievement_info.json";
            File.WriteAllText(path, json);
 
        }
        #endregion
 
    }
}
 
 
 

 

2. AchievementData class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_015
{
    class AchievementData
    {
        public int id;
        public string name;
        public string desc;
        public int reward_id;
        public int reward_amount;
        public int goal;
    }
}
 

 

 

3. RewardData class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_015
{
    class RewardData
    {
        public int reward_id;
        public string reward_name;
        public int reward_type;
        public string reward_icon;
    }
}
 
 

 

 

4. GameInfo class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_015
{
    class GameInfo
    {
        public Dictionary<int, AchievementInfo> dicAcievementInfo;
        public GameInfo()
        {
            this.dicAcievementInfo = new Dictionary<int, AchievementInfo>();
        }
    }
}
 
 

 

 

5. AchievementInfo class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_015
{
    class AchievementInfo
    {
        public int id;
        public int count;
 
        public AchievementInfo(int id, int count)
        {
            this.id = id;
            this.count = count;
 
        }
    }
}
 
 

 

 

6. 결과값

 

1) 처음 실행하고 업적을 1개 수행했을 때

 

 

2) 다시 실행해서 출력만 했을 때

 

 

3) 저장 파일이 없을 때(신규 유저 일때)