c# 강의/과제
20200429 - 일일보상 + 연속보상 + Date연산
쪼혀
2020. 5. 1. 00:06
연속 출석 했을때 받는 보상을, 누적하면 추가로 주는 보너스 보상으로 생각했었다.
가장 최근에 했던 검은사막m 했던 기억이 나를 지배했다. ㄷㄷ 좀더 귀를 열고 강사님 말씀에 집중해야 할 듯
최근에 과제를 하면서, 확실이 게임 개발은 각 기능의 알고리즘을 이해하는것이 특히나 중요한 것 같다.
> 결과
> 코드
1. Data.cs
더보기
class EverydayData
{
public int id;
public int everyday_date;
public int everyday_item;
public int everyday_item_amount;
}
class ItemData
{
public int id;
public string item_name;
public string icon_item_image;
public int amount;
}
2. DataManager.cs
더보기
class DataManager
{
private static DataManager singleTone;
private Dictionary<int, ItemData> dicItemData;
private Dictionary<int, EverydayData> dicEverydataData;
private DataManager()
{
this.dicEverydataData = new Dictionary<int, EverydayData>();
this.dicItemData = new Dictionary<int, ItemData>();
}
public bool LoadDatas()
{
bool reuslt = false;
string path = "";
string json = "";
try
{
path = "./data/everyday_data.json";
if (File.Exists(path))
{
json = File.ReadAllText(path);
// Console.WriteLine(json);
this.dicEverydataData = JsonConvert
.DeserializeObject<EverydayData[]>(json)
.ToDictionary(v => v.id, v => v);
}
path = "./data/item_data.json";
if (File.Exists(path))
{
json = File.ReadAllText(path);
this.dicItemData = JsonConvert
.DeserializeObject<ItemData[]>(json)
.ToDictionary(v => v.id, v => v);
}
reuslt = true;
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
throw;
}
return reuslt;
}
public static DataManager GetInstance()
{
if (DataManager.singleTone == null)
{
DataManager.singleTone = new DataManager();
return DataManager.singleTone;
}
else
{
return DataManager.singleTone;
}
}
public Dictionary<int, EverydayData> GetEverydataDatas()
{
return this.dicEverydataData;
}
public EverydayData GetEverydayData(int id)
{
return this.dicEverydataData[id];
}
public Dictionary<int, ItemData> GetItemDatas()
{
return this.dicItemData;
}
public ItemData GetItemData(int id)
{
return this.dicItemData[id];
}
}
3. App.cs
더보기
class App
{
DataManager dm;
UserInfo info;
public static DateTime TODAY;
DateTime endDay;
public App()
{
this.Init();
this.endDay = TODAY.AddDays(28);
while (true)
{
this.Login();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.Write("* test 입력 (1번 출석 / 2번 다음날 접속): ");
string input = Console.ReadLine();
Console.WriteLine();
if (TODAY > this.endDay)
{
Console.WriteLine("!!!!! 출석 이벤트 종료 !!!!!");
return;
}
this.View(input);
Console.WriteLine();
TODAY = TODAY.AddDays(1);
this.info.SaveUserInfoData();
}
}
public void Init()
{
dm = DataManager.GetInstance();
dm.LoadDatas();
TODAY = DateTime.Now;
this.info = new UserInfo();
}
public void GetItemPrint(ItemData item)
{
Console.WriteLine("ㅡㅡㅡㅡ 일간 출석 보상 ㅡㅡㅡㅡ");
Console.WriteLine("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
Console.WriteLine($"- {this.info.dayCount}일자 아이템 입니다. 아이템 우편으로 획득");
Console.WriteLine($"{item.item_name}({item.amount})");
Console.WriteLine("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
}
public void ChainDayPrint(int eventDay, ItemData chainItem, int amount)
{
Console.WriteLine("ㅡㅡㅡㅡ 연속 보상 ㅡㅡㅡㅡ");
Console.WriteLine("□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□");
Console.WriteLine($"- 누적{this.info.rewardedCount}일 입니다. 아이템 추가증정");
Console.WriteLine($"{chainItem.item_name}({amount})");
Console.WriteLine("□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□");
this.info.RewardedItem(this.info.userId, chainItem);
}
public void View(string check)
{
var data = dm.GetEverydataDatas();
List<int> list = new List<int>(data.Keys);
if (check != "1")
{
Console.WriteLine("ㅡㅡㅡ 하루소요 ㅡㅡㅡ");
Console.WriteLine($"오늘날짜:[{TODAY.Month}월{TODAY.Day}일] / 이벤트 남은일자:[{(this.endDay - TODAY).Days}일]");
return;
}
foreach (var key in list)
{
var item = dm.GetItemData(data[key].everyday_item);
Console.Write($"[{data[key].everyday_date}일]{item.item_name}");
Console.Write($"({data[key].everyday_item_amount})");
Console.Write(" ");
// 줄바꿈
if (data[key].everyday_date % 7 == 0) { Console.WriteLine(); }
}
Console.WriteLine();
Console.WriteLine($"[출석:{this.info.dayCount}]/[누적:{this.info.rewardedCount}일] / 오늘날짜:[{TODAY.Month}월{TODAY.Day}일] / 이벤트 남은일자:[{(this.endDay - TODAY).Days}일]");
Console.WriteLine();
//일일 보상 ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
// 전체 일정에서 금일 보상에 따른 아이템 찾기
var todayRewardItemData = data.FirstOrDefault(v => v.Value.everyday_date == this.info.dayCount).Value;
// 금일 지급할 아이템 키
int todayRewardItemKey = todayRewardItemData.everyday_item;
// 금일 지급할 아이템 지급 수량
int todayRewardItemAmount = todayRewardItemData.everyday_item_amount;
// 찾은 키로 아이템 가져오기
ItemData todayRewardItem = dm.GetItemData(todayRewardItemKey);
todayRewardItem.amount = todayRewardItemAmount;
this.ItemGet(todayRewardItem, todayRewardItemAmount);
//연속 보상 ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
var awChack = this.info.chainDayList.Contains(this.info.rewardedCount);
if (awChack)
{
switch (this.info.rewardedCount)
{
case 5:
this.info.chainDayList.Remove(this.info.rewardedCount);
this.ChainDayPrint(5, dm.GetItemData(1008), 1);
break;
case 10:
this.info.chainDayList.Remove(this.info.rewardedCount);
this.ChainDayPrint(10, dm.GetItemData(1004), 200);
break;
case 20:
this.info.chainDayList.Remove(this.info.rewardedCount);
this.ChainDayPrint(20, dm.GetItemData(1004), 500);
break;
default:
break;
}
}
}
public void ItemGet(ItemData rewardedItem, int amount)
{
// 일일 보상 획득
this.GetItemPrint(rewardedItem);
bool reuslt = this.info.RewardedItem(this.info.userId, rewardedItem);
}
public void Login()
{
this.info.LoadUserInfoData();
}
}
4. UserInfo.cs
더보기
class UserInfo
{
public string userId;
public string rewardedDate;
public int dayCount;
public int rewardedCount;
public List<int> chainDayList;
public Dictionary<int, ItemData> dicRewardedItems;
public UserInfo()
{
this.dicRewardedItems = new Dictionary<int, ItemData>();
}
public UserInfo(string userId, string rewardedDate, Dictionary<int, ItemData> dicRewardedItems)
{
this.userId = userId;
this.rewardedDate = rewardedDate;
this.dicRewardedItems = dicRewardedItems;
}
public bool RewardedItem(string userid, ItemData rewardedItem)
{
this.userId = userid;
this.rewardedDate = App.TODAY.ToString("yyyy-MM-dd HH:mm:s"); ;
// 같은 아이템의경우 수량만 추가
if (!this.dicRewardedItems.ContainsKey(rewardedItem.id))
{
this.dicRewardedItems.Add(rewardedItem.id, rewardedItem);
}
else
{
this.dicRewardedItems[rewardedItem.id].amount += rewardedItem.amount;
Console.WriteLine("{0}(소지수량:{1}) + {2}획득",
this.dicRewardedItems[rewardedItem.id].item_name,
this.dicRewardedItems[rewardedItem.id].amount,
rewardedItem.amount
);
Console.WriteLine();
}
return this.SaveUserInfoData();
}
public bool SaveUserInfoData()
{
bool result = false;
var json = JsonConvert.SerializeObject(this);
try
{
File.WriteAllText("./info/userInfo.json", json);
result = true;
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
throw;
}
return result;
}
public void LoadUserInfoData()
{
DirectoryInfo folder = new DirectoryInfo("./info");
if (!folder.Exists)
{
folder.Create();
}
var path = "./info/userInfo.json";
try
{
if (File.Exists(path))
{
var json = File.ReadAllText(path);
UserInfo readInfo = new UserInfo();
readInfo = JsonConvert.DeserializeObject<UserInfo>(json);
this.rewardedDate = readInfo.rewardedDate;
this.userId = readInfo.userId;
this.dicRewardedItems = readInfo.dicRewardedItems;
this.dayCount = readInfo.dayCount;
this.rewardedCount = readInfo.rewardedCount;
this.chainDayList = readInfo.chainDayList;
}
else
{
Console.WriteLine("-신규유저입니다.");
Console.WriteLine("-ID는 lion 입니다.");
this.userId = "lion";
this.chainDayList = new List<int> { 5, 10, 15 };
}
}
catch (Exception e)
{
Console.Error.WriteLine(e);
throw;
}
this.dayCount++;
if (!this.RewardedCountCheck())
{
this.rewardedCount = 0;
}
this.rewardedCount++;
}
public bool RewardedCountCheck()
{
DateTime loadDate = Convert.ToDateTime(this.rewardedDate);
if (this.rewardedDate != null)
{
if (App.TODAY.AddDays(-1).Day == loadDate.Day)
{
return true;
}
}
this.rewardedDate = App.TODAY.ToString("yyyy-MM-dd HH:mm:s");
return false;
}
}