using Server.System; using Newtonsoft.Json; using Server.SQL; using Server.Manager; using NLog; using System.Diagnostics; using Server.SQL.Excel; namespace Server.Service { public class BuyShopItem : AbstractService { private static readonly NLog.ILogger logger = LogManager.GetCurrentClassLogger(); private BuyShopItemReq req; private void SaveSQL(ItemManager item) { Statics.userSQL.SaveChanges(); Statics.resetShopItemSQL.SaveChanges(); Statics.shopItemSQL.SaveChanges(); item.box.SaveSQL(); } public override string Process() { User user = Statics.userSQL.SelectUuid(req.uuid); eBuyType buy_type; int buy; int reward; int id; ResetShopItem resetShopItem = null; if (req.reset_id == -1) { ShopItemData shopItemData = Statics.shopItemExcel.getShopItemData(req.shopItemIndex); //구매 가능한지 검사 if(shopItemData.buy_count != -1) { List shopItems = Statics.shopItemSQL.SelectUid(user.id, shopItemData.index); Console.WriteLine(shopItems.Count); if(shopItems.Count >= shopItemData.buy_count) { throw new RuntimeException("Not Buy Count", Error.ErrorData); } } buy_type = shopItemData.buy_type; buy = shopItemData.buy; reward = shopItemData.reward; id = shopItemData.index; } else { resetShopItem = Statics.resetShopItemSQL.SelectUid(user.id).Find(n => n.id == req.reset_id); //구매 가능한지 검사 if (resetShopItem.count == 0) { throw new RuntimeException("Not Buy Count", Error.ErrorData); } buy_type = resetShopItem.buy_type; buy = resetShopItem.buy; reward = resetShopItem.reward; id = resetShopItem.id; } //획득 가능한 아이템인지 확인 switch (buy_type) { case eBuyType.gold: if (user.gold < buy) throw new RuntimeException("Not gold", Error.NoGold); user.gold -= buy; break; case eBuyType.cash: if (!user.buyCash(buy)) throw new RuntimeException("Not cash", Error.NoGold); break; case eBuyType.money://현금결제 현재로서는 무조건 결제완료가 나오도록 처리할것 break; default: throw new RuntimeException("Not case", Error.NoData); } ItemManager item = new ItemManager(user); if (reward != 0) { item.addReward(reward); if(resetShopItem == null) { ShopItem shopItem = new ShopItem(); shopItem.user_id = user.id; shopItem.shop_item_data_id = id; shopItem.buy_date = DateTime.UtcNow; Statics.shopItemSQL.Insert(shopItem); } else { resetShopItem.count--; } } else { throw new RuntimeException("ServerError", Error.RuntimeException); } SaveSQL(item); return makeResp(user, item); } public override Protocol ProtocolValue() => Protocol.BuyShopItem; public override Req Requst(string json) { req = JsonConvert.DeserializeObject(json); return req; } private string makeResp(User user, ItemManager itemManager) { BuyShopItemResp resp = new BuyShopItemResp(); resp.gold = user.gold; resp.cash = user.free_cash + user.pay_cash; resp.deck_unit = itemManager.box.addDeckUnitInfo; resp.equipment = itemManager.box.addEquipment; resp.consumableItem = itemManager.box.addConsumableItem; resp.etcItem = itemManager.box.addEtcItem; return resp.ToJson(); } } public class BuyShopItemReq : Req { public long shopItemIndex; public long reset_id; public override bool IsReceivedAllField() { if (uuid == "" || shopItemIndex == 0) return false; return true; } } public class BuyShopItemResp : Resp { public long gold; public long cash; public List deck_unit; public List equipment; public List consumableItem; public List etcItem; } }