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;
namespace Study_019_2
{
public class App
{
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(2020, 05, day, 10, 20, 10));
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 Newtonsoft.Json;
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 = null, Dictionary<int, ComboRewardInfo> comboInfoDict = null, int presentCount = 0, int comboPresent = 0)
{
this.id = id;
this.presentCount = presentCount;
this.comboPresent = comboPresent;
this.comboInfoDict = (comboInfoDict == null) ? DefaultComboRewardInfoDict() : comboInfoDict;
this.SaveCharacterInfo();
}
public static CharacterInfo LoadCharacterInfo()
{
if (File.Exists(filePath))
{
return JsonConvert.DeserializeObject<CharacterInfo>(File.ReadAllText(filePath));
}
else
}
public Dictionary<int, ComboRewardInfo> DefaultComboRewardInfoDict()
{
var rtnDict = new Dictionary<int, ComboRewardInfo>();
var comboDatas = DataManager.Instance.GetAllDatasByType<ComboRewardData>();
foreach (var combo in comboDatas)
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);
//보상 받기.
{
this.GetPresentReward();
this.presentCount++;
}
//연속보상 업데이트.
//연속 들어왔을 때.
this.comboPresent++;
//같은날 또 들어왔을 떄.
//끊길때.
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);
//아이템 획득
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);
{
Console.WriteLine("연속 출석 {0}일차 보상을 얻습니다.", comboData.combo_day);
this.GetItem(comboData.item_id, comboData.reward_amount);
}
}
public void GetItem(int itemId, int amount = 1)
{
//아이템이 없을 때.
if (ItemInfo == null)
itemList.Add(new ItemInfo(itemId, amount));
else
Console.WriteLine("아이템 {0}을\t {1}개 획득했습니다.\n", DataManager.Instance.GetDataById<ItemData>(itemId).name, amount);
}
public void PrintAllItems()
{
Console.WriteLine("아이템 목록\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
foreach(var item in this.itemList)
{
}
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.IO;
using Newtonsoft.Json;
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)
foreach (var data in comboDatas)
foreach (var data in presentRewardDatas)
Console.WriteLine("데이터 불러오기 완료=====\n");
}
public List<T> GetAllDatasByType<T>() where T : RawData
{
var rtnList = new List<T>();
{
if (data is T)
rtnList.Add(data as T);
}
return rtnList;
}
public T GetDataById<T>(int id) where T : RawData
{
}
public void PrintAllDatasByType<T>() where T : RawData
{
foreach(var data in this.GetAllDatasByType<T>())
{
if(data is ItemData)
{
var casted = data as ItemData;
}
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}",
}
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;
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, 등등의 데이터 클래스 입니다.
Study_004 - UnityActionExmaple (0) | 2020.05.11 |
---|---|
Unity Study_003 5Run (0) | 2020.05.08 |
Study_018-1 아이템 상점 구현. (0) | 2020.04.28 |
Study_014 - json변환, 파일입출력, Dictionary (0) | 2020.04.22 |
json파일 예제. With File 3가지 출력. (0) | 2020.04.21 |