61 lines
1.7 KiB
C#
61 lines
1.7 KiB
C#
namespace ET.Server
|
|
{
|
|
[FriendOf(typeof(BagComponent))]
|
|
[FriendOf(typeof(ItemUnit))]
|
|
public static class BagService
|
|
{
|
|
private static bool IsEquipItem(int itemId)
|
|
{
|
|
return itemId >= 10000;
|
|
}
|
|
|
|
public static bool TryUseItem(Player player, long itemIndex, int count, out string reason, out ItemUnit item)
|
|
{
|
|
reason = string.Empty;
|
|
item = null;
|
|
|
|
BagComponent bag = player?.GetComponent<BagComponent>();
|
|
if (bag == null || !bag.ItemMap.TryGetValue(itemIndex, out EntityRef<ItemUnit> unitRef))
|
|
{
|
|
reason = "item_not_found";
|
|
return false;
|
|
}
|
|
|
|
ItemUnit unit = unitRef;
|
|
item = unit;
|
|
if (count <= 0 || unit.Count < count)
|
|
{
|
|
reason = "count_invalid";
|
|
return false;
|
|
}
|
|
|
|
if (unit.HeroId > 0)
|
|
{
|
|
reason = "equip_in_use";
|
|
return false;
|
|
}
|
|
|
|
if (IsEquipItem(unit.ItemId))
|
|
{
|
|
reason = "equip_can_not_use";
|
|
return false;
|
|
}
|
|
|
|
unit.Count -= count;
|
|
if (unit.Count <= 0)
|
|
{
|
|
bag.RemoveItemByIndex(unit.ItemIndex);
|
|
item = null;
|
|
}
|
|
else
|
|
{
|
|
BagNoticeHelper.NotifyItemChange(player, unit);
|
|
}
|
|
|
|
player.SetDirty();
|
|
LogExtensions.Audit("Bag", "UseItem", $"{{\"playerId\":{player.Id},\"itemIndex\":{itemIndex},\"itemId\":{unit.ItemId},\"count\":{count}}}");
|
|
return true;
|
|
}
|
|
}
|
|
}
|