您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 

204 行
4.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using SUISS.Core;
  6. using SUISS.Core.Utilities;
  7. using SUISS.Storage;
  8. namespace SUISSEngine
  9. {
  10. public sealed class Daily : Singleton<Daily>
  11. {
  12. public Daily()
  13. {
  14. this._calendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
  15. this.CheckCurrentDay();
  16. if (SingletonMonobehaviour<TimerManager>.IsAvailable)
  17. {
  18. SingletonMonobehaviour<TimerManager>.Instance.CreateClockTimer(DateTimeKind.Local, new TimeSpan(3, 0, 0), new TimeSpan(1, 0, 0, 0), delegate()
  19. {
  20. this.CheckCurrentDay();
  21. });
  22. }
  23. Storage.NewGameEvent += this.OnNewGame;
  24. }
  25. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  26. public event Daily.CurrentDayChangedEventHandler CurrentDayChanged;
  27. public Daily.Day CurrentDay
  28. {
  29. get
  30. {
  31. return this._currentDay;
  32. }
  33. }
  34. private void FireCurrentDayChangedEvent(Daily.Day previousDay, Daily.Day newDay)
  35. {
  36. if (this.CurrentDayChanged != null)
  37. {
  38. this.CurrentDayChanged(previousDay, newDay);
  39. }
  40. }
  41. private void CheckCurrentDay()
  42. {
  43. DateTime time = DateTime.Now;
  44. if (this._calendar.GetHour(time) < 3)
  45. {
  46. time = this._calendar.AddHours(time, -3);
  47. }
  48. Daily.Day day = new Daily.Day(this._calendar.GetYear(time), this._calendar.GetMonth(time), this._calendar.GetDayOfMonth(time));
  49. Daily.Day day2 = (Daily.Day)Storage.Get(StorageLifecycle.Game).Root.Get("Daily.PreviousDay", null);
  50. if (day2 != null && day.CompareTo(day2) < 0)
  51. {
  52. day = day2;
  53. }
  54. if (!object.Equals(day, this._currentDay))
  55. {
  56. Daily.Day currentDay = this._currentDay;
  57. this._currentDay = day;
  58. Storage.Get(StorageLifecycle.Game).Root["Daily.PreviousDay"] = this._currentDay;
  59. this.FireCurrentDayChangedEvent(currentDay, this._currentDay);
  60. }
  61. }
  62. private void OnNewGame()
  63. {
  64. this.CheckCurrentDay();
  65. }
  66. public const int NewDayHourOffset = 3;
  67. private const string PreviousDayStorageKey = "Daily.PreviousDay";
  68. private Calendar _calendar;
  69. private Daily.Day _currentDay;
  70. public delegate void CurrentDayChangedEventHandler(Daily.Day previousDay, Daily.Day newDay);
  71. public class Day : IStorable, IComparable<Daily.Day>
  72. {
  73. public Day()
  74. {
  75. this._year = 1970;
  76. this._month = 1;
  77. this._dayOfMonth = 1;
  78. }
  79. public Day(int year, int month, int dayOfMonth)
  80. {
  81. this._year = year;
  82. this._month = month;
  83. this._dayOfMonth = dayOfMonth;
  84. }
  85. void IStorable.FromStorage(IDictionary<string, object> dict)
  86. {
  87. this._year = dict.GetInt("Year", 1970);
  88. this._month = dict.GetInt("Month", 1);
  89. this._dayOfMonth = dict.GetInt("DayOfMonth", 1);
  90. }
  91. IDictionary<string, object> IStorable.ToStorage()
  92. {
  93. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  94. dictionary["Year"] = this._year;
  95. dictionary["Month"] = this._month;
  96. dictionary["DayOfMonth"] = this._dayOfMonth;
  97. return dictionary;
  98. }
  99. public int Year
  100. {
  101. get
  102. {
  103. return this._year;
  104. }
  105. }
  106. public int Month
  107. {
  108. get
  109. {
  110. return this._month;
  111. }
  112. }
  113. public int DayOfMonth
  114. {
  115. get
  116. {
  117. return this._dayOfMonth;
  118. }
  119. }
  120. public DateTime Date
  121. {
  122. get
  123. {
  124. return DateTimeExtensionsWS.NewDateTime(this._year, this._month, this._dayOfMonth, 0, 0, 0, 0, Singleton<Daily>.Instance._calendar, DateTimeKind.Local);
  125. }
  126. }
  127. public DateTime UtcDate
  128. {
  129. get
  130. {
  131. return DateTimeExtensionsWS.NewDateTime(this._year, this._month, this._dayOfMonth, 0, 0, 0, 0, Singleton<Daily>.Instance._calendar, DateTimeKind.Utc);
  132. }
  133. }
  134. public int CalculateTimeRemaining()
  135. {
  136. DateTime dateTime = this.Date.Add(new TimeSpan(1, 3, 0, 0));
  137. DateTime now = DateTime.Now;
  138. return (int)Math.Ceiling(dateTime.Subtract(now).TotalSeconds);
  139. }
  140. public int CompareTo(Daily.Day other)
  141. {
  142. int num = this._year - other._year;
  143. if (num == 0)
  144. {
  145. num = this._month - other._month;
  146. if (num == 0)
  147. {
  148. num = this._dayOfMonth - other._dayOfMonth;
  149. }
  150. }
  151. return num;
  152. }
  153. public override string ToString()
  154. {
  155. return string.Format(CultureInfo.InvariantCulture, "{0:D4}-{1:D2}-{2:D2}", new object[]
  156. {
  157. this._year,
  158. this._month,
  159. this._dayOfMonth
  160. });
  161. }
  162. public override int GetHashCode()
  163. {
  164. return this._year * 10000 + this._month * 100 + this._dayOfMonth;
  165. }
  166. public override bool Equals(object obj)
  167. {
  168. return obj is Daily.Day && ((Daily.Day)obj)._year == this._year && ((Daily.Day)obj)._month == this._month && ((Daily.Day)obj)._dayOfMonth == this._dayOfMonth;
  169. }
  170. private int _year;
  171. private int _month;
  172. private int _dayOfMonth;
  173. }
  174. }
  175. }