You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

236 lines
6.2 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using SUISS.Core;
  5. using SUISS.Storage;
  6. using UnityEngine;
  7. namespace SUISSEngine
  8. {
  9. public abstract class RatingRequestManager : IRatingRequestManager
  10. {
  11. public RatingRequestManager(MonoBehaviour anyMonoBehaviour, INativeBindings nativeBindings, Messenger messenger, string appID, int buildVersion)
  12. {
  13. this.InitializeInternalVariables();
  14. this._anyMB = anyMonoBehaviour;
  15. this._nativeBindings = nativeBindings;
  16. this._appID = appID;
  17. this._buildVersion = buildVersion;
  18. this._messenger = messenger;
  19. }
  20. private DateTime LastShown
  21. {
  22. get
  23. {
  24. Dictionary<string, object> root = Storage.Get(StorageLifecycle.Player).Root;
  25. if (root.ContainsKey("LastShown_persistentkey"))
  26. {
  27. string s = (string)root["LastShown_persistentkey"];
  28. return DateTime.Parse(s);
  29. }
  30. return new DateTime(0L);
  31. }
  32. set
  33. {
  34. Storage.Get(StorageLifecycle.Player).Root["LastShown_persistentkey"] = value.ToString();
  35. }
  36. }
  37. private bool MayShowReviewPopup
  38. {
  39. get
  40. {
  41. return this.UserInterestingForReview && !this.ReviewPopupHasBeenShownLately && this.UserSessionLongEnough && !this.UserHasReviewed;
  42. }
  43. }
  44. private bool UserSessionLongEnough
  45. {
  46. get
  47. {
  48. return Time.realtimeSinceStartup >= 60f;
  49. }
  50. }
  51. private bool ReviewPopupHasBeenShownLately
  52. {
  53. get
  54. {
  55. return DateTime.UtcNow < this.LastShown + TimeSpan.FromDays(7.0);
  56. }
  57. }
  58. public bool UserInterestingForReview
  59. {
  60. get
  61. {
  62. Dictionary<string, object> root = Storage.Get(StorageLifecycle.Player).Root;
  63. return root.ContainsKey("UserInterestingForReview_persistentkey") && (bool)root["UserInterestingForReview_persistentkey"];
  64. }
  65. set
  66. {
  67. Storage.Get(StorageLifecycle.Player).Root["UserInterestingForReview_persistentkey"] = value;
  68. }
  69. }
  70. private bool NewAdjustEventAfterLastReviewRequest
  71. {
  72. get
  73. {
  74. Dictionary<string, object> root = Storage.Get(StorageLifecycle.Player).Root;
  75. return root.ContainsKey("NewAdjustEventAfterLastReviewRequestKey_persistentkey") && (bool)root["NewAdjustEventAfterLastReviewRequestKey_persistentkey"];
  76. }
  77. set
  78. {
  79. Storage.Get(StorageLifecycle.Player).Root["NewAdjustEventAfterLastReviewRequestKey_persistentkey"] = value;
  80. }
  81. }
  82. private int LastVersionReviewed
  83. {
  84. get
  85. {
  86. Dictionary<string, object> root = Storage.Get(StorageLifecycle.Player).Root;
  87. if (root.ContainsKey("LastVersionReviewed_persistentkey"))
  88. {
  89. return (int)root["LastVersionReviewed_persistentkey"];
  90. }
  91. return 0;
  92. }
  93. set
  94. {
  95. Storage.Get(StorageLifecycle.Player).Root["LastVersionReviewed_persistentkey"] = value;
  96. }
  97. }
  98. public bool UserHasReviewed
  99. {
  100. get
  101. {
  102. return this.LastVersionReviewed != 0;
  103. }
  104. }
  105. protected abstract bool IsRatingRequestPopupConvenient { get; }
  106. private void InitializeInternalVariables()
  107. {
  108. this._getRatedCoroutine = null;
  109. EdwinServerEventHandler.RecievedNewNonRegisteredEventToken += this.HandleRecievedNewEventToken;
  110. }
  111. ~RatingRequestManager()
  112. {
  113. EdwinServerEventHandler.RecievedNewNonRegisteredEventToken -= this.HandleRecievedNewEventToken;
  114. }
  115. public void NotifyInterestingEvent()
  116. {
  117. if (this._getRatedCoroutine == null && this.MayShowReviewPopup)
  118. {
  119. this._getRatedCoroutine = this.TryGetRatedCoroutine();
  120. if (this._anyMB != null)
  121. {
  122. this._anyMB.StartCoroutine(this._getRatedCoroutine);
  123. }
  124. else
  125. {
  126. UnityEngine.Debug.LogError("MonoBehaviour has been destroyed, cannot start RatedCoroutine");
  127. }
  128. }
  129. }
  130. protected abstract void ShowRatingRequestPopup(Action<bool> answerCallback);
  131. private IEnumerator TryGetRatedCoroutine()
  132. {
  133. int startDelay = UnityEngine.Random.Range(2, 8);
  134. yield return new WaitForSeconds((float)startDelay);
  135. UnityEngine.Debug.Log("Trying to show get rated popup.");
  136. int count = 30;
  137. while (count > 0)
  138. {
  139. if (this.IsRatingRequestPopupConvenient)
  140. {
  141. this.ShowRatingRequestPopupWrapper(new Action<bool>(this.HandleRatingRequestAnswer));
  142. this._getRatedCoroutine = null;
  143. yield break;
  144. }
  145. count--;
  146. yield return new WaitForSeconds(1f);
  147. }
  148. this._getRatedCoroutine = null;
  149. yield break;
  150. }
  151. private void HandleRatingRequestAnswer(bool userWantsToRate)
  152. {
  153. if (userWantsToRate)
  154. {
  155. this.OpenRatingScreen();
  156. this.HandleRated();
  157. }
  158. else
  159. {
  160. this.UserInterestingForReview = false;
  161. }
  162. }
  163. private void OpenRatingScreen()
  164. {
  165. SingletonMonobehaviour<CIGGameStats>.Instance.AddScreenViewed("rating_rate_game");
  166. this._nativeBindings.RateThisApp(this._appID);
  167. }
  168. private void HandleRated()
  169. {
  170. this.LastVersionReviewed = this._buildVersion;
  171. }
  172. private void ShowRatingRequestPopupWrapper(Action<bool> answerCallback)
  173. {
  174. this.ShowRatingRequestPopup(answerCallback);
  175. this.LastShown = DateTime.UtcNow;
  176. }
  177. private void HandleRecievedNewEventToken(string token)
  178. {
  179. UnityEngine.Debug.Log("Recieved new event token! Token: " + token);
  180. this.UserInterestingForReview = true;
  181. }
  182. private const int MinimalUserSessionLengthForPopupMinutes = 1;
  183. private const int MinimumWaitTimeForPopupAfterInteresingEventSeconds = 2;
  184. private const int MaximumWaitTimeForPopupAfterInteresingEventSeconds = 8;
  185. private const int TryShowPopupDurationSeconds = 30;
  186. private const int DaysAfterRatingDismissalToShowNewPopup = 7;
  187. private const string LastShownKey = "LastShown_persistentkey";
  188. private const string UserInterestingForReviewKey = "UserInterestingForReview_persistentkey";
  189. private const string LastVersionReviewedKey = "LastVersionReviewed_persistentkey";
  190. private const string NewAdjustEventAfterLastReviewRequestKey = "NewAdjustEventAfterLastReviewRequestKey_persistentkey";
  191. private const StorageLifecycle LocalStorageLifecycle = StorageLifecycle.Player;
  192. private IEnumerator _getRatedCoroutine;
  193. private MonoBehaviour _anyMB;
  194. private INativeBindings _nativeBindings;
  195. protected Messenger _messenger;
  196. private string _appID;
  197. private int _buildVersion;
  198. }
  199. }