using System; using System.Collections.Generic; using CIG.Translation; using SUISS.Core.Utilities; using SUISS.Storage; using SUISSEngine; public static class IAPPackSaleLocalNotifications { private static Dictionary _storage { get { return Storage.Get(StorageLifecycle.Game).GetDictionary("cig_iap_sale_notifications"); } } private static Dictionary _scheduledNotifications { get { if (!IAPPackSaleLocalNotifications._storage.ContainsKey("iap_notifications_scheduled")) { IAPPackSaleLocalNotifications._storage.Add("iap_notifications_scheduled", new Dictionary()); } return IAPPackSaleLocalNotifications._storage["iap_notifications_scheduled"] as Dictionary; } } public static void Init() { foreach (KeyValuePair keyValuePair in IAPPackSaleLocalNotifications._scheduledNotifications) { Dictionary dictionary = keyValuePair.Value as Dictionary; DateTime t = DateTime.FromBinary((long)dictionary["notificationTime"]); if (DateTime.UtcNow > t) { IAPPackSaleLocalNotifications._iapSaleNotificationsSeen.Add(keyValuePair.Key); } } IAPPackSaleLocalNotifications._scheduledNotifications.RemoveAll(delegate(KeyValuePair kvp) { Dictionary dictionary2 = kvp.Value as Dictionary; DateTime t2 = DateTime.FromBinary((long)dictionary2["endOfSaleTime"]); return DateTime.UtcNow > t2; }); } public static PlannedNotification GetNotification() { IAPPackDealManager instance = Singleton.Instance; if (instance.HasDeal) { IAPPackDealManager.PackDeal firstActiveDeal = instance.FirstActiveDeal; DateTime dateTime = firstActiveDeal.DealStart + firstActiveDeal.DealDuration; if (IAPPackSaleLocalNotifications._iapSaleNotificationsSeen.Contains(IAPPackSaleLocalNotifications.EncodeSale(instance.FirstActiveDeal.PackName, dateTime))) { return PlannedNotification.Empty; } TimeSpan timeLeft = dateTime - DateTime.UtcNow; TimeSpan? notificationTimeToSaleEnd = IAPPackSaleLocalNotifications.GetNotificationTimeToSaleEnd(timeLeft); if (notificationTimeToSaleEnd != null) { return IAPPackSaleLocalNotifications.ScheduleSaleNotification(instance.FirstActiveDeal.PackName, dateTime - notificationTimeToSaleEnd.Value, dateTime); } } return PlannedNotification.Empty; } private static TimeSpan? GetNotificationTimeToSaleEnd(TimeSpan timeLeft) { if (timeLeft < IAPPackSaleLocalNotifications.minimumSaleTimeForNotification + IAPPackSaleLocalNotifications.minimumTimeAfterClosingApp) { return null; } if (timeLeft < IAPPackSaleLocalNotifications.maximumTimeBeforeExpiration + IAPPackSaleLocalNotifications.minimumTimeAfterClosingApp) { return new TimeSpan?(timeLeft - IAPPackSaleLocalNotifications.minimumTimeAfterClosingApp); } return new TimeSpan?(IAPPackSaleLocalNotifications.maximumTimeBeforeExpiration); } private static string EncodeSale(string packName, DateTime endOfSaleTime) { return string.Format("{0}.endsAt:{1}", packName, endOfSaleTime.ToBinary()); } private static PlannedNotification ScheduleSaleNotification(string packName, DateTime notificationTime, DateTime endOfSaleTime) { TimeSpan t = endOfSaleTime - notificationTime; int seconds = t.Seconds; t -= TimeSpan.FromSeconds((double)seconds); ILocalizedString description = Localization.Format(Localization.Key("iap_sale_notification.before_end"), new ILocalizedString[] { Localization.Integer((int)Math.Round(t.TotalMinutes)) }); TimeSpan t2 = notificationTime - DateTime.UtcNow; t2 += TimeSpan.FromSeconds((double)seconds); IAPPackSaleLocalNotifications._scheduledNotifications[IAPPackSaleLocalNotifications.EncodeSale(packName, endOfSaleTime)] = new Dictionary { { "notificationTime", notificationTime.ToBinary() }, { "endOfSaleTime", endOfSaleTime.ToBinary() } }; return new PlannedNotification((int)Math.Round(t2.TotalSeconds), description, false); } private const string StorageKey = "cig_iap_sale_notifications"; private const string ScheduledNotificationsKey = "iap_notifications_scheduled"; private const string NotificationTimeKey = "notificationTime"; private const string EndOfSaleTimeKey = "endOfSaleTime"; private static List _iapSaleNotificationsSeen = new List(); private static readonly TimeSpan minimumSaleTimeForNotification = TimeSpan.FromMinutes(15.0); private static readonly TimeSpan minimumTimeAfterClosingApp = TimeSpan.FromMinutes(5.0); private static readonly TimeSpan maximumTimeBeforeExpiration = new TimeSpan(0, 30, 1); }