using System; using System.Collections.Generic; using System.Diagnostics; using SUISS.Core; using SUISS.Storage; public sealed class CIGSaleManager : SingletonMonobehaviour { //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event CIGSaleManager.SaleChangedHandler SaleChanged; protected override void Awake() { base.Awake(); this.state = Storage.Get(StorageLifecycle.Game).GetDictionary("SaleManager"); object obj; object obj2; if (this.state.TryGetValue("WebserviceSaleExpiration", out obj) && (long)obj > DateTime.UtcNow.Ticks && this.state.TryGetValue("WebserviceSaletype", out obj2)) { this.webserviceSale = new CIGSaleManager.Sale(new DateTime((long)obj, DateTimeKind.Utc), new string[] { (string)obj2 }); } else { this.webserviceSale = new CIGSaleManager.Sale(); } this.currentSale = this.webserviceSale; } private void Start() { SingletonMonobehaviour.Instance.PullRequestCompleted += this.PullRequestCompleted; } public CIGSaleManager.Sale CurrentSale { get { return this.currentSale; } } private void Update() { DateTime utcNow = DateTime.UtcNow; if (utcNow.Ticks >= this.currentSale.Expiration.Ticks) { if (utcNow.Ticks >= this.webserviceSale.Expiration.Ticks) { this.webserviceSale = new CIGSaleManager.Sale(); } CIGSaleManager.Sale oldSale = this.currentSale; this.currentSale = this.webserviceSale; this.OnSaleChanged(oldSale, this.currentSale); } } private void PullRequestCompleted() { CIGWebService instance = SingletonMonobehaviour.Instance; string sale = instance.Sale; DateTime saleExpiry = instance.SaleExpiry; this.state["WebserviceSaletype"] = sale; this.state["WebserviceSaleExpiration"] = saleExpiry.Ticks; this.webserviceSale = new CIGSaleManager.Sale(saleExpiry, new string[] { sale }); CIGSaleManager.Sale oldSale = this.currentSale; this.currentSale = this.webserviceSale; this.OnSaleChanged(oldSale, this.currentSale); } private void OnSaleChanged(CIGSaleManager.Sale oldSale, CIGSaleManager.Sale newSale) { if (this.SaleChanged != null) { this.SaleChanged(oldSale, newSale); } } private const string WebserviceSaleExpirationKey = "WebserviceSaleExpiration"; private const string WebserviceSaletypeKey = "WebserviceSaletype"; private Dictionary state; private CIGSaleManager.Sale webserviceSale; private CIGSaleManager.Sale currentSale; public delegate void SaleChangedHandler(CIGSaleManager.Sale oldSale, CIGSaleManager.Sale newSale); public class Sale { public Sale() : this(DateTime.MaxValue, new string[0]) { } public Sale(params string[] saleTypes) : this(DateTime.MaxValue, saleTypes) { } public Sale(DateTime expiration, params string[] saleTypes) { this.expiration = expiration; if (saleTypes == null || saleTypes.Length == 0) { this.saleTypes = new string[0]; } else { List list = new List(); foreach (string text in saleTypes) { if (text != null) { list.AddRange(text.Split(new char[] { ',' })); } } this.saleTypes = list.ToArray(); } this.cash = false; this.gold = false; this.offerwall = false; this.expansions = false; this.items = false; foreach (string text2 in this.saleTypes) { if (text2.Equals("cash")) { this.cash = true; } else if (text2.Equals("gold")) { this.gold = true; } else if (text2.Equals("offerwall")) { this.offerwall = true; } else if (text2.Equals("expansions")) { this.expansions = true; } else if (text2.StartsWith("unit.")) { this.items = true; } } } public DateTime Expiration { get { return this.expiration; } } public TimeSpan SaleTimeLeft { get { DateTime utcNow = DateTime.UtcNow; if (this.expiration < utcNow) { return TimeSpan.Zero; } return this.expiration - utcNow; } } public bool Cash { get { return this.cash; } } public bool Gold { get { return this.gold; } } public bool Offerwall { get { return this.offerwall; } } public bool Expansions { get { return this.expansions; } } public bool Items { get { return this.items; } } public bool HasSale { get { return this.Cash || this.Gold || this.Offerwall || this.Expansions; } } public override string ToString() { string text = string.Empty; foreach (string str in this.saleTypes) { if (text.Length > 0) { text += ","; } text += str; } 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"); } public const string CashSaleType = "cash"; public const string GoldSaleType = "gold"; public const string OfferwallSaleType = "offerwall"; public const string ExpansionsSaleType = "expansions"; public const string ItemSalePrefix = "unit."; private string[] saleTypes; private DateTime expiration; private bool cash; private bool gold; private bool offerwall; private bool expansions; private bool items; } }