Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

129 Zeilen
4.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using CIG.Translation;
  4. using SUISS.Core.Utilities;
  5. using SUISS.Storage;
  6. using SUISSEngine;
  7. public static class IAPPackSaleLocalNotifications
  8. {
  9. private static Dictionary<string, object> _storage
  10. {
  11. get
  12. {
  13. return Storage.Get(StorageLifecycle.Game).GetDictionary("cig_iap_sale_notifications");
  14. }
  15. }
  16. private static Dictionary<string, object> _scheduledNotifications
  17. {
  18. get
  19. {
  20. if (!IAPPackSaleLocalNotifications._storage.ContainsKey("iap_notifications_scheduled"))
  21. {
  22. IAPPackSaleLocalNotifications._storage.Add("iap_notifications_scheduled", new Dictionary<string, object>());
  23. }
  24. return IAPPackSaleLocalNotifications._storage["iap_notifications_scheduled"] as Dictionary<string, object>;
  25. }
  26. }
  27. public static void Init()
  28. {
  29. foreach (KeyValuePair<string, object> keyValuePair in IAPPackSaleLocalNotifications._scheduledNotifications)
  30. {
  31. Dictionary<string, object> dictionary = keyValuePair.Value as Dictionary<string, object>;
  32. DateTime t = DateTime.FromBinary((long)dictionary["notificationTime"]);
  33. if (DateTime.UtcNow > t)
  34. {
  35. IAPPackSaleLocalNotifications._iapSaleNotificationsSeen.Add(keyValuePair.Key);
  36. }
  37. }
  38. IAPPackSaleLocalNotifications._scheduledNotifications.RemoveAll(delegate(KeyValuePair<string, object> kvp)
  39. {
  40. Dictionary<string, object> dictionary2 = kvp.Value as Dictionary<string, object>;
  41. DateTime t2 = DateTime.FromBinary((long)dictionary2["endOfSaleTime"]);
  42. return DateTime.UtcNow > t2;
  43. });
  44. }
  45. public static PlannedNotification GetNotification()
  46. {
  47. IAPPackDealManager instance = Singleton<IAPPackDealManager>.Instance;
  48. if (instance.HasDeal)
  49. {
  50. IAPPackDealManager.PackDeal firstActiveDeal = instance.FirstActiveDeal;
  51. DateTime dateTime = firstActiveDeal.DealStart + firstActiveDeal.DealDuration;
  52. if (IAPPackSaleLocalNotifications._iapSaleNotificationsSeen.Contains(IAPPackSaleLocalNotifications.EncodeSale(instance.FirstActiveDeal.PackName, dateTime)))
  53. {
  54. return PlannedNotification.Empty;
  55. }
  56. TimeSpan timeLeft = dateTime - DateTime.UtcNow;
  57. TimeSpan? notificationTimeToSaleEnd = IAPPackSaleLocalNotifications.GetNotificationTimeToSaleEnd(timeLeft);
  58. if (notificationTimeToSaleEnd != null)
  59. {
  60. return IAPPackSaleLocalNotifications.ScheduleSaleNotification(instance.FirstActiveDeal.PackName, dateTime - notificationTimeToSaleEnd.Value, dateTime);
  61. }
  62. }
  63. return PlannedNotification.Empty;
  64. }
  65. private static TimeSpan? GetNotificationTimeToSaleEnd(TimeSpan timeLeft)
  66. {
  67. if (timeLeft < IAPPackSaleLocalNotifications.minimumSaleTimeForNotification + IAPPackSaleLocalNotifications.minimumTimeAfterClosingApp)
  68. {
  69. return null;
  70. }
  71. if (timeLeft < IAPPackSaleLocalNotifications.maximumTimeBeforeExpiration + IAPPackSaleLocalNotifications.minimumTimeAfterClosingApp)
  72. {
  73. return new TimeSpan?(timeLeft - IAPPackSaleLocalNotifications.minimumTimeAfterClosingApp);
  74. }
  75. return new TimeSpan?(IAPPackSaleLocalNotifications.maximumTimeBeforeExpiration);
  76. }
  77. private static string EncodeSale(string packName, DateTime endOfSaleTime)
  78. {
  79. return string.Format("{0}.endsAt:{1}", packName, endOfSaleTime.ToBinary());
  80. }
  81. private static PlannedNotification ScheduleSaleNotification(string packName, DateTime notificationTime, DateTime endOfSaleTime)
  82. {
  83. TimeSpan t = endOfSaleTime - notificationTime;
  84. int seconds = t.Seconds;
  85. t -= TimeSpan.FromSeconds((double)seconds);
  86. ILocalizedString description = Localization.Format(Localization.Key("iap_sale_notification.before_end"), new ILocalizedString[]
  87. {
  88. Localization.Integer((int)Math.Round(t.TotalMinutes))
  89. });
  90. TimeSpan t2 = notificationTime - DateTime.UtcNow;
  91. t2 += TimeSpan.FromSeconds((double)seconds);
  92. IAPPackSaleLocalNotifications._scheduledNotifications[IAPPackSaleLocalNotifications.EncodeSale(packName, endOfSaleTime)] = new Dictionary<string, object>
  93. {
  94. {
  95. "notificationTime",
  96. notificationTime.ToBinary()
  97. },
  98. {
  99. "endOfSaleTime",
  100. endOfSaleTime.ToBinary()
  101. }
  102. };
  103. return new PlannedNotification((int)Math.Round(t2.TotalSeconds), description, false);
  104. }
  105. private const string StorageKey = "cig_iap_sale_notifications";
  106. private const string ScheduledNotificationsKey = "iap_notifications_scheduled";
  107. private const string NotificationTimeKey = "notificationTime";
  108. private const string EndOfSaleTimeKey = "endOfSaleTime";
  109. private static List<string> _iapSaleNotificationsSeen = new List<string>();
  110. private static readonly TimeSpan minimumSaleTimeForNotification = TimeSpan.FromMinutes(15.0);
  111. private static readonly TimeSpan minimumTimeAfterClosingApp = TimeSpan.FromMinutes(5.0);
  112. private static readonly TimeSpan maximumTimeBeforeExpiration = new TimeSpan(0, 30, 1);
  113. }