using Server.System; using Newtonsoft.Json; using Server.SQL; using Server.Manager; using Microsoft.AspNetCore.DataProtection.KeyManagement; using System.Collections.Generic; using Server.SQL.Excel; using static Server.Scheduler.SessionScheduler; namespace Server.Service { //프라이빗uuid가아닌 기기 ip를 받아와 확인 //유저 정보를 확인할때 ip도 같이 확인해서 만약 다르면 차단 public class Login : AbstractService { private LoginReq req; public override string Process() { //string uniqueSessionId = Guid.NewGuid().ToString(); 이러한 형태를 사용해 key값을 저장 //서버에서 데이터를 받아 user의 데이터들을 redis에 저장 //이러한 형태로 세션을 관리. User user; bool newUser = false; #region 로그인 List dynamicDataList = Statics.dynamicDataSQL.Select(); List deckInfoList = null; ItemManager itemManager = null; if (req.uuid == "") { //최초 메일 로그인 //게스트 로그인은 허용하지 않고 무조건 구글로그인 혹은 마스토돈 로그인만 가능하게 처리하기 user = Statics.userSQL.SelectMail(req.mail); if (user != null) { deckInfoList = Statics.deckInfoSQL.SelectUid(user.id); itemManager = new ItemManager(user); } } else { //기존 유저 로그인 user = Statics.userSQL.SelectUuid(req.uuid); if (user == null) { throw new RuntimeException("Not User", Error.NoData); } deckInfoList = Statics.deckInfoSQL.SelectUid(user.id); itemManager = new ItemManager(user); } if(user == null) { if (req.mail == "") { throw new RuntimeException("Not User", Error.NoData); } #region 신규유저 생성 newUser = true; user = new User(); user.mail = req.mail; user.uuid = Guid.NewGuid().ToString(); user.nickname = Statics.dynamicDataSQL.SelectName("defaultNick").value; Statics.userSQL.Insert(user); //저장하고 유닛의 id를 얻어오기 위함. Statics.userSQL.SaveChanges(); itemManager = new ItemManager(user); #endregion #region 초기 유닛 지급 itemManager.addUnit(100001); itemManager.addUnit(100002); itemManager.addUnit(100003); itemManager.addUnit(100004); itemManager.addUnit(100005); itemManager.addUnit(100006); itemManager.addUnit(100007); itemManager.addUnit(100008); itemManager.addUnit(100009); itemManager.box.SaveSQL(); #endregion #region 신규 덱 추가 DeckInfo deckInfo; deckInfo = new DeckInfo(); deckInfo.user_id= user.id; deckInfo.deck_type = 1; deckInfo.deck_unit0_id = itemManager.box.deckUnitInfo[0].id; deckInfo.deck_unit1_id = itemManager.box.deckUnitInfo[1].id; deckInfo.deck_unit2_id = itemManager.box.deckUnitInfo[2].id; deckInfo.deck_unit3_id = itemManager.box.deckUnitInfo[3].id; deckInfo.deck_unit4_id = itemManager.box.deckUnitInfo[4].id; deckInfo.deck_unit5_id = itemManager.box.deckUnitInfo[5].id; deckInfo.deck_unit6_id = itemManager.box.deckUnitInfo[6].id; deckInfo.deck_unit7_id = itemManager.box.deckUnitInfo[7].id; deckInfo.deck_unit8_id = itemManager.box.deckUnitInfo[8].id; Statics.deckInfoSQL.Insert(deckInfo); deckInfoList = new List { deckInfo }; #endregion } #endregion #region 세션등록 //TODO 기존 중복세션에 대한 처리도 필요 //TODO 모든 세션 처리를 세션 스케줄러에서 처리가 가능하도록 수정할것 string loginUUID = Guid.NewGuid().ToString(); Statics.redis.SetList(loginUUID, "LoginUsers"); Dictionary userInfo = new Dictionary(); userInfo.Add("id", user.id); userInfo.Add("uuid", user.uuid); userInfo.Add("mail", user.mail); userInfo.Add("nickname ", user.nickname); userInfo.Add("gold ", user.gold); userInfo.Add("free_cash", user.free_cash); userInfo.Add("pay_cash", user.pay_cash); userInfo.Add("end_login", DateTime.Now); Statics.redis.SetHash(userInfo, "UserInfo", loginUUID); #endregion #region 상점 세팅 //리셋 상점 List resetShopItem = Statics.resetShopItemSQL.SelectUid(user.id); List addItem = new List(); List shopItemData = null; Random rand = new Random(); if (resetShopItem.FindIndex(data => data.shop_index == 10001) == -1) { //일일 상품 추가 if (shopItemData == null) shopItemData = Statics.resetShopItemExcel.getResetShopItemData(); List < ResetShopItemData > shopList = new List(); foreach(var item in shopItemData) { if(item.shop_index == 10001) { shopList.Add(item); } } int count = shopList[0].buy_count; DateTime saveDate = DateTime.UtcNow.AddDays(1); for (int n = 0; n < count; n++) { int select = rand.Next(0, shopList.Count); int index = addItem.FindIndex(data => data.reset_shop_item_data_id == shopList[select].index); if (index != -1) { addItem[index].count++; } else { ResetShopItem newShopItem = new ResetShopItem(); newShopItem.user_id = user.id; newShopItem.reset_shop_item_data_id = shopList[select].index; newShopItem.name = shopList[select].name; newShopItem.shop_index = shopList[select].shop_index; newShopItem.buy_type = shopList[select].buy_type; newShopItem.buy = shopList[select].buy; newShopItem.reward = shopList[select].reward; newShopItem.buy_count = shopList[select].buy_count; newShopItem.count = 1; newShopItem.end_date = saveDate; addItem.Add(newShopItem); } } } if (resetShopItem.FindIndex(data => data.shop_index == 10002) == -1) { //주간 상품 추가 if (shopItemData == null) shopItemData = Statics.resetShopItemExcel.getResetShopItemData(); List shopList = new List(); foreach (var item in shopItemData) { if (item.shop_index == 10002) { shopList.Add(item); } } int count = shopList[0].buy_count; DateTime today = DateTime.UtcNow;//Saturday int daysUntilNext = ((int)DayOfWeek.Wednesday - (int)today.DayOfWeek + 7) % 7; DateTime saveDate = today.AddDays(daysUntilNext); for (int n = 0; n < count; n++) { int select = rand.Next(0, shopList.Count); int index = addItem.FindIndex(data => data.reset_shop_item_data_id == shopList[select].index); if (index != -1) { addItem[index].count++; } else { ResetShopItem newShopItem = new ResetShopItem(); newShopItem.user_id = user.id; newShopItem.reset_shop_item_data_id = shopList[select].index; newShopItem.name = shopList[select].name; newShopItem.shop_index = shopList[select].shop_index; newShopItem.buy_type = shopList[select].buy_type; newShopItem.buy = shopList[select].buy; newShopItem.reward = shopList[select].reward; newShopItem.buy_count = shopList[select].buy_count; newShopItem.count = 1; newShopItem.end_date = saveDate; addItem.Add(newShopItem); } } } if (resetShopItem.FindIndex(data => data.shop_index == 10003) == -1) { //월간 상품 추가 if (shopItemData == null) shopItemData = Statics.resetShopItemExcel.getResetShopItemData(); List shopList = new List(); foreach (var item in shopItemData) { if (item.shop_index == 10003) { shopList.Add(item); } } int count = shopList[0].buy_count; DateTime today = DateTime.UtcNow; int year = today.Year; int month = today.Month; if (month == 12) { year++; month = 1; } else { month++; } DateTime saveDate = new DateTime(year, month, 1, 0, 0, 0, DateTimeKind.Utc); for (int n = 0; n < count; n++) { int select = rand.Next(0, shopList.Count); int index = addItem.FindIndex(data => data.reset_shop_item_data_id == shopList[select].index); if (index != -1) { addItem[index].count++; } else { ResetShopItem newShopItem = new ResetShopItem(); newShopItem.user_id = user.id; newShopItem.reset_shop_item_data_id = shopList[select].index; newShopItem.name = shopList[select].name; newShopItem.shop_index = shopList[select].shop_index; newShopItem.buy_type = shopList[select].buy_type; newShopItem.buy = shopList[select].buy; newShopItem.reward = shopList[select].reward; newShopItem.buy_count = shopList[select].buy_count; newShopItem.count = 1; newShopItem.end_date = saveDate; addItem.Add(newShopItem); } } } if(addItem.Count != 0) { Statics.resetShopItemSQL.Insert(addItem); Statics.resetShopItemSQL.SaveChanges(); resetShopItem.AddRange(addItem); } //상점 구매 목록 List shopItem = Statics.shopItemSQL.SelectUid(user.id); #endregion return makeResp(user, dynamicDataList, deckInfoList, itemManager, newUser, resetShopItem, shopItem); } public override Protocol ProtocolValue() => Protocol.Login; public override Req Requst(string json) { req = JsonConvert.DeserializeObject(json); return req; } private string makeResp(User user, List dynamicData, List deckInfo, ItemManager itemManager, bool newUser, List resetShopItem, List shopItem) { LoginResp resp = new LoginResp(); resp.nickname = user.nickname; resp.uuid = user.uuid; resp.gold = (ulong)user.gold; resp.cash = (ulong)(user.free_cash + user.pay_cash); resp.new_user = newUser; resp.dynamic_data = dynamicData; resp.deck_unit = itemManager.box.deckUnitInfo; resp.deck_info = deckInfo; resp.equipment = itemManager.box.equipment; resp.consumable_item = itemManager.box.consumableItem; resp.etc_item = itemManager.box.etcItem; resp.reset_shop_item = resetShopItem; resp.shop_item = shopItem; resp.status = 200; return resp.ToJson(); } } public class LoginReq : Req { public string mail; public override bool IsReceivedAllField() { if(mail == null && uuid == null) return false; return true; } public override bool GetRedis() { return true; } } public class LoginResp : Resp { public string uuid; public string nickname; public ulong gold; public ulong cash; public bool new_user; public List dynamic_data; public List deck_unit; public List deck_info; public List equipment; public List consumable_item; public List etc_item; public List reset_shop_item; public List shop_item; } }