using Google.Protobuf; using Google.Protobuf.WellKnownTypes; using ServerCore; using ServerBase; using ServerCommon; using ServerCommon.BusinessLogDomain; using MetaAssets; using static ClientToGameRes.Types; namespace GameServer.PacketHandler; [PacketHandler(typeof(ClientToGameReq), typeof(ClientToGameReq.Types.C2GS_REQ_RENT_FLOOR), typeof(RentFloorPacketHandler), typeof(GameLoginListener))] internal class RentFloorPacketHandler : PacketRecvHandler { public static bool send_S2C_ACK_RENT_FLOOR(Player owner, Result result, GS2C_ACK_RENT_FLOOR res) { var ack_packet = new ClientToGame(); ack_packet.Response = new ClientToGameRes(); ack_packet.Response.ErrorCode = result.ErrorCode; ack_packet.Response.AckRentFloor = res; if (false == GameServerApp.getServerLogic().onSendPacket(owner, ack_packet)) { return false; } return true; } public override async Task onProcessPacket(ISession entityWithSession, Google.Protobuf.IMessage recvMessage) { var result = new Result(); var err_msg = string.Empty; var player = entityWithSession as Player; NullReferenceCheckHelper.throwIfNull(player, () => $"player is null !!!"); var server_logic = GameServerApp.getServerLogic(); var req_msg = recvMessage as ClientToGame; NullReferenceCheckHelper.throwIfNull(req_msg, () => $"req_msg is null !!! - {player.toBasicString()}"); var request = req_msg.Request.ReqRentFloor; NullReferenceCheckHelper.throwIfNull(request, () => $"request is null !!! - {player.toBasicString()}"); var res = new GS2C_ACK_RENT_FLOOR(); // 랜드 if (!MetaData.Instance._LandTable.TryGetValue(request.LandId, out var land_meta_data)) { err_msg = $"Failed to MetaData.TryGetValue() !!! : LandMetaId:{request.LandId} - {player.toBasicString()}"; result.setFail(ServerErrorCode.LandMetaDataNotFound, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } if (!MapManager.Instance.GetLandMapTree(request.LandId, out var land_map_tree)) { err_msg = $"Failed to GetLandMapTree() !!! : LandMetaId:{request.LandId} - {player.toBasicString()}"; result.setFail(ServerErrorCode.LandMapTreeDataNotFound, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } if (land_meta_data.LandType != MetaAssets.LandType.RENTAL) { err_msg = $"LandType is not RENTAL !!! : LandMetaId:{land_meta_data.LandId} - {player.toBasicString()}"; result.setFail(ServerErrorCode.RentalNotAvailableLand, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } if (!server_logic.getLandManager().tryGetLand(request.LandId, out var land)) { err_msg = $"Failed to tryGetLand() !!! : landMetaId:{request.LandId} - {player.toBasicString()}"; result.setFail(ServerErrorCode.LandNotFound, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } // 빌딩 var building_map_tree = land_map_tree.ChildBuildingMapTree; if (building_map_tree == null) { err_msg = $"Not Exist LandMapTree ChildBuilding !!! : LandMap:{land_map_tree.LandMapFileName} - {player.toBasicString()}"; result.setFail(ServerErrorCode.LandMapTreeChildBuildingNotFound, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } if (!MetaData.Instance._BuildingTable.TryGetValue(request.BuildingId, out var building_meta_data)) { err_msg = $"Failed to MetaData.TryGetValue() !!! : BuildingMetaId:{request.BuildingId} - {player.toBasicString()}"; result.setFail(ServerErrorCode.BuildingMetaDataNotFound, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } if (building_map_tree.BuildingMetaId != request.BuildingId) { err_msg = $"Not Match LandMap ChildBuilding !!! : ChildBuildingMetaId:{building_map_tree.BuildingMetaId}, inputBuildingMetaId:{request.BuildingId} - {player.toBasicString()}"; result.setFail(ServerErrorCode.LandMapTreeChildBuildingNotFound, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } if (!server_logic.getBuildingManager().tryGetBuilding(request.BuildingId, out var building)) { err_msg = $"Failed to tryGetBuilding() !!! : buildingMetaId:{request.BuildingId} - {player.toBasicString()}"; result.setFail(ServerErrorCode.BuildingNotFound, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } var building_action = building.getEntityAction(); NullReferenceCheckHelper.throwIfNull(building_action, () => $"building_action is null !!! - {player.toBasicString()}"); var is_load_from_db = building_action.isLoadFromDb(); if (land_meta_data.RentalStateSwitch && is_load_from_db) { if (!building_action.isRentalOpen()) { err_msg = $"Land Rental is not Open !!! : LandMetaId:{land_meta_data.LandId} - {player.toBasicString()}"; result.setFail(ServerErrorCode.RentalNotAvailableLand, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } } else { if (!land_meta_data.RentalAvailable) { err_msg = $"Land RentalAvailable is false !!! : LandMetaId:{land_meta_data.LandId} - {player.toBasicString()}"; result.setFail(ServerErrorCode.RentalNotAvailableLand, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } } // 층 if (request.Floor <= 0 || request.Floor > building_meta_data.InstanceSocket_) { err_msg = $"Floor value is invalid !!! : BuildingMetaId:{building_map_tree.BuildingMetaId}, Floor:{request.Floor} - {player.toBasicString()}"; result.setFail(ServerErrorCode.RentalAddressInvalid, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } if (building_map_tree.ChildRoomMapTrees.TryGetValue(request.Floor, out var room_map_tree)) { err_msg = $"Address is not Empty !!! : BuildingMetaId:{building_map_tree.BuildingMetaId}, Floor:{request.Floor} - {player.toBasicString()}"; result.setFail(ServerErrorCode.RentalAddressIsNotEmpty, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } var building_floor_agent_action = building.getEntityAction(); NullReferenceCheckHelper.throwIfNull(building_floor_agent_action, () => $"building_floor_agent_action is null !!! - {player.toBasicString()}"); if (!building_floor_agent_action.isEmptyFloor(request.Floor)) { err_msg = $"Building Floor is not Empty !!! : BuildingMetaId:{request.BuildingId}, Floor:{request.Floor} - {player.toBasicString()}"; result.setFail(ServerErrorCode.BuildingFloorIsNotEmpty, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } // 마이홈 var myhome_agent_action = player.getEntityAction(); NullReferenceCheckHelper.throwIfNull(myhome_agent_action, () => $"myhome_agent_action is null !!! - {player.toBasicString()}"); if (!myhome_agent_action.tryGetMyHome(request.MyhomeGuid, out var myhome)) { err_msg = $"Failed to tryGetMyHome() !!! : myhomeGuid:{request.MyhomeGuid} - {player.toBasicString()}"; result.setFail(ServerErrorCode.MyHomeNotFound, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } var rental_agent_action = player.getEntityAction(); NullReferenceCheckHelper.throwIfNull(rental_agent_action, () => $"rental_agent_action is null !!! - {player.toBasicString()}"); if (rental_agent_action.isRentalMyhome(request.MyhomeGuid)) { err_msg = $"Myhome is On Rental !!! : MyhomeGuid:{request.MyhomeGuid} - {player.toBasicString()}"; result.setFail(ServerErrorCode.MyhomeIsOnRental, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } var myhome_action = myhome.getEntityAction(); NullReferenceCheckHelper.throwIfNull(myhome_action, () => $"myhome_action is null !!! - {player.toBasicString()}"); var myhome_ugc_info = myhome_action.getMyhomeUgcInfo(); var myhome_ugc_npc_infos = myhome_ugc_info.getUgcNpcAnchorInfos(); var myhome_ugc_npc_guids = new List(); var modify_floor_linked_infos = new List(); foreach (var myhome_ugc_npc_info in myhome_ugc_npc_infos.Values) { myhome_ugc_npc_guids.Add(myhome_ugc_npc_info.EntityGuid); } if (myhome_ugc_npc_guids.Count > 0) { var modify_floor_linked_info = MapHelper.makeModifyFloorLinkedInfo(ModifyType.Add, request.LandId, request.BuildingId, request.Floor, myhome_ugc_npc_guids); modify_floor_linked_infos.Add(modify_floor_linked_info); } result = MyhomeHelper.getMyhomeInstanceId(myhome_ugc_info.RoomType, out var instance_meta_id); if (result.isFail()) { err_msg = $"Fail to getMyhomeInstanceId() !!! : {result.toBasicString()} - {player.toBasicString()}"; Log.getLogger().error(err_msg); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } if (!MetaData.Instance._RentalfeeTable.TryGetValue((land_meta_data.Editor, land_meta_data.LandSize), out var rentalfee_meta_data)) { err_msg = $"Failed to MetaData.TryGetValue() !!! : Editor:{land_meta_data.Editor}, Size:{land_meta_data.LandSize} - {player.toBasicString()}"; result.setFail(ServerErrorCode.RentalfeeMetaDataNotFound, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } var charge_type = (CurrencyType)rentalfee_meta_data.CurrencyType; var total_charge = 0.0d; var rental_profits = new Dictionary(); ReceivedMailDoc? reward_mail_doc = null; var is_burn_calium = false; var user_guid = player.getUserGuid(); var is_building_owner = building_action.isBuildingOwner(user_guid); if (!is_building_owner) { // 비용 var daily_charge = 0.0d; if (is_load_from_db) { (charge_type, daily_charge) = building_action.getRentalCurrency(); } else { daily_charge = rentalfee_meta_data.CurrencyValue; } if (charge_type == CurrencyType.Calium && land_meta_data.Editor == EditorType.CALIVERSE) { is_burn_calium = true; } var is_equal_amount = CaliumStorageHelper.compareDoubleByLong(request.RentalCurrencyAmount, daily_charge); if (request.RentalCurrencyType != charge_type || !is_equal_amount) { err_msg = $"Rental Contract Info Update !!!"; result.setFail(ServerErrorCode.RentalContractInfoUpdate, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } total_charge = CaliumStorageHelper.MultiplyDoubleByLong(request.RentalPeriod, daily_charge); rental_profits[charge_type] = total_charge; // 보상 if (!MetaData.Instance.SystemMailMetaData.TryGetValue(ServerCommon.Constant.RENTAL_REWARD_SYSTEM_MAIL_META_KEY, out var system_mail_meta_data)) { err_msg = $"Failed to MetaData.TryGetValue() !!! : SystemMailKey:{ServerCommon.Constant.RENTAL_REWARD_SYSTEM_MAIL_META_KEY} - {player.toBasicString()}"; result.setFail(ServerErrorCode.SystemMailMetaDataNotFound, err_msg); Log.getLogger().error(result.toBasicString()); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } var nickname = player.getUserNickname(); var mail_items = new List() { new ServerCommon.MailItem() { ItemId = (uint)rentalfee_meta_data.RentalReward, Count = rentalfee_meta_data.RentalQuantity * request.RentalPeriod } }; (result, reward_mail_doc) = await Mail.createSystemMailWithMeta(user_guid, nickname, system_mail_meta_data, mail_items, MetaHelper.GameConfigMeta.SystemMailStoragePeriod); if (result.isFail()) { err_msg = $"Failed to createSystemMailWithMeta() !!! : {result.toBasicString()}"; Log.getLogger().error(err_msg); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } NullReferenceCheckHelper.throwIfNull(reward_mail_doc, () => $"reward_mail_doc is null !!!"); } // 랜탈 레디스 등록 var address = RentalOccupyCacheRequest.makeShareKey(request.LandId, request.BuildingId, request.Floor); var rental_occupy_cache_request = new RentalOccupyCacheRequest(address, server_logic.getRedisConnector()); result = await rental_occupy_cache_request.tryOccupyRental(user_guid); if (result.isFail()) { err_msg = $"Failed to tryOccupyRental() !!! : {result.toBasicString()}"; Log.getLogger().error(err_msg); result.setFail(ServerErrorCode.RentalAddressIsNotEmpty, result.ResultString); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } var building_profit_agent_action = building.getEntityAction(); NullReferenceCheckHelper.throwIfNull(building_profit_agent_action, () => $"building_profit_agent_action is null !!! - {player.toBasicString()}"); using (building_profit_agent_action.getAsyncLock().Lock()) { BuildingProfitBusinessLog? building_profit_business_log = null; DynamoDbDocBase? new_building_profit_doc = null; DynamoDbItemRequestQueryContext? building_profit_update_item_query_context = null; if (!building_profit_agent_action.tryGetBuildingProfit(request.Floor, out var building_profit)) { (result, building_profit, new_building_profit_doc) = await BuildingProfitHelper.tryMakeBuildingProfit(building, request.BuildingId, request.Floor, charge_type, total_charge); if (result.isFail()) { err_msg = $"Failed to tryMakeBuildingProfit() !!! : {result.toBasicString()}"; Log.getLogger().error(err_msg); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } NullReferenceCheckHelper.throwIfNull(building_profit, () => $"building_profit is null !!!"); NullReferenceCheckHelper.throwIfNull(new_building_profit_doc, () => $"new_building_profit_doc is null !!!"); var building_profit_log_info = BuildingProfitBusinessLogHelper.toBuildingProfitLogInfo(building_map_tree.BuildingMetaId, request.Floor, charge_type, AmountDeltaType.Acquire, total_charge, total_charge); building_profit_business_log = new BuildingProfitBusinessLog(building_profit_log_info); } else { // UpdateItemRequest DB (result, building_profit_update_item_query_context) = BuildingProfitHelper.tryMakeUpdateItemRequestFromBuildingProfit(building_map_tree.BuildingMetaId, request.Floor, charge_type, total_charge); if (result.isFail()) { err_msg = $"Failed to tryMakeUpdateItemRequestFromBuildingProfit() !!! : {result.toBasicString()}"; Log.getLogger().error(err_msg); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } NullReferenceCheckHelper.throwIfNull(building_profit_update_item_query_context, () => $"building_profit_update_item_query_context is null !!! - {player.toBasicString()}"); var profit_action = building_profit.getEntityAction(); NullReferenceCheckHelper.throwIfNull(profit_action, () => $"profit_action is null !!! - {player.toBasicString()}"); var current_profit = profit_action.getProfit(charge_type); var building_profit_log_info = BuildingProfitBusinessLogHelper.toBuildingProfitLogInfo(building_map_tree.BuildingMetaId, request.Floor, charge_type, AmountDeltaType.Acquire, total_charge, current_profit + total_charge); building_profit_business_log = new BuildingProfitBusinessLog(building_profit_log_info); } var building_profit_action = building_profit.getEntityAction(); NullReferenceCheckHelper.throwIfNull(building_profit_action, () => $"building_profit_action is null !!!"); var building_profit_history_agent_action = building.getEntityAction(); NullReferenceCheckHelper.throwIfNull(building_profit_history_agent_action, () => $"building_profit_history_agent_action is null !!!"); var building_rental_history_agent_action = building.getEntityAction(); NullReferenceCheckHelper.throwIfNull(building_rental_history_agent_action, () => $"building_rental_history_agent_action is null !!!"); var fn_transaction_runner = async delegate () { var result = new Result(); // Money var money_action = player.getEntityAction(); NullReferenceCheckHelper.throwIfNull(money_action, () => $"money_action is null !!! - {player.toBasicString()}"); result = await money_action.changeMoney(charge_type, -total_charge, useCaliumEvent:is_burn_calium); if (result.isFail()) { err_msg = $"Failed to changeMoney() !!! : {result.toBasicString()}"; Log.getLogger().error(err_msg); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } // Rental var rent_floor_request_info = request.toRentFloorRequestInfo(player.getUserGuid()); var rental = await RentalHelper.makeRental(player, rent_floor_request_info); var room_map_tree = MapHelper.makeRoomMapTree(building_map_tree, instance_meta_id, rent_floor_request_info, myhome_ugc_npc_guids); // Building Floor (result, var building_floor, var building_floor_doc) = await BuildingFloorHelper.tryMakeBuildingFloor(building, rent_floor_request_info); if (result.isFail()) { err_msg = $"Failed to tryMakeBuildingFloor() !!! : {result.toBasicString()}"; Log.getLogger().error(err_msg); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } NullReferenceCheckHelper.throwIfNull(building_floor, () => $"building_floor is null !!!"); NullReferenceCheckHelper.throwIfNull(building_floor_doc, () => $"building_floor_doc is null !!!"); // Building Profit History (result, var building_profit_history, var building_profit_history_doc) = await BuildingProfitHistoryHelper.tryMakeBuildingProfitHistory(building, request.BuildingId, request.Floor, DateTime.UtcNow, ProfitHistoryType.Stack, rental_profits); if (result.isFail()) { err_msg = $"Failed to tryMakeBuildingProfitHistory() !!! : {result.toBasicString()}"; Log.getLogger().error(err_msg); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } NullReferenceCheckHelper.throwIfNull(building_profit_history, () => $"building_profit_history is null !!!"); NullReferenceCheckHelper.throwIfNull(building_profit_history_doc, () => $"building_profit_history_doc is null !!!"); // Building Rental History (result, var building_rental_history, var building_rental_history_doc) = await BuildingRentalHistoryHelper.tryMakeBuildingRentalHistory(building, request.BuildingId, request.Floor, user_guid, DateTime.UtcNow, request.RentalPeriod); if (result.isFail()) { err_msg = $"Failed to tryMakeBuildingRentalHistory() !!! : {result.toBasicString()}"; Log.getLogger().error(err_msg); send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } NullReferenceCheckHelper.throwIfNull(building_rental_history, () => $"building_rental_history is null !!!"); NullReferenceCheckHelper.throwIfNull(building_rental_history_doc, () => $"building_rental_history_doc is null !!!"); var rental_log_info = RentalBusinessLogHelper.toRentalLogInfo(rent_floor_request_info); var rental_business_log = new RentalBusinessLog(rental_log_info); var batch = new QueryBatchEx(player, LogActionType.RentFloor, server_logic.getDynamoDbClient()); { batch.addQuery(new DBQWriteToAttributeAllWithTransactionRunner()); if (new_building_profit_doc != null) { batch.addQuery(new DBQEntityWrite(new_building_profit_doc)); } if (building_profit_update_item_query_context != null) { batch.addQuery(new DBQWithItemRequestQueryContext(building_profit_update_item_query_context)); } batch.addQuery(new DBQEntityWrite(building_floor_doc)); batch.addQuery(new DBQEntityWrite(building_profit_history_doc)); batch.addQuery(new DBQEntityWrite(building_rental_history_doc)); if (reward_mail_doc != null) { batch.addQuery(new DBQEntityWrite(reward_mail_doc)); } batch.addQuery(new QueryFinal()); } batch.appendBusinessLog(rental_business_log); batch.appendBusinessLog(building_profit_business_log); result = await QueryHelper.sendQueryAndBusinessLog(batch); if (result.isFail()) { send_S2C_ACK_RENT_FLOOR(player, result, res); return result; } var found_transaction_runner = player.findTransactionRunner(TransactionIdType.PrivateContents); NullReferenceCheckHelper.throwIfNull(found_transaction_runner, () => $"found_transaction_runner is null !!! - {player.toBasicString()}"); res.CommonResult = found_transaction_runner.getCommonResult(); // Memory if (new_building_profit_doc != null) { building_profit_agent_action.addBuildingProfit(request.Floor, building_profit); } else { building_profit_action.modifyProfit(charge_type, total_charge); } rental_agent_action.addRental(rent_floor_request_info.LandId, rent_floor_request_info.BuildingId, rent_floor_request_info.Floor, rental); MapManager.Instance.addRoomMaptree(building_map_tree, rent_floor_request_info.Floor, room_map_tree); building_floor_agent_action.addBuildingFloor(rent_floor_request_info.Floor, building_floor); building_profit_history_agent_action.addBuildingProfitHistory(building_profit_history); building_rental_history_agent_action.addBuildingRentalHistory(building_rental_history); var mail_action = player.getEntityAction(); NullReferenceCheckHelper.throwIfNull(mail_action, () => $"mail_action is null !!! - {player.toBasicString()}"); mail_action.NewReceivedMail(); // Packet send_S2C_ACK_RENT_FLOOR(player, result, res); player.send_S2C_NTF_MODIFY_OWNED_RENTAL_INFOS(ModifyType.Add, rental); RentalNotifyHelper.send_GS2GS_NTF_RENT_FLOOR(rent_floor_request_info, instance_meta_id); MapNotifyHelper.sendNtfModifyFloorLinkedInfos(modify_floor_linked_infos); await BuildingNotifyHelper.sendNtfModifyBuildingInfo(building); BuildingProfitNotifyHelper.send_GS2GS_NTF_MODIFY_BUILDING_PROFIT(rent_floor_request_info.BuildingId, rent_floor_request_info.Floor, charge_type, total_charge); BuildingProfitHistoryNotifyHelper.send_GS2GS_NTF_ADD_BUILDING_PROFIT_HISTORY(building_profit_history); BuildingRentalHistoryNotifyHelper.send_GS2GS_NTF_ADD_BUILDING_RENTAL_HISTORY(building_rental_history); return result; }; result = await player.runTransactionRunnerSafely(TransactionIdType.PrivateContents, "RentFloor", fn_transaction_runner); if (result.isFail()) { err_msg = $"Failed to runTransactionRunnerSafely() !!! : {result.toBasicString()} - {player.toBasicString()}"; Log.getLogger().error(err_msg); } }; return result; } }