您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

188 行
6.1 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using SUISS.Core;
  5. namespace CIG
  6. {
  7. public sealed class InterstitialAdsManager : SingletonMonobehaviour<InterstitialAdsManager>
  8. {
  9. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  10. public event InterstitialAdsManager.AvailabilityChangedEventHandler AvailabilityChangedEvent;
  11. private void FireAvailabilityChangedEvent()
  12. {
  13. if (this.AvailabilityChangedEvent != null)
  14. {
  15. this.AvailabilityChangedEvent();
  16. }
  17. }
  18. protected override void Awake()
  19. {
  20. base.Awake();
  21. if (this._isValidNewInstance)
  22. {
  23. this._adWaterfall = new AdWaterfall(new AdProviderType[]
  24. {
  25. AdProviderType.Admob
  26. });
  27. }
  28. }
  29. private void Start()
  30. {
  31. SingletonMonobehaviour<CIGGameState>.Instance.SecondsPlayedChangedEvent += this.OnSecondsPlayedChanged;
  32. CIGWebService instance = SingletonMonobehaviour<CIGWebService>.Instance;
  33. instance.PullRequestCompleted += this.OnPullRequestCompleted;
  34. if (instance.HasPulled)
  35. {
  36. this.OnPullRequestCompleted();
  37. }
  38. SingletonMonobehaviour<AdProviderPool>.Instance.AdAvailabilityChangedEvent += this.OnAdAvailabilityChangedEvent;
  39. }
  40. protected override void OnDestroy()
  41. {
  42. if (SingletonMonobehaviour<CIGWebService>.IsAvailable)
  43. {
  44. SingletonMonobehaviour<CIGWebService>.Instance.PullRequestCompleted -= this.OnPullRequestCompleted;
  45. }
  46. if (SingletonMonobehaviour<AdProviderPool>.IsAvailable)
  47. {
  48. SingletonMonobehaviour<AdProviderPool>.Instance.AdAvailabilityChangedEvent -= this.OnAdAvailabilityChangedEvent;
  49. }
  50. if (SingletonMonobehaviour<CIGGameState>.IsAvailable)
  51. {
  52. SingletonMonobehaviour<CIGGameState>.Instance.SecondsPlayedChangedEvent -= this.OnSecondsPlayedChanged;
  53. }
  54. base.OnDestroy();
  55. }
  56. public bool TryShowingLevelUpAd(int level)
  57. {
  58. CIGGameState instance = SingletonMonobehaviour<CIGGameState>.Instance;
  59. long totalMinutesPlayed = instance.TotalMinutesPlayed;
  60. if (totalMinutesPlayed < (long)this._levelUpMinMinutesPlayed || this._levelUpNextInterstitialLevel > level)
  61. {
  62. return false;
  63. }
  64. if (this.ShowAd())
  65. {
  66. this._levelUpNextInterstitialLevel = level + this._levelUpsBetweenInterstitials;
  67. return true;
  68. }
  69. return false;
  70. }
  71. public bool ShowAd()
  72. {
  73. return this.IsReady && this._adWaterfall.ShowAd(new Action<bool, bool>(this.OnInterstitialWatched));
  74. }
  75. public bool IsReady
  76. {
  77. get
  78. {
  79. if (this._adWaterfall == null || !this._pullRequestCompleted || !this._interstitialsAllowed || this._interstitialsShownCount >= this._maxInterstitials || !SingletonMonobehaviour<PopupManager>.IsAvailable)
  80. {
  81. return false;
  82. }
  83. PopupManager instance = SingletonMonobehaviour<PopupManager>.Instance;
  84. return instance.PopupsMayOpen && !instance.PopupIsOpen() && this._adWaterfall.IsReady;
  85. }
  86. }
  87. private void OnPullRequestCompleted()
  88. {
  89. bool isReady = this.IsReady;
  90. Dictionary<string, string> properties = SingletonMonobehaviour<CIGWebService>.Instance.Properties;
  91. this._inGameInterstitialTimeoutSeconds = properties.GetInt("ingame_interstitial_interval", 240);
  92. this._inGameMinMinutesPlayed = properties.GetInt("ingame_interstitial_minutes", 0);
  93. this._inGameSecondsToFirst = properties.GetInt("ingame_interstitial_seconds_to_first", 240);
  94. this._levelUpsBetweenInterstitials = properties.GetInt("level_interstitial_amount", 5);
  95. this._levelUpMinMinutesPlayed = properties.GetInt("level_interstitial_minutes", 10);
  96. this._interstitialsAllowed = properties.GetBoolean("interstitials_allowed", true);
  97. this._maxInterstitials = properties.GetInt("max_interstitials", 100);
  98. if (!this._pullRequestCompleted)
  99. {
  100. this._inGameNextInterstitialSecondsPlayed = this._inGameSecondsToFirst;
  101. this._levelUpNextInterstitialLevel = SingletonMonobehaviour<CIGGameState>.Instance.Level + 1;
  102. }
  103. this._pullRequestCompleted = true;
  104. if (isReady != this.IsReady)
  105. {
  106. this.FireAvailabilityChangedEvent();
  107. }
  108. }
  109. private void OnInterstitialWatched(bool success, bool clicked)
  110. {
  111. this._interstitialsShownCount++;
  112. }
  113. private void OnAdAvailabilityChangedEvent(AdProviderType providerType)
  114. {
  115. if (this._adWaterfall.ContainsAdProvider(providerType))
  116. {
  117. this.FireAvailabilityChangedEvent();
  118. }
  119. }
  120. private void OnSecondsPlayedChanged()
  121. {
  122. CIGGameState instance = SingletonMonobehaviour<CIGGameState>.Instance;
  123. long totalMinutesPlayed = instance.TotalMinutesPlayed;
  124. int secondsPlayedInThisSession = instance.SecondsPlayedInThisSession;
  125. if (totalMinutesPlayed < (long)this._inGameMinMinutesPlayed || this._inGameNextInterstitialSecondsPlayed > secondsPlayedInThisSession)
  126. {
  127. return;
  128. }
  129. if (this.ShowAd())
  130. {
  131. this._inGameNextInterstitialSecondsPlayed = secondsPlayedInThisSession + this._inGameInterstitialTimeoutSeconds;
  132. }
  133. }
  134. private const string InGameInterstitialIntervalServerKey = "ingame_interstitial_interval";
  135. private const string InGameInterstitialMinutesServerKey = "ingame_interstitial_minutes";
  136. private const string InGameInterstitialSecondsToFirstServerKey = "ingame_interstitial_seconds_to_first";
  137. private const string LevelInterstitialAmountServerKey = "level_interstitial_amount";
  138. private const string LevelInterstitialMinutesServerKey = "level_interstitial_minutes";
  139. private const string MaxInterstitialsPerSessionServerKey = "max_interstitials";
  140. private const string InterstitialsAllowedServerKey = "interstitials_allowed";
  141. private bool _interstitialsAllowed;
  142. private int _maxInterstitials;
  143. private int _inGameSecondsToFirst;
  144. private int _inGameMinMinutesPlayed;
  145. private int _inGameInterstitialTimeoutSeconds;
  146. private int _levelUpsBetweenInterstitials;
  147. private int _levelUpMinMinutesPlayed;
  148. private bool _pullRequestCompleted;
  149. private AdWaterfall _adWaterfall;
  150. private int _interstitialsShownCount;
  151. private int _levelUpNextInterstitialLevel;
  152. private int _inGameNextInterstitialSecondsPlayed;
  153. public delegate void AvailabilityChangedEventHandler();
  154. }
  155. }