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 lastGamePopup = new Dictionary(); 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 callback, bool downloadIcons = true) { if (callback != null) { GetSparkSocGames(Referer.MoreGames, delegate (List 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 callback) { StartCoroutine(DoDownloadIconRoutine(item, callback)); } public void GetOtherGamesList(bool includeAlreadyInstalledGames, Action callback, bool downloadBanners = true) { if (callback != null) { GetSparkSocGames(Referer.OtherGames, delegate (List games) { if (downloadBanners) { LoadBanners(games, delegate { if (!includeAlreadyInstalledGames) { RemoveInstalled(games); } Sort(games); callback(games.ToArray()); }); } else { callback(games.ToArray()); } }); } } public void GetPromoItem(Action callback) { GetSparkSocGames(Referer.Promo, delegate (List games) { RemoveInstalled(games); RemoveRecentlyWatched(games); List list = new List(); 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 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> callback) { if (callback != null) { string url = RefererToUrl(referer); DownloadJSON(url, delegate (bool hasError, string result) { if (hasError) { callback(new List()); } else { List list = ParseJSON(result); InitialiseItems(referer, list); callback(list); } }); } } private void DownloadJSON(string url, Action callback) { StartCoroutine(DoDownloadJSONRoutine(url, callback)); } private IEnumerator DoDownloadJSONRoutine(string url, Action 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 ParseJSON(string json) { List list = new List(); SparkSocGame[] array = null; try { array = JsonHelper.getJsonArray(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 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 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 items) { for (int num = items.Count - 1; num >= 0; num--) { if (items[num].IsInstalled) { items.Remove(items[num]); } } } private void RemoveRecentlyWatched(List 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 items, Action callback) { StartCoroutine(DoLoadIcons(items, callback)); } private IEnumerator DoLoadIcons(List items, Action callback) { List moreGamesItems = new List(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 items, Action callback) { StartCoroutine(DoLoadBanners(items, callback)); } private IEnumerator DoLoadBanners(List items, Action callback) { List otherGamesItems = new List(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 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 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 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); } } }