Files
caliverse_server/ServerCommon/Entity/Inventory/SlotBase.cs
2025-05-01 07:20:41 +09:00

208 lines
6.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Axion.Collections.Concurrent;
using ServerCore; using ServerBase;
using SESSION_ID = System.Int32;
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 ServerCommon
{
public abstract class SlotsBase
{
private AtomicInt32 m_slot_max_count = new(0);
public virtual bool onInitSlots(int slotMaxCount)
{
m_slot_max_count.set(slotMaxCount);
return true;
}
public abstract UInt32 onGetCurrSlotCount();
public UInt32 getSlotMaxCount()
{
return (UInt32)m_slot_max_count.Value;
}
public virtual string toBasicString()
{
return $"{this.getTypeName()}, UsingSlotCount:{onGetCurrSlotCount()}, SlotMaxCount:{m_slot_max_count.Value}";
}
}
public class Slots<TSlotType> : SlotsBase, ISlotsWithType<TSlotType>
where TSlotType : Enum
{
private ConcurrentHashSet<TSlotType> m_slots = new();
public Slots()
{ }
public override bool onInitSlots(int slotMaxCount)
{
m_slots = new ConcurrentHashSet<TSlotType>(MultiThreadHelper.getThreadOptimalCount(), (int)slotMaxCount);
return true;
}
public virtual ServerErrorCode onIsEquipable(TSlotType targetSlot)
{
return (false == m_slots.Contains(targetSlot)) ? ServerErrorCode.Success : ServerErrorCode.SlotsAlreadyEquiped;
}
public virtual ServerErrorCode onTryEquip(TSlotType targetSlot)
{
return (true == m_slots.TryAdd(targetSlot, out _)) ? ServerErrorCode.Success : ServerErrorCode.SlotsAlreadyEquiped;
}
public virtual ServerErrorCode onIsUnequipable(TSlotType targetSlot)
{
return (true == m_slots.Contains(targetSlot)) ? ServerErrorCode.Success : ServerErrorCode.SlotsAlreadyUnequiped;
}
public virtual ServerErrorCode onTryUnequip(TSlotType targetSlot)
{
return (true == m_slots.TryRemove(targetSlot, out _)) ? ServerErrorCode.Success : ServerErrorCode.SlotsAlreadyUnequiped;
}
public virtual ServerErrorCode onTryEquipWithEntityBase(TSlotType targetSlot, EntityBase entityBase) => ServerErrorCode.FunctionNotImplemented;
public virtual ServerErrorCode onTryUnequipWithEntityBase(TSlotType targetSlot, out EntityBase? entityBase)
{
entityBase = null;
return ServerErrorCode.FunctionNotImplemented;
}
public virtual EntityBase? onFindEntityBase(TSlotType targetSlot)
{
return null;
}
public bool isExistSlotType(TSlotType slotType)
{
return true == m_slots.Contains(slotType);
}
public override UInt32 onGetCurrSlotCount()
{
return (UInt32)m_slots.Count;
}
public List<EntityBase> getEntityBases()
{
return new List<EntityBase>();
}
public ICollection getSlots() => m_slots;
}
public class SlotsWithEntityBase<TSlotType> : SlotsBase, ISlotsWithType<TSlotType>
where TSlotType : notnull
{
private ConcurrentDictionary<TSlotType, EntityBase> m_slots = new();
public SlotsWithEntityBase()
{
}
public override bool onInitSlots(int slotMaxCount)
{
m_slots = new ConcurrentDictionary<TSlotType, EntityBase>(MultiThreadHelper.getThreadOptimalCount(), (int)slotMaxCount);
return true;
}
public virtual ServerErrorCode onIsEquipable(TSlotType targetSlot)
{
return (false == m_slots.ContainsKey(targetSlot)) ? ServerErrorCode.Success : ServerErrorCode.SlotsAlreadyEquiped;
}
public virtual ServerErrorCode onTryEquipWithEntityBase(TSlotType targetSlot, EntityBase entityBase)
{
return (true == m_slots.TryAdd(targetSlot, entityBase)) ? ServerErrorCode.Success : ServerErrorCode.SlotsAlreadyEquiped;
}
public virtual ServerErrorCode onIsUnequipable(TSlotType targetSlot)
{
return (true == m_slots.ContainsKey(targetSlot)) ? ServerErrorCode.Success : ServerErrorCode.SlotsAlreadyUnequiped;
}
public virtual ServerErrorCode onTryUnequipWithEntityBase(TSlotType targetSlot, out EntityBase? removedEntityBase)
{
removedEntityBase = null;
if (false == m_slots.TryRemove(targetSlot, out var removed_entity_base))
{
return ServerErrorCode.SlotsAlreadyUnequiped;
}
removedEntityBase = removed_entity_base;
return ServerErrorCode.Success;
}
public virtual EntityBase? onFindEntityBase(TSlotType targetSlot)
{
return findEntityBaseInSlotType(targetSlot);
}
public EntityBase? findEntityBaseInSlotType(TSlotType targetSlot)
{
if(false == m_slots.TryGetValue(targetSlot, out var found_entity_base))
{
return null;
}
return found_entity_base;
}
public EntityBase? findEntityBaseBy(Func<EntityBase, bool> isFound)
{
foreach(var each in m_slots)
{
var is_success = isFound(each.Value);
if(true == is_success)
{
return each.Value;
}
}
return null;
}
public bool isExistSlotType(TSlotType slotType)
{
return true == m_slots.ContainsKey(slotType);
}
public override UInt32 onGetCurrSlotCount()
{
return (UInt32)m_slots.Count;
}
public List<EntityBase> getEntityBases()
{
return m_slots.Values.ToList();
}
public ICollection getSlots() => m_slots;
public virtual ServerErrorCode onTryEquip(TSlotType targetSlot) => ServerErrorCode.FunctionNotImplemented;
public virtual ServerErrorCode onTryUnequip(TSlotType targetSlot) => ServerErrorCode.FunctionNotImplemented;
}
}