using System; 
 | 
using System.Collections; 
 | 
using System.Collections.Generic; 
 | 
using System.Text; 
 | 
  
 | 
using UnityEngine; 
 | 
  
 | 
public class VirtualPackManager : GameSystemManager<VirtualPackManager> 
 | 
{ 
 | 
    Dictionary<PackType, List<VirtualPackItem>> virtualPackItems 
 | 
        = new Dictionary<PackType, List<VirtualPackItem>>(); 
 | 
    Dictionary<PackType, ObjectPool<VirtualPackItem>> m_VirtualItemPool 
 | 
        = new Dictionary<PackType, ObjectPool<VirtualPackItem>>(); 
 | 
    Dictionary<PackType, int> virtualPackCapacityDict = new Dictionary<PackType, int>(); 
 | 
  
 | 
    public event Action<PackType> virtualPackRefresh; 
 | 
    int getA206Count = 0;          //收到了几次A206包 
 | 
    bool isFrist = false;          //是不是第一次获得聚魂碎片 
 | 
    Dictionary<int, int> noPackItemCountDict = new Dictionary<int, int>();  //无需背包的物品数量刷新,自动转化为数值 
 | 
    public event Action OnNoPackItemCountRefresh; 
 | 
  
 | 
    public void OnBeforePlayerDataInitialize() 
 | 
    { 
 | 
        foreach (var packType in virtualPackItems.Keys) 
 | 
        { 
 | 
            var pool = m_VirtualItemPool[packType]; 
 | 
            var items = virtualPackItems[packType]; 
 | 
            foreach (var item in items) 
 | 
            { 
 | 
                pool.Release(item); 
 | 
            } 
 | 
        } 
 | 
        virtualPackItems.Clear(); 
 | 
        noPackItemCountDict.Clear(); 
 | 
        getA206Count = 0; 
 | 
        isFrist = false; 
 | 
    } 
 | 
  
 | 
    public void OnPlayerLoginOk() 
 | 
    { 
 | 
        isFrist = noPackItemCountDict.Count == 0; 
 | 
    } 
 | 
  
 | 
    public override void Init() 
 | 
    { 
 | 
        base.Init(); 
 | 
        ParseConfig(); 
 | 
        m_VirtualItemPool.Add(PackType.GatherSoul, new ObjectPool<VirtualPackItem>(OnGetItem, OnReleaseItem)); 
 | 
        m_VirtualItemPool.Add(PackType.RunePack, new ObjectPool<VirtualPackItem>(OnGetItem, OnReleaseItem)); 
 | 
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitialize; 
 | 
        DTC0403_tagPlayerLoginLoadOK.playerLoginOkEvent += OnPlayerLoginOk; 
 | 
    } 
 | 
  
 | 
    public override void Release() 
 | 
    { 
 | 
        base.Release(); 
 | 
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent -= OnBeforePlayerDataInitialize; 
 | 
        DTC0403_tagPlayerLoginLoadOK.playerLoginOkEvent -= OnPlayerLoginOk; 
 | 
    } 
 | 
  
 | 
    void ParseConfig() 
 | 
    { 
 | 
        // virtualPackCapacityDict.Add(PackType.RunePack, int.Parse(funcConfig.Numerical1)); 
 | 
    } 
 | 
  
 | 
    public bool IsVirtualPack(PackType packType) 
 | 
    { 
 | 
        switch (packType) 
 | 
        { 
 | 
            case PackType.GatherSoul: 
 | 
            case PackType.RunePack: 
 | 
                return true; 
 | 
        } 
 | 
        return false; 
 | 
    } 
 | 
  
 | 
    public int GetPackCapacity(PackType packType) 
 | 
    { 
 | 
        if (virtualPackCapacityDict.ContainsKey(packType)) 
 | 
        { 
 | 
            return virtualPackCapacityDict[packType]; 
 | 
        } 
 | 
        return 0; 
 | 
    } 
 | 
  
 | 
    public int GetPackRemainCount(PackType packType) 
 | 
    { 
 | 
        var capacity = GetPackCapacity(packType); 
 | 
        return capacity - GetPackItemCount(packType); 
 | 
    } 
 | 
  
 | 
    public int GetPackItemCount(PackType packType) 
 | 
    { 
 | 
        if (virtualPackItems.ContainsKey(packType)) 
 | 
        { 
 | 
            return virtualPackItems[packType].Count; 
 | 
        } 
 | 
        return 0; 
 | 
    } 
 | 
  
 | 
    public int GetItemCountById(PackType packType, int id) 
 | 
    { 
 | 
        var count = 0; 
 | 
        List<int> list; 
 | 
        if (TryGetItems(packType, out list)) 
 | 
        { 
 | 
            for (int i = 0; i < list.Count; i++) 
 | 
            { 
 | 
                VirtualPackItem item; 
 | 
                if (TryGetItem(packType, list[i], out item) 
 | 
                    && item.id == id) 
 | 
                { 
 | 
                    count++; 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        return count; 
 | 
    } 
 | 
  
 | 
    public void GetItemIndexs(PackType packType, ref List<int> list) 
 | 
    { 
 | 
        list.Clear(); 
 | 
        List<VirtualPackItem> _list; 
 | 
        if (virtualPackItems.TryGetValue(packType, out _list)) 
 | 
        { 
 | 
            for (int i = 0; i < _list.Count; i++) 
 | 
            { 
 | 
                list.Add(_list[i].index); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public bool TryGetItems(PackType packType, out List<int> list) 
 | 
    { 
 | 
        list = new List<int>(); 
 | 
        List<VirtualPackItem> _list; 
 | 
        if (virtualPackItems.TryGetValue(packType, out _list)) 
 | 
        { 
 | 
            for (int i = 0; i < _list.Count; i++) 
 | 
            { 
 | 
                list.Add(_list[i].index); 
 | 
            } 
 | 
            return true; 
 | 
        } 
 | 
        return false; 
 | 
    } 
 | 
  
 | 
    public bool TryGetItem<T>(PackType packType, int packIndex, out T item) where T : VirtualPackItem 
 | 
    { 
 | 
        item = default(T); 
 | 
        if (virtualPackItems.ContainsKey(packType)) 
 | 
        { 
 | 
            var _index = virtualPackItems[packType].FindIndex((x) => 
 | 
                { 
 | 
                    return x.index == packIndex; 
 | 
                }); 
 | 
            if (_index != -1) 
 | 
            { 
 | 
                item = virtualPackItems[packType][_index] as T; 
 | 
            } 
 | 
            return _index != -1; 
 | 
        } 
 | 
        return false; 
 | 
    } 
 | 
  
 | 
    public void OnReceiveServerPack(HA204_tagMCVPackRefresh package) 
 | 
    { 
 | 
        var packType = (PackType)package.PackType; 
 | 
        if (!IsVirtualPack(packType)) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
        List<VirtualPackItem> list; 
 | 
        if (!virtualPackItems.TryGetValue(packType, out list)) 
 | 
        { 
 | 
            list = new List<VirtualPackItem>(); 
 | 
            virtualPackItems.Add(packType, list); 
 | 
        } 
 | 
        SetVirtualItem(packType, ref list, package.VPacklItemList); 
 | 
        if (virtualPackRefresh != null) 
 | 
        { 
 | 
            virtualPackRefresh((PackType)package.PackType); 
 | 
        } 
 | 
  
 | 
    } 
 | 
  
 | 
    public void OnReceiveServerPack(HA205_tagMCVPackClear package) 
 | 
    { 
 | 
        var packType = (PackType)package.PackType; 
 | 
        if (!IsVirtualPack(packType)) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
        List<VirtualPackItem> list; 
 | 
        if (virtualPackItems.TryGetValue(packType, out list)) 
 | 
        { 
 | 
            var pool = m_VirtualItemPool[packType]; 
 | 
            for (int i = 0; i < package.Count; i++) 
 | 
            { 
 | 
                var index = package.ItemPlaceList[i]; 
 | 
                var items = list.FindAll((x) => 
 | 
                { 
 | 
                    return x.index == index; 
 | 
                }); 
 | 
                foreach (var item in items) 
 | 
                { 
 | 
                    list.Remove(item); 
 | 
                    pool.Release(item); 
 | 
                } 
 | 
            } 
 | 
            if (virtualPackRefresh != null) 
 | 
            { 
 | 
                virtualPackRefresh(packType); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    public void UpdateAutoItemCountRefresh(HA206_tagMCAutoItemCountRefresh netPack) 
 | 
    { 
 | 
  
 | 
        for (int i = 0; i < netPack.Count; i++) 
 | 
        { 
 | 
            noPackItemCountDict[(int)netPack.ItemCountList[i].ItemID] = (int)netPack.ItemCountList[i].ItemCount; 
 | 
        } 
 | 
        OnNoPackItemCountRefresh?.Invoke(); 
 | 
    } 
 | 
  
 | 
  
 | 
    public int GetNoPackItemCount(int id) 
 | 
    { 
 | 
        if (noPackItemCountDict.ContainsKey(id)) 
 | 
        { 
 | 
            return noPackItemCountDict[id]; 
 | 
        } 
 | 
        return 0; 
 | 
    } 
 | 
  
 | 
    public Dictionary<int, int> GetAllNoPackItem() 
 | 
    { 
 | 
        return noPackItemCountDict; 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
    void SetVirtualItem(PackType packType, ref List<VirtualPackItem> list, HA204_tagMCVPackRefresh.tagMCVPackItem[] items) 
 | 
    { 
 | 
        var pool = m_VirtualItemPool[packType]; 
 | 
        for (int i = 0; i < items.Length; i++) 
 | 
        { 
 | 
            var item = list.Find((x) => 
 | 
            { 
 | 
                return x.index == items[i].ItemPlace; 
 | 
            }); 
 | 
            if (item != null) 
 | 
            { 
 | 
                item.Release(); 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                if (pool.inactivedCount > 0) 
 | 
                { 
 | 
                    item = pool.Get(); 
 | 
                    list.Add(item); 
 | 
                } 
 | 
            } 
 | 
            if (item == null) 
 | 
            { 
 | 
                item = VirtualPackItem.Get(packType); 
 | 
                list.Add(item); 
 | 
            } 
 | 
            item.ParsePackItem(items[i].ItemPlace, items[i].ItemData); 
 | 
  
 | 
        } 
 | 
    } 
 | 
  
 | 
    static void OnGetItem(VirtualPackItem item) 
 | 
    { 
 | 
  
 | 
    } 
 | 
  
 | 
    static void OnReleaseItem(VirtualPackItem item) 
 | 
    { 
 | 
        item.Release(); 
 | 
    } 
 | 
} 
 | 
  
 | 
public abstract class VirtualPackItem 
 | 
{ 
 | 
    public int index { get; private set; } 
 | 
  
 | 
    public int id { get; private set; } 
 | 
  
 | 
    public int level { get; private set; } 
 | 
  
 | 
    protected string dataString { get; set; } 
 | 
  
 | 
    protected static StringBuilder sb = new StringBuilder(); 
 | 
  
 | 
    public virtual void ParsePackItem(int index, uint data) 
 | 
    { 
 | 
        this.index = index; 
 | 
  
 | 
        dataString = data.ToString(); 
 | 
        int delta = 10 - dataString.Length; 
 | 
        sb.Length = 0; 
 | 
        sb.Append('0', delta); 
 | 
        dataString = dataString.Insert(0, sb.ToString()); 
 | 
  
 | 
        id = int.Parse(dataString.Substring(5, 5)); 
 | 
        level = int.Parse(dataString.Substring(2, 3)) + 1; 
 | 
    } 
 | 
  
 | 
    public virtual void Release() 
 | 
    { 
 | 
        dataString = null; 
 | 
    } 
 | 
  
 | 
    public static VirtualPackItem Get(PackType packType) 
 | 
    { 
 | 
        // switch (packType) 
 | 
        // { 
 | 
        //     case PackType.RunePack: 
 | 
        //         return new RuneItem(); 
 | 
        // } 
 | 
        return null; 
 | 
    } 
 | 
} 
 | 
  
 | 
public struct VirtualItem 
 | 
{ 
 | 
    public PackType packType; 
 | 
    public int itemId; 
 | 
    public int index; 
 | 
    public int level; 
 | 
} 
 |