Created
December 8, 2014 15:02
-
-
Save tayl0r/959b1aebb871080fa6ae to your computer and use it in GitHub Desktop.
FCSponsorPay.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if !UNITY_EDITOR || FCLOG | |
using Debug = FC.Debug; | |
#else | |
using Debug = UnityEngine.Debug; | |
#endif | |
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public static class FCSponsorPay : System.Object { | |
public static bool _isIncentVideoReady; | |
public static bool _isInterstitialReady; | |
static SponsorPay.SponsorPayPlugin _plugin; | |
static bool __busy; | |
public static bool _busy { | |
get { | |
return __busy; | |
} | |
set { | |
__busy = value; | |
if (value) { | |
AudioPlayer.FadeMusic(); | |
} else { | |
AudioPlayer.ResumeMusic(); | |
} | |
} | |
} | |
static double _pendingCoins; | |
static HashSet<string> _receivedTranxIds = new HashSet<string>(); | |
static TrackingEventSub1 _lastThingDone; | |
#if UNITY_IPHONE | |
const string APP_ID = "26099"; | |
const string SECURITY_TOKEN = "d53fe85d329b6c10fd856f481f690d59"; | |
#elif UNITY_ANDROID && !AMAZON | |
const string APP_ID = "26102"; | |
const string SECURITY_TOKEN = "4abe20ce3ae5853b33cd46212dbcd750"; | |
#endif | |
const string CURRENCY_NAME = "coins"; | |
static public void SetGuid(string guid) { | |
#if !(UNITY_IPHONE || (UNITY_ANDROID && !AMAZON)) | |
return; | |
#else | |
Debug.Log(string.Format("FCSponsorPay.SetGuid: init({0}) ads({1})", _plugin != null, TuningDataManager._data.ads)); | |
// already init? | |
if (_plugin != null) { return; } | |
// watch for tuning data updates | |
TuningDataManager.instance.RegisterGotNewTuningDataEvent(GotTuningData, false); | |
// watch for persisted data updates | |
DataPersister.instance.RemoveOnDataLoadedCallback(GotPersistedData); | |
DataPersister.instance.AddOnDataLoadedCallback(GotPersistedData); | |
// if we dont have ads enabled, bail out | |
if (TuningDataManager._data.ads == 0) { return; } | |
_plugin = SponsorPayPluginMonoBehaviour.PluginInstance; | |
if (_plugin == null) { | |
Debug.LogError("FCSponsorPay.SetGuid: plugin failed to start"); | |
} | |
var credentials = _plugin.Start(APP_ID, guid, SECURITY_TOKEN); | |
if (string.IsNullOrEmpty(credentials)) { | |
Debug.LogError("FCSponsorPay.SetGuid: bailing out because credentials is null"); | |
_plugin = null; | |
return; | |
} | |
Debug.Log("FCSponsorPay.SetGuid: plugin started with credentials: " + credentials); | |
// listeners | |
_plugin.OnNativeExceptionReceived += new SponsorPay.NativeExceptionHandler(OnNativeExceptionReceivedFromSDK); | |
_plugin.OnBrandEngageRequestResponseReceived += new SponsorPay.BrandEngageRequestResponseReceivedHandler(OnSPIncentVideoAvailable); | |
_plugin.OnBrandEngageRequestErrorReceived += new SponsorPay.BrandEngageRequestErrorReceivedHandler(OnSPIncentVideoErrorReceived); | |
_plugin.OnBrandEngageResultReceived += new SponsorPay.BrandEngageResultHandler(OnIncentVideoFinished); | |
_plugin.OnOfferWallResultReceived += new SponsorPay.OfferWallResultHandler(OnOfferWallFinished); | |
_plugin.OnSuccessfulCurrencyRequestReceived += new SponsorPay.SuccessfulCurrencyResponseReceivedHandler(OnSuccessfulCurrencyResponse); | |
_plugin.OnDeltaOfCoinsReceived += new SponsorPay.DeltaOfCoinsResponseReceivedHandler(OnDeltaOfCoinsReceived); | |
_plugin.OnDeltaOfCoinsRequestFailed += new SponsorPay.ErrorHandler(OnDeltaOfCoinsRequestFailed); | |
_plugin.OnInterstitialRequestErrorReceived += new SponsorPay.InterstitialRequestErrorReceivedHandler(OnInterstitialAvailableError); | |
_plugin.OnInterstitialRequestResponseReceived += new SponsorPay.InterstitialRequestResponseReceivedHandler(OnInterstitialAvailable); | |
_plugin.OnInterstitialStatusCloseReceived += new SponsorPay.InterstitialStatusCloseHandler(OnInterstitialFinished); | |
_plugin.OnInterstitialStatusErrorReceived += new SponsorPay.InterstitialStatusErrorHandler(OnInterstitialError); | |
// logging | |
#if RELEASE | |
_plugin.EnableLogging(false); | |
#else | |
_plugin.EnableLogging(true); | |
#endif | |
#endif | |
// setup notifs | |
_plugin.ShowBrandEngageRewardNotification(true); | |
_plugin.ShowVCSNotifications(true); | |
// wait a few frames before we check for any ads, if we do it on the same frame it seems to cause problems | |
UiManager.instance.StartCoroutine(UiManager.instance.RunAfterDelay(RequestAds, .5f)); | |
UiManager.instance.StartCoroutine(UiManager.instance.RunAfterDelay(RefreshCoinRewards, .5f)); | |
} | |
private static void GotPersistedData(PersistedData pd) { | |
// add their last id so they cant claim it again | |
if (string.IsNullOrEmpty(pd.lastAdId) == false && _receivedTranxIds.Contains(pd.lastAdId) == false) { | |
_receivedTranxIds.Add(pd.lastAdId); | |
} | |
} | |
//static void Test() { | |
// GotCoins(20.0, "1587844667"); | |
//} | |
public static void RefreshCoinRewards() { | |
//UiManager.instance.StartCoroutine(UiManager.instance.RunAfterDelay(Test, .55f)); | |
if (_plugin != null) { | |
_plugin.RequestNewCoins(CURRENCY_NAME); | |
} | |
} | |
public static void RequestAds() { | |
if (_plugin != null) { | |
// check if incent video is available | |
_plugin.RequestBrandEngageOffers(CURRENCY_NAME, true); | |
// setup interstitials - disabled for now | |
//_plugin.RequestInterstitialAds(); | |
} | |
} | |
static public bool LaunchOfferwall() { | |
Debug.Log(string.Format("FCSponsorPay.LaunchOfferwall: starting: plugin({0}) busy({1})", _plugin != null, _busy)); | |
if (_plugin != null && _busy == false) { | |
_busy = true; | |
_lastThingDone = TrackingEventSub1.offer_wall; | |
_plugin.LaunchOfferWall(CURRENCY_NAME); | |
return true; | |
} | |
return false; | |
} | |
static public bool LaunchInterstitial() { | |
Debug.Log(string.Format("FCSponsorPay.LaunchInterstitial: starting: plugin({0}) available({1}) busy({2})", _plugin != null, _isInterstitialReady, _busy)); | |
if (_plugin != null && _isInterstitialReady && _busy == false) { | |
_busy = true; | |
_lastThingDone = TrackingEventSub1.interstitial; | |
_plugin.ShowInterstitialAd(); | |
return true; | |
} | |
return false; | |
} | |
static public bool LaunchIncentVideo() { | |
Debug.Log(string.Format("FCSponsorPay.LaunchIncentVideo: starting: plugin({0}) available({1}) busy({2})", _plugin != null, _isIncentVideoReady, _busy)); | |
if (_plugin != null && _isIncentVideoReady && _busy == false) { | |
_busy = true; | |
_lastThingDone = TrackingEventSub1.incent_video; | |
_plugin.StartBrandEngage(); | |
return true; | |
} | |
return false; | |
} | |
public static void ShowRewardDialog() { | |
if (_pendingCoins > 0.0) { | |
Debug.Log(string.Format("FCSponsorPay.ShowRewardDialog: showing dialog for {0} coins", _pendingCoins)); | |
Brain.instance.ShowGenericInfoMessage(string.Format("Now claim your reward of {0:N0} coins!", _pendingCoins), ClaimPendingCoins, "CLAIM", "Offer Complete!", true); | |
IAPScreen.ClearError(); | |
} | |
} | |
private static void ClaimPendingCoins() { | |
IAPScreen.ClearError(); | |
if (_pendingCoins > 0.0) { | |
var dialog = DownloadingAndErrorDialog.GetInstance(DownloadingAndErrorDialog.DialogState.Info); | |
if (dialog != null) { | |
var coins = _pendingCoins; | |
_pendingCoins = 0.0; | |
Debug.Log(string.Format("FCSponsorPay.ClaimPendingCoins: spewing {0} coins", coins)); | |
TreasureChest.instance.SpewCoins(coins, .1f, dialog._oneButtons.transform, Vector3.zero, CreditBalance.instanceTotalCredits, .5f); | |
DataPersister.instance.SaveData(); | |
} | |
} | |
} | |
static void GotCoins(double deltaOfCoins, string transactionId) { | |
IAPScreen.ClearError(); | |
if (deltaOfCoins > 0 && (_receivedTranxIds.Contains(transactionId) == false)) { | |
TrackingEngine.UploadEvent(new TrackingEvent(TrackingEventCategory.ads) { | |
sub1 = _lastThingDone, | |
sub2 = TrackingEventSub2.got_coins, | |
sub3 = "fyber", | |
cnt = 1, | |
val = deltaOfCoins, | |
txt_val = transactionId, | |
}); | |
_receivedTranxIds.Add(transactionId); | |
_pendingCoins += deltaOfCoins; | |
} | |
if (_pendingCoins > 0.0) { | |
Debug.Log("FCSponsorPay.GotCoins: showing reward dialog shortly..."); | |
UiManager.instance.StartCoroutine(UiManager.instance.RunAfterDelay(ShowRewardDialog, .25f)); | |
} | |
} | |
#region callbacks | |
/// <summary> | |
/// interstitial is done - error | |
/// </summary> | |
private static void OnInterstitialError(string message) { | |
_busy = false; | |
Debug.LogError(string.Format("FCSponsorPay.OnInterstitialError: {0}", message)); | |
} | |
/// <summary> | |
/// interstitial is done - success | |
/// </summary> | |
private static void OnInterstitialFinished(string closeReason) { | |
_busy = false; | |
Debug.Log(string.Format("FCSponsorPay.OnInterstitialFinished: {0}", closeReason)); | |
TrackingEngine.UploadEvent(new TrackingEvent(TrackingEventCategory.ads) { | |
sub1 = TrackingEventSub1.interstitial, | |
sub2 = TrackingEventSub2.finished, | |
sub5 = closeReason, | |
cnt = 1, | |
}); | |
} | |
/// <summary> | |
/// do we have interstitials? success response | |
/// </summary> | |
private static void OnInterstitialAvailable(bool offersAvailable) { | |
Debug.Log(string.Format("FCSponsorPay.OnInterstitialAvailable: {0}", offersAvailable)); | |
if (_isInterstitialReady != offersAvailable) { | |
TrackingEngine.UploadEvent(new TrackingEvent(TrackingEventCategory.ads) { | |
sub1 = TrackingEventSub1.interstitial, | |
sub2 = TrackingEventSub2.availability, | |
cnt = _isInterstitialReady ? 1 : 0, | |
}); | |
} | |
_isInterstitialReady = offersAvailable; | |
} | |
/// <summary> | |
/// do we have interstitials? fail response | |
/// </summary> | |
private static void OnInterstitialAvailableError(string message) { | |
Debug.LogError(string.Format("FCSponsorPay.OnInterstitialAvailableError: {0}", message)); | |
} | |
/// <summary> | |
/// user got new coins - failed | |
/// </summary> | |
private static void OnDeltaOfCoinsRequestFailed(SponsorPay.RequestError error) { | |
Debug.LogError(string.Format("FCSponsorPay.OnDeltaOfCoinsRequestFailed: type({0}) code({1}) msg({2})", error.Type, error.Code, error.Message)); | |
} | |
/// <summary> | |
/// user got new coins - success | |
/// </summary> | |
static void OnDeltaOfCoinsReceived(double deltaOfCoins, string transactionId) { | |
Debug.Log(string.Format("FCSponsorPay.OnDeltaOfCoinsReceived: coinsDelta({0}), transactionId({1})", deltaOfCoins, transactionId)); | |
GotCoins(deltaOfCoins, transactionId); | |
} | |
/// <summary> | |
/// user got new coins - success | |
/// </summary> | |
static void OnSuccessfulCurrencyResponse(SponsorPay.SuccessfulCurrencyResponse response) { | |
Debug.Log(string.Format("FCSponsorPay.OnSuccessfulCurrencyResponse: currency({0} - {1}), coinsDelta({2}), lastTranxId({3})", response.CurrencyId, response.CurrencyName, response.DeltaOfCoins, response.LatestTransactionId)); | |
GotCoins(response.DeltaOfCoins, response.LatestTransactionId); | |
} | |
/// <summary> | |
/// offer wall finished - response | |
/// </summary> | |
/// <param name="message"></param> | |
private static void OnOfferWallFinished(string message) { | |
_busy = false; | |
Debug.Log("FCSponsorPay.OnOfferWallFinished: " + message); | |
TrackingEngine.UploadEvent(new TrackingEvent(TrackingEventCategory.ads) { | |
sub1 = TrackingEventSub1.offer_wall, | |
sub2 = TrackingEventSub2.finished, | |
sub5 = message, | |
cnt = 1, | |
}); | |
RefreshCoinRewards(); | |
UiManager.instance.StartCoroutine(UiManager.instance.RunAfterDelay(RefreshCoinRewards, 5f)); | |
} | |
/// <summary> | |
/// incent video finished - response | |
/// response values: | |
/// CLOSE_FINISHED User has successfully completed the engagement. | |
/// CLOSE_ABORTED User has cancelled the engagement before finishing it. | |
/// ERROR An unknown error has occurred. | |
/// </summary> | |
private static void OnIncentVideoFinished(string message) { | |
_busy = false; | |
Debug.Log("FCSponsorPay.OnIncentVideoFinished: " + message); | |
TrackingEngine.UploadEvent(new TrackingEvent(TrackingEventCategory.ads) { | |
sub1 = TrackingEventSub1.incent_video, | |
sub2 = TrackingEventSub2.finished, | |
sub5 = message, | |
cnt = 1, | |
}); | |
// request more ads | |
RequestAds(); | |
} | |
/// <summary> | |
/// incent video available - error received | |
/// </summary> | |
private static void OnSPIncentVideoErrorReceived(string message) { | |
_busy = false; | |
Debug.LogError("FCSponsorPay.OnSPIncentVideoErrorReceived: " + message); | |
// request more ads | |
RequestAds(); | |
} | |
/// <summary> | |
/// incent video available - callback | |
/// </summary> | |
private static void OnSPIncentVideoAvailable(bool offersAvailable) { | |
Debug.Log(string.Format("FCSponsorPay.OnSPIncentVideoAvailable: {0}", offersAvailable)); | |
if (_isIncentVideoReady != offersAvailable) { | |
TrackingEngine.UploadEvent(new TrackingEvent(TrackingEventCategory.ads) { | |
sub1 = TrackingEventSub1.incent_video, | |
sub2 = TrackingEventSub2.availability, | |
cnt = offersAvailable ? 1 : 0, | |
}); | |
} | |
_isIncentVideoReady = offersAvailable; | |
IAPScreen.SetVideoAdsAvailable(_isIncentVideoReady); | |
} | |
/// <summary> | |
/// generic error received | |
/// </summary> | |
private static void OnNativeExceptionReceivedFromSDK(string message) { | |
_busy = false; | |
Debug.LogError("FCSponsorPay.OnNativeExceptionReceivedFromSDK: " + message); | |
} | |
private static void GotTuningData(TuningData tuning) { | |
SetGuid(DataPersister.instance._data.guid); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment