|
- 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<string, object> _storage
- {
- get
- {
- return Storage.Get(StorageLifecycle.Game).GetDictionary("cig_iap_sale_notifications");
- }
- }
-
- private static Dictionary<string, object> _scheduledNotifications
- {
- get
- {
- if (!IAPPackSaleLocalNotifications._storage.ContainsKey("iap_notifications_scheduled"))
- {
- IAPPackSaleLocalNotifications._storage.Add("iap_notifications_scheduled", new Dictionary<string, object>());
- }
- return IAPPackSaleLocalNotifications._storage["iap_notifications_scheduled"] as Dictionary<string, object>;
- }
- }
-
- public static void Init()
- {
- foreach (KeyValuePair<string, object> keyValuePair in IAPPackSaleLocalNotifications._scheduledNotifications)
- {
- Dictionary<string, object> dictionary = keyValuePair.Value as Dictionary<string, object>;
- DateTime t = DateTime.FromBinary((long)dictionary["notificationTime"]);
- if (DateTime.UtcNow > t)
- {
- IAPPackSaleLocalNotifications._iapSaleNotificationsSeen.Add(keyValuePair.Key);
- }
- }
- IAPPackSaleLocalNotifications._scheduledNotifications.RemoveAll(delegate(KeyValuePair<string, object> kvp)
- {
- Dictionary<string, object> dictionary2 = kvp.Value as Dictionary<string, object>;
- DateTime t2 = DateTime.FromBinary((long)dictionary2["endOfSaleTime"]);
- return DateTime.UtcNow > t2;
- });
- }
-
- public static PlannedNotification GetNotification()
- {
- IAPPackDealManager instance = Singleton<IAPPackDealManager>.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<string, object>
- {
- {
- "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<string> _iapSaleNotificationsSeen = new List<string>();
-
- 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);
- }
|