少年修仙传客户端代码仓库
hch
4 天以前 600733c8f592cb9e65f2b7a3e110ac1d686e6bfe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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 bool IsRegister(Component comp)
    {
        return timeItems.ContainsKey(comp);
    }
 
    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,
    }
}