Files
2025-05-01 07:23:28 +09:00

610 lines
25 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Grpc.Core;
using ServerCore; using ServerBase;
using ServerCommon;
using ServerCommon.BusinessLogDomain;
using MetaAssets;
using SESSION_ID = System.Int32;
using WORLD_META_ID = System.UInt32;
using META_ID = System.UInt32;
using ENTITY_GUID = System.String;
using ACCOUNT_ID = System.String;
using OWNER_GUID = System.String;
using USER_GUID = System.String;
using CHARACTER_GUID = System.String;
using ITEM_GUID = System.String;
namespace GameServer
{
public class ItemClothAction : EntityActionBase
{
public ItemClothAction(EntityBase owner)
: base(owner)
{
}
public override async Task<Result> onInit()
{
await Task.CompletedTask;
var result = new Result();
return result;
}
public override void onClear()
{
return;
}
public async Task<Result> tryApplyClothsByMetaId(ClothInfo? clothInfo)
{
var result = new Result();
var err_msg = string.Empty;
var owner = getOwner();
NullReferenceCheckHelper.throwIfNull(owner, () => toBasicString());
if (null == clothInfo)
{
err_msg = $"ClothInfo is null !!! - {owner.toBasicString()}";
Log.getLogger().info(err_msg);
return result;
}
ITEM_GUID equiped_item_guid = string.Empty;
if (clothInfo.ClothAvatar > 0)
{
(result, equiped_item_guid) = await fillupItemAfterEquipedClothByMetaId(clothInfo.ClothAvatar);
if (result.isFail())
{
return result;
}
}
if (clothInfo.ClothHeadwear > 0)
{
(result, equiped_item_guid) = await fillupItemAfterEquipedClothByMetaId(clothInfo.ClothHeadwear);
if (result.isFail())
{
return result;
}
}
if (clothInfo.ClothMask > 0)
{
(result, equiped_item_guid) = await fillupItemAfterEquipedClothByMetaId(clothInfo.ClothMask);
if (result.isFail())
{
return result;
}
}
if (clothInfo.ClothBag > 0)
{
(result, equiped_item_guid) = await fillupItemAfterEquipedClothByMetaId(clothInfo.ClothBag);
if (result.isFail())
{
return result;
}
}
if (clothInfo.ClothShoes > 0)
{
(result, equiped_item_guid) = await fillupItemAfterEquipedClothByMetaId(clothInfo.ClothShoes);
if (result.isFail())
{
return result;
}
}
if (clothInfo.ClothOuter > 0)
{
(result, equiped_item_guid) = await fillupItemAfterEquipedClothByMetaId(clothInfo.ClothOuter);
if (result.isFail())
{
return result;
}
}
if (clothInfo.ClothTops > 0)
{
(result, equiped_item_guid) = await fillupItemAfterEquipedClothByMetaId(clothInfo.ClothTops);
if (result.isFail())
{
return result;
}
}
if (clothInfo.ClothBottoms > 0)
{
(result, equiped_item_guid) = await fillupItemAfterEquipedClothByMetaId(clothInfo.ClothBottoms);
if (result.isFail())
{
return result;
}
}
if (clothInfo.ClothGloves > 0)
{
(result, equiped_item_guid) = await fillupItemAfterEquipedClothByMetaId(clothInfo.ClothGloves);
if (result.isFail())
{
return result;
}
}
if (clothInfo.ClothEarrings > 0)
{
(result, equiped_item_guid) = await fillupItemAfterEquipedClothByMetaId(clothInfo.ClothEarrings);
if (result.isFail())
{
return result;
}
}
if (clothInfo.ClothNeckless > 0)
{
(result, equiped_item_guid) = await fillupItemAfterEquipedClothByMetaId(clothInfo.ClothNeckless);
if (result.isFail())
{
return result;
}
}
if (clothInfo.ClothSocks > 0)
{
(result, equiped_item_guid) = await fillupItemAfterEquipedClothByMetaId(clothInfo.ClothSocks);
if (result.isFail())
{
return result;
}
}
return result;
}
private async Task<(Result result, ITEM_GUID equipedItemGuid)> fillupItemAfterEquipedClothByMetaId(META_ID metaId)
{
var result = new Result();
var owner = getOwner();
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
var inventory_action = owner.getEntityAction<InventoryActionBase>();
NullReferenceCheckHelper.throwIfNull(inventory_action, () => toBasicString());
(result, var take_in_items) = await inventory_action.tryTakableToBagWithEquip(metaId, 1);
if (result.isFail())
{
return (result, string.Empty);
}
NullReferenceCheckHelper.throwIfNull(take_in_items, () => toBasicString());
var item_attribute = take_in_items[0].getEntityAttribute<ItemAttributeBase>();
NullReferenceCheckHelper.throwIfNull(item_attribute, () => toBasicString());
return (result, item_attribute.ItemGuid);
}
public async Task tryApplyClothsByClothInfo(ClothInfo clothInfo)
{
var result = new Result();
var err_msg = string.Empty;
var owner = getOwner();
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
NullReferenceCheckHelper.throwIfNull(clothInfo, () => $"clothInfo is null !!! - {owner.toBasicString()}");
if (clothInfo.ClothAvatarItemGuid.isNullOrWhiteSpace())
{
(result, _) = await tryUnequipClothByClothType(ClothSlotType.Avatar);
if (result.isFail()) { Log.getLogger().error(result.toBasicString()); }
}
else
{
(result, _) = await tryEquipClothByItemGuid(clothInfo.ClothAvatarItemGuid);
if (result.isFail() && result.getErrorCode() != ServerErrorCode.SlotsAlreadyEquiped) { Log.getLogger().error(result.toBasicString()); }
}
if (clothInfo.ClothHeadwearItemGuid.isNullOrWhiteSpace())
{
(result, _) = await tryUnequipClothByClothType(ClothSlotType.Headwear);
if (result.isFail()) { Log.getLogger().error(result.toBasicString()); }
}
else
{
(result, _) = await tryEquipClothByItemGuid(clothInfo.ClothHeadwearItemGuid);
if (result.isFail() && result.getErrorCode() != ServerErrorCode.SlotsAlreadyEquiped) { Log.getLogger().error(result.toBasicString()); }
}
if (clothInfo.ClothMaskItemGuid.isNullOrWhiteSpace())
{
(result, _) = await tryUnequipClothByClothType(ClothSlotType.Mask);
if (result.isFail()) { Log.getLogger().error(result.toBasicString()); }
}
else
{
(result, _) = await tryEquipClothByItemGuid(clothInfo.ClothMaskItemGuid);
if (result.isFail() && result.getErrorCode() != ServerErrorCode.SlotsAlreadyEquiped) { Log.getLogger().error(result.toBasicString()); }
}
if (clothInfo.ClothBagItemGuid.isNullOrWhiteSpace())
{
(result, _) = await tryUnequipClothByClothType(ClothSlotType.Bag);
if (result.isFail()) { Log.getLogger().error(result.toBasicString()); }
}
else
{
(result, _) = await tryEquipClothByItemGuid(clothInfo.ClothBagItemGuid);
if (result.isFail() && result.getErrorCode() != ServerErrorCode.SlotsAlreadyEquiped) { Log.getLogger().error(result.toBasicString()); }
}
if (clothInfo.ClothShoesItemGuid.isNullOrWhiteSpace())
{
(result, _) = await tryUnequipClothByClothType(ClothSlotType.Shoes);
if (result.isFail()) { Log.getLogger().error(result.toBasicString()); }
}
else
{
(result, _) = await tryEquipClothByItemGuid(clothInfo.ClothShoesItemGuid);
if (result.isFail() && result.getErrorCode() != ServerErrorCode.SlotsAlreadyEquiped) { Log.getLogger().error(result.toBasicString()); }
}
if (clothInfo.ClothOuterItemGuid.isNullOrWhiteSpace())
{
(result, _) = await tryUnequipClothByClothType(ClothSlotType.Outer);
if (result.isFail()) { Log.getLogger().error(result.toBasicString()); }
}
else
{
(result, _) = await tryEquipClothByItemGuid(clothInfo.ClothOuterItemGuid);
if (result.isFail() && result.getErrorCode() != ServerErrorCode.SlotsAlreadyEquiped) { Log.getLogger().error(result.toBasicString()); }
}
if (clothInfo.ClothTopsItemGuid.isNullOrWhiteSpace())
{
(result, _) = await tryUnequipClothByClothType(ClothSlotType.Tops);
if (result.isFail()) { Log.getLogger().error(result.toBasicString()); }
}
else
{
(result, _) = await tryEquipClothByItemGuid(clothInfo.ClothTopsItemGuid);
if (result.isFail() && result.getErrorCode() != ServerErrorCode.SlotsAlreadyEquiped) { Log.getLogger().error(result.toBasicString()); }
}
if (clothInfo.ClothBottomsItemGuid.isNullOrWhiteSpace())
{
(result, _) = await tryUnequipClothByClothType(ClothSlotType.Bottoms);
if (result.isFail()) { Log.getLogger().error(result.toBasicString()); }
}
else
{
(result, _) = await tryEquipClothByItemGuid(clothInfo.ClothBottomsItemGuid);
if (result.isFail() && result.getErrorCode() != ServerErrorCode.SlotsAlreadyEquiped) { Log.getLogger().error(result.toBasicString()); }
}
if (clothInfo.ClothGlovesItemGuid.isNullOrWhiteSpace())
{
(result, _) = await tryUnequipClothByClothType(ClothSlotType.Gloves);
if (result.isFail()) { Log.getLogger().error(result.toBasicString()); }
}
else
{
(result, _) = await tryEquipClothByItemGuid(clothInfo.ClothGlovesItemGuid);
if (result.isFail() && result.getErrorCode() != ServerErrorCode.SlotsAlreadyEquiped) { Log.getLogger().error(result.toBasicString()); }
}
if (clothInfo.ClothEarringsItemGuid.isNullOrWhiteSpace())
{
(result, _) = await tryUnequipClothByClothType(ClothSlotType.Earrings);
if (result.isFail()) { Log.getLogger().error(result.toBasicString()); }
}
else
{
(result, _) = await tryEquipClothByItemGuid(clothInfo.ClothEarringsItemGuid);
if (result.isFail() && result.getErrorCode() != ServerErrorCode.SlotsAlreadyEquiped) { Log.getLogger().error(result.toBasicString()); }
}
if (clothInfo.ClothNecklessItemGuid.isNullOrWhiteSpace())
{
(result, _) = await tryUnequipClothByClothType(ClothSlotType.Neckless);
if (result.isFail()) { Log.getLogger().error(result.toBasicString()); }
}
else
{
(result, _) = await tryEquipClothByItemGuid(clothInfo.ClothNecklessItemGuid);
if (result.isFail() && result.getErrorCode() != ServerErrorCode.SlotsAlreadyEquiped) { Log.getLogger().error(result.toBasicString()); }
}
if (clothInfo.ClothSocksItemGuid.isNullOrWhiteSpace())
{
(result, _) = await tryUnequipClothByClothType(ClothSlotType.Socks);
if (result.isFail()) { Log.getLogger().error(result.toBasicString()); }
}
else
{
(result, _) = await tryEquipClothByItemGuid(clothInfo.ClothSocksItemGuid);
if (result.isFail() && result.getErrorCode() != ServerErrorCode.SlotsAlreadyEquiped) { Log.getLogger().error(result.toBasicString()); }
}
}
private async Task<(Result, Item?)> tryEquipClothByItemGuid(ITEM_GUID itemGuid)
{
var result = new Result();
var err_msg = string.Empty;
var owner = getOwner();
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
var inventory_action = owner.getEntityAction<InventoryActionBase>();
NullReferenceCheckHelper.throwIfNull(inventory_action, () => $"inventory_action is null !!! - {owner.toBasicString()}");
var found_item = inventory_action.tryGetItemByItemGuid(itemGuid);
if (null == found_item)
{
err_msg = $"Not found Item in Bag !!! : itemGuid:{itemGuid} - {owner.toBasicString()}";
result.setFail(ServerErrorCode.BagItemNotFound, err_msg);
Log.getLogger().error(result.toBasicString());
return (result, null);
}
var item_meta = found_item.getItemMeta();
NullReferenceCheckHelper.throwIfNull(item_meta, () => $"item_meta is null !!! - {owner.toBasicString()}");
if (false == item_meta.TypeLarge.isClothType())
{
err_msg = $"Not EItemLargeType of Cloth !!! : EItemLargeType.Cloth == {item_meta.TypeLarge}, ItemMetaId:{item_meta.ItemId} - {owner.toBasicString()}";
result.setFail(ServerErrorCode.ItemClothInvalidLargeType, err_msg);
Log.getLogger().error(result.toBasicString());
return (result, null);
}
if (false == item_meta.TypeSmall.isClothType())
{
err_msg = $"Not EItemSmallType of Cloth !!! : {item_meta.TypeSmall}, ItemMetaId:{item_meta.ItemId} - {owner.toBasicString()}";
result.setFail(ServerErrorCode.ItemClothInvalidSmallType, err_msg);
Log.getLogger().error(result.toBasicString());
return (result, null);
}
var item_attribute = found_item.getOriginEntityAttribute<ItemAttributeBase>();
NullReferenceCheckHelper.throwIfNull(item_attribute, () => $"item_attribute is null !!! - {owner.toBasicString()}");
var equiped_inven_type = item_attribute.EquipedInvenType;
var equiped_pos = item_attribute.EquipedPos;
if ( InvenEquipType.None != equiped_inven_type )
{
err_msg = $"Already equiped Cloth Item !!! : InvenEquipType.None == equipedInvenType{equiped_inven_type}, equipedPos:{equiped_pos}"
+ $" - ItemMetaId:{item_meta.ItemId}, ItemGuid:{item_attribute.ItemGuid}, {owner.toBasicString()}";
result.setFail(ServerErrorCode.SlotsAlreadyEquiped, err_msg);
Log.getLogger().warn(result.toBasicString());
return (result, found_item);
}
(result, _) = await tryUnequipClothByItemMeta(item_meta, false);
if(result.isFail())
{
return (result, null);
}
result = await inventory_action.tryTakableToEquip(found_item);
if (result.isFail())
{
return (result, null);
}
return (result, null);
}
private async Task<(Result, Item?)> tryUnequipClothByItemMeta(ItemMetaData itemMeta, bool isFailOnItemNotFound = true)
{
var owner = getOwner();
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
ArgumentNullReferenceCheckHelper.throwIfNull(itemMeta, () => $"itemMeta is null !!! - {owner.toBasicString()}");
var result = new Result();
var err_msg = string.Empty;
var inventory_action = owner.getEntityAction<InventoryActionBase>();
NullReferenceCheckHelper.throwIfNull(inventory_action, () => $"inventory_action is null !!! - {owner.toBasicString()}");
var inven_rule = GameServerApp.getServerLogic().findRule<InventoryRule>();
if (null == inven_rule)
{
err_msg = $"Not found InventoryRule !!! - {owner.toBasicString()}";
result.setFail(ServerErrorCode.InventoryRuleNotFound, err_msg);
Log.getLogger().error(result.toBasicString());
return (result, null);
}
inventory_action.getEquipInvens().TryGetValue(InvenEquipType.Cloth, out var found_equip_inven);
NullReferenceCheckHelper.throwIfNull(found_equip_inven, () => $"found_equip_inven is null !!! - {owner.toBasicString()}");
var cloth_inven = found_equip_inven as ClothEquipInven;
NullReferenceCheckHelper.throwIfNull(inventory_action, () => $"cloth_inven is null !!! - {owner.toBasicString()}");
var equip_rule_bases = inven_rule.getEquipRuleBasesByItemLargeType(itemMeta.TypeLarge);
if (null == equip_rule_bases)
{
err_msg = $"Not found EquipRuleBase !!! - {owner.toBasicString()}";
Log.getLogger().warn(err_msg);
return (result, null);
}
else
{
foreach (var equip_rule in equip_rule_bases)
{
var cloth_equip_rule = equip_rule as ClothEquipRule;
NullReferenceCheckHelper.throwIfNull(cloth_equip_rule, () => $"cloth_equip_rule is null !!! - {owner.toBasicString()}");
if (false == cloth_equip_rule.m_slots_by_item_type.TryGetValue(itemMeta.TypeSmall, out var found_cloth_slot_types))
{
err_msg = $"Not found ClothSlotType !!! : itemSmallType:{itemMeta.TypeSmall} - {owner.toBasicString()}";
result.setFail(ServerErrorCode.ClothEquipRuleClothSlotTypeNotFound, err_msg);
Log.getLogger().error(result.toBasicString());
return (result, null);
}
NullReferenceCheckHelper.throwIfNull(found_cloth_slot_types, () => $"found_cloth_slot_types is null !!! - {owner.toBasicString()}");
foreach (var cloth_slot_type in found_cloth_slot_types)
{
(var unequip_result, var unequiped_item) = await tryUnequipClothByClothType(cloth_slot_type, isFailOnItemNotFound);
if(unequip_result.isFail())
{
continue;
}
return (unequip_result, unequiped_item);
}
}
}
return (result, null);
}
private async Task<(Result, Item?)> tryUnequipClothByClothType( ClothSlotType clothSlotType
, bool isFailOnItemNotFound = true )
{
var result = new Result();
var err_msg = string.Empty;
var owner = getOwner();
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
var inventory_action = owner.getEntityAction<InventoryActionBase>();
NullReferenceCheckHelper.throwIfNull(inventory_action, () => $"inventory_action is null !!! - {owner.toBasicString()}");
(var get_item_result, var found_item) = await inventory_action.tryGetEquipItemByInvenEquipType<ClothSlotType>( InvenEquipType.Cloth, clothSlotType
, isFailOnItemNotFound );
if (null == found_item)
{
if (get_item_result.isFail())
{
return (get_item_result, null);
}
return (result, null);
}
(result, var updated_item) = await inventory_action.tryTakableOutFromEquip(found_item);
if (result.isFail())
{
return (result, null);
}
return (result, null);
}
public Result fillupCloth(ClothSlotType clothSlotType, Item item, ref ClothInfo clothInfo)
{
var owner = getOwner();
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
ArgumentNullReferenceCheckHelper.throwIfNull(item, () => $"item is null !!! - {owner.toBasicString()}");
ArgumentNullReferenceCheckHelper.throwIfNull(clothInfo, () => $"item is null !!! - {owner.toBasicString()}");
var result = new Result();
var err_msg = string.Empty;
var item_meta = item.getItemMeta();
NullReferenceCheckHelper.throwIfNull(item_meta, () => $"item_meta is null !!! - {owner.toBasicString()}");
var item_attribute = item.getOriginEntityAttribute<ItemAttributeBase>();
NullReferenceCheckHelper.throwIfNull(item_attribute, () => $"item_attribute is null !!! - {owner.toBasicString()}");
switch (clothSlotType)
{
case ClothSlotType.Avatar:
clothInfo.ClothAvatar = (UInt32)item_meta.ItemId;
clothInfo.ClothAvatarItemGuid = item_attribute.ItemGuid;
break;
case ClothSlotType.Headwear:
clothInfo.ClothHeadwear = (UInt32)item_meta.ItemId;
clothInfo.ClothHeadwearItemGuid = item_attribute.ItemGuid;
break;
case ClothSlotType.Mask:
clothInfo.ClothMask = (UInt32)item_meta.ItemId;
clothInfo.ClothMaskItemGuid = item_attribute.ItemGuid;
break;
case ClothSlotType.Bag:
clothInfo.ClothBag = (UInt32)item_meta.ItemId;
clothInfo.ClothBagItemGuid = item_attribute.ItemGuid;
break;
case ClothSlotType.Shoes:
clothInfo.ClothShoes = (UInt32)item_meta.ItemId;
clothInfo.ClothShoesItemGuid = item_attribute.ItemGuid;
break;
case ClothSlotType.Outer:
clothInfo.ClothOuter = (UInt32)item_meta.ItemId;
clothInfo.ClothOuterItemGuid = item_attribute.ItemGuid;
break;
case ClothSlotType.Tops:
clothInfo.ClothTops = (UInt32)item_meta.ItemId;
clothInfo.ClothTopsItemGuid = item_attribute.ItemGuid;
break;
case ClothSlotType.Bottoms:
clothInfo.ClothBottoms = (UInt32)item_meta.ItemId;
clothInfo.ClothBottomsItemGuid = item_attribute.ItemGuid;
break;
case ClothSlotType.Gloves:
clothInfo.ClothGloves = (UInt32)item_meta.ItemId;
clothInfo.ClothGlovesItemGuid = item_attribute.ItemGuid;
break;
case ClothSlotType.Earrings:
clothInfo.ClothEarrings = (UInt32)item_meta.ItemId;
clothInfo.ClothEarringsItemGuid = item_attribute.ItemGuid;
break;
case ClothSlotType.Neckless:
clothInfo.ClothNeckless = (UInt32)item_meta.ItemId;
clothInfo.ClothNecklessItemGuid = item_attribute.ItemGuid;
break;
case ClothSlotType.Socks:
clothInfo.ClothSocks = (UInt32)item_meta.ItemId;
clothInfo.ClothSocksItemGuid = item_attribute.ItemGuid;
break;
default:
err_msg = $"Invalid ClothSlotType !!! : {clothSlotType} - {owner.toBasicString()}";
result.setFail(ServerErrorCode.ClothSlotTypeInvalid, err_msg);
Log.getLogger().warn(err_msg);
return result;
}
return result;
}
}
}