using System;  
 | 
using System.Collections;  
 | 
using System.Collections.Generic;  
 | 
  
 | 
using UnityEngine;  
 | 
namespace Snxxz.UI  
 | 
{  
 | 
      
 | 
    public class RedEnvelopeModel : Model, IBeforePlayerDataInitialize, IPlayerLoginOk  
 | 
    {  
 | 
        Dictionary<int, string> redpackSfxDict = new Dictionary<int, string>();  
 | 
  
 | 
        RedPacketModel redPackdata { get { return ModelCenter.Instance.GetModel<RedPacketModel>(); } }  
 | 
        OSRedEnvelopeModel envelopeModel { get { return ModelCenter.Instance.GetModel<OSRedEnvelopeModel>(); } }  
 | 
        FestivalRedpackModel festivalRedpackModel { get { return ModelCenter.Instance.GetModel<FestivalRedpackModel>(); } }  
 | 
  
 | 
        public override void Init()  
 | 
        {  
 | 
            ParseConfig();  
 | 
            SysNotifyMgr.Instance.sysNotifyEvent += OnSystemNotifyEvent;  
 | 
        }  
 | 
  
 | 
        public override void UnInit()  
 | 
        {  
 | 
  
 | 
        }  
 | 
  
 | 
        public void OnPlayerLoginOk()  
 | 
        {  
 | 
            serverInited = true;  
 | 
        }  
 | 
  
 | 
        public void OnBeforePlayerDataInitialize()  
 | 
        {  
 | 
            serverInited = false;  
 | 
            m_RedEnvelopeDict.Clear();  
 | 
            redpacks.Clear();  
 | 
        }  
 | 
  
 | 
        bool serverInited = false;  
 | 
  
 | 
        Dictionary<int, RedEnvelope> m_RedEnvelopeDict = new Dictionary<int, RedEnvelope>();  
 | 
        Dictionary<int, List<int>> redpacks = new Dictionary<int, List<int>>();  
 | 
  
 | 
        public event Action EnvelopeUpdateEvent;  
 | 
        public event Action<int> EnvelopeGetEvent;  
 | 
  
 | 
        public void UpdateRedEnvelope(HA404_tagGCFamilyRedPacketInfo _pak)  
 | 
        {  
 | 
            for (int i = 0; i < _pak.Count; i++)  
 | 
            {  
 | 
                var info = _pak.RedPacketInfo[i];  
 | 
                RedEnvelope _envelope = new RedEnvelope()  
 | 
                {  
 | 
                    id = (int)info.RedPacketID,  
 | 
                    player = (int)info.PlayerID,  
 | 
                    playerName = info.Name,  
 | 
                    time = info.Time,  
 | 
                    job = info.PlayeJob,  
 | 
                    moneyType = info.MoneyType,  
 | 
                    moneyNum = (int)info.MoneyNum,  
 | 
                    type = info.GetWay,  
 | 
                    packCount = info.PacketCnt,  
 | 
                    state = info.State,  
 | 
                    Wish = UIHelper.ServerStringTrim(info.Wish)  
 | 
                };  
 | 
  
 | 
                List<int> list;  
 | 
                if (!redpacks.TryGetValue(info.GetWay, out list))  
 | 
                {  
 | 
                    list = new List<int>();  
 | 
                    redpacks.Add(info.GetWay, list);  
 | 
                }  
 | 
                if (!list.Contains((int)info.RedPacketID))  
 | 
                {  
 | 
                    list.Add((int)info.RedPacketID);  
 | 
                }  
 | 
  
 | 
                m_RedEnvelopeDict[(int)info.RedPacketID] = _envelope;  
 | 
                if (EnvelopeGetEvent != null)  
 | 
                {  
 | 
                    EnvelopeGetEvent(_envelope.id);  
 | 
                }  
 | 
            }  
 | 
            if (EnvelopeUpdateEvent != null)  
 | 
            {  
 | 
                EnvelopeUpdateEvent();  
 | 
            }  
 | 
        }  
 | 
  
 | 
  
 | 
        public void UpdateRedEnvelope(HA405_tagGCFamilyRedPacketGrabInfo vNetData)  
 | 
        {  
 | 
            RedEnvelope envelope;  
 | 
            if (TryGetEnvelope((int)vNetData.RedPacketID, out envelope))  
 | 
            {  
 | 
                if (envelope.packCount == vNetData.Count  
 | 
                    && envelope.state != 3)  
 | 
                {  
 | 
                    UpdateRedEnvelope(envelope, 3);  
 | 
                }  
 | 
            }  
 | 
            bool self = false;  
 | 
            for (int i = 0; i < vNetData.Count; i++)  
 | 
            {  
 | 
                if (UIHelper.ServerStringTrim(vNetData.GrabInfo[i].Name).Equals(  
 | 
                    UIHelper.ServerStringTrim(PlayerDatas.Instance.baseData.PlayerName)))  
 | 
                {  
 | 
                    self = true;  
 | 
                    break;  
 | 
                }  
 | 
            }  
 | 
            if (vNetData.RedPacketID == cacheEnvelopeId)  
 | 
            {  
 | 
                if (!envelope.Equals(default(RedEnvelope)))  
 | 
                {  
 | 
                    if (envelope.type == FestivalRedpackModel.FESTIVALSYSTEMREDPACK1  
 | 
                        || envelope.type == FestivalRedpackModel.FESTIVALSYSTEMREDPACK2)  
 | 
                    {  
 | 
                        if (!WindowCenter.Instance.IsOpen<RedpackSystemDetailsWin>())  
 | 
                        {  
 | 
                            envelopeSfx = vNetData.RedPacketID == envelopeSfx && self ? envelopeSfx : 0;  
 | 
                            ModelCenter.Instance.GetModel<RedPacketModel>().redpackDetailId = (int)vNetData.RedPacketID;  
 | 
                            WindowCenter.Instance.Open<RedpackSystemDetailsWin>();  
 | 
                        }  
 | 
                        cacheEnvelopeId = 0;  
 | 
                        return;  
 | 
                    }  
 | 
                }  
 | 
                if (!WindowCenter.Instance.IsOpen<RedDetailsTipsWin>())  
 | 
                {  
 | 
                    envelopeSfx = vNetData.RedPacketID == envelopeSfx && self ? envelopeSfx : 0;  
 | 
                    ModelCenter.Instance.GetModel<RedPacketModel>().redpackDetailId = (int)vNetData.RedPacketID;  
 | 
                    WindowCenter.Instance.Open<RedDetailsTipsWin>();  
 | 
                }  
 | 
                cacheEnvelopeId = 0;  
 | 
            }  
 | 
        }  
 | 
  
 | 
        public void UpdateRedEnvelope(RedEnvelope envelope, int _state)  
 | 
        {  
 | 
            m_RedEnvelopeDict[(int)envelope.id] = new RedEnvelope()  
 | 
            {  
 | 
                id = envelope.id,  
 | 
                player = envelope.player,  
 | 
                playerName = envelope.playerName,  
 | 
                time = envelope.time,  
 | 
                job = envelope.job,  
 | 
                moneyType = envelope.moneyType,  
 | 
                moneyNum = envelope.moneyNum,  
 | 
                type = envelope.type,  
 | 
                packCount = envelope.packCount,  
 | 
                state = _state,  
 | 
                Wish = UIHelper.ServerStringTrim(envelope.Wish)  
 | 
            };  
 | 
            if (EnvelopeUpdateEvent != null)  
 | 
            {  
 | 
                EnvelopeUpdateEvent();  
 | 
            }  
 | 
        }  
 | 
  
 | 
        public event Action EnvelopeDelEvent;  
 | 
  
 | 
        public bool TryGetEnvelope(int _id,out RedEnvelope _envelope)  
 | 
        {  
 | 
            return m_RedEnvelopeDict.TryGetValue(_id, out _envelope);  
 | 
        }  
 | 
  
 | 
        public void UpdateRedEnvelope(HAC05_tagGCRedPacketDel _pak)  
 | 
        {  
 | 
            for (int i = 0; i < _pak.Cnt; i++)  
 | 
            {  
 | 
                var _id = (int)_pak.DelRedPacketID[i];  
 | 
                if (m_RedEnvelopeDict.ContainsKey(_id))  
 | 
                {  
 | 
                    var type = m_RedEnvelopeDict[_id].type;  
 | 
                    if (redpacks.ContainsKey(type))  
 | 
                    {  
 | 
                        var list = redpacks[type];  
 | 
                        if (list.Contains(_id))  
 | 
                        {  
 | 
                            list.Remove(_id);  
 | 
                        }  
 | 
                    }  
 | 
  
 | 
                    m_RedEnvelopeDict.Remove(_id);  
 | 
                }  
 | 
            }  
 | 
            if (EnvelopeDelEvent != null)  
 | 
            {  
 | 
                EnvelopeDelEvent();  
 | 
            }  
 | 
        }  
 | 
  
 | 
        public void SendGetEnvelope(int _id,int getWay)  
 | 
        {  
 | 
            RedEnvelope _envelope;  
 | 
            if(m_RedEnvelopeDict.TryGetValue(_id,out _envelope))  
 | 
            {  
 | 
                if (_envelope.state == 1)  
 | 
                {  
 | 
                    CAB12_tagCMGrabFamilyRedPacket _pak = new CAB12_tagCMGrabFamilyRedPacket();  
 | 
                    _pak.RedPaketID = (uint)_id;  
 | 
                    _pak.GetWay = (byte)getWay;  
 | 
                    GameNetSystem.Instance.SendInfo(_pak);  
 | 
                }  
 | 
            }  
 | 
        }  
 | 
  
 | 
        public bool TryGetLatestEnvelope(out RedEnvelope envelope)  
 | 
        {  
 | 
            envelope = default(RedEnvelope);  
 | 
            bool _has = false;  
 | 
            foreach (var _envelope in m_RedEnvelopeDict.Values)  
 | 
            {  
 | 
                if (_envelope.state == 1)  
 | 
                {  
 | 
                    envelope = _envelope;  
 | 
                    _has = true;  
 | 
                }  
 | 
            }  
 | 
            return _has;  
 | 
        }  
 | 
  
 | 
        public bool TryGetEnvelopes(int type, out List<int> list)  
 | 
        {  
 | 
            return redpacks.TryGetValue(type, out list);  
 | 
        }  
 | 
  
 | 
        public void GetEnvelopes(int _type, ref List<int> _list)  
 | 
        {  
 | 
            _list.Clear();  
 | 
            foreach (var _id in m_RedEnvelopeDict.Keys)  
 | 
            {  
 | 
                if (m_RedEnvelopeDict[_id].type == _type)  
 | 
                {  
 | 
                    _list.Add(_id);  
 | 
                }  
 | 
            }  
 | 
        }  
 | 
  
 | 
        public bool CheckEnvelopeLimit(int _id, bool _tip = false)  
 | 
        {  
 | 
            if (!m_RedEnvelopeDict.ContainsKey(_id))  
 | 
            {  
 | 
                return false;  
 | 
            }  
 | 
            RedEnvelope _envelope = m_RedEnvelopeDict[_id];  
 | 
            var _OSRedModel = ModelCenter.Instance.GetModel<OSRedEnvelopeModel>();  
 | 
            var _OSRedType = _OSRedModel.OSRedEnvelopeType;  
 | 
            if (_envelope.type == _OSRedType)  
 | 
            {  
 | 
                if (TimeUtility.OpenDay >= _OSRedModel.openDays)  
 | 
                {  
 | 
                    if (_tip)  
 | 
                    {  
 | 
                        SysNotifyMgr.Instance.ShowTip("ActiveOutTime");  
 | 
                    }  
 | 
                    return false;  
 | 
                }  
 | 
                if (_OSRedModel.getEnvelopeTimes <= 0)  
 | 
                {  
 | 
                    if (_tip)  
 | 
                    {  
 | 
                        SysNotifyMgr.Instance.ShowTip("ActiveOutTime");  
 | 
                    }  
 | 
                    return false;  
 | 
                }  
 | 
            }  
 | 
            return true;  
 | 
        }  
 | 
  
 | 
        public int cacheEnvelopeId = 0;  
 | 
        public int envelopeSfx { get; set; }  
 | 
        public void SendOpenRedpackPackage(int _id,int getWay)  
 | 
        {  
 | 
            cacheEnvelopeId = _id;  
 | 
            CAB12_tagCMGrabFamilyRedPacket _pak = new CAB12_tagCMGrabFamilyRedPacket();  
 | 
            _pak.RedPaketID = (uint)_id;  
 | 
            _pak.GetWay = (byte)getWay;  
 | 
            GameNetSystem.Instance.SendInfo(_pak);  
 | 
        }  
 | 
  
 | 
  
 | 
        #region 红包雨特效  
 | 
        DateTime sfxRedpackTime = DateTime.Now;  
 | 
  
 | 
        private void OnSystemNotifyEvent(string _key, ArrayList list)  
 | 
        {  
 | 
            var config = SysInfoConfig.Get(_key);  
 | 
            if (config != null && string.IsNullOrEmpty(config.richText))  
 | 
            {  
 | 
                foreach (var key in redpackSfxDict.Keys)  
 | 
                {  
 | 
                    if (_key.Equals(redpackSfxDict[key]))  
 | 
                    {  
 | 
                        var effectId = 0;  
 | 
                        int.TryParse(config.effect, out effectId);  
 | 
                        PlayRedpackSfx(key, effectId);  
 | 
                        return;  
 | 
                    }  
 | 
                }  
 | 
            }  
 | 
            if (_key.Equals("OSRedpackSfx") && list.Count > 0)  
 | 
            {  
 | 
                var type = 0;  
 | 
                int.TryParse(list[0].ToString(), out type);  
 | 
                if (type != 0)  
 | 
                {  
 | 
                    switch (type)  
 | 
                    {  
 | 
                        case 5:  
 | 
                            CheckRedpackSfx(type);  
 | 
                            break;  
 | 
                        case FestivalRedpackModel.FESTIVALTASKREDPACKTYPE:  
 | 
                            CheckFestivalRedpackSfx(type);  
 | 
                            break;  
 | 
                    }  
 | 
                }  
 | 
            }  
 | 
        }  
 | 
  
 | 
        public void CheckRedpackSfx(int type)  
 | 
        {  
 | 
            if (!FuncOpen.Instance.IsFuncOpen((int)FuncOpenEnum.OpenServerRedEnvelope))  
 | 
            {  
 | 
                return;  
 | 
            }  
 | 
            if (envelopeModel.getEnvelopeTimes <= 0)  
 | 
            {  
 | 
                return;  
 | 
            }  
 | 
            if (!serverInited)  
 | 
            {  
 | 
                return;  
 | 
            }  
 | 
            if (!(StageLoad.Instance.currentStage is DungeonStage)  
 | 
                || WindowCenter.Instance.IsOpen<LoadingWin>())  
 | 
            {  
 | 
                return;  
 | 
            }  
 | 
            if (DateTime.Now >= sfxRedpackTime)  
 | 
            {  
 | 
                var _effect = EffectMgr.Instance.PlayUIEffect(5162, 4500, WindowCenter.Instance.uiRoot.tipsCanvas, false);  
 | 
                sfxRedpackTime = sfxRedpackTime.AddSeconds(_effect != null ? _effect.duration : 0);  
 | 
            }  
 | 
        }  
 | 
  
 | 
        public void PlayRedpackSfx(int type, int effectId)  
 | 
        {  
 | 
            if (!serverInited)  
 | 
            {  
 | 
                return;  
 | 
            }  
 | 
            festivalRedpackModel.JudgePlayRedpackVoice(type);  
 | 
            if (!(StageLoad.Instance.currentStage is DungeonStage)  
 | 
                || WindowCenter.Instance.IsOpen<LoadingWin>())  
 | 
            {  
 | 
                return;  
 | 
            }  
 | 
            if (DateTime.Now >= sfxRedpackTime)  
 | 
            {  
 | 
                var _effect = EffectMgr.Instance.PlayUIEffect(effectId, 4500, WindowCenter.Instance.uiRoot.tipsCanvas, false);  
 | 
                sfxRedpackTime = sfxRedpackTime.AddSeconds(2f);  
 | 
            }  
 | 
        }  
 | 
  
 | 
        public void CheckFestivalRedpackSfx(int type)  
 | 
        {  
 | 
            if (!festivalRedpackModel.IsOpen)  
 | 
            {  
 | 
                return;  
 | 
            }  
 | 
            if (festivalRedpackModel.grabRedpackTimes <= 0)  
 | 
            {  
 | 
                return;  
 | 
            }  
 | 
            if (!serverInited)  
 | 
            {  
 | 
                return;  
 | 
            }  
 | 
            if (!(StageLoad.Instance.currentStage is DungeonStage)  
 | 
                || WindowCenter.Instance.IsOpen<LoadingWin>())  
 | 
            {  
 | 
                return;  
 | 
            }  
 | 
            if (DateTime.Now >= sfxRedpackTime)  
 | 
            {  
 | 
                var _effect = EffectMgr.Instance.PlayUIEffect(5162, 4500, WindowCenter.Instance.uiRoot.tipsCanvas, false);  
 | 
                sfxRedpackTime = sfxRedpackTime.AddSeconds(2f);  
 | 
            }  
 | 
        }  
 | 
        #endregion  
 | 
  
 | 
        void ParseConfig()  
 | 
        {  
 | 
            var config = FuncConfigConfig.Get("OpenServerRedPacketRain");  
 | 
            var json = LitJson.JsonMapper.ToObject(config.Numerical2);  
 | 
            foreach (var key in json.Keys)  
 | 
            {  
 | 
                var type = int.Parse(key);  
 | 
                redpackSfxDict.Add(type, json[key].ToString());  
 | 
            }  
 | 
        }  
 | 
  
 | 
        public struct RedEnvelope  
 | 
        {  
 | 
            public int id;  
 | 
            public int player;  
 | 
            public string playerName;  
 | 
            public uint time;  
 | 
            public int job;  
 | 
            public int moneyType;  
 | 
            public int moneyNum;  
 | 
            public int type;  
 | 
            public int packCount;  
 | 
            public int state;        //0未发,1未领取,2已领取,3全部领完  
 | 
            public string Wish;        //祝福语  
 | 
        }  
 | 
    }  
 | 
}  
 | 
  
 |