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 : SlotsBase, ISlotsWithType where TSlotType : Enum { private ConcurrentHashSet m_slots = new(); public Slots() { } public override bool onInitSlots(int slotMaxCount) { m_slots = new ConcurrentHashSet(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 getEntityBases() { return new List(); } public ICollection getSlots() => m_slots; } public class SlotsWithEntityBase : SlotsBase, ISlotsWithType where TSlotType : notnull { private ConcurrentDictionary m_slots = new(); public SlotsWithEntityBase() { } public override bool onInitSlots(int slotMaxCount) { m_slots = new ConcurrentDictionary(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 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 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; } }