using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnityEngine; namespace vnxbqy.UI { public class MysticalQGModel : Model, IBeforePlayerDataInitialize, IPlayerLoginOk,IOpenServerActivity { public event Action UpdateMysticalShopEvent; public event Action onStateUpdate; StoreModel storeModel { get { return ModelCenter.Instance.GetModel(); } } bool isLogin = true; public bool isOpenPrompting { get; set;} public override void Init() { OpenServerActivityCenter.Instance.Register(18, this); } public void OnBeforePlayerDataInitialize() { isOpenPrompting = true; isLogin = true; mysticalShopDict.Clear(); GlobalTimeEvent.Instance.halfMinuteEvent -= UpdateSecond; storeModel.RefreshBuyShopLimitEvent -= UpdateBuyTimes; } public void OnPlayerLoginOk() { isLogin = false; GlobalTimeEvent.Instance.halfMinuteEvent += UpdateSecond; storeModel.RefreshBuyShopLimitEvent += UpdateBuyTimes; UpdateRedpoint(); } public override void UnInit() { } public bool IsOpen { get { List mysticalShops = null; return TryGetCurSellShop(out mysticalShops); } } public bool IsAdvance { get { return false; } } public bool priorityOpen { get { return mysticalRedpoint.state == RedPointState.Simple; } } /// /// 更新物品购买次数 /// private void UpdateBuyTimes() { if (UpdateMysticalShopEvent != null) { UpdateMysticalShopEvent(); } if (onStateUpdate != null) { onStateUpdate(18); } } public void UpdateSellTimeEnd() { if (UpdateMysticalShopEvent != null) { UpdateMysticalShopEvent(); } if (onStateUpdate != null) { onStateUpdate(18); } } public bool TryGetCurSellShop(out List mysticals) { mysticals = new List(); var mysticalShops = mysticalShopDict.Values.ToList(); mysticalShops.Sort(CompareBySellTime); for(int i = 0; i < mysticalShops.Count; i++) { var mystical = mysticalShops[i]; if (!mystical.IsSellOut() && mystical.GetRemainSellTime() > 0) { mysticals.Add(mystical); } } return mysticals.Count > 0; } public int CompareBySellTime(MysticalShop start, MysticalShop end) { int x = start.GetRemainSellTime(); int y = end.GetRemainSellTime(); if (x.CompareTo(y) != 0) return x.CompareTo(y); return 0; } #region 协议 private Dictionary mysticalShopDict = new Dictionary(); public void UpdateMysticalShopInfo(HA806_tagMCMysticalShopTimeInfo info) { mysticalShopDict.Clear(); for (int i = 0; i < info.Count; i++) { var shopInfo = info.ShopTimeList[i]; MysticalShop mystical = new MysticalShop((int)shopInfo.GoodsID,(int)shopInfo.StartTime); if(!mysticalShopDict.ContainsKey((int)shopInfo.GoodsID)) { mysticalShopDict.Add((int)shopInfo.GoodsID,mystical); } else { mysticalShopDict[(int)shopInfo.GoodsID] = mystical; } } if(UpdateMysticalShopEvent != null) { UpdateMysticalShopEvent(); } if (onStateUpdate != null) { onStateUpdate(18); } UpdateRedpoint(); } public MysticalShop GetMysticalShop(int shopId) { MysticalShop mystical = null; mysticalShopDict.TryGetValue(shopId,out mystical); return mystical; } #endregion public class MysticalShop { public int shopdId; public int sellTime; public StoreConfig storeConfig; StoreModel storeModel { get { return ModelCenter.Instance.GetModel(); } } public MysticalShop(int id,int sellTime) { this.shopdId = id; this.sellTime = sellTime; this.storeConfig = StoreConfig.Get(shopdId); } /// ///是否卖光 /// /// public bool IsSellOut() { int remainNum = 0; return storeModel.TryGetIsSellOut(storeConfig,out remainNum); } public int GetRemainSellTime() { int seconds = sellTime + storeConfig.LimitValue; DateTime endTime = TimeUtility.GetTime((uint)seconds); int endSec = Mathf.CeilToInt((float)(endTime - TimeUtility.ServerNow).TotalSeconds); return endSec > 0 ? endSec : 0; } } #region 红点 public event Action UpdateNewMysticalEvent; public const int MysticalRedKey = 20918; public Redpoint mysticalRedpoint = new Redpoint(MainRedDot.REDPOINT_OPENSERVER, MysticalRedKey); public void UpdateRedpoint() { if (isLogin) return; if(IsOpen) { mysticalRedpoint.state = RedPointState.Simple; } else { mysticalRedpoint.state = RedPointState.None; } if(UpdateNewMysticalEvent != null) { UpdateNewMysticalEvent(); } } private void UpdateSecond() { if (isLogin || !priorityOpen) return; if(!IsOpen) { CloseRedPoint(); } } public void CloseRedPoint() { mysticalRedpoint.state = RedPointState.None; if (UpdateNewMysticalEvent != null) { UpdateNewMysticalEvent(); } } #endregion } }