using System;  
 | 
using System.Collections.Generic;  
 | 
using UnityEngine;  
 | 
  
 | 
  
 | 
public class TimeMgr : SingletonMonobehaviour<TimeMgr>  
 | 
  
 | 
{  
 | 
    private Dictionary<Component, TimeItem> timeItems = new Dictionary<Component, TimeItem>();  
 | 
    private List<TimeItem> timeItemList = new List<TimeItem>();  
 | 
    private int hourBuff = -1;  
 | 
    private int minuteBuff = -1;  
 | 
    public int dayBuff = -1;  
 | 
    public int monthBuff { get { return LocalSave.GetInt("month"); } private set { LocalSave.SetInt("month", value); } }  
 | 
    public int weekBuff { get { return LocalSave.GetInt("week"); } private set { LocalSave.SetInt("week", value); } }  
 | 
    public event Action OnDayEvent;  
 | 
    public event Action OnHourEvent;  
 | 
    public event Action OnMinuteEvent;  
 | 
    public event Action OnMonthAfterPlayerDataInitializeEvent;  
 | 
    public event Action OnWeekAfterPlayerDataInitializeEvent;  
 | 
  
 | 
    private void LateUpdate()  
 | 
    {  
 | 
        try  
 | 
        {  
 | 
            if (weekBuff != GetWeekOfYear(TimeUtility.ServerNow))  
 | 
            {  
 | 
                if (OnWeekAfterPlayerDataInitializeEvent != null && DTC0102_tagCDBPlayer.isAfterPlayerDataInitialize)  
 | 
                {  
 | 
  
 | 
                    OnWeekAfterPlayerDataInitializeEvent();  
 | 
                    weekBuff = GetWeekOfYear(TimeUtility.ServerNow);  
 | 
                }  
 | 
            }  
 | 
            if (monthBuff != TimeUtility.ServerNow.Month)  
 | 
            {  
 | 
                if (OnMonthAfterPlayerDataInitializeEvent != null && DTC0102_tagCDBPlayer.isAfterPlayerDataInitialize)  
 | 
                {  
 | 
                    OnMonthAfterPlayerDataInitializeEvent();  
 | 
                    monthBuff = TimeUtility.ServerNow.Month;  
 | 
                }  
 | 
            }  
 | 
            if (dayBuff != TimeUtility.ServerNow.Day)  
 | 
            {  
 | 
                if (OnDayEvent != null)  
 | 
                {  
 | 
                    OnDayEvent();  
 | 
                }  
 | 
                dayBuff = TimeUtility.ServerNow.Day;  
 | 
            }  
 | 
            if (hourBuff != TimeUtility.ServerNow.Hour)  
 | 
            {  
 | 
                if (OnHourEvent != null)  
 | 
                {  
 | 
                    OnHourEvent();  
 | 
                }  
 | 
                hourBuff = TimeUtility.ServerNow.Hour;  
 | 
            }  
 | 
            if (minuteBuff != TimeUtility.ServerNow.Minute)  
 | 
            {  
 | 
                if (OnMinuteEvent != null)  
 | 
                {  
 | 
                    OnMinuteEvent();  
 | 
                }  
 | 
                minuteBuff = TimeUtility.ServerNow.Minute;  
 | 
            }  
 | 
        }  
 | 
        catch (Exception e)  
 | 
        {  
 | 
            DebugEx.Log(e.StackTrace);  
 | 
        }  
 | 
        for (int i = 0; i < syntonyList.Count; i++)  
 | 
        {  
 | 
            if ((TimeUtility.ServerNow - syntonyList[i].endTime).TotalSeconds > 0)  
 | 
            {  
 | 
                if (syntonyList[i].callback != null)  
 | 
                {  
 | 
                    syntonyList[i].callback();  
 | 
                }  
 | 
                var _type = syntonyList[i].type;  
 | 
                syntonyList.RemoveAt(i);  
 | 
                if (OnSyntonyEvent != null)  
 | 
                {  
 | 
                    OnSyntonyEvent(_type);  
 | 
                }  
 | 
                i--;  
 | 
            }  
 | 
        }  
 | 
        if (timeItems.Count > 0)  
 | 
        {  
 | 
            timeItemList.RemoveRange(0, timeItemList.Count);  
 | 
            foreach (Component item in timeItems.Keys)  
 | 
            {  
 | 
                if (item == null) continue;  
 | 
                timeItemList.Add(timeItems[item]);  
 | 
            }  
 | 
            for (int i = 0; i < timeItemList.Count; i++)  
 | 
            {  
 | 
                timeItemList[i].Update();  
 | 
            }  
 | 
        }  
 | 
    }  
 | 
  
 | 
    //获取当前datatime是今年的第几周  
 | 
    int GetWeekOfYear(DateTime date)  
 | 
    {  
 | 
        // 获取该年第一天  
 | 
        DateTime firstDayOfYear = new DateTime(date.Year, 1, 1);  
 | 
  
 | 
        // 计算第一周的开始日期(如果1月1日不是周一,则向前推到最近的周一)  
 | 
        int daysOffset = DayOfWeek.Thursday - firstDayOfYear.DayOfWeek;  
 | 
  
 | 
        // 如果是周五、周六、周日,我们需要向后推  
 | 
        if (daysOffset < 0)  
 | 
            daysOffset += 7;  
 | 
  
 | 
        // 计算该年第一个周四的日期  
 | 
        DateTime firstThursday = firstDayOfYear.AddDays(daysOffset);  
 | 
  
 | 
        // 计算给定日期与第一个周四之间的天数差  
 | 
        int daysSinceFirstThursday = (date - firstThursday).Days;  
 | 
  
 | 
        // 计算周数  
 | 
        int weekNumber = (daysSinceFirstThursday / 7) + 1;  
 | 
  
 | 
        // 处理年初和年末的特殊情况  
 | 
        if (weekNumber < 1)  
 | 
        {  
 | 
            // 如果周数小于1,说明是上一年的最后一周  
 | 
            return GetWeekOfYear(new DateTime(date.Year - 1, 12, 31));  
 | 
        }  
 | 
        else if (weekNumber > 52)  
 | 
        {  
 | 
            // 计算下一年的第一个周四  
 | 
            DateTime nextYearFirstDay = new DateTime(date.Year + 1, 1, 1);  
 | 
            int nextYearDaysOffset = DayOfWeek.Thursday - nextYearFirstDay.DayOfWeek;  
 | 
            if (nextYearDaysOffset < 0)  
 | 
                nextYearDaysOffset += 7;  
 | 
            DateTime nextYearFirstThursday = nextYearFirstDay.AddDays(nextYearDaysOffset);  
 | 
  
 | 
            // 如果给定日期在下一年的第一个周四之前,那么它属于本年的最后一周  
 | 
            if (date < nextYearFirstThursday)  
 | 
            {  
 | 
                return weekNumber;  
 | 
            }  
 | 
            else  
 | 
            {  
 | 
                // 否则,这是下一年的第一周  
 | 
                return 1;  
 | 
            }  
 | 
        }  
 | 
  
 | 
        return weekNumber;  
 | 
    }  
 | 
  
 | 
    public void Register(Component comp, Action<Component> func, float t, int loopCnt = 1)  
 | 
    {  
 | 
        if (comp == null) return;  
 | 
        if (!timeItems.ContainsKey(comp))  
 | 
        {  
 | 
            TimeItem item = new TimeItem(t, comp, loopCnt, func);  
 | 
            timeItems.Add(comp, item);  
 | 
        }  
 | 
    }  
 | 
  
 | 
    public void SetInterval(Component comp, float t)  
 | 
    {  
 | 
        if (timeItems.ContainsKey(comp))  
 | 
        {  
 | 
            TimeItem item = timeItems[comp];  
 | 
            item.time = t;  
 | 
        }  
 | 
    }  
 | 
  
 | 
    public void UnRegister(Component comp)  
 | 
    {  
 | 
        if (comp == null) return;  
 | 
        if (timeItems.ContainsKey(comp))  
 | 
        {  
 | 
            TimeItem item = timeItems[comp];  
 | 
            timeItems.Remove(comp);  
 | 
            item = null;  
 | 
        }  
 | 
    }  
 | 
  
 | 
    public void Register(SyntonyType type, float _totalTime, Action _func = null)  
 | 
    {  
 | 
        int index;  
 | 
        if (ContainsSyntony(type, out index))  
 | 
        {  
 | 
            syntonyList[index].SetTime(_totalTime, _func);  
 | 
        }  
 | 
        else  
 | 
        {  
 | 
            Syntony syntony = new Syntony();  
 | 
            syntony.type = type;  
 | 
            syntony.SetTime(_totalTime, _func);  
 | 
            syntonyList.Add(syntony);  
 | 
        }  
 | 
    }  
 | 
  
 | 
    public void UnRegister(SyntonyType type)  
 | 
    {  
 | 
        int index;  
 | 
        if (ContainsSyntony(type, out index))  
 | 
        {  
 | 
            syntonyList.RemoveAt(index);  
 | 
        }  
 | 
    }  
 | 
  
 | 
    public struct Syntony  
 | 
    {  
 | 
        public SyntonyType type;  
 | 
        public DateTime endTime { get; private set; }  
 | 
        public Action callback;  
 | 
        public void SetTime(float _totalTime, Action _func = null)  
 | 
        {  
 | 
            endTime = TimeUtility.ServerNow.AddSeconds(_totalTime);  
 | 
            callback = _func;  
 | 
            DebugEx.LogFormat("{0}{1}", type, endTime);  
 | 
        }  
 | 
    }  
 | 
  
 | 
    private List<Syntony> syntonyList = new List<Syntony>();  
 | 
  
 | 
    public event Action<SyntonyType> OnSyntonyEvent;  
 | 
  
 | 
    private bool ContainsSyntony(SyntonyType type, out int index)  
 | 
    {  
 | 
        index = 0;  
 | 
        for (int i = 0; i < syntonyList.Count; i++)  
 | 
        {  
 | 
            if (syntonyList[i].type == type)  
 | 
            {  
 | 
                index = i;  
 | 
                return true;  
 | 
            }  
 | 
        }  
 | 
        return false;  
 | 
    }  
 | 
  
 | 
    public enum SyntonyType  
 | 
    {  
 | 
        PrayerRedpoint,  
 | 
        BossShow,  
 | 
        BossShowName,  
 | 
        BossShowDialogue,  
 | 
        BossShowSound,  
 | 
        FairyLeaugeStage,  
 | 
        VipExperirnceOverdue,  
 | 
        HeavenBattleState,  
 | 
        Realm,  
 | 
        RealmSit,  
 | 
        GuardHurtEffect,  
 | 
        GetFairyInfo,  
 | 
        Audio,  
 | 
        OSRedEnvelope,  
 | 
        AutoPlayVoice,  
 | 
    }  
 | 
}  
 |