Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

266 рядки
5.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using SUISS.Core;
  5. using SUISS.Storage;
  6. public sealed class CIGSaleManager : SingletonMonobehaviour<CIGSaleManager>
  7. {
  8. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  9. public event CIGSaleManager.SaleChangedHandler SaleChanged;
  10. protected override void Awake()
  11. {
  12. base.Awake();
  13. this.state = Storage.Get(StorageLifecycle.Game).GetDictionary("SaleManager");
  14. object obj;
  15. object obj2;
  16. if (this.state.TryGetValue("WebserviceSaleExpiration", out obj) && (long)obj > DateTime.UtcNow.Ticks && this.state.TryGetValue("WebserviceSaletype", out obj2))
  17. {
  18. this.webserviceSale = new CIGSaleManager.Sale(new DateTime((long)obj, DateTimeKind.Utc), new string[]
  19. {
  20. (string)obj2
  21. });
  22. }
  23. else
  24. {
  25. this.webserviceSale = new CIGSaleManager.Sale();
  26. }
  27. this.currentSale = this.webserviceSale;
  28. }
  29. private void Start()
  30. {
  31. SingletonMonobehaviour<CIGWebService>.Instance.PullRequestCompleted += this.PullRequestCompleted;
  32. }
  33. public CIGSaleManager.Sale CurrentSale
  34. {
  35. get
  36. {
  37. return this.currentSale;
  38. }
  39. }
  40. private void Update()
  41. {
  42. DateTime utcNow = DateTime.UtcNow;
  43. if (utcNow.Ticks >= this.currentSale.Expiration.Ticks)
  44. {
  45. if (utcNow.Ticks >= this.webserviceSale.Expiration.Ticks)
  46. {
  47. this.webserviceSale = new CIGSaleManager.Sale();
  48. }
  49. CIGSaleManager.Sale oldSale = this.currentSale;
  50. this.currentSale = this.webserviceSale;
  51. this.OnSaleChanged(oldSale, this.currentSale);
  52. }
  53. }
  54. private void PullRequestCompleted()
  55. {
  56. CIGWebService instance = SingletonMonobehaviour<CIGWebService>.Instance;
  57. string sale = instance.Sale;
  58. DateTime saleExpiry = instance.SaleExpiry;
  59. this.state["WebserviceSaletype"] = sale;
  60. this.state["WebserviceSaleExpiration"] = saleExpiry.Ticks;
  61. this.webserviceSale = new CIGSaleManager.Sale(saleExpiry, new string[]
  62. {
  63. sale
  64. });
  65. CIGSaleManager.Sale oldSale = this.currentSale;
  66. this.currentSale = this.webserviceSale;
  67. this.OnSaleChanged(oldSale, this.currentSale);
  68. }
  69. private void OnSaleChanged(CIGSaleManager.Sale oldSale, CIGSaleManager.Sale newSale)
  70. {
  71. if (this.SaleChanged != null)
  72. {
  73. this.SaleChanged(oldSale, newSale);
  74. }
  75. }
  76. private const string WebserviceSaleExpirationKey = "WebserviceSaleExpiration";
  77. private const string WebserviceSaletypeKey = "WebserviceSaletype";
  78. private Dictionary<string, object> state;
  79. private CIGSaleManager.Sale webserviceSale;
  80. private CIGSaleManager.Sale currentSale;
  81. public delegate void SaleChangedHandler(CIGSaleManager.Sale oldSale, CIGSaleManager.Sale newSale);
  82. public class Sale
  83. {
  84. public Sale() : this(DateTime.MaxValue, new string[0])
  85. {
  86. }
  87. public Sale(params string[] saleTypes) : this(DateTime.MaxValue, saleTypes)
  88. {
  89. }
  90. public Sale(DateTime expiration, params string[] saleTypes)
  91. {
  92. this.expiration = expiration;
  93. if (saleTypes == null || saleTypes.Length == 0)
  94. {
  95. this.saleTypes = new string[0];
  96. }
  97. else
  98. {
  99. List<string> list = new List<string>();
  100. foreach (string text in saleTypes)
  101. {
  102. if (text != null)
  103. {
  104. list.AddRange(text.Split(new char[]
  105. {
  106. ','
  107. }));
  108. }
  109. }
  110. this.saleTypes = list.ToArray();
  111. }
  112. this.cash = false;
  113. this.gold = false;
  114. this.offerwall = false;
  115. this.expansions = false;
  116. this.items = false;
  117. foreach (string text2 in this.saleTypes)
  118. {
  119. if (text2.Equals("cash"))
  120. {
  121. this.cash = true;
  122. }
  123. else if (text2.Equals("gold"))
  124. {
  125. this.gold = true;
  126. }
  127. else if (text2.Equals("offerwall"))
  128. {
  129. this.offerwall = true;
  130. }
  131. else if (text2.Equals("expansions"))
  132. {
  133. this.expansions = true;
  134. }
  135. else if (text2.StartsWith("unit."))
  136. {
  137. this.items = true;
  138. }
  139. }
  140. }
  141. public DateTime Expiration
  142. {
  143. get
  144. {
  145. return this.expiration;
  146. }
  147. }
  148. public TimeSpan SaleTimeLeft
  149. {
  150. get
  151. {
  152. DateTime utcNow = DateTime.UtcNow;
  153. if (this.expiration < utcNow)
  154. {
  155. return TimeSpan.Zero;
  156. }
  157. return this.expiration - utcNow;
  158. }
  159. }
  160. public bool Cash
  161. {
  162. get
  163. {
  164. return this.cash;
  165. }
  166. }
  167. public bool Gold
  168. {
  169. get
  170. {
  171. return this.gold;
  172. }
  173. }
  174. public bool Offerwall
  175. {
  176. get
  177. {
  178. return this.offerwall;
  179. }
  180. }
  181. public bool Expansions
  182. {
  183. get
  184. {
  185. return this.expansions;
  186. }
  187. }
  188. public bool Items
  189. {
  190. get
  191. {
  192. return this.items;
  193. }
  194. }
  195. public bool HasSale
  196. {
  197. get
  198. {
  199. return this.Cash || this.Gold || this.Offerwall || this.Expansions;
  200. }
  201. }
  202. public override string ToString()
  203. {
  204. string text = string.Empty;
  205. foreach (string str in this.saleTypes)
  206. {
  207. if (text.Length > 0)
  208. {
  209. text += ",";
  210. }
  211. text += str;
  212. }
  213. return string.Format("[Sale: {0} (Expires in: {1})]", (text.Length != 0) ? text : "empty", (!(this.expiration == DateTime.MaxValue)) ? TimeSpan.FromSeconds(this.expiration.Subtract(DateTime.UtcNow).TotalSeconds).ToString() : "never");
  214. }
  215. public const string CashSaleType = "cash";
  216. public const string GoldSaleType = "gold";
  217. public const string OfferwallSaleType = "offerwall";
  218. public const string ExpansionsSaleType = "expansions";
  219. public const string ItemSalePrefix = "unit.";
  220. private string[] saleTypes;
  221. private DateTime expiration;
  222. private bool cash;
  223. private bool gold;
  224. private bool offerwall;
  225. private bool expansions;
  226. private bool items;
  227. }
  228. }