using Google.Protobuf; using Google.Protobuf.WellKnownTypes; using ServerCore; using ServerBase; using ServerCommon; using ServerCommon.BusinessLogDomain; using MetaAssets; using META_ID = System.UInt32; namespace GameServer.PacketHandler; [PacketHandler(typeof(ClientToGameReq), typeof(ClientToGameReq.Types.ShopPurchaseItemReq), typeof(ShopPurchaseItemPacketHandler), typeof(GameLoginListener))] public class ShopPurchaseItemPacketHandler : PacketRecvHandler { private static void send_S2C_ACK_SHOP_PURCHASE_ITEM(Player owner, int shop_id, Result result, List? purchasedItems, List? spendItems) { var ack_packet = new ClientToGame { Response = new ClientToGameRes { ErrorCode = result.ErrorCode, ShopPurchaseItemRes = new ClientToGameRes.Types.ShopPurchaseItemRes() } }; var purchase_action = owner.getEntityAction().getMyShopProduct(shop_id) .getEntityAction(); if (null == purchase_action) { ack_packet.Response.ErrorCode = ServerErrorCode.EntityActionNotFound; var err_msg = $"Fail to get entity action: {nameof(PurchaseShopProductAction)}"; Log.getLogger().error(err_msg); result.setFail(ack_packet.Response.ErrorCode, err_msg); } if (result.isSuccess()) { if (null != purchasedItems) { foreach (var item in purchasedItems.Select(info => info.toItemData4Client())) { ack_packet.Response.ShopPurchaseItemRes.Items.Add(item); } } if (null != spendItems) { foreach (var del in spendItems.Select(info => info.toItemData4Client())) { ack_packet.Response.ShopPurchaseItemRes.DelItems.Add(del); } } var account_attribute = owner.getOriginEntityAttribute(); var level_attribute = owner.getOriginEntityAttribute(); var money_attribute = owner.getOriginEntityAttribute(); var nickname_attribute = owner.getOriginEntityAttribute(); var char_info = new CharInfo { Level = (int)level_attribute!.Level, Exp = (int)level_attribute.Exp, Gold = money_attribute!.Gold, Sapphire = money_attribute.Sapphire, Calium = money_attribute.Calium, Ruby = money_attribute.Ruby, Usergroup = account_attribute!.AuthAdminLevelType.ToString(), Operator = (int)account_attribute.AuthAdminLevelType, DisplayName = nickname_attribute!.Nickname, LanguageInfo = (int)account_attribute.LanguageType, IsIntroComplete = 1 }; ack_packet.Response.ShopPurchaseItemRes.CurrencyInfo = char_info; } GameServerApp.getServerLogic().onSendPacket(owner, ack_packet); } public override async Task onProcessPacket(ISession entityWithSession, IMessage recvMessage) { var result = new Result(); string err_msg; var entity_player = entityWithSession as Player; NullReferenceCheckHelper.throwIfNull(entity_player, () => $"entity_player is null !!!"); // 1. 기본 정보 체크 var request = (recvMessage as ClientToGame)?.Request.ShopPurchaseItemReq; if (null == request) { err_msg = $"Failed to get Request !!! : {nameof(ClientToGame.Request.ShopPurchaseItemReq)}"; result.setFail(ServerErrorCode.InvalidArgument, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_SHOP_PURCHASE_ITEM(entity_player, 0, result, null, null); return result; } var check = ShopHelper.checkShopIdFromTableData(request.ShopID); if (check.result.isFail()) { err_msg = $"Failed to check Shop Id from Table Data !!! : {request.ShopID}"; result.setFail(ServerErrorCode.NotFoundShopId, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_SHOP_PURCHASE_ITEM(entity_player, request.ShopID, result, null, null); return result; } // 2. 구매 로직 시행 result = await entity_player.runTransactionRunnerSafely(TransactionIdType.PrivateContents, "ShopPurchase", purchaseShopItemDelegate); if (result.isFail()) { err_msg = $"Failed to runTransactionRunnerSafely()!!! : {result.toBasicString()} - {entity_player.toBasicString()}"; Log.getLogger().error(err_msg); send_S2C_ACK_SHOP_PURCHASE_ITEM(entity_player, request.ShopID, result, null, null); return result; } if (true == MetaData.Instance._ShopProductMetaTable.TryGetValue(request.ProductID, out var shop_product_meta_data)) { if (shop_product_meta_data.ProductData.Item is not null) { await QuestManager.It.QuestCheck(entity_player, new QuestItem(EQuestEventTargetType.ITEM, EQuestEventNameType.BOUGHT, shop_product_meta_data.ProductData.Item.Id)); } } return result; async Task purchaseShopItemDelegate() => await purchaseShopItemAsync(entity_player, request.ShopID, request.ProductID, request.Count); } private async Task purchaseShopItemAsync(Player entity_player, int shop_id, int product_id, int purchase_count) { var result = new Result(); string err_msg; var shop_purchase_info = new ShopPurchaseInfo(); shop_purchase_info.m_shop_id = shop_id; shop_purchase_info.m_shop_product_id = product_id; shop_purchase_info.m_shop_product_count = purchase_count; // 0. action 가져오기 var purchase_action = entity_player.getEntityAction().getMyShopProduct(shop_id).getEntityAction(); if (null == purchase_action) { err_msg = $"Failed to get EntityAction !!! : EntityActionType:PurchaseShopProductAction - {entity_player.toBasicString()}"; result.setFail(ServerErrorCode.EntityActionNotFound, err_msg); Log.getLogger().error(result.toBasicString()); return result; } // 0. 구매 타입 체크 ( get discount type / discount rate ) var discount = purchase_action.checkPurchaseDiscountType(product_id); // 1. 구매 조건 체크 : 상품 조건 var check_shop_product_conditions = purchase_action.checkPurchaseShopProductConditions(shop_id, product_id, purchase_count); if (check_shop_product_conditions.result.isFail()) return check_shop_product_conditions.result; NullReferenceCheckHelper.throwIfNull(check_shop_product_conditions.product_data, () => $"check_shop_product_conditions.product_data is null !!!"); // 2. 구매 조건 체크 : 캐릭터 조건 result = purchase_action.checkPurchaseCharacterConditions(check_shop_product_conditions.product_data); if (result.isFail()) return result; // 3. 구매 처리 result = await purchase_action.processPurchase(check_shop_product_conditions.product_data, purchase_count, discount.discount_rate, shop_purchase_info); if (result.isFail()) return result; NullReferenceCheckHelper.throwIfNull(shop_purchase_info.m_spent, () => $"shop_purchase_info.m_spent is null !!!"); NullReferenceCheckHelper.throwIfNull(shop_purchase_info.m_purchase, () => $"shop_purchase_info.m_purchase is null !!!"); // 4. 재화 소모 var spent = await purchase_action.processSpent(shop_purchase_info.m_spent); if (spent.result.isFail()) return spent.result; shop_purchase_info.m_purchase.m_spend_items = spent.del_items?.ToList(); // 5. 상점 물품 갱신 result = await purchase_action.updateShopProductMeter(product_id, purchase_count); if (result.isFail()) return result; // 6. 첫 구매 히스토리 갱신 if (check_shop_product_conditions.product_data.ProductData.Item != null) { result = await purchase_action.updateItemFirstPurchaseHistory(discount.discount_type, check_shop_product_conditions.product_data.ProductData.Item.Id); if (result.isFail()) return result; } // 7. DB 갱신 var batch = new QueryBatchEx( entity_player, LogActionType.ShopPurchase , GameServerApp.getServerLogic().getDynamoDbClient(), true); { batch.addQuery(new DBQWriteToAttributeAllWithTransactionRunner()); batch.addQuery(new QueryFinal()); } // 8. Business Log 기록 writeBusinessLog(batch, shop_purchase_info); result = await QueryHelper.sendQueryAndBusinessLog(batch); send_S2C_ACK_SHOP_PURCHASE_ITEM(entity_player, shop_id, result, shop_purchase_info.m_purchase.m_purchase_items, shop_purchase_info.m_purchase.m_spend_items?.ToList()); return result; } private void writeBusinessLog(QueryBatchBase queryBatchBase, ShopPurchaseInfo shopPurchaseInfo) { // Shop 정보 기록 var shop_log_data = new ShopLogData { ShopMId = (META_ID)shopPurchaseInfo.m_shop_id, ProductMId = (META_ID)shopPurchaseInfo.m_shop_product_id, ProductCount = shopPurchaseInfo.m_shop_product_count }; var shop_business_log = new ShopBusinessLog(shop_log_data); queryBatchBase.appendBusinessLog(shop_business_log); } }