상세 컨텐츠

본문 제목

과제 - 롤 챔프, 스킨 구매 시뮬레이션..?

C#/과제

by McRobbin 2020. 4. 30. 22:55

본문

저는 Lol 챔프와 스킨 구매를 가지고 과제를 했습니다. 생각보다 할게 많더군요...

 

 

주요 구현 내용.

1. 재화가 둘로 나뉩니다. Rp, Be 둘을 eMoneyType으로 놨으며 충전시 둘중 하나를 선택합니다.

 

2. 챔프 구매는 Rp 또는 Be를 선택해서 구매할 수 있습니다.

   챔프를 구매할 시 기본 스킨을 지급하며 이미 있는 챔프는 구매할 수 없습니다.

   돈이 모자라도 구매할 수 없으며 챔피언 이름으로도 구매가 가능하게 했습니다.

 

3. 스킨 구매는 Rp로만 구매가 가능하며 재화가 모자를 경우 구매할 수 없습니다.

   또한 일부 스킨의 경우 한정 판매가 있어 기간이 지났다면 구매할 수 없습니다.

   해당 챔프를 가지고 있지 않거나 이미 소유하고 있어도 구매가 안됩니다.

   스킨 이름만 가지고도 구매가 가능합니다.

 

4. 무한 while문으로 구매 시뮬레이션을 계속해서 진행합니다. 사용자 입력을 통해 이뤄집니다.

 

 

코드.

 

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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace LolApp
{
    public class App
    {
        public static DateTime GameDateTime = DateTime.Now;
 
        UserInfo user;
 
        public App()
        {
 
            user = this.LoadUserInfo();
            DataManager dm = DataManager.Instance;
            this.RunGame();
        }
 
        public void SetGameDateTime(DateTime dateTime)
        {
            Console.WriteLine("바뀌기 전 시간 : {0:yyyy}년 {0:MM}월 {0:dd}일", GameDateTime);
            GameDateTime = dateTime;
            Console.WriteLine("바뀐 후 시간 : {0:yyyy}년 {0:MM}월 {0:dd}일\n", GameDateTime);
        }
 
        public UserInfo LoadUserInfo()
        {
            string infoPath = @"C:\Users\skagu\Desktop\C#알고리즘\user_info.json";
 
            //기존 유저 정보가 있을 때.
            if (File.Exists(infoPath))
            {
                Console.WriteLine("기존 유저 불러오기 완료.");
                UserInfo user = JsonConvert.DeserializeObject<UserInfo>(File.ReadAllText(infoPath));
                return user;
            }
 
            //기존 유저 정보가 없을 때.
            else
            {
                var created = new UserInfo(10"남현석");
                Console.WriteLine("새로운 유저 생성 완료.");
                created.SaveUserInfo();
                return created;
            }
        }
 
        public void RunGame()
        {
            while (true)
            {
                Console.WriteLine("명령어를 입력하세요.");
                Console.Write("1 : 챔프 구매, 2 : 스킨 구매, 3 : 돈 구매, 4 : 정보 출력. \t입력 : ");
 
                int command;
                if (!int.TryParse(Console.ReadLine(), out command))
                {
                    Console.WriteLine("잘못된 명령어 입니다.\n");
                    continue;
                }
 
                else
                {
                    Console.WriteLine();
                    switch (command)
                    {
                        //챔프 구매.
                        case 1:
                            {
                                this.BuyChampProcess();
                                break;
                            }
 
                        //스킨 구매.
                        case 2:
                            {
                                this.BuySkinProcess();
                                break;
 
                            }
 
                        //돈 구매.
                        case 3:
                            {
                                this.BuyMoneyProcess();
                                break;
                            }
 
                        case 4:
                            user.PrintAllUserInfo();
                            break;
 
                        default:
                            Console.WriteLine("잘못된 명령어 입니다.\n");
                            break;
                    }
                }
            }
        }
 
        public void BuyChampProcess()
        {
            Console.WriteLine("명령어를 입력해 주세요.");
            Console.Write("1 : 모든 챔프 출력, 2 : 구매할 챔프 입력, 3 : 이전으로.\t입력 : ");
            int command;
            if (int.TryParse(Console.ReadLine(), out command))
            {
                Console.WriteLine();
                switch (command)
                {
                    case 1:
                        DataManager.Instance.PrintAllChampData();
                        break;
                    case 2:
                        {
                            Console.WriteLine("챔프의 이름 또는 id와 Rp 또는 Be를 입력하세요.");
                            Console.Write("예시 : 야스오 Rp 또는 마스터이 Be\t입력 : ");
                            Console.WriteLine();
                            int id;
                            string[] splitInput = Console.ReadLine().Split(' ');
                            string champName = splitInput[0];
                            GameEnum.eMoneyType type = (splitInput[1== "Rp") ? 
 
                            //id로 들어왔을 떄.
                            if (int.TryParse(splitInput[0], out id))
                            {
                                user.BuyChampById(id, type);
                            }
                            //이름으로 들어왔을 때.
                            else
                            {
                                user.BuyChampByChampName(champName, type);
                            }
                            break;
                        }
                    case 3:
                        break;
 
                    default:
                        Console.WriteLine("잘못된 명령어 입니다.\n");
                        break;
                }
            }
            else
            {
                Console.WriteLine("잘못된 명령어 입니다.\n");
            }
 
        }
 
        public void BuySkinProcess()
        {
            Console.WriteLine("명령어를 입력하세요.");
            Console.Write("1 : 모든 스킨 출력, 2 : 구매할 스킨 입력, 3 : 이전으로\t입력 : ");
            int command;
 
            if(int.TryParse(Console.ReadLine(), out command))
            {
                Console.WriteLine();
                switch (command)
                {
                    case 1:
                        DataManager.Instance.PrintAllSkinData();
                        break;
                    case 2:
                        {
                            Console.WriteLine("스킨의 이름 또는 id를 입력하세요.");
                            Console.Write("예시 : 지옥의 아무무 또는 210\t입력 : ");
                            
                            int id;
                            string skinName = Console.ReadLine();
                            Console.WriteLine();
                            //id로 왔을 떄.
                            if (int.TryParse(skinName, out id))
                            {
                                user.BuySkinById(id);
                            }
                            //이름으로 왔을 때.
                            else
                            {
                                user.BuySkinByName(skinName);
                            }
                            break;
                        }
                    case 3:
                        break;
                    default:
                        Console.WriteLine("잘못된 명령어 입니다.\n");
                        break;
                }
            }
            else
            {
                Console.WriteLine("잘못된 명령어 입니다.\n");
            }
        }
 
        public void BuyMoneyProcess()
        {
            Console.Write("충전할 종류를 입력하세요. 1 : Rp, 2 : Be\t입력 : ");
            int command;
            if(int.TryParse(Console.ReadLine(), out command))
            {
                Console.Write("\n충전할 양을 입력하세요.\t입력 : ");
                int amount;
 
                if(int.TryParse(Console.ReadLine(), out amount)){
                    if (command == 1)
                        user.BuyRPMoney(amount);
                    else
                        user.BuyBeMoney(amount);
                }
                else
                {
                    Console.WriteLine("잘못된 양 입력 입니다.\n");
                }
            }
            else
            {
                Console.WriteLine("없는 종류의 재화 입니다.\n");
            }
        }
    }
}
 
 
 

App.cs 입니다.

게임이 동작하는 것처럼 무한으로 돌기위한 코드가 있습니다.

또한 user의 데이터를 불러옵니다.

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
 
namespace LolApp
{
    public class DataManager
    {
        //챔피언, 리밋 데이트, 스킨 데이터 모두를 여기에 한번에 저장.
        //id가 겹치는 것이 하나도 없어야함.
        private Dictionary<int, RawData> dataDict;
 
        private static DataManager dataManager;
        public static DataManager Instance
        {
            get
            {
                if (dataManager == null)
                    dataManager = new DataManager();
                return dataManager;
            }
        }
 
        private DataManager()
        {
            dataDict = new Dictionary<int, RawData>();
            this.LoadData();
        }
 
        //데이터 정보 불러오는 메소드.
        private void LoadData()
        {
            string[] filePath =
            {
                @"C:\Users\skagu\Desktop\C#알고리즘\lol_champ_data.json",
                @"C:\Users\skagu\Desktop\C#알고리즘\lol_skin_data.json",
                @"C:\Users\skagu\Desktop\C#알고리즘\skin_limit_data.json"
            };
 
            foreach (var data in JsonConvert.DeserializeObject<LolChampData[]>(File.ReadAllText(filePath[0])))
                dataDict.Add(data.id, data as LolChampData);
            foreach (var data in JsonConvert.DeserializeObject<LolSkinData[]>(File.ReadAllText(filePath[1])))
                dataDict.Add(data.id, data as LolSkinData);
            foreach (var data in JsonConvert.DeserializeObject<SkinLimitData[]>(File.ReadAllText(filePath[2])))
                dataDict.Add(data.id, data as SkinLimitData);
 
            Console.WriteLine("데이터 불러오기 완료.\n");
        }
 
        //데이터 타입과 일치하는 모든 데이터 가져옴.
        public List<T> GetAllDataByType<T>() where T : RawData
        {
            return dataDict.Values.OfType<T>().ToList();
        }
 
        //데이터 타입과 일치하고 해당 id를 가지는 데이터 가져오기.
        public T GetDataById<T>(int id) where T : RawData
        {
            return GetAllDataByType<T>().Find(x => x.id == id);
        }
 
        //모든 챔피언 정보 출력.
        public void PrintAllChampData()
        {
            Console.WriteLine("모든 챔프 정보 출력 =================================");
            foreach (var data in GetAllDataByType<LolChampData>())
                Console.WriteLine("id : {0}\t, 이름 : {1}\t, 설명 : {2}\n, Rp가격 : {3}\t, Be가격 : {4}\n"
        }
 
        //모든 스킨 정보 출력.
        public void PrintAllSkinData()
        {
            Console.WriteLine("모든 스킨 정보 출력 ==============================");
            foreach(var data in GetAllDataByType<LolSkinData>())
            {
                Console.WriteLine("id : {0}\t, 챔피언 : {1}\t, 스킨 이름 : {2}\t, Rp가격 : {3}",
                    data.id, GetDataById<LolChampData>(data.champ_id).name, data.name, data.rp_price);
            
                //한정 기간 스킨일 때.
                if (data.is_limited)
                {
                    var limitData = GetDataById<SkinLimitData>(data.limit_id);
                    //기간이 이미 지났을 때.
                    if (App.GameDateTime > limitData.limit_date)
                        Console.WriteLine("판매 기간이 끝났습니다.\n");
 
                    //기간이 아직 남았을 때.
                    else
                        Console.WriteLine("남은 기간 : {0:dd}일 {0:hh}시간 {0:mm}분.\n",
                            limitData.limit_date - App.GameDateTime);
                }
 
                //일반 스킨일 때.
                else
                    Console.WriteLine("무제한 기간 판매 스킨 입니다.\n");
                
            }
        }
 
        //이름으로 챔피언 데이터 검색.
        public LolChampData GetChampDataByName(string name)
        {
            return this.GetAllDataByType<LolChampData>().Find(x => x.name == name);
        }
 
        //챔피언 이름으로 스킨들 검색.
        public List<LolSkinData> GetSkinDataByChampName(string champName)
        {
            var targetChamp = this.GetChampDataByName(champName);
            return this.GetAllDataByType<LolSkinData>().FindAll(x => x.champ_id == targetChamp.id);
        }
 
        //스킨 이름으로 스킨 검색.
        public LolSkinData GetSkinDataBySkinName(string skinName)
        {
            return this.GetAllDataByType<LolSkinData>().Find(x => x.name == skinName);
        }
    }
}
 
 
 

DataManager.cs 입니다.

데이터 파일의 읽기, 데이터 파일을 검색하는 기능을 담당합니다.

모든 데이터 클래스가 RawData를 상속받아 딕셔너리 타입을 int, RawData로 설정하고

모든 데이터를 전부 때려 넣었습니다. 이 경우 champ의 id와 skin의 id가 겹쳐선 안됩니다.

일부 메서드의 경우 제너릭을 사용해 원하는 Data타입만 가져오도록 했습니다.

 

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 LolApp
{
    public class GameEnum
    {
        public enum eMoneyType
        {
            None = 0,
            Rp = 1,
            Be = 2
        }
    }
}
 
 
 

GameEnum.cs 입니다.

eMoneyType은 두가지 재화 타입을 가집니다.

 

 

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
//------------------------------------------------------------------------------
// <auto-generated>
//     이 코드는 도구를 사용하여 생성되었습니다.
//     런타임 버전:4.0.30319.42000
//
//     파일 내용을 변경하면 잘못된 동작이 발생할 수 있으며, 코드를 다시 생성하면
//     이러한 변경 내용이 손실됩니다.
// </auto-generated>
//------------------------------------------------------------------------------
 
namespace LolApp
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    public class LolChampData : RawData
    {
                
        public string name { get; }
        
        public int rp_price { get; }
 
        public int be_price { get; }
 
        public string desc { get; }
 
        public string shop_icon { get; }
 
        public LolChampData(int id, string name, int rp_price, int be_price, string desc, string shop_icon) : base(id)
        {
            this.name = name;
            this.rp_price = rp_price;
            this.be_price = be_price;
            this.desc = desc;
            this.shop_icon = shop_icon;
        }
    }
}
 
 
 

LolChampData.cs 입니다.

멤버 변수들을 get으로 값이 바뀌지 않게 했으며 RawData를 상속 받습니다.

 

 

 

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
//------------------------------------------------------------------------------
// <auto-generated>
//     이 코드는 도구를 사용하여 생성되었습니다.
//     런타임 버전:4.0.30319.42000
//
//     파일 내용을 변경하면 잘못된 동작이 발생할 수 있으며, 코드를 다시 생성하면
//     이러한 변경 내용이 손실됩니다.
// </auto-generated>
//------------------------------------------------------------------------------
 
namespace LolApp
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    public class LolSkinData : RawData
    {
        public int champ_id { get; }
 
        public string name { get; }
 
        public string skin_icon { get; }
 
        public int rp_price { get; }
 
        public bool is_limited { get; }
 
        public int limit_id { get; }
 
        public LolSkinData(int id, int champ_id, string name, string skin_icon,
int rp_price, bool is_limited, int limit_id) : base(id)
        {
            this.champ_id = champ_id;
            this.name = name;
            this.skin_icon = skin_icon;
            this.rp_price = rp_price;
            this.is_limited = is_limited;
            this.limit_id = limit_id;
        }
    }
}
 
 
 

LolSkinData.cs 입니다.

위와 내용은 같습니다.

 

 

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 LolApp
{
    public class RawData
    {
        public int id { get; }
 
        public RawData(int id) => this.id = id;
    }
}
 
 

RawData.cs 입니다.

id만을 가지며 기본 생성자를 가집니다. 모든 Data들의 부모 클래스 입니다.

 

 

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
//------------------------------------------------------------------------------
// <auto-generated>
//     이 코드는 도구를 사용하여 생성되었습니다.
//     런타임 버전:4.0.30319.42000
//
//     파일 내용을 변경하면 잘못된 동작이 발생할 수 있으며, 코드를 다시 생성하면
//     이러한 변경 내용이 손실됩니다.
// </auto-generated>
//------------------------------------------------------------------------------
 
namespace LolApp
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    public class SkinLimitData : RawData
    {
                
        public int skin_id { get; }
 
        public System.DateTime limit_date { get; }
 
        public SkinLimitData(int id, int skin_id, DateTime limit_date) : base(id)
        {
            this.skin_id = skin_id;
            this.limit_date = limit_date;
        }
    }
}
 
 
 

SkinLimitData.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace LolApp
{
    public class UserInfo
    {
        public int id;
        public string name;
        public Dictionary<GameEnum.eMoneyType, int> moneyDict;
        public List<int> skinList;
        public List<int> champList;
 
        public UserInfo(int id, string name, Dictionary<GameEnum.eMoneyType, int> moneyDict = null
List<int> skinList = nullList<int> champList = null)
        {
            this.id = id;
            this.name = name;
            //처음 시작하는 유저라면 일정량의 재화 제공.
            if (moneyDict == null)
            {
                this.moneyDict = new Dictionary<GameEnum.eMoneyType, int>();
                this.moneyDict.Add(GameEnum.eMoneyType.Be, 3000);
                this.moneyDict.Add(GameEnum.eMoneyType.Rp, 0);
            }
            else
                this.moneyDict = moneyDict;
 
            if (skinList == null)
                this.skinList = new List<int>();
            else
                this.skinList = skinList;
 
            if (champList == null)
                this.champList = new List<int>();
            else
                this.champList = champList;
        }
 
        //유저 정보 저장.
        public void SaveUserInfo()
        {
            string path = @"C:\Users\skagu\Desktop\C#알고리즘\user_info.json";
            File.WriteAllText(path, JsonConvert.SerializeObject(this));
            Console.WriteLine("UserInfo 저장 완료.\n");
        }
 
        //가진 돈 정보 출력
        public void PrintMoneyInfo()
        {
            Console.WriteLine("보유중인 Be : {0}", moneyDict[GameEnum.eMoneyType.Be]);
            Console.WriteLine("보유중인 Rp : {0}\n", moneyDict[GameEnum.eMoneyType.Rp]);
        }
 
        //가진 모든 챔피언 정보 출력.
        public void PrintHavingAllChamp()
        {
            Console.WriteLine("보유중인 챔프 목록==============");
            foreach(var id in champList)
            {
                var data = DataManager.Instance.GetDataById<LolChampData>(id);
                Console.WriteLine("챔피언 이름 : {0}"data.name);
            }
            Console.WriteLine();
        }
 
        //보유중인 해당 챔피언 스킨 출력.
        public void PrintHavingChampSkin(int champId)
        {
            foreach(var skinId in skinList)
            {
                var skinData = DataManager.Instance.GetDataById<LolSkinData>(skinId);
                if (skinData.champ_id == champId)
                    Console.WriteLine("\t스킨 id : {0}\t스킨 이름 : {1}, "skinData.id, skinData.name);
            }
        }
 
        //보유중인 돈, 챔프, 스킨까지 모두 출력.
        public void PrintAllUserInfo()
        {
            this.PrintMoneyInfo();
            Console.WriteLine("보유중인 챔프 목록==============");
            foreach (var id in champList)
            {
                var data = DataManager.Instance.GetDataById<LolChampData>(id);
                Console.WriteLine("챔피언 이름 : {0}"data.name);
                this.PrintHavingChampSkin(id);
            }
            Console.WriteLine();
        }
 
        //rp구매.
        public void BuyRPMoney(int amount)
        {
            this.moneyDict[GameEnum.eMoneyType.Rp] += amount;
            Console.WriteLine("Rp {0}개 구매 완료. 현재 Rp보유량 : {1}\n",
amount, this.moneyDict[GameEnum.eMoneyType.Rp]);
        }
 
        //be구매.
        public void BuyBeMoney(int amount)
        {
            this.moneyDict[GameEnum.eMoneyType.Be] += amount;
            Console.WriteLine("Be {0}개 구매 완료. 현재 Be보유량 : {1}\n",
amount, this.moneyDict[GameEnum.eMoneyType.Be]);
        }
 
        //챔피언 아이디로 구매.
        public void BuyChampById(int champId, GameEnum.eMoneyType eMoneyType)
        {
            var target = DataManager.Instance.GetDataById<LolChampData>(champId);
 
            if (target == null)
            {
                Console.WriteLine("없는 챔프 id 입니다.\n");
                return;
            }
            //해당 챔피언이 존재하지 않을 때.
            if (target == null)
                Console.WriteLine("해당 챔피언은 존재하지 않습니다.\n", champId);
 
            //해당 챔피언을 찾았을 때.
            else
            {
                //이미 챔피언이 있을 때.
                if (this.champList.Find(x => x == champId) != 0)
                    Console.WriteLine("{0} 챔피언은 이미 보유중 입니다."target.name);
 
                //Be가 모자랄 때.
                else if(eMoneyType == GameEnum.eMoneyType.Be &&
                    this.moneyDict[GameEnum.eMoneyType.Be] < target.be_price)
                {
                    Console.WriteLine("Be가 모자랍니다.\n");
                }
 
                //Rp가 모자랄 때.
                else if(eMoneyType == GameEnum.eMoneyType.Rp &&
                    this.moneyDict[GameEnum.eMoneyType.Rp] < target.rp_price)
                {
                    Console.WriteLine("Rp가 모자랍니다.");
                }
 
                //구매할 수 있을 때.
                else
                {
                    if(eMoneyType == GameEnum.eMoneyType.Be)
                    {
                        this.champList.Add(target.id);
                        this.moneyDict[GameEnum.eMoneyType.Be] -= target.be_price;
                        Console.WriteLine("{0} 챔피언을 구매했습니다. 남은 Be : {1}",
                            target.name, this.moneyDict[GameEnum.eMoneyType.Be]);
                    }
 
                    else if (eMoneyType == GameEnum.eMoneyType.Rp)
                    {
                        this.champList.Add(target.id);
                        this.moneyDict[GameEnum.eMoneyType.Rp] -= target.rp_price;
                        Console.WriteLine("{0} 챔피언을 구매했습니다. 남은 Rp : {1}",
                            target.name, this.moneyDict[GameEnum.eMoneyType.Rp]);
                    }
 
                    //챔프를 구매하면 기본 스킨을 함께 줌.
                    var defaultSkin = DataManager.Instance.GetSkinDataByChampName(target.name)[0];
                    this.skinList.Add(defaultSkin.id);
                }
 
            }
            this.SaveUserInfo();
        }
 
        //챔피언 이름으로 구매.
        public void BuyChampByChampName(string champName, GameEnum.eMoneyType eMoneyType)
        {
            if (DataManager.Instance.GetChampDataByName(champName) == null)
            {
                Console.WriteLine("없는 챔프 이름 입니다.\n");
                return;
            }
            this.BuyChampById(DataManager.Instance.GetChampDataByName(champName).id, eMoneyType);
        }
 
        //스킨 구매.
        public void BuySkinById(int skinId)
        {
            var skinData = DataManager.Instance.GetDataById<LolSkinData>(skinId);
            var skinLimitData = DataManager.Instance.GetDataById<SkinLimitData>(skinData.limit_id);
 
            if(skinData == null)
            {
                Console.WriteLine("없는 스킨 id 입니다.\n");
                return;
            }
 
            //기간이 지나지 않았는지 확인.
            if (skinLimitData != null && skinLimitData.limit_date < App.GameDateTime)
            {
                Console.WriteLine("기간이 이미 지났습니다.\n");
                return;
            }
 
            //해당 챔프가 있는지 확인.
            else if (this.champList.Find(x => x == skinData.champ_id) == 0)
            {
                Console.WriteLine("{0} 챔프가 없어 구매할 수 없습니다.\n",
                    DataManager.Instance.GetDataById<LolChampData>(skinData.champ_id).name);
                return;
            }
 
            //해당 챔프가 있을 때.
 
            //rp가 모자를 때.
            if (this.moneyDict[GameEnum.eMoneyType.Rp] < skinData.rp_price)
            {
                Console.WriteLine("Rp가 부족합니다. 현재 : {0}, 가격 : {1}\n",
                    this.moneyDict[GameEnum.eMoneyType.Rp], skinData.rp_price);
                return;
            }
 
            //해당 스킨이 이미 있을 때.
            else if(this.skinList.Find(x => x == skinId) != 0)
            {
                Console.WriteLine("{0} 스킨은 이미 보유중입니다.\n"skinData.name);
                return;
            }
 
            //구매 가능함.
            else
            {
                this.moneyDict[GameEnum.eMoneyType.Rp] -= skinData.rp_price;
                this.skinList.Add(skinId);
                Console.WriteLine("{0} 스킨을 구매했습니다. 현재 Rp : {1}\n",
                    skinData.name, this.moneyDict[GameEnum.eMoneyType.Rp]);
            }
 
            this.SaveUserInfo();
        }
 
        //스킨 이름으로 구매.
        public void BuySkinByName(string skinName)
        {
            if(DataManager.Instance.GetSkinDataBySkinName(skinName) == null)
            {
                Console.WriteLine("없는 스킨 이름 입니다.");
                return;
            }
            this.BuySkinById(DataManager.Instance.GetSkinDataBySkinName(skinName).id);
        }
    }
}
 
 
 

UserInfo.cs 입니다.

주 기능으로는 데이터의 저장, 스킨 챔프 재화의 구매, 가진 것들의 출력 등이 있습니다.

재화의 양은 종류에 따라 딕셔너리에, 가지고 있는 스킨과 챔프는 List로 해당 id값만 가져오도록 했습니다.

 

 

실행화면 입니다.

이와 같이 입력에 따라 챔프, 스킨을 구매할 수 있으며 모든 스킨과 챔프를 확인할 수도 있습니다.

재화를 충전해가며 무한히 돌려볼 수 있습니다.

 

 

 

LolApp.zip
5.61MB

마지막으로 프로젝트 파일을 엑셀 파일과 함께 올립니다.

이걸 돌려보고 싶으시다면 파일 경로를 재설정하고 돌리시면 되겠습니다.

'C# > 과제' 카테고리의 다른 글

Unity 5Runs 과제.  (0) 2020.05.09
2048 콘솔 게임.  (0) 2020.05.01
레시피 과제.  (0) 2020.04.17
홍길동과 임꺽정의 전투 클래스 사용.  (0) 2020.04.10
과제 04-03  (0) 2020.04.03

관련글 더보기