본문 바로가기

C#/Problems

2020-04-27 복습 중 뇌정지. 살려주세요

DataManager는 도무지 진짜 왜 이해가 안갈까ㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎ

구현하려고 했던 것은, 수업시간에 한번 했었던 24일 예제문제를 싱글톤 클래스를 적용해 다시 만들어 볼려고 했는데, DataManager를 보는 순간 뇌정지가 오고 온몸의 모세혈관이 막히는 느낌을 받았다.

 

뭐 다른건 둘째치고 DataManager 저장 로드 부터 막혔다.

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

복습예제문제

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

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

json은 제대로 나오는것 같다.

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

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

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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Repeat_016
{
    class App
    {
        public App()
        {
            var dataManager = DataManager.GetInstance();
            dataManager.LoadData();
            this.CheckNewbie();
        }
        public void CheckNewbie()
        {
            bool isNewbie = true;
            //파일 확인
            if (File.Exists("./save/data_manager.json"))
            {
                Console.WriteLine("기존 유저입니다.");
                isNewbie = false;
            }
            else
            {
                Console.WriteLine("신규 유저입니다.");
            }
            if (isNewbie == true)
            {
                DataManager.GetInstance().SaveToLocal();
            }
            else
            {
                DataManager.GetInstance().LoadInfoData();
            }
            
        }
    }
}
 
 
 

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

DataManager Class

데이터 매니져와 인포를 묶어서 저장하려고 했는데, 얼추 저장은 되는 것 같다.

사실 왜 되는지 모르겠다..SavaToLocal에서 뇌정지가 왔다.

전에 GameInfo에 info 사전을 넣어서 저장하는 걸 어설프게 이해한 탓인지, instance.dicAchievementInfo 여기에 왜 키값이 붙었는지, GameInfo는 객체를 만들어 this.gameInfo를 json으로 넘겼는데, 왜 데이터매니져는 instance를 넘기는게 안되는 건지, 어쩌다가 키값과 info값이 저장이 되었는데, 어떻게 받아야 하는건지.

앞서 GameInfo에 아이디값을 넣을 멤버변수조차도 없는데 어디서 아이디값이 붙는건지.

어설프게 이해한 것 하나때문에 갑자기 다 꼬여버려서 그냥 똥멍청이가 된거같다.

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

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace Repeat_016
{
    class DataManager
    {
        private static DataManager instance;    //싱글톤 클래스
 
        private Dictionary<int, AchievementData> dicAchievementData;
        private Dictionary<int, AchievementInfo> dicAchievementInfo;
        private Dictionary<int, AchievementStepData> dicAchievementStepData;
        private Dictionary<int, RewardData> dicRewardData;
 
        private DataManager()
        {
            this.Init(); //초기화 함수 호출
        }
        public void Init() //초기화 함수
        {
            dicAchievementData = new Dictionary<int, AchievementData>();
            dicAchievementInfo = new Dictionary<int, AchievementInfo>();
            dicAchievementStepData = new Dictionary<int, AchievementStepData>();
            dicRewardData = new Dictionary<int, RewardData>();
        }
 
        public static DataManager GetInstance() //싱글톤 클래스
        {
            if (DataManager.instance == null)
            {
                DataManager.instance = new DataManager();
                return DataManager.instance;
            }
            else
            {   
                return DataManager.instance;
            }
        }
 
        public void LoadData()
        {
            this.dicAchievementData = (JsonConvert.DeserializeObject<AchievementData[]>
                (File.ReadAllText("./data/achievement_data.json"))).ToDictionary(x => x.id, x => x);
 
            this.dicAchievementStepData = (JsonConvert.DeserializeObject<AchievementStepData[]>
                (File.ReadAllText("./data/achievement_step_data.json"))).ToDictionary(x => x.id, x => x);
 
            this.dicRewardData = (JsonConvert.DeserializeObject<RewardData[]>
                (File.ReadAllText("./data/reward_data.json"))).ToDictionary(x => x.id, x => x);
        }
 
        public void LoadInfoData()
        {
            instance.dicAchievementInfo = (JsonConvert.DeserializeObject<AchievementInfo[]>(File.ReadAllText("./save/data_manager.json"))).ToDictionary(x=>x.id,x=>x);
            Console.WriteLine(DataManager.instance);
 
            foreach (var pair in instance.dicAchievementInfo)
            {
                Console.WriteLine("{0}{1}",pair.Value.id,pair.Value.count);
            }
        }
 
        public void SaveToLocal()
        {
            //---------------------------------------------------------------------------
            foreach (var pair in this.dicAchievementData)
            {
                AchievementInfo info = new AchievementInfo(pair.Value.id, 0);
 
                instance.dicAchievementInfo.Add(info.id, info);
            }
 
            string json = JsonConvert.SerializeObject(instance.dicAchievementInfo);
            Console.WriteLine(json);
 
            File.WriteAllText("./save/data_manager.json", JsonConvert.SerializeObject(instance.dicAchievementInfo));      
 
            //---------------------------------------------------------------------------
        }
 
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

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

틀리기도 힘든 무결점 AchievementData Class

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Repeat_016
{
    class AchievementData
    {
        public int id;
        public string name;
        public string icon_name;
        public int reward_id_0;
        public int reward_amount_0;
        public int reward_id_1;
        public int reward_amount_1;
        public string desc;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

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

무결점 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 Repeat_016
{
    class RewardData
    {
        public int id;
        public string name;
        public int reward_type;
        public string icon_reward_gold;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

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

무결점 AchievementStepData Class

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Repeat_016
{
    class AchievementStepData
    {
        public int id;
        public int achievement_id;
        public int goal;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

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

무결점 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 Repeat_016
{
    class AchievementInfo
    {
        public int id;
        public int count;
        public AchievementInfo(int id, int count)
        {
            this.id = id;
            this.count = count;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

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

출력 에러-1 

이것도 왜안되지 15분동안 쳐다보고 있었다.

File.ReadAllText....

왜 안했을까..

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

 

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

출력 에러-2

온몸에 모세혈관을 막아버린 SaveToLocal, 저장한거 불러올려다 Newtonsoft의 응징을 받았다. 

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