thewar_server/Server/Service/BuyShopItem.cs

101 lines
3.1 KiB
C#

using Server.System;
using Newtonsoft.Json;
using Server.SQL;
using Server.Manager;
using NLog;
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();
item.box.SaveSQL();
}
public override string Process()
{
User user = Statics.userSQL.SelectUuid(req.uuid);
ShopItemData shopItemData = Statics.shopItemExcel.getShopItemData(req.shopItemIndex);
//획득 가능한 아이템인지 확인
switch (shopItemData.buy_type)
{
case eBuyType.gold:
if (user.gold < shopItemData.buy)
throw new RuntimeException("Not gold", Error.nogold);
user.gold -= shopItemData.buy;
break;
case eBuyType.cash:
if (!user.buyCash(shopItemData.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 (shopItemData.reward != 0)
item.addReward(shopItemData.reward);
else
logger.Error("처리 필요");
SaveSQL(item);
return makeResp(user, item);
}
public override Protocol ProtocolValue() => Protocol.BuyShopItem;
public override Req Requst(string json)
{
req = JsonConvert.DeserializeObject<BuyShopItemReq>(json);
return req;
}
private string makeResp(User user, ItemManager itemManager)
{
BuyShopItemResp resp = new BuyShopItemResp();
resp.status = 200;
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 string uuid;
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<DeckUnitInfo> deck_unit;
public List<Equipment> equipment;
public List<ConsumableItem> consumableItem;
public List<EtcItem> etcItem;
}
}