C#/수업내용

2020-04-22 업적시스템 만들기(파일입출력)

J월드 2020. 4. 22. 15:04

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

AchievementData Class

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study_014_1
{
    enum eAchieveResult
    { 
        미완료,
        완료,
    }
    class AchievementData
    {
        public int id;
        public string name;
        public int goal;
        public int reward_amount;
        public eAchieveResult reward_result;
        public string desc;
    }
}
 
 

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

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 Study_014_1
{
    class AchievementInfo
    {
        public int id;
        public int count;
        public AchievementInfo(int id, int count)
        {
            this.id = id;
            this.count = count;
        }
    }
}
 
 
 

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

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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
 
namespace Study_014_1
{
    class App
    {
        Dictionary<int, AchievementData> dicAchievementDatas;
        Dictionary<int, AchievementInfo> dicAchievementInfos;
        public App()
        {
            //data json파일 불러오기
            string path = "./data/achievement_data.json";
            string dataJson = File.ReadAllText(path);
 
            AchievementData[] arrAchievementDatas =
            JsonConvert.DeserializeObject<AchievementData[]>(dataJson);
 
            //데이터 딕셔너리 객체에 저장
 
            dicAchievementDatas = new Dictionary<int, AchievementData>();
 
            //info 맵핑 클래스만듬 (id,count)
            dicAchievementInfos = new Dictionary<int, AchievementInfo>();
 
 
            foreach (AchievementData data in arrAchievementDatas)
            {
                this.dicAchievementDatas.Add(data.id, data);
            }
 
 
 
            string infoPath = "./achievement_info.json";
 
            if (File.Exists(infoPath))
            {
                Console.WriteLine("*** 기존 유저 ***");
                string infoJason = File.ReadAllText(infoPath);
                AchievementInfo[] infos = JsonConvert.DeserializeObject<AchievementInfo[]>(infoJason);
 
                foreach (AchievementInfo info in infos)
                {
                    this.dicAchievementInfos.Add(info.id, info);
                    //Console.WriteLine("{0} {1}", info.id, info.count);
                }
 
 
            }
            else
            {
                Console.WriteLine("*** 신규 유저 ***");
 
                foreach (KeyValuePair<int,AchievementData> pair in dicAchievementDatas)
                {
                    AchievementData data = pair.Value;
                    AchievementInfo info = new AchievementInfo(data.id, 0);
                    this.dicAchievementInfos.Add(info.id, info);
                }
 
                this.SaveAchievement();
            }
 
 
 
            
 
            //잘들어왔는지 확인.
            //foreach (var element in dicAchievementDatas)
            //{
            //    Console.WriteLine("{0},{1}", element.Key, element.Value.reward_result);
            //}
 
 
          
 
            this.PrintAchievement(1000); //아이디값이 넘어왔을때 업적정보를 출력해주는 메서드
            this.DoAchievement(1000);
            this.SaveAchievement();
 
 
        }
 
        public void PrintAchievement(int id) // 아이디값이 넘어왔을때 업적정보를 출력해주는 메서드
 
        {
            AchievementData showData = this.dicAchievementDatas[id];
            Console.WriteLine("업적의 정보입니다.");
            AchievementInfo showInfo = this.dicAchievementInfos[id];
            Console.WriteLine("id : {0} name : {1} count : {2}/{3}"showData.id, showData.name, showInfo.count,showData.goal);
 
        }
 
        public void DoAchievement(int id) // 업적을 달성하는 메서드
        {
            AchievementInfo DoInfo = this.dicAchievementInfos[id];
            DoInfo.count += 1;
            Console.WriteLine("업적을 1회 달성하셨습니다.");
        }
 
        public void SaveAchievement()
        {
            int length = this.dicAchievementInfos.Count;
            AchievementInfo[] arrInfo = new AchievementInfo[length];
 
            int inx = 0;
 
            foreach (KeyValuePair<int,AchievementInfo> pair in this.dicAchievementInfos)
            {
                arrInfo[inx++= pair.Value;
            }
 
            foreach (AchievementInfo info in arrInfo)
            {
                string format = string.Format("id : {0} , count : {1}"info.id, info.count);
 
                Console.WriteLine(format);
            }
 
            //객체 -> json 형식의 문자열로 변환 
            string json = JsonConvert.SerializeObject(arrInfo);
            Console.WriteLine(json);
 
            //파일저장
            string path = "./achievement_info.json";
            File.WriteAllText(path,json);
        }
 
    }
}
 
 
 

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

이 폼을 많이 연습해야됨