상세 컨텐츠

본문 제목

Study_019_2 일일 출석 보상

C#/수업내용

by McRobbin 2020. 4. 29. 15:36

본문

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_019_2
{
    public class App
    {
        public static DateTime GameDateTime = DateTime.Now;
            
        public App()
        {
            DataManager.Instance.PrintAllDatasByType<ItemData>();
            DataManager.Instance.PrintAllDatasByType<ComboRewardData>();
            DataManager.Instance.PrintAllDatasByType<PresentRewardData>();
 
            //오늘 접속.
            CharacterInfo character = CharacterInfo.LoadCharacterInfo();
            
            while (true)
            {
                Console.Write("날짜 : 5월 : ");
                int day = int.Parse(Console.ReadLine());
                SetGameDateTime(new DateTime(202005, day, 102010));
                character.Login();
                character.PrintCharacterInfo();
                character.PrintAllItems();
                Console.WriteLine("===================================");
            }
 
        }
 
        public void SetGameDateTime(DateTime dateTime)
        {
            Console.WriteLine("현재 날짜 : {0}", GameDateTime);
            GameDateTime = dateTime;
            Console.WriteLine("바뀐 날짜 : {0}\n", GameDateTime);
        }
    }
}
 
 
 

App.cs 파일 입니다.

while문으로 5월을 가정하고 날짜를 무한으로 받게 했습니다.

 

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
155
156
157
158
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace Study_019_2
{
    public class CharacterInfo
    {
        public int id;
 
        public DateTime lastLogin;
 
        public List<ItemInfo> itemList;
        public Dictionary<int, ComboRewardInfo> comboInfoDict;
 
        public int presentCount;
        public int comboPresent;
 
 
        public CharacterInfo(int id, DateTime lastLogin, List<ItemInfo> itemList = nullDictionary<int, ComboRewardInfo> comboInfoDict = nullint presentCount = 0int comboPresent = 0)
        {
            this.id = id;
            this.presentCount = presentCount;
            this.comboPresent = comboPresent;
            this.lastLogin = (lastLogin == null) ? DateTime.Now : lastLogin;
            this.itemList = (itemList == null || itemList.Count == 0) ? new List<ItemInfo>() : itemList;
            this.comboInfoDict = (comboInfoDict == null) ? DefaultComboRewardInfoDict() : comboInfoDict;
 
            this.SaveCharacterInfo();
        }
 
        public static CharacterInfo LoadCharacterInfo()
        {
            string filePath = @"E:\DataFile\character_info.json";
            if (File.Exists(filePath))
            {
                return JsonConvert.DeserializeObject<CharacterInfo>(File.ReadAllText(filePath));
            }
            else
                return new CharacterInfo(10DateTime.Now);
        }
 
        public Dictionary<int, ComboRewardInfo> DefaultComboRewardInfoDict()
        {
            var rtnDict = new Dictionary<int, ComboRewardInfo>();
            var comboDatas = DataManager.Instance.GetAllDatasByType<ComboRewardData>();
 
            foreach (var combo in comboDatas)
                rtnDict.Add(combo.id, new ComboRewardInfo(combo.id));
 
            return rtnDict;
        }
 
        public void SaveCharacterInfo()
        {
            string text = JsonConvert.SerializeObject(this);
            File.WriteAllText(@"E:\DataFile\character_info.json", text);
        }
 
        public void Login()
        {
            Console.WriteLine("id : {0} 님이 접속했습니다.\n"this.id);
            //보상 받기.
            if (App.GameDateTime.Day >= lastLogin.AddDays(1).Day || presentCount == 0)
            {
                this.GetPresentReward();
                this.presentCount++;
 
            }
 
            //연속보상 업데이트.
            //연속 들어왔을 때.
            if (App.GameDateTime.Day == this.lastLogin.AddDays(1).Day || comboPresent == 0)
                this.comboPresent++;
 
            //같은날 또 들어왔을 떄.
            else if (App.GameDateTime.Day == this.lastLogin.Day) { }
 
            //끊길때.
            else
                this.comboPresent = 1;
 
            //연속 보상 획득.
            if(this.comboPresent % 5 == 0)
            {
                GetComboPresentReward();
            }
 
            this.lastLogin = App.GameDateTime;
 
            this.SaveCharacterInfo();
        }
 
        public void GetPresentReward()
        {
            Console.WriteLine("{0}일차 출석 보상을 얻습니다."this.presentCount + 1);
            var presentData = DataManager.Instance.GetAllDatasByType<PresentRewardData>()[presentCount];
            var itemData = DataManager.Instance.GetDataById<ItemData>(presentData.item_id);
 
            ItemInfo targetInfo = this.itemList.Find(x => x.id == itemData.id);
 
            //아이템 획득
            if (presentData != null)
                this.GetItem(itemData.id, presentData.reward_amount);
 
        }
 
        public void GetComboPresentReward()
        {
            var comboData = DataManager.Instance.GetAllDatasByType<ComboRewardData>().Find(x => x.combo_day == this.comboPresent);
 
            if(comboData != null && comboInfoDict[comboData.id].is_gotten == false)
            {
                Console.WriteLine("연속 출석 {0}일차 보상을 얻습니다.", comboData.combo_day);
                this.GetItem(comboData.item_id, comboData.reward_amount);
                this.comboInfoDict[comboData.id].is_gotten = true;
            }
 
        }
 
        public void GetItem(int itemId, int amount = 1)
        {
 
            var ItemInfo = this.itemList.Find(x => x.id == itemId);
 
            //아이템이 없을 때.
            if (ItemInfo == null)
                itemList.Add(new ItemInfo(itemId, amount));
            else
                ItemInfo.amount += amount;
 
            Console.WriteLine("아이템 {0}을\t {1}개 획득했습니다.\n", DataManager.Instance.GetDataById<ItemData>(itemId).name, amount);
        }
 
        public void PrintAllItems()
        {
            Console.WriteLine("아이템 목록\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
            foreach(var item in this.itemList)
            {
                var data = DataManager.Instance.GetDataById<ItemData>(item.id);
                Console.WriteLine("아이템 : \t{0}\t갯수 : \t{1}"data.name, item.amount);
            }
 
            Console.WriteLine("");
        }
 
        public void PrintCharacterInfo()
        {
            Console.WriteLine("id : \t{0},\t 출석 일수 : \t{1}, 연속 출석 일수 : " +
                "\t{2}"this.id, this.presentCount, this.comboPresent);
        }
    }
}
 
 
 

CharacterInfo.cs 입니다.

Login() 함수에서 일일보상을 받는것을 자동으로 호출하게 했습니다.

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace Study_019_2
{
    public class DataManager
    {
        private Dictionary<int, RawData> dataDict;
 
        private static DataManager dataManager;
        public static DataManager Instance
        {
            get
            {
                if (dataManager == null)
                    dataManager = new DataManager();
 
                return dataManager;
            }
        }
 
        private DataManager()
        {
            this.dataDict = new Dictionary<int, RawData>();
            this.LoadData();
        }
 
        private void LoadData()
        {
            var pathList = new List<string>();
 
            pathList.Add(@"E:\DataFile\combo_present_reward_data.json");
            pathList.Add(@"E:\DataFile\present_reward_data.json");
            pathList.Add(@"E:\DataFile\item_data.json");
 
            ItemData[] itemDatas = JsonConvert.DeserializeObject<ItemData[]>(File.ReadAllText(pathList[2]));
            ComboRewardData[] comboDatas = JsonConvert.DeserializeObject<ComboRewardData[]>(File.ReadAllText(pathList[0]));
            PresentRewardData[] presentRewardDatas = JsonConvert.DeserializeObject<PresentRewardData[]>(File.ReadAllText(pathList[1]));
 
            foreach (var data in itemDatas)
                dataDict.Add(data.id, data);
 
            foreach (var data in comboDatas)
                dataDict.Add(data.id, data);
 
            foreach (var data in presentRewardDatas)
                dataDict.Add(data.id, data);
 
            Console.WriteLine("데이터 불러오기 완료=====\n");
        }
 
        public List<T> GetAllDatasByType<T>() where T : RawData
        {
            var rtnList = new List<T>();
            foreach(var data in dataDict.Values)
            {
                if (data is T)
                    rtnList.Add(data as T);
            }
 
            return rtnList;
        }
 
        public T GetDataById<T>(int id) where T : RawData
        {
            return dataDict.Values.ToList().Find(x => x.id == id) as T;
        }
 
        public void PrintAllDatasByType<T>() where T : RawData
        {
            foreach(var data in this.GetAllDatasByType<T>())
            {
                
                if(data is ItemData)
                {
                    var casted = data as ItemData;
                    Console.WriteLine("id : {0}, 이름 : {1}"casted.id, casted.name);
                }
 
                else if(data is PresentRewardData)
                {
                    var casted = data as PresentRewardData;
                    var item = this.GetDataById<ItemData>(casted.item_id);
                    Console.WriteLine("{0}일차 보상 : \t{1}\t갯수 : {2}",
                        casted.day, item.name,  casted.reward_amount);
                }
 
                else if(data is ComboRewardData)
                {
                    var casted = data as ComboRewardData;
                    var item = this.GetDataById<ItemData>(casted.item_id);
                    Console.WriteLine("{0}일 연속 보상 : \t{1}\t갯수 : {2}"
                        , casted.combo_day, item.name, casted.reward_amount);
                }
            }
            Console.WriteLine();
        }
    }
}
 
 
 

 

DataManager.cs파일 입니다. id가 겹치지 않게 모든 테이블을 구성해서 한개의 딕셔너리에 모두 넣었습니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_019_2
{
    public class ItemInfo : RawInfo
    {
        public int amount;
 
        public ItemInfo(int id, int amount = 1) : base(id)
        {
            this.amount = amount;
        }
    }
}
 
 
 

ItemInfo.cs 입니다. 갯수와 id만 가지도록 했습니다.

 

그 외에는 기본 RawInfo, RawData, 등등의 데이터 클래스 입니다.

 

 

관련글 더보기