using System.Collections.Concurrent; using Google.Protobuf; using Google.Protobuf.WellKnownTypes; using ServerCore; using ServerBase; using ServerCommon; using ServerCommon.BusinessLogDomain; using MetaAssets; using ITEM_GUID = System.String; using META_ID = System.UInt32; namespace GameServer; public class ShopAction : EntityActionBase { private readonly ConcurrentDictionary m_my_shop_products = new(); private readonly MySoldProduct m_my_sold_product; public ShopAction(Player owner) : base(owner) { m_my_sold_product = new MySoldProduct(owner); } public override async Task onInit() { await Task.CompletedTask; var result = new Result(); return result; } public override void onClear() { return; } public MySoldProduct getMySoldProduct() => m_my_sold_product; public ShopSoldProductAction getSolProductAction() { var shop_sold_product_action = m_my_sold_product.getEntityAction(); NullReferenceCheckHelper.throwIfNull(shop_sold_product_action, () => $"shop_sold_product_action is null !!!"); return shop_sold_product_action; } public IEnumerable getMyShopProducts() => m_my_shop_products.Select(shop => shop.Value).ToList(); public MyShopProduct getMyShopProduct(int shop_id) { if (m_my_shop_products.TryGetValue(shop_id, out var my_shop_product)) { return my_shop_product; } var owner = getOwner() as Player; my_shop_product = new MyShopProduct(owner!); setMyShopProduct(shop_id, my_shop_product); return my_shop_product; } public async Task setMyShopProductFromDoc(ShopProductTradingMeterDoc docBase) { var result = new Result(); string err_msg; var owner = getOwner() as Player; NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!"); var doc_attr = docBase.getAttrib(); if (null == doc_attr) { err_msg = $"Fail to get doc attrib : {nameof(ShopProductTradingMeterDoc)} is null"; result.setFail(ServerErrorCode.EntityAttributeNotFound, err_msg); Log.getLogger().error(err_msg); return result; } var my_shop_product = getMyShopProduct(doc_attr.ShopId); var my_shop_attribute = my_shop_product.getEntityAttribute(); if (null == my_shop_attribute) { err_msg = $"Fail to get entity attribute : {nameof(ShopProductTradingMeterAttribute)} is null"; result.setFail(ServerErrorCode.EntityAttributeIsNull, err_msg); Log.getLogger().error(err_msg); return result; } // doc 에서 직접 copy 된 경우 pk, sk 를 셋팅해야 한다. docBase.setCombinationKeyForPK(owner.getUserGuid()); docBase.setCombinationKeyForSK(doc_attr.ShopId.ToString()); var is_apply = docBase.onApplyPKSK(); if (ServerErrorCode.Success != is_apply) { err_msg = $"Fail to copy to attribute from doc : invalid argument {nameof(setMyShopProductFromDoc)}"; result.setFail(is_apply, err_msg); Log.getLogger().error(err_msg); return result; } var is_success = my_shop_attribute.copyEntityAttributeFromDoc(docBase); if (false == is_success) { err_msg = $"Fail to copy shop product trading meter attribute : to {nameof(ShopProductTradingMeterAttribute)} from {nameof(ShopProductTradingMeterDoc)}"; result.setFail(ServerErrorCode.DynamoDbDocCopyToEntityAttributeFailed, err_msg); Log.getLogger().error(err_msg); } return await Task.FromResult(result); } public async Task cheatInitResetTime() { foreach (var shop_product in m_my_shop_products) { var attribute = shop_product.Value.getEntityAttribute(); if (attribute == null) continue; attribute.initEndTime(); attribute.modifiedEntityAttribute(); } return await Task.FromResult(new Result()); } public async Task cheatInitShopRenewalCount(Int32 shopId) { await Task.CompletedTask; var shop = getMyShopProduct(shopId); var result = new Result(); var attribute = shop.getEntityAttribute (); var owner = getOwner(); if (attribute is null) { Log.getLogger().error($"Shop Product Attribute not exist shopId : {shopId}, owner : {owner.toBasicString()}"); return result; } attribute.setCurrentRenewalCount(0); attribute.modifiedEntityAttribute(); return result; } private void setMyShopProduct(int shop_id, MyShopProduct product) => m_my_shop_products.TryAdd(shop_id, product); public async Task<(Result result, GameServer.Item? changed_item, CurrencyType spent_currency_type, double spent_currency)> processRePurchase(MetaAssets.ItemMetaData item_data, SoldProduct sold) { var result = new Result(); var player = getOwner() as Player; // 1. inventory 아이템 추가 var inventory_action = player?.getEntityAction(); if (null == inventory_action) { var err_msg = $"Fail to get Inventory Action : {nameof(InventoryActionBase)}."; result.setFail(ServerErrorCode.EntityActionNotFound, err_msg); Log.getLogger().error(err_msg); return (result, null, CurrencyType.None, 0); } var item_attrib = sold.ItemDoc.getAttrib(); NullReferenceCheckHelper.throwIfNull(item_attrib, () => $"item_attrib is null !!! - {player?.toBasicString()}"); (result, var changed_item) = await inventory_action.tryTakableToBag(sold.ItemDoc); if(result.isFail() || null == changed_item) return (result, null, CurrencyType.None, 0); // 2. sold item list 갱신 var sold_action = player?.getEntityAction().getMySoldProduct().getEntityAction(); if (null == sold_action) { var err_msg = $"Fail to get Sold Product Action : {nameof(ShopSoldProductAction)}."; result.setFail(ServerErrorCode.EntityActionNotFound, err_msg); Log.getLogger().error(err_msg); return (result, null, CurrencyType.None, 0); } result = await sold_action.updateSoldProduct(item_attrib.ItemGuid, -1 * item_attrib.ItemStackCount); var check_currency_type = ShopHelper.checkCurrencyTypeFromCurrencyId(item_data.SellId); if (check_currency_type.result.isFail()) return (check_currency_type.result, null, CurrencyType.None, 0); return (result, changed_item, check_currency_type.currencyType, item_data.SellPrice * item_attrib.ItemStackCount); } public async Task processSpent(CurrencyType spent_currency_type, double spent_currency) { var result = new Result(); var my_shop_product = getOwner(); var player = my_shop_product.getRootParent() as Player; var money_action = player?.getEntityAction(); if (null == money_action) { var err_msg = $"Fail to get Money Action : {nameof(MoneyAction)}."; result.setFail(ServerErrorCode.EntityActionNotFound, err_msg); Log.getLogger().error(err_msg); return result; } result = await money_action.changeMoney(spent_currency_type, -1 * spent_currency); return result; } public Result checkItemSellConditionAsync(ItemAttributeBase attribute, int sellCount) { var result = new Result(); string err_msg; var player = getOwner() as Player; NullReferenceCheckHelper.throwIfNull(player, () => $"player is null !!!"); // 0. 재판매 여부 확인 var check_item = ShopHelper.checkItemIdFromTableData((int)attribute.ItemMetaId); if (check_item.result.isFail() || null == check_item.item_data) return check_item.result; if (false == check_item.item_data.IsSystemTradable) { err_msg = $"failed to check sell item : cannot sell item !! - itemId[{attribute.ItemMetaId}] , isSystemTrade[{check_item.item_data.IsSystemTradable}]"; result.setFail(ServerErrorCode.ShopItemCannotSell, err_msg); Log.getLogger().error(result.toBasicString()); return result; } // 1. 착용 여부 확인 if (InvenEquipType.None != attribute.EquipedInvenType) { err_msg = $"Failed to check equipped item : already equipped - {attribute.ItemGuid}"; result.setFail(ServerErrorCode.SlotsAlreadyEquiped, err_msg); Log.getLogger().error(err_msg); return result; } // 2. 마이홈 배치 여부 확인 var myhome_agent_action = player.getEntityAction(); NullReferenceCheckHelper.throwIfNull(myhome_agent_action, () => $"myhome_agent_action is null !!! - {player.toBasicString()}"); if (false == myhome_agent_action.tryGetSelectedMyhome(out var my_home)) return result; var myhome_inventory_action = my_home.getEntityAction(); NullReferenceCheckHelper.throwIfNull(myhome_inventory_action, () => $"myhome_inventory_action is null !!! - {player.toBasicString()}"); if (myhome_inventory_action.tryGetItemByItemGuid(attribute.ItemGuid) != null) { err_msg = $"Failed to check setting my home item : item guid - {attribute.ItemGuid}"; result.setFail(ServerErrorCode.ShopIsMyHomeItem, err_msg); Log.getLogger().error(err_msg); } // 3. stack 수량 체크 if (attribute.ItemStackCount < sellCount) { err_msg = $"Failed to check stack count : stackCount[{attribute.ItemStackCount}] / sellCount[{sellCount}]"; result.setFail(ServerErrorCode.InvalidItemCount, err_msg); Log.getLogger().error(err_msg); } return result; } }