少年修仙传客户端代码仓库
client_wuxijin
2018-08-09 d78dfd57501d2343fd9e950ce03b4d4d31a7a430
System/BetterItemGet/PreciousItemGetModel.cs
@@ -1,205 +1,205 @@
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Sunday, January 21, 2018
//--------------------------------------------------------
using System;
using System.Collections.Generic;
using TableConfig;
namespace Snxxz.UI
{
    public class PreciousItemGetModel : Model, IBeforePlayerDataInitialize, ISwitchAccount
    {
        Dictionary<string, PreciousItem> itemStack = new Dictionary<string, PreciousItem>();
        List<string> itemGuids = new List<string>();
        PlayerPackModel playerPack { get { return ModelCenter.Instance.GetModel<PlayerPackModel>(); } }
        PackModelInterface modelInterface { get { return ModelCenter.Instance.GetModel<PackModelInterface>(); } }
        DungeonModel dungeonModel { get { return ModelCenter.Instance.GetModel<DungeonModel>(); } }
        public PreciousItem currentShowItem { get; private set; }
        public event Action showItemRefreshEvent;
        public bool isGetNewItem { get;set; }
        public override void Init()
        {
            modelInterface.GetPreciousItemEvent += OnGetPreciousItem;
            playerPack.RefreshPackAct += OnPackageRefresh;
            playerPack.RefreshItemCountAct += OnPackageItemRefresh;
        }
        public override void UnInit()
        {
            modelInterface.GetPreciousItemEvent -= OnGetPreciousItem;
            playerPack.RefreshPackAct -= OnPackageRefresh;
            playerPack.RefreshItemCountAct -= OnPackageItemRefresh;
        }
        public void OnBeforePlayerDataInitialize()
        {
            isGetNewItem = false;
        }
        public void OnSwitchAccount()
        {
            currentShowItem = default(PreciousItem);
        }
        public bool TryGetItem(string _guid, out PreciousItem _itemModel)
        {
            return itemStack.TryGetValue(_guid, out _itemModel);
        }
        public void ReportConfirmPreciousItem(PreciousItem _preciousItem)
        {
            if (itemGuids.Contains(_preciousItem.guid))
            {
                itemGuids.Remove(_preciousItem.guid);
            }
            if (itemStack.ContainsKey(_preciousItem.guid))
            {
                itemStack.Remove(_preciousItem.guid);
            }
            RefreshCurrrentShowPreciousItem();
        }
        public int GetShowItemId()
        {
            if (currentShowItem == default(PreciousItem))
            {
                return 0;
            }
            else
            {
                var item = playerPack.GetItemModelByGUID(currentShowItem.guid);
                return item == null ? 0 : item.chinItemModel.ID;
            }
        }
        private void OnGetPreciousItem(PackType type, string guid)
        {
            var preciousItem = new PreciousItem(type, guid);
            if (!itemGuids.Contains(guid))
            {
                itemGuids.Add(guid);
            }
            itemStack[guid] = preciousItem;
            RefreshCurrrentShowPreciousItem();
        }
        private void OnPackageRefresh(PackType _type)
        {
            if (_type != PackType.rptItem)
            {
                return;
            }
            RefreshCurrrentShowPreciousItem();
        }
        private void OnPackageItemRefresh(PackType _type, int _index, int _itemId)
        {
            OnPackageRefresh(_type);
        }
        private bool FindLatestItem(out PreciousItem _item)
        {
            if (itemGuids.Count == 0)
            {
                _item = default(PreciousItem);
                return false;
            }
            var guid = itemGuids[itemGuids.Count - 1];
            _item = itemStack[guid];
            return true;
        }
        private void Trim()
        {
            for (int i = itemGuids.Count - 1; i >= 0; i--)
            {
                var item = itemStack[itemGuids[i]];
                if (playerPack.GetItemModelByGUID(item.guid) == null
                    || playerPack.GetItemModelByGUID(item.guid).packType != PackType.rptItem)
                {
                    itemGuids.RemoveAt(i);
                }
            }
        }
        private void RefreshCurrrentShowPreciousItem()
        {
            Trim();
            PreciousItem tempItem;
            if (FindLatestItem(out tempItem))
            {
                if (tempItem != currentShowItem)
                {
                    currentShowItem = tempItem;
                }
            }
            else
            {
                currentShowItem = default(PreciousItem);
            }
            if (showItemRefreshEvent != null)
            {
                isGetNewItem = true;
                showItemRefreshEvent();
            }
        }
        public bool ShowPreciousItemAble()
        {
            var mapId = PlayerDatas.Instance.baseData.MapID;
            var lineId = PlayerDatas.Instance.baseData.dungeonLineId;
            var dungeonId = dungeonModel.DungeonMap(dungeonModel.GetDungeonDataIdByMapId(mapId), lineId);
            if (dungeonId == 0)
            {
                return true;
            }
            else
            {
                var config = ConfigManager.Instance.GetTemplate<DungeonConfig>(dungeonId);
                return config.ShowNewItemTip == 1;
            }
        }
        public struct PreciousItem
        {
            public PackType packType;
            public string guid;
            public PreciousItem(PackType _packType, string _guid)
            {
                this.packType = _packType;
                this.guid = _guid;
            }
            public static bool operator ==(PreciousItem _lhs, PreciousItem _rhs)
            {
                return _lhs.packType == _rhs.packType && _lhs.guid == _rhs.guid;
            }
            public static bool operator !=(PreciousItem _lhs, PreciousItem _rhs)
            {
                return _lhs.packType != _rhs.packType || _lhs.guid != _rhs.guid;
            }
        }
    }
}
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Sunday, January 21, 2018
//--------------------------------------------------------
using System;
using System.Collections.Generic;
using TableConfig;
namespace Snxxz.UI
{
    public class PreciousItemGetModel : Model, IBeforePlayerDataInitialize, ISwitchAccount
    {
        Dictionary<string, PreciousItem> itemStack = new Dictionary<string, PreciousItem>();
        List<string> itemGuids = new List<string>();
        PlayerPackModel playerPack { get { return ModelCenter.Instance.GetModel<PlayerPackModel>(); } }
        PackModelInterface modelInterface { get { return ModelCenter.Instance.GetModel<PackModelInterface>(); } }
        DungeonModel dungeonModel { get { return ModelCenter.Instance.GetModel<DungeonModel>(); } }
        public PreciousItem currentShowItem { get; private set; }
        public event Action showItemRefreshEvent;
        public bool isGetNewItem { get;set; }
        public override void Init()
        {
            modelInterface.GetPreciousItemEvent += OnGetPreciousItem;
            playerPack.RefreshPackAct += OnPackageRefresh;
            playerPack.RefreshItemCountAct += OnPackageItemRefresh;
        }
        public override void UnInit()
        {
            modelInterface.GetPreciousItemEvent -= OnGetPreciousItem;
            playerPack.RefreshPackAct -= OnPackageRefresh;
            playerPack.RefreshItemCountAct -= OnPackageItemRefresh;
        }
        public void OnBeforePlayerDataInitialize()
        {
            isGetNewItem = false;
        }
        public void OnSwitchAccount()
        {
            currentShowItem = default(PreciousItem);
        }
        public bool TryGetItem(string _guid, out PreciousItem _itemModel)
        {
            return itemStack.TryGetValue(_guid, out _itemModel);
        }
        public void ReportConfirmPreciousItem(PreciousItem _preciousItem)
        {
            if (itemGuids.Contains(_preciousItem.guid))
            {
                itemGuids.Remove(_preciousItem.guid);
            }
            if (itemStack.ContainsKey(_preciousItem.guid))
            {
                itemStack.Remove(_preciousItem.guid);
            }
            RefreshCurrrentShowPreciousItem();
        }
        public int GetShowItemId()
        {
            if (currentShowItem == default(PreciousItem))
            {
                return 0;
            }
            else
            {
                var item = playerPack.GetItemModelByGUID(currentShowItem.guid);
                return item == null ? 0 : item.chinItemModel.ID;
            }
        }
        private void OnGetPreciousItem(PackType type, string guid)
        {
            var preciousItem = new PreciousItem(type, guid);
            if (!itemGuids.Contains(guid))
            {
                itemGuids.Add(guid);
            }
            itemStack[guid] = preciousItem;
            RefreshCurrrentShowPreciousItem();
        }
        private void OnPackageRefresh(PackType _type)
        {
            if (_type != PackType.rptItem)
            {
                return;
            }
            RefreshCurrrentShowPreciousItem();
        }
        private void OnPackageItemRefresh(PackType _type, int _index, int _itemId)
        {
            OnPackageRefresh(_type);
        }
        private bool FindLatestItem(out PreciousItem _item)
        {
            if (itemGuids.Count == 0)
            {
                _item = default(PreciousItem);
                return false;
            }
            var guid = itemGuids[itemGuids.Count - 1];
            _item = itemStack[guid];
            return true;
        }
        private void Trim()
        {
            for (int i = itemGuids.Count - 1; i >= 0; i--)
            {
                var item = itemStack[itemGuids[i]];
                if (playerPack.GetItemModelByGUID(item.guid) == null
                    || playerPack.GetItemModelByGUID(item.guid).packType != PackType.rptItem)
                {
                    itemGuids.RemoveAt(i);
                }
            }
        }
        private void RefreshCurrrentShowPreciousItem()
        {
            Trim();
            PreciousItem tempItem;
            if (FindLatestItem(out tempItem))
            {
                if (tempItem != currentShowItem)
                {
                    currentShowItem = tempItem;
                }
            }
            else
            {
                currentShowItem = default(PreciousItem);
            }
            if (showItemRefreshEvent != null)
            {
                isGetNewItem = true;
                showItemRefreshEvent();
            }
        }
        public bool ShowPreciousItemAble()
        {
            var mapId = PlayerDatas.Instance.baseData.MapID;
            var lineId = PlayerDatas.Instance.baseData.dungeonLineId;
            var dungeonId = dungeonModel.DungeonMap(dungeonModel.GetDungeonDataIdByMapId(mapId), lineId);
            if (dungeonId == 0)
            {
                return true;
            }
            else
            {
                var config = ConfigManager.Instance.GetTemplate<DungeonConfig>(dungeonId);
                return config.ShowNewItemTip == 1;
            }
        }
        public struct PreciousItem
        {
            public PackType packType;
            public string guid;
            public PreciousItem(PackType _packType, string _guid)
            {
                this.packType = _packType;
                this.guid = _guid;
            }
            public static bool operator ==(PreciousItem _lhs, PreciousItem _rhs)
            {
                return _lhs.packType == _rhs.packType && _lhs.guid == _rhs.guid;
            }
            public static bool operator !=(PreciousItem _lhs, PreciousItem _rhs)
            {
                return _lhs.packType != _rhs.packType || _lhs.guid != _rhs.guid;
            }
        }
    }
}