|
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using SUISS.Core;
-
- namespace CIG
- {
- public sealed class InterstitialAdsManager : SingletonMonobehaviour<InterstitialAdsManager>
- {
- //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
- public event InterstitialAdsManager.AvailabilityChangedEventHandler AvailabilityChangedEvent;
-
- private void FireAvailabilityChangedEvent()
- {
- if (this.AvailabilityChangedEvent != null)
- {
- this.AvailabilityChangedEvent();
- }
- }
-
- protected override void Awake()
- {
- base.Awake();
- if (this._isValidNewInstance)
- {
- this._adWaterfall = new AdWaterfall(new AdProviderType[]
- {
- AdProviderType.Admob
- });
- }
- }
-
- private void Start()
- {
- SingletonMonobehaviour<CIGGameState>.Instance.SecondsPlayedChangedEvent += this.OnSecondsPlayedChanged;
- CIGWebService instance = SingletonMonobehaviour<CIGWebService>.Instance;
- instance.PullRequestCompleted += this.OnPullRequestCompleted;
- if (instance.HasPulled)
- {
- this.OnPullRequestCompleted();
- }
- SingletonMonobehaviour<AdProviderPool>.Instance.AdAvailabilityChangedEvent += this.OnAdAvailabilityChangedEvent;
- }
-
- protected override void OnDestroy()
- {
- if (SingletonMonobehaviour<CIGWebService>.IsAvailable)
- {
- SingletonMonobehaviour<CIGWebService>.Instance.PullRequestCompleted -= this.OnPullRequestCompleted;
- }
- if (SingletonMonobehaviour<AdProviderPool>.IsAvailable)
- {
- SingletonMonobehaviour<AdProviderPool>.Instance.AdAvailabilityChangedEvent -= this.OnAdAvailabilityChangedEvent;
- }
- if (SingletonMonobehaviour<CIGGameState>.IsAvailable)
- {
- SingletonMonobehaviour<CIGGameState>.Instance.SecondsPlayedChangedEvent -= this.OnSecondsPlayedChanged;
- }
- base.OnDestroy();
- }
-
- public bool TryShowingLevelUpAd(int level)
- {
- CIGGameState instance = SingletonMonobehaviour<CIGGameState>.Instance;
- long totalMinutesPlayed = instance.TotalMinutesPlayed;
- if (totalMinutesPlayed < (long)this._levelUpMinMinutesPlayed || this._levelUpNextInterstitialLevel > level)
- {
- return false;
- }
- if (this.ShowAd())
- {
- this._levelUpNextInterstitialLevel = level + this._levelUpsBetweenInterstitials;
- return true;
- }
- return false;
- }
-
- public bool ShowAd()
- {
- return this.IsReady && this._adWaterfall.ShowAd(new Action<bool, bool>(this.OnInterstitialWatched));
- }
-
- public bool IsReady
- {
- get
- {
- if (this._adWaterfall == null || !this._pullRequestCompleted || !this._interstitialsAllowed || this._interstitialsShownCount >= this._maxInterstitials || !SingletonMonobehaviour<PopupManager>.IsAvailable)
- {
- return false;
- }
- PopupManager instance = SingletonMonobehaviour<PopupManager>.Instance;
- return instance.PopupsMayOpen && !instance.PopupIsOpen() && this._adWaterfall.IsReady;
- }
- }
-
- private void OnPullRequestCompleted()
- {
- bool isReady = this.IsReady;
- Dictionary<string, string> properties = SingletonMonobehaviour<CIGWebService>.Instance.Properties;
- this._inGameInterstitialTimeoutSeconds = properties.GetInt("ingame_interstitial_interval", 240);
- this._inGameMinMinutesPlayed = properties.GetInt("ingame_interstitial_minutes", 0);
- this._inGameSecondsToFirst = properties.GetInt("ingame_interstitial_seconds_to_first", 240);
- this._levelUpsBetweenInterstitials = properties.GetInt("level_interstitial_amount", 5);
- this._levelUpMinMinutesPlayed = properties.GetInt("level_interstitial_minutes", 10);
- this._interstitialsAllowed = properties.GetBoolean("interstitials_allowed", true);
- this._maxInterstitials = properties.GetInt("max_interstitials", 100);
- if (!this._pullRequestCompleted)
- {
- this._inGameNextInterstitialSecondsPlayed = this._inGameSecondsToFirst;
- this._levelUpNextInterstitialLevel = SingletonMonobehaviour<CIGGameState>.Instance.Level + 1;
- }
- this._pullRequestCompleted = true;
- if (isReady != this.IsReady)
- {
- this.FireAvailabilityChangedEvent();
- }
- }
-
- private void OnInterstitialWatched(bool success, bool clicked)
- {
- this._interstitialsShownCount++;
- }
-
- private void OnAdAvailabilityChangedEvent(AdProviderType providerType)
- {
- if (this._adWaterfall.ContainsAdProvider(providerType))
- {
- this.FireAvailabilityChangedEvent();
- }
- }
-
- private void OnSecondsPlayedChanged()
- {
- CIGGameState instance = SingletonMonobehaviour<CIGGameState>.Instance;
- long totalMinutesPlayed = instance.TotalMinutesPlayed;
- int secondsPlayedInThisSession = instance.SecondsPlayedInThisSession;
- if (totalMinutesPlayed < (long)this._inGameMinMinutesPlayed || this._inGameNextInterstitialSecondsPlayed > secondsPlayedInThisSession)
- {
- return;
- }
- if (this.ShowAd())
- {
- this._inGameNextInterstitialSecondsPlayed = secondsPlayedInThisSession + this._inGameInterstitialTimeoutSeconds;
- }
- }
-
- private const string InGameInterstitialIntervalServerKey = "ingame_interstitial_interval";
-
- private const string InGameInterstitialMinutesServerKey = "ingame_interstitial_minutes";
-
- private const string InGameInterstitialSecondsToFirstServerKey = "ingame_interstitial_seconds_to_first";
-
- private const string LevelInterstitialAmountServerKey = "level_interstitial_amount";
-
- private const string LevelInterstitialMinutesServerKey = "level_interstitial_minutes";
-
- private const string MaxInterstitialsPerSessionServerKey = "max_interstitials";
-
- private const string InterstitialsAllowedServerKey = "interstitials_allowed";
-
- private bool _interstitialsAllowed;
-
- private int _maxInterstitials;
-
- private int _inGameSecondsToFirst;
-
- private int _inGameMinMinutesPlayed;
-
- private int _inGameInterstitialTimeoutSeconds;
-
- private int _levelUpsBetweenInterstitials;
-
- private int _levelUpMinMinutesPlayed;
-
- private bool _pullRequestCompleted;
-
- private AdWaterfall _adWaterfall;
-
- private int _interstitialsShownCount;
-
- private int _levelUpNextInterstitialLevel;
-
- private int _inGameNextInterstitialSecondsPlayed;
-
- public delegate void AvailabilityChangedEventHandler();
- }
- }
|