//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Sunday, January 21, 2018 //-------------------------------------------------------- using System; using System.Collections.Generic; using TableConfig; namespace Snxxz.UI { public class PreciousItemGetModel : Model, IBeforePlayerDataInitialize,ISwitchAccount,IPlayerLoginOk { Dictionary itemStack = new Dictionary(); List itemGuids = new List(); PlayerPackModel playerPack { get { return ModelCenter.Instance.GetModel(); } } PackModelInterface modelInterface { get { return ModelCenter.Instance.GetModel(); } } DungeonModel dungeonModel { get { return ModelCenter.Instance.GetModel(); } } 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; PlayerDatas.Instance.PlayerDataRefreshInfoEvent -= RefreshRealm; } public void OnSwitchAccount() { currentShowItem = default(PreciousItem); } public void OnPlayerLoginOk() { PlayerDatas.Instance.PlayerDataRefreshInfoEvent += RefreshRealm; } 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 RefreshRealm(PlayerDataRefresh type) { int realmLv = PlayerDatas.Instance.baseData.realmLevel; if (type != PlayerDataRefresh.OfficialRank || NewBieCenter.Instance.inGuiding) { return; } List druglist = modelInterface.GetDruglistByRealm(); for(int i = 0; i < druglist.Count; i++) { OnGetPreciousItem(druglist[i].packType,druglist[i].itemInfo.ItemGUID); } } 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.GetDungeonId(dungeonModel.GetDataMapIdByMapId(mapId), lineId); if (dungeonId == 0) { return true; } else { var config = Config.Instance.Get(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; } } } }