thewar_server/Server/Service/Login.cs

326 lines
13 KiB
C#

using Server.System;
using Newtonsoft.Json;
using Server.SQL;
using Server.Manager;
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<DynamicData> dynamicDataList = Statics.dynamicDataSQL.Select();
List<DeckInfo> 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);
Statics.deckInfoSQL.SaveChanges();
deckInfoList = new List<DeckInfo> { deckInfo };
#endregion
}
//세션 추가
string session = addSession(user);
#endregion
#region
//리셋 상점
List<ResetShopItem> resetShopItem = Statics.resetShopItemSQL.SelectUid(user.id);
List<ResetShopItem> addItem = new List<ResetShopItem>();
List<ResetShopItemData> 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<ResetShopItemData>();
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<ResetShopItemData> shopList = new List<ResetShopItemData>();
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;
if (daysUntilNext == 0) daysUntilNext = 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<ResetShopItemData> shopList = new List<ResetShopItemData>();
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> shopItem = Statics.shopItemSQL.SelectUid(user.id);
#endregion
return makeResp(user, session, dynamicDataList, deckInfoList, itemManager, newUser, resetShopItem, shopItem);
}
public override Protocol ProtocolValue() => Protocol.Login;
public override Req Requst(string json)
{
req = JsonConvert.DeserializeObject<LoginReq>(json);
return req;
}
private string makeResp(User user, string session, List<DynamicData> dynamicData, List<DeckInfo> deckInfo, ItemManager itemManager, bool newUser, List<ResetShopItem> resetShopItem, List<ShopItem> shopItem)
{
LoginResp resp = new LoginResp();
resp.nickname = user.nickname;
resp.uuid = user.uuid;
resp.session = session;
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 session;
public string nickname;
public ulong gold;
public ulong cash;
public bool new_user;
public List<DynamicData> dynamic_data;
public List<DeckUnitInfo> deck_unit;
public List<DeckInfo> deck_info;
public List<Equipment> equipment;
public List<ConsumableItem> consumable_item;
public List<EtcItem> etc_item;
public List<ResetShopItem> reset_shop_item;
public List<ShopItem> shop_item;
}
}