|
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- namespace SUISS.Promo
- {
- public class SparkSocGames : MonoBehaviour
- {
- public enum Referer
- {
- Promo,
- MoreGames,
- OtherGames
- }
-
- public delegate void OpenAppInStoreDelegate(string applicationId, string link, string referrer);
-
- public delegate bool IsAppInstalledDelegate(string applicationId);
-
- private const int JSON_CACHE_EXPERATION_DURATION_IN_DAYS = 1;
-
- private const int TEXTURE_CACHE_EXPERATION_DURATION_IN_DAYS = 7;
-
- private const string REFERER_MORE_GAMES = "sparksoc_moregames";
-
- private const string REFERER_INGAME_PROMO = "sparksoc_promo";
-
- private const string RefererOtherGames = "sparksoc_othergames";
-
- private const string LastGamePopupPlayerPrefsKey = "SparkSocGames.lastGamePopup.";
-
- private OpenAppInStoreDelegate openAppInStore;
-
- private IsAppInstalledDelegate isAppInstalledMethod;
-
- private string applicationId;
-
- private Dictionary<string, long> lastGamePopup = new Dictionary<string, long>();
-
- private int perGameIntervalMinutes = 720;
-
- private string MoreGamesURL
- {
- get
- {
- string empty = string.Empty;
- empty = "https://crosspromo.sparklingsociety.net/googleplay/more_games.php?app=";
- return empty + applicationId;
- }
- }
-
- private string PromoURL
- {
- get
- {
- string empty = string.Empty;
- empty = "https://crosspromo.sparklingsociety.net/googleplay/promo.php?app=";
- return empty + applicationId;
- }
- }
-
- private string OtherGamesURL
- {
- get
- {
- string empty = string.Empty;
- empty = "https://crosspromo.sparklingsociety.net/googleplay/other_games.php?app=";
- return empty + applicationId;
- }
- }
-
- public void Initialise(string applicationId, OpenAppInStoreDelegate openAppInStore, IsAppInstalledDelegate isAppInstalled, int perGameIntervalMinutes = 720)
- {
- this.applicationId = applicationId;
- this.openAppInStore = openAppInStore;
- isAppInstalledMethod = isAppInstalled;
- this.perGameIntervalMinutes = perGameIntervalMinutes;
- }
-
- public void GetMoreGamesList(bool includeAlreadyInstalledGames, Action<SparkSocGame[]> callback, bool downloadIcons = true)
- {
- if (callback != null)
- {
- GetSparkSocGames(Referer.MoreGames, delegate (List<SparkSocGame> games)
- {
- if (downloadIcons)
- {
- LoadIcons(games, delegate
- {
- if (!includeAlreadyInstalledGames)
- {
- RemoveInstalled(games);
- }
- Sort(games);
- callback(games.ToArray());
- });
- }
- else
- {
- callback(games.ToArray());
- }
- });
- }
- }
-
- public void DownloadGameIcon(SparkSocGame item, Action<SparkSocGame> callback)
- {
- StartCoroutine(DoDownloadIconRoutine(item, callback));
- }
-
- public void GetOtherGamesList(bool includeAlreadyInstalledGames, Action<SparkSocGame[]> callback, bool downloadBanners = true)
- {
- if (callback != null)
- {
- GetSparkSocGames(Referer.OtherGames, delegate (List<SparkSocGame> games)
- {
- if (downloadBanners)
- {
- LoadBanners(games, delegate
- {
- if (!includeAlreadyInstalledGames)
- {
- RemoveInstalled(games);
- }
- Sort(games);
- callback(games.ToArray());
- });
- }
- else
- {
- callback(games.ToArray());
- }
- });
- }
- }
-
- public void GetPromoItem(Action<SparkSocGame> callback)
- {
- GetSparkSocGames(Referer.Promo, delegate (List<SparkSocGame> games)
- {
- RemoveInstalled(games);
- RemoveRecentlyWatched(games);
- List<SparkSocGame> list = new List<SparkSocGame>();
- for (int i = 0; i < games.Count; i++)
- {
- SparkSocGame sparkSocGame = games[i];
- for (int j = 0; j <= sparkSocGame.Factor; j++)
- {
- list.Add(sparkSocGame);
- }
- }
- if (list.Count > 0)
- {
- SparkSocGame item = list[UnityEngine.Random.Range(0, list.Count)];
- StartCoroutine(DoDownloadLargeImageRoutine(item, delegate (SparkSocGame game)
- {
- if (game.PromoImage == null)
- {
- callback(null);
- }
- else
- {
- callback(game);
- }
- }));
- }
- else
- {
- callback(null);
- }
- });
- }
-
- public void DidShowPromo(SparkSocGame game)
- {
- lastGamePopup[game.AppId] = DateTime.UtcNow.ToFileTimeUtc();
- Serialize();
- }
-
- protected void Serialize()
- {
- foreach (KeyValuePair<string, long> item in lastGamePopup)
- {
- PlayerPrefs.SetString("SparkSocGames.lastGamePopup." + item.Key, item.Value.ToString());
- }
- PlayerPrefs.Save();
- }
-
- private long LastShown(string appId)
- {
- if (lastGamePopup.ContainsKey(appId))
- {
- return lastGamePopup[appId];
- }
- string @string = PlayerPrefs.GetString("SparkSocGames.lastGamePopup." + appId, "0");
- long result = 0L;
- if (!long.TryParse(@string, out result))
- {
- UnityEngine.Debug.LogWarningFormat("[SparkSocGames] Unable to parse '{0}' as long.", @string);
- }
- lastGamePopup[appId] = result;
- return result;
- }
-
- private void GetSparkSocGames(Referer referer, Action<List<SparkSocGame>> callback)
- {
- if (callback != null)
- {
- string url = RefererToUrl(referer);
- DownloadJSON(url, delegate (bool hasError, string result)
- {
- if (hasError)
- {
- callback(new List<SparkSocGame>());
- }
- else
- {
- List<SparkSocGame> list = ParseJSON(result);
- InitialiseItems(referer, list);
- callback(list);
- }
- });
- }
- }
-
- private void DownloadJSON(string url, Action<bool, string> callback)
- {
- StartCoroutine(DoDownloadJSONRoutine(url, callback));
- }
-
- private IEnumerator DoDownloadJSONRoutine(string url, Action<bool, string> callback)
- {
- CachedWWW download = new CachedWWW(url);
- yield return download;
- if (download.Error == null)
- {
- callback(false, download.Text);
- yield break;
- }
- UnityEngine.Debug.LogError("Failed downloading more games list from " + url + Environment.NewLine + " Error: " + download.Error);
- callback(true, download.Error);
- }
-
- private List<SparkSocGame> ParseJSON(string json)
- {
- List<SparkSocGame> list = new List<SparkSocGame>();
- SparkSocGame[] array = null;
- try
- {
- array = JsonHelper.getJsonArray<SparkSocGame>(json);
- }
- catch (Exception ex)
- {
- UnityEngine.Debug.LogWarning("Failed parsing gameslist JSON: " + Environment.NewLine + json + Environment.NewLine + ex.ToString());
- }
- if (array != null)
- {
- list.AddRange(array);
- }
- return list;
- }
-
- private void InitialiseItems(Referer referer, List<SparkSocGame> items)
- {
- for (int num = items.Count - 1; num >= 0; num--)
- {
- SparkSocGame sparkSocGame = items[num];
- if (isAppInstalledMethod != null)
- {
- string text = RefererToCampaign(referer);
- string refererLink = "&referrer=utm_source%3D" + applicationId + "%26utm_medium%3D" + text + "%26utm_campaign%3D" + text;
- sparkSocGame.Initialise(refererLink, IsGameInstalled(sparkSocGame), openAppInStore);
- }
- if (sparkSocGame.AppId == applicationId)
- {
- items.Remove(sparkSocGame);
- }
- }
- }
-
- private void Sort(List<SparkSocGame> items)
- {
- items.Sort((SparkSocGame a, SparkSocGame b) => (a.IsInstalled != b.IsInstalled) ? a.IsInstalled.CompareTo(b.IsInstalled) : b.Factor.CompareTo(a.Factor));
- }
-
- private void RemoveInstalled(List<SparkSocGame> items)
- {
- for (int num = items.Count - 1; num >= 0; num--)
- {
- if (items[num].IsInstalled)
- {
- items.Remove(items[num]);
- }
- }
- }
-
- private void RemoveRecentlyWatched(List<SparkSocGame> items)
- {
- for (int num = items.Count - 1; num >= 0; num--)
- {
- long fileTime = LastShown(items[num].AppId);
- if ((DateTime.UtcNow - DateTime.FromFileTimeUtc(fileTime)).TotalMinutes < (double)perGameIntervalMinutes)
- {
- items.Remove(items[num]);
- }
- }
- }
-
- private void LoadIcons(List<SparkSocGame> items, Action callback)
- {
- StartCoroutine(DoLoadIcons(items, callback));
- }
-
- private IEnumerator DoLoadIcons(List<SparkSocGame> items, Action callback)
- {
- List<SparkSocGame> moreGamesItems = new List<SparkSocGame>(items);
- for (int i = 0; i < items.Count; i++)
- {
- SparkSocGame item = items[i];
- StartCoroutine(DoDownloadIconRoutine(item, delegate (SparkSocGame processedItem)
- {
- moreGamesItems.Remove(processedItem);
- }));
- }
- while (moreGamesItems.Count != 0)
- {
- yield return null;
- }
- callback?.Invoke();
- }
-
- private void LoadBanners(List<SparkSocGame> items, Action callback)
- {
- StartCoroutine(DoLoadBanners(items, callback));
- }
-
- private IEnumerator DoLoadBanners(List<SparkSocGame> items, Action callback)
- {
- List<SparkSocGame> otherGamesItems = new List<SparkSocGame>(items);
- int i = 0;
- for (int count = otherGamesItems.Count; i < count; i++)
- {
- SparkSocGame item = items[i];
- StartCoroutine(DoDownloadBannerRoutine(item, delegate (SparkSocGame processedItem)
- {
- otherGamesItems.Remove(processedItem);
- }));
- }
- while (otherGamesItems.Count != 0)
- {
- yield return null;
- }
- callback?.Invoke();
- }
-
- private IEnumerator DoDownloadIconRoutine(SparkSocGame item, Action<SparkSocGame> callback)
- {
- CachedWWW appIconWWW = new CachedWWW(item.AppIconUrl);
- yield return appIconWWW;
- if (appIconWWW.Error == null)
- {
- item.SetAppIcon(appIconWWW.TextureNonReadable);
- }
- else
- {
- UnityEngine.Debug.LogError("Error downloading from " + appIconWWW.Url + " " + appIconWWW.Error + " " + appIconWWW.Text);
- }
- callback?.Invoke(item);
- }
-
- private IEnumerator DoDownloadLargeImageRoutine(SparkSocGame item, Action<SparkSocGame> callback)
- {
- CachedWWW www = new CachedWWW(item.LargeImageUrl, 7);
- yield return www;
- if (www.Error != null)
- {
- UnityEngine.Debug.LogError("Error downloading from " + www.Url + " " + www.Error + " " + www.Text);
- }
- if (callback != null)
- {
- item.SetPromoImage((www.Error != null) ? null : www.TextureNonReadable);
- callback(item);
- }
- }
-
- private IEnumerator DoDownloadBannerRoutine(SparkSocGame item, Action<SparkSocGame> callback)
- {
- CachedWWW www = new CachedWWW(item.BannerUrl, 7);
- yield return www;
- if (www.Error == null)
- {
- item.SetBannerImage(www.TextureNonReadable);
- }
- else
- {
- UnityEngine.Debug.LogErrorFormat("Error downloading from {0}, {1} {2}", www.Url, www.Error, www.Text);
- }
- callback?.Invoke(item);
- }
-
- private string RefererToCampaign(Referer r)
- {
- string result = string.Empty;
- switch (r)
- {
- case Referer.Promo:
- result = "sparksoc_promo";
- break;
- case Referer.MoreGames:
- result = "sparksoc_moregames";
- break;
- case Referer.OtherGames:
- result = "sparksoc_othergames";
- break;
- }
- return result;
- }
-
- private string RefererToUrl(Referer r)
- {
- string empty = string.Empty;
- switch (r)
- {
- default:
- return MoreGamesURL;
- case Referer.Promo:
- return PromoURL;
- case Referer.OtherGames:
- return OtherGamesURL;
- }
- }
-
- private bool IsGameInstalled(SparkSocGame game)
- {
- return isAppInstalledMethod(game.AppId);
- }
- }
- }
|