업적 - Dictionary/File Read Write

2020. 4. 22. 14:45C#/수업내용

<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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace Study_013
{
    class App
    {
        // Dic 객체 선언
        Dictionary<int, AchievementData> dicAchievementDatas;   
        Dictionary<int, AchievementInfo> dicAchievementInfos;
 
        public App()
        {
            // Dic 객체 생성
            this.dicAchievementDatas = new Dictionary<int, AchievementData>();
            this.dicAchievementInfos = new Dictionary<int, AchievementInfo>();
 
            // 테이블 데이터 Read
            string dataJson = File.ReadAllText("./Achievement.json");
            // 역직렬화 (String -> 배열객체)
            AchievementData[] arrAchievementDatas = JsonConvert.DeserializeObject<AchievementData[]>(dataJson);
            // Dic 객체에 역직렬화한 배열객체 할당 (옮기기)
            foreach(AchievementData data in arrAchievementDatas)
            {
                this.dicAchievementDatas.Add(data.id, data);
            }
 
            // Info 데이터 Read
            // 변화 가능한 데이터이므로 데이터 변경 여부를 조건 분할
            string infoPath = "./Achievement_info.json";
            if (File.Exists(infoPath))  // 기존 유저
            {
                string InfoJson = File.ReadAllText(infoPath);
                AchievementInfo[] arrAchievementInfos = JsonConvert.DeserializeObject<AchievementInfo[]>(InfoJson);
 
                foreach(AchievementInfo info in arrAchievementInfos)
                {
                    this.dicAchievementInfos.Add(info.id, info);
                }
            }
            else  // 신규 유저
            {
                foreach(KeyValuePair<int, AchievementData> pair in this.dicAchievementDatas)
                {
                    AchievementData data = pair.Value;
                    AchievementInfo info = new AchievementInfo(data.id, 0);
                    this.dicAchievementInfos.Add(info.id, info);
                }
                this.SaveAchievements();
            }
            PrintAchievement(1000);
            DoAchievement(1000);
            SaveAchievements();
 
        }
        public void PrintAchievement(int id)
        {
            AchievementData data = this.dicAchievementDatas[id];
            AchievementInfo info = this.dicAchievementInfos[id];
            Console.WriteLine($"id:{data.id}, name:{data.name}, goal:({info.count}/{data.goal}), reward:({data.reward}), desc:{data.desc}");
        }
        public void DoAchievement(int id)
        {
            AchievementData data = this.dicAchievementDatas[id];
            AchievementInfo info = this.dicAchievementInfos[id];
            info.count += 1;
            Console.WriteLine($"\"{data.name}\" 업적 1개를 했습니다. ({info.count}/{data.goal})");
        }
        public void SaveAchievements()
        {
            int length = this.dicAchievementInfos.Count;
            AchievementInfo[] arrAchievementInfos = new AchievementInfo[length];
 
            int idx = 0;
            foreach(KeyValuePair<int, AchievementInfo> pair in this.dicAchievementInfos)
            {
                arrAchievementInfos[idx] = pair.Value;
                idx++;
            }
            string infoJson = JsonConvert.SerializeObject(arrAchievementInfos);
            string path = "./Achievement_info.json";
            File.WriteAllText(path, infoJson);            
        }
    }
}
 

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

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