using System; using System.Collections; using System.Collections.Generic; using SUISS.Core; using SUISS.Storage; using UnityEngine; namespace SUISSEngine { public abstract class RatingRequestManager : IRatingRequestManager { public RatingRequestManager(MonoBehaviour anyMonoBehaviour, INativeBindings nativeBindings, Messenger messenger, string appID, int buildVersion) { this.InitializeInternalVariables(); this._anyMB = anyMonoBehaviour; this._nativeBindings = nativeBindings; this._appID = appID; this._buildVersion = buildVersion; this._messenger = messenger; } private DateTime LastShown { get { Dictionary root = Storage.Get(StorageLifecycle.Player).Root; if (root.ContainsKey("LastShown_persistentkey")) { string s = (string)root["LastShown_persistentkey"]; return DateTime.Parse(s); } return new DateTime(0L); } set { Storage.Get(StorageLifecycle.Player).Root["LastShown_persistentkey"] = value.ToString(); } } private bool MayShowReviewPopup { get { return this.UserInterestingForReview && !this.ReviewPopupHasBeenShownLately && this.UserSessionLongEnough && !this.UserHasReviewed; } } private bool UserSessionLongEnough { get { return Time.realtimeSinceStartup >= 60f; } } private bool ReviewPopupHasBeenShownLately { get { return DateTime.UtcNow < this.LastShown + TimeSpan.FromDays(7.0); } } public bool UserInterestingForReview { get { Dictionary root = Storage.Get(StorageLifecycle.Player).Root; return root.ContainsKey("UserInterestingForReview_persistentkey") && (bool)root["UserInterestingForReview_persistentkey"]; } set { Storage.Get(StorageLifecycle.Player).Root["UserInterestingForReview_persistentkey"] = value; } } private bool NewAdjustEventAfterLastReviewRequest { get { Dictionary root = Storage.Get(StorageLifecycle.Player).Root; return root.ContainsKey("NewAdjustEventAfterLastReviewRequestKey_persistentkey") && (bool)root["NewAdjustEventAfterLastReviewRequestKey_persistentkey"]; } set { Storage.Get(StorageLifecycle.Player).Root["NewAdjustEventAfterLastReviewRequestKey_persistentkey"] = value; } } private int LastVersionReviewed { get { Dictionary root = Storage.Get(StorageLifecycle.Player).Root; if (root.ContainsKey("LastVersionReviewed_persistentkey")) { return (int)root["LastVersionReviewed_persistentkey"]; } return 0; } set { Storage.Get(StorageLifecycle.Player).Root["LastVersionReviewed_persistentkey"] = value; } } public bool UserHasReviewed { get { return this.LastVersionReviewed != 0; } } protected abstract bool IsRatingRequestPopupConvenient { get; } private void InitializeInternalVariables() { this._getRatedCoroutine = null; EdwinServerEventHandler.RecievedNewNonRegisteredEventToken += this.HandleRecievedNewEventToken; } ~RatingRequestManager() { EdwinServerEventHandler.RecievedNewNonRegisteredEventToken -= this.HandleRecievedNewEventToken; } public void NotifyInterestingEvent() { if (this._getRatedCoroutine == null && this.MayShowReviewPopup) { this._getRatedCoroutine = this.TryGetRatedCoroutine(); if (this._anyMB != null) { this._anyMB.StartCoroutine(this._getRatedCoroutine); } else { UnityEngine.Debug.LogError("MonoBehaviour has been destroyed, cannot start RatedCoroutine"); } } } protected abstract void ShowRatingRequestPopup(Action answerCallback); private IEnumerator TryGetRatedCoroutine() { int startDelay = UnityEngine.Random.Range(2, 8); yield return new WaitForSeconds((float)startDelay); UnityEngine.Debug.Log("Trying to show get rated popup."); int count = 30; while (count > 0) { if (this.IsRatingRequestPopupConvenient) { this.ShowRatingRequestPopupWrapper(new Action(this.HandleRatingRequestAnswer)); this._getRatedCoroutine = null; yield break; } count--; yield return new WaitForSeconds(1f); } this._getRatedCoroutine = null; yield break; } private void HandleRatingRequestAnswer(bool userWantsToRate) { if (userWantsToRate) { this.OpenRatingScreen(); this.HandleRated(); } else { this.UserInterestingForReview = false; } } private void OpenRatingScreen() { SingletonMonobehaviour.Instance.AddScreenViewed("rating_rate_game"); this._nativeBindings.RateThisApp(this._appID); } private void HandleRated() { this.LastVersionReviewed = this._buildVersion; } private void ShowRatingRequestPopupWrapper(Action answerCallback) { this.ShowRatingRequestPopup(answerCallback); this.LastShown = DateTime.UtcNow; } private void HandleRecievedNewEventToken(string token) { UnityEngine.Debug.Log("Recieved new event token! Token: " + token); this.UserInterestingForReview = true; } private const int MinimalUserSessionLengthForPopupMinutes = 1; private const int MinimumWaitTimeForPopupAfterInteresingEventSeconds = 2; private const int MaximumWaitTimeForPopupAfterInteresingEventSeconds = 8; private const int TryShowPopupDurationSeconds = 30; private const int DaysAfterRatingDismissalToShowNewPopup = 7; private const string LastShownKey = "LastShown_persistentkey"; private const string UserInterestingForReviewKey = "UserInterestingForReview_persistentkey"; private const string LastVersionReviewedKey = "LastVersionReviewed_persistentkey"; private const string NewAdjustEventAfterLastReviewRequestKey = "NewAdjustEventAfterLastReviewRequestKey_persistentkey"; private const StorageLifecycle LocalStorageLifecycle = StorageLifecycle.Player; private IEnumerator _getRatedCoroutine; private MonoBehaviour _anyMB; private INativeBindings _nativeBindings; protected Messenger _messenger; private string _appID; private int _buildVersion; } }