상세 컨텐츠

본문 제목

Study_014 - json변환, 파일입출력, Dictionary

C#/수업내용

by McRobbin 2020. 4. 22. 14:43

본문

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace Study_014_2
{
    public class App
    {
        public string FilePath;
        public string DataFileName;
        public string InfoFileName;
 
        public Dictionary<int, QuestData> QuestDataDict;
        public Dictionary<int, QuestInfo> QuestInfoDict;
 
        public App()
        {
            this.FilePath = @"E:/DataFile/";
            this.DataFileName = @"quest_data2.json";
            this.InfoFileName = @"quest_info2.json";
 
            //퀘스트 딕트에 값 설정.
            this.SetDataFileDict();
            this.SetInfoFileDict();
 
            //딕셔너리 한번씩 출력.
            this.PrintQuestDataDict();
            this.PrintQuestInfoDict();
 
            //퀘스트 수행.
            this.DoQuest(10115);
            this.PrintQuestInfoDict();
            this.DoQuest(10020);
            this.PrintQuestInfoDict();
            this.DoQuest(10115);
            this.PrintQuestInfoDict();
 
            //종료 직전에 인포파일 한번더 저장.
            this.SaveInfoFile();
        }
 
        //처음 데이터파일 읽어오기.
        public void SetDataFileDict()
        {
            var rtnDict = new Dictionary<int, QuestData>();
            string fileText = File.ReadAllText(this.FilePath + this.DataFileName);
            QuestData[] dataArr = JsonConvert.DeserializeObject<QuestData[]>(fileText);
 
            foreach (QuestData data in dataArr)
                rtnDict.Add(data.id, data);
 
            Console.WriteLine("데이터파일 불러오기 완료.\n");
            this.QuestDataDict = rtnDict;
        }
        
        //인포 파일이 있는지 검사후 새로 생성 또는 불러오기.
        public void SetInfoFileDict()
        {
            var rtnDict = new Dictionary<int, QuestInfo>();
 
            if(File.Exists(this.FilePath + this.InfoFileName))
            {
                //이미 인포파일이 있을 때.
                string fileText = File.ReadAllText(this.FilePath + this.InfoFileName);
                QuestInfo[] infoArr = JsonConvert.DeserializeObject<QuestInfo[]>(fileText);
 
                foreach (QuestInfo info in infoArr)
                    rtnDict.Add(info.id, info);
 
                this.QuestInfoDict = rtnDict;
 
                Console.WriteLine("기존 인포파일 불러옴\n");
            }
 
            else
            {
                //인포파일 없을 때.
                foreach (var data in this.QuestDataDict)
                    rtnDict.Add(data.Key, new QuestInfo(data.Key, 0false));
 
                Console.WriteLine("새로운 인포파일 생성.");
 
                this.QuestInfoDict = rtnDict;
 
                //여기에 새로운 인포파일 저장.
                this.SaveInfoFile();
            }
 
        }
 
        //인포파일 저장.
        public void SaveInfoFile()
        {
            List<QuestInfo> infoList = new List<QuestInfo>();
            foreach (var info in this.QuestInfoDict)
                infoList.Add(info.Value);
 
            string fileText = JsonConvert.SerializeObject(infoList.ToArray());
            File.WriteAllText(this.FilePath + this.InfoFileName, fileText);
        }
        
        //데이터파일 출력.
        public void PrintQuestDataDict()
        {
            Console.WriteLine("\nQuestDataDict 출력=======");
            foreach (var data in this.QuestDataDict)
                Console.WriteLine("id : {0}, name : {1}, goal : {2}, reward : {3}, desc : {4}",
        }
 
        //인포파일 출력.
        public void PrintQuestInfoDict()
        {
            Console.WriteLine("\nQuestDataDict 출력=======");
            foreach (var info in this.QuestInfoDict)
                Console.WriteLine("id : {0}, goal : {1} / {2}, is_cleared : {3}",
                    info.Value.id, info.Value.now_goal,
                    this.QuestDataDict[info.Key].goal, info.Value.is_cleared);
        }
 
        //퀘스트 한번만 수행.
        public void DoQuest(int id) => this.DoQuest(id, 1);
 
        //퀘스트 반복 수행.
        public void DoQuest(int id, int count)
        {
            QuestInfo targetInfo = this.QuestInfoDict[id];
            QuestData targetData = this.QuestDataDict[id];
 
            if (!targetInfo.is_cleared)
            {
                targetInfo.DoQuest(count);
 
                if (targetInfo.now_goal >= targetData.goal)
                    targetInfo.ClearQuest(targetData);
            }
        }
    }
 
}
 
 
 

 

App파일.

 

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_014_2
{
    public class QuestData
    {
        public int id;
        public string name;
        public int goal;
        public int reward;
        public string desc;
    }
}
 
 
 

 

QuestData파일.

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_014_2
{
    public class QuestInfo
    {
        public int id;
        public int now_goal;
        public bool is_cleared;
 
        public QuestInfo(int id, int now_goal, bool is_cleared)
        {
            this.id = id;
            this.now_goal = now_goal;
            this.is_cleared = is_cleared;
        }
 
        public void ClearQuest(QuestData data)
        {
            this.is_cleared = true;
            this.now_goal = data.goal;
            Console.WriteLine("\n{0} : {1} 퀘스트가 완료되었습니다."data.id, data.name);
        }
 
        public void DoQuest() => this.DoQuest(1);
 
        public void DoQuest(int count)
        {
            if (!this.is_cleared)
            {
                this.now_goal += count;
            }
        }
    }
}
 
 
 

 

QuestInfo파일.

'C# > 수업내용' 카테고리의 다른 글

Study_019_2 일일 출석 보상  (0) 2020.04.29
Study_018-1 아이템 상점 구현.  (0) 2020.04.28
json파일 예제. With File 3가지 출력.  (0) 2020.04.21
Study_012  (0) 2020.04.20
Study_010-2  (0) 2020.04.16

관련글 더보기