업적 달성 - #Dictionary #Json #Table

2020. 4. 24. 14:40C#/수업내용

1. 이미 들어간 데이터 내에서도 문법적인 적용이 이루어 질 수 있다.

 - string.Format()

2. Initialize()를 적극 활용하는 구조로 만들자. App의 생성자는 최대한 깔끔하게.

 

<App.cs>

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace Study_015
{
    class App
    {
        private const string ACHIEVEMENT_DATA_PATH = "./data";
        private const string ACHIEVEMENT_DATA_FILE = "Achievement_Data.json";
        private const string ACHIEVEMENT_INFO_PATH = "./info";
        private const string ACHIEVEMENT_INFO_FILE = "Achievement_Info.json";
 
        private Dictionary<int, AchievementData> dicAchievementData;
        private GameInfo gameInfo;
 
 
        public App()
        {
 
            this.Initialize();
 
            while (true)
            {
                Console.WriteLine("(1: Print, 2: 업적 달성)");
                string input = Console.ReadLine();
                if (input == "1")
                {
                    PrintAchievement();
                }
                else if (input == "2")
                {
                    Console.Write("업적 ID 입력 : ");
                    string id = Console.ReadLine();
                    Console.Write("업적 달성량 입력 : ");
                    string count = Console.ReadLine();
 
                    this.DoAchievement(int.Parse(id), int.Parse(count));
 
                }
            }
 
        }
        public void Initialize()
        {
            dicAchievementData = new Dictionary<int, AchievementData>();
            gameInfo = new GameInfo();
 
            string dataJson = File.ReadAllText(this.FullFilePath(ACHIEVEMENT_DATA_PATH, ACHIEVEMENT_DATA_FILE));
            AchievementData[] arrAchievementDatas = JsonConvert.DeserializeObject<AchievementData[]>(dataJson);
 
            foreach (AchievementData data in arrAchievementDatas)
            {
                dicAchievementData.Add(data.id, data);
            }
 
            if (this.CheckNewbie())
            {
                foreach (KeyValuePair<int, AchievementData> pair in dicAchievementData)
                {
                    AchievementData data = pair.Value;
                    AchievementInfo info = new AchievementInfo(data.id, 0);
                    this.gameInfo.dicAchievementInfo.Add(info.id, info);
                }
                this.SaveToLocal();
            }
            else
            {
                string dataInfo = File.ReadAllText(this.FullFilePath(ACHIEVEMENT_INFO_PATH, ACHIEVEMENT_INFO_FILE));
                this.gameInfo = JsonConvert.DeserializeObject<GameInfo>(dataInfo);
            }
 
            Console.WriteLine($"==== 게임 준비 완료 ====");
 
        }
 
        public bool CheckNewbie()
        {
            bool isNewbie = true;
            string fullPath = FullFilePath(ACHIEVEMENT_INFO_PATH, ACHIEVEMENT_INFO_FILE);
            if (Directory.Exists(ACHIEVEMENT_INFO_PATH))
            {
                if(File.Exists(fullPath))
                {
                    isNewbie = false;
                }
            }
            
            if(isNewbie)
            {
                Console.WriteLine($"<신규 유저>");
            }
            else
            {
                Console.WriteLine($"<기존 유저>");
            }
            return isNewbie;
        }
        public string FullFilePath(string path, string file)
        {
            var fullPath = string.Format($"{path}/{file}");
            return fullPath;
        }
        private void DoAchievement(int id, int count)
        {
            AchievementInfo info = this.gameInfo.dicAchievementInfo[id];
            info.count += count;
 
            AchievementData data = this.dicAchievementData[id];
 
            if(info.count >= data.goal)
            {
                info.count = data.goal;
                Console.WriteLine($"\"{data.name}\" 업적 완료");
            }
            this.SaveToLocal();
        }
        private void SaveToLocal()
        {
            string infoJson = JsonConvert.SerializeObject(this.gameInfo);
            string fullFilePath = FullFilePath(ACHIEVEMENT_INFO_PATH, ACHIEVEMENT_INFO_FILE);
 
            Directory.CreateDirectory(ACHIEVEMENT_INFO_PATH);
            File.WriteAllText(fullFilePath, infoJson);
            if (File.Exists(fullFilePath))
            {
                Console.WriteLine($"파일이 {fullFilePath}에 저장되었습니다.");
            }
        }
        private void PrintAchievement()
        {
            foreach (KeyValuePair<int, AchievementInfo> pair in this.gameInfo.dicAchievementInfo)
            {
                var data = this.dicAchievementData[pair.Key];
                var info = pair.Value;
 
                string description = string.Format(data.desc, data.goal);
 
                Console.WriteLine("=======================================");
                Console.WriteLine($"ID : {data.id}, Name : {data.name}");
                Console.WriteLine($"Description : {description}, Goal : ({info.count}/{data.goal})");
                Console.WriteLine($"Reward : {data.reward}, Exp Reward : {data.reward_exp}");
                Console.WriteLine("=======================================");
                Console.WriteLine();
 
            }
        }
    }
}
 

<GameInfo.cs>

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> dicAchievementInfo;
        public GameInfo()
        {
            dicAchievementInfo = new Dictionary<int, AchievementInfo>();
        }
    }
}
 

<AchievementInfo.cs>

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

<AchievementData.cs>

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_015
{
    class AchievementData
    {
        public int id;
        public string name;
        public string desc;
        public int reward;
        public int reward_exp;
        public int goal;
        public int icon;
    }
}