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

440 行
15 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace SUISS.Promo
  6. {
  7. public class SparkSocGames : MonoBehaviour
  8. {
  9. public enum Referer
  10. {
  11. Promo,
  12. MoreGames,
  13. OtherGames
  14. }
  15. public delegate void OpenAppInStoreDelegate(string applicationId, string link, string referrer);
  16. public delegate bool IsAppInstalledDelegate(string applicationId);
  17. private const int JSON_CACHE_EXPERATION_DURATION_IN_DAYS = 1;
  18. private const int TEXTURE_CACHE_EXPERATION_DURATION_IN_DAYS = 7;
  19. private const string REFERER_MORE_GAMES = "sparksoc_moregames";
  20. private const string REFERER_INGAME_PROMO = "sparksoc_promo";
  21. private const string RefererOtherGames = "sparksoc_othergames";
  22. private const string LastGamePopupPlayerPrefsKey = "SparkSocGames.lastGamePopup.";
  23. private OpenAppInStoreDelegate openAppInStore;
  24. private IsAppInstalledDelegate isAppInstalledMethod;
  25. private string applicationId;
  26. private Dictionary<string, long> lastGamePopup = new Dictionary<string, long>();
  27. private int perGameIntervalMinutes = 720;
  28. private string MoreGamesURL
  29. {
  30. get
  31. {
  32. string empty = string.Empty;
  33. empty = "https://crosspromo.sparklingsociety.net/googleplay/more_games.php?app=";
  34. return empty + applicationId;
  35. }
  36. }
  37. private string PromoURL
  38. {
  39. get
  40. {
  41. string empty = string.Empty;
  42. empty = "https://crosspromo.sparklingsociety.net/googleplay/promo.php?app=";
  43. return empty + applicationId;
  44. }
  45. }
  46. private string OtherGamesURL
  47. {
  48. get
  49. {
  50. string empty = string.Empty;
  51. empty = "https://crosspromo.sparklingsociety.net/googleplay/other_games.php?app=";
  52. return empty + applicationId;
  53. }
  54. }
  55. public void Initialise(string applicationId, OpenAppInStoreDelegate openAppInStore, IsAppInstalledDelegate isAppInstalled, int perGameIntervalMinutes = 720)
  56. {
  57. this.applicationId = applicationId;
  58. this.openAppInStore = openAppInStore;
  59. isAppInstalledMethod = isAppInstalled;
  60. this.perGameIntervalMinutes = perGameIntervalMinutes;
  61. }
  62. public void GetMoreGamesList(bool includeAlreadyInstalledGames, Action<SparkSocGame[]> callback, bool downloadIcons = true)
  63. {
  64. if (callback != null)
  65. {
  66. GetSparkSocGames(Referer.MoreGames, delegate (List<SparkSocGame> games)
  67. {
  68. if (downloadIcons)
  69. {
  70. LoadIcons(games, delegate
  71. {
  72. if (!includeAlreadyInstalledGames)
  73. {
  74. RemoveInstalled(games);
  75. }
  76. Sort(games);
  77. callback(games.ToArray());
  78. });
  79. }
  80. else
  81. {
  82. callback(games.ToArray());
  83. }
  84. });
  85. }
  86. }
  87. public void DownloadGameIcon(SparkSocGame item, Action<SparkSocGame> callback)
  88. {
  89. StartCoroutine(DoDownloadIconRoutine(item, callback));
  90. }
  91. public void GetOtherGamesList(bool includeAlreadyInstalledGames, Action<SparkSocGame[]> callback, bool downloadBanners = true)
  92. {
  93. if (callback != null)
  94. {
  95. GetSparkSocGames(Referer.OtherGames, delegate (List<SparkSocGame> games)
  96. {
  97. if (downloadBanners)
  98. {
  99. LoadBanners(games, delegate
  100. {
  101. if (!includeAlreadyInstalledGames)
  102. {
  103. RemoveInstalled(games);
  104. }
  105. Sort(games);
  106. callback(games.ToArray());
  107. });
  108. }
  109. else
  110. {
  111. callback(games.ToArray());
  112. }
  113. });
  114. }
  115. }
  116. public void GetPromoItem(Action<SparkSocGame> callback)
  117. {
  118. GetSparkSocGames(Referer.Promo, delegate (List<SparkSocGame> games)
  119. {
  120. RemoveInstalled(games);
  121. RemoveRecentlyWatched(games);
  122. List<SparkSocGame> list = new List<SparkSocGame>();
  123. for (int i = 0; i < games.Count; i++)
  124. {
  125. SparkSocGame sparkSocGame = games[i];
  126. for (int j = 0; j <= sparkSocGame.Factor; j++)
  127. {
  128. list.Add(sparkSocGame);
  129. }
  130. }
  131. if (list.Count > 0)
  132. {
  133. SparkSocGame item = list[UnityEngine.Random.Range(0, list.Count)];
  134. StartCoroutine(DoDownloadLargeImageRoutine(item, delegate (SparkSocGame game)
  135. {
  136. if (game.PromoImage == null)
  137. {
  138. callback(null);
  139. }
  140. else
  141. {
  142. callback(game);
  143. }
  144. }));
  145. }
  146. else
  147. {
  148. callback(null);
  149. }
  150. });
  151. }
  152. public void DidShowPromo(SparkSocGame game)
  153. {
  154. lastGamePopup[game.AppId] = DateTime.UtcNow.ToFileTimeUtc();
  155. Serialize();
  156. }
  157. protected void Serialize()
  158. {
  159. foreach (KeyValuePair<string, long> item in lastGamePopup)
  160. {
  161. PlayerPrefs.SetString("SparkSocGames.lastGamePopup." + item.Key, item.Value.ToString());
  162. }
  163. PlayerPrefs.Save();
  164. }
  165. private long LastShown(string appId)
  166. {
  167. if (lastGamePopup.ContainsKey(appId))
  168. {
  169. return lastGamePopup[appId];
  170. }
  171. string @string = PlayerPrefs.GetString("SparkSocGames.lastGamePopup." + appId, "0");
  172. long result = 0L;
  173. if (!long.TryParse(@string, out result))
  174. {
  175. UnityEngine.Debug.LogWarningFormat("[SparkSocGames] Unable to parse '{0}' as long.", @string);
  176. }
  177. lastGamePopup[appId] = result;
  178. return result;
  179. }
  180. private void GetSparkSocGames(Referer referer, Action<List<SparkSocGame>> callback)
  181. {
  182. if (callback != null)
  183. {
  184. string url = RefererToUrl(referer);
  185. DownloadJSON(url, delegate (bool hasError, string result)
  186. {
  187. if (hasError)
  188. {
  189. callback(new List<SparkSocGame>());
  190. }
  191. else
  192. {
  193. List<SparkSocGame> list = ParseJSON(result);
  194. InitialiseItems(referer, list);
  195. callback(list);
  196. }
  197. });
  198. }
  199. }
  200. private void DownloadJSON(string url, Action<bool, string> callback)
  201. {
  202. StartCoroutine(DoDownloadJSONRoutine(url, callback));
  203. }
  204. private IEnumerator DoDownloadJSONRoutine(string url, Action<bool, string> callback)
  205. {
  206. CachedWWW download = new CachedWWW(url);
  207. yield return download;
  208. if (download.Error == null)
  209. {
  210. callback(false, download.Text);
  211. yield break;
  212. }
  213. UnityEngine.Debug.LogError("Failed downloading more games list from " + url + Environment.NewLine + " Error: " + download.Error);
  214. callback(true, download.Error);
  215. }
  216. private List<SparkSocGame> ParseJSON(string json)
  217. {
  218. List<SparkSocGame> list = new List<SparkSocGame>();
  219. SparkSocGame[] array = null;
  220. try
  221. {
  222. array = JsonHelper.getJsonArray<SparkSocGame>(json);
  223. }
  224. catch (Exception ex)
  225. {
  226. UnityEngine.Debug.LogWarning("Failed parsing gameslist JSON: " + Environment.NewLine + json + Environment.NewLine + ex.ToString());
  227. }
  228. if (array != null)
  229. {
  230. list.AddRange(array);
  231. }
  232. return list;
  233. }
  234. private void InitialiseItems(Referer referer, List<SparkSocGame> items)
  235. {
  236. for (int num = items.Count - 1; num >= 0; num--)
  237. {
  238. SparkSocGame sparkSocGame = items[num];
  239. if (isAppInstalledMethod != null)
  240. {
  241. string text = RefererToCampaign(referer);
  242. string refererLink = "&referrer=utm_source%3D" + applicationId + "%26utm_medium%3D" + text + "%26utm_campaign%3D" + text;
  243. sparkSocGame.Initialise(refererLink, IsGameInstalled(sparkSocGame), openAppInStore);
  244. }
  245. if (sparkSocGame.AppId == applicationId)
  246. {
  247. items.Remove(sparkSocGame);
  248. }
  249. }
  250. }
  251. private void Sort(List<SparkSocGame> items)
  252. {
  253. items.Sort((SparkSocGame a, SparkSocGame b) => (a.IsInstalled != b.IsInstalled) ? a.IsInstalled.CompareTo(b.IsInstalled) : b.Factor.CompareTo(a.Factor));
  254. }
  255. private void RemoveInstalled(List<SparkSocGame> items)
  256. {
  257. for (int num = items.Count - 1; num >= 0; num--)
  258. {
  259. if (items[num].IsInstalled)
  260. {
  261. items.Remove(items[num]);
  262. }
  263. }
  264. }
  265. private void RemoveRecentlyWatched(List<SparkSocGame> items)
  266. {
  267. for (int num = items.Count - 1; num >= 0; num--)
  268. {
  269. long fileTime = LastShown(items[num].AppId);
  270. if ((DateTime.UtcNow - DateTime.FromFileTimeUtc(fileTime)).TotalMinutes < (double)perGameIntervalMinutes)
  271. {
  272. items.Remove(items[num]);
  273. }
  274. }
  275. }
  276. private void LoadIcons(List<SparkSocGame> items, Action callback)
  277. {
  278. StartCoroutine(DoLoadIcons(items, callback));
  279. }
  280. private IEnumerator DoLoadIcons(List<SparkSocGame> items, Action callback)
  281. {
  282. List<SparkSocGame> moreGamesItems = new List<SparkSocGame>(items);
  283. for (int i = 0; i < items.Count; i++)
  284. {
  285. SparkSocGame item = items[i];
  286. StartCoroutine(DoDownloadIconRoutine(item, delegate (SparkSocGame processedItem)
  287. {
  288. moreGamesItems.Remove(processedItem);
  289. }));
  290. }
  291. while (moreGamesItems.Count != 0)
  292. {
  293. yield return null;
  294. }
  295. callback?.Invoke();
  296. }
  297. private void LoadBanners(List<SparkSocGame> items, Action callback)
  298. {
  299. StartCoroutine(DoLoadBanners(items, callback));
  300. }
  301. private IEnumerator DoLoadBanners(List<SparkSocGame> items, Action callback)
  302. {
  303. List<SparkSocGame> otherGamesItems = new List<SparkSocGame>(items);
  304. int i = 0;
  305. for (int count = otherGamesItems.Count; i < count; i++)
  306. {
  307. SparkSocGame item = items[i];
  308. StartCoroutine(DoDownloadBannerRoutine(item, delegate (SparkSocGame processedItem)
  309. {
  310. otherGamesItems.Remove(processedItem);
  311. }));
  312. }
  313. while (otherGamesItems.Count != 0)
  314. {
  315. yield return null;
  316. }
  317. callback?.Invoke();
  318. }
  319. private IEnumerator DoDownloadIconRoutine(SparkSocGame item, Action<SparkSocGame> callback)
  320. {
  321. CachedWWW appIconWWW = new CachedWWW(item.AppIconUrl);
  322. yield return appIconWWW;
  323. if (appIconWWW.Error == null)
  324. {
  325. item.SetAppIcon(appIconWWW.TextureNonReadable);
  326. }
  327. else
  328. {
  329. UnityEngine.Debug.LogError("Error downloading from " + appIconWWW.Url + " " + appIconWWW.Error + " " + appIconWWW.Text);
  330. }
  331. callback?.Invoke(item);
  332. }
  333. private IEnumerator DoDownloadLargeImageRoutine(SparkSocGame item, Action<SparkSocGame> callback)
  334. {
  335. CachedWWW www = new CachedWWW(item.LargeImageUrl, 7);
  336. yield return www;
  337. if (www.Error != null)
  338. {
  339. UnityEngine.Debug.LogError("Error downloading from " + www.Url + " " + www.Error + " " + www.Text);
  340. }
  341. if (callback != null)
  342. {
  343. item.SetPromoImage((www.Error != null) ? null : www.TextureNonReadable);
  344. callback(item);
  345. }
  346. }
  347. private IEnumerator DoDownloadBannerRoutine(SparkSocGame item, Action<SparkSocGame> callback)
  348. {
  349. CachedWWW www = new CachedWWW(item.BannerUrl, 7);
  350. yield return www;
  351. if (www.Error == null)
  352. {
  353. item.SetBannerImage(www.TextureNonReadable);
  354. }
  355. else
  356. {
  357. UnityEngine.Debug.LogErrorFormat("Error downloading from {0}, {1} {2}", www.Url, www.Error, www.Text);
  358. }
  359. callback?.Invoke(item);
  360. }
  361. private string RefererToCampaign(Referer r)
  362. {
  363. string result = string.Empty;
  364. switch (r)
  365. {
  366. case Referer.Promo:
  367. result = "sparksoc_promo";
  368. break;
  369. case Referer.MoreGames:
  370. result = "sparksoc_moregames";
  371. break;
  372. case Referer.OtherGames:
  373. result = "sparksoc_othergames";
  374. break;
  375. }
  376. return result;
  377. }
  378. private string RefererToUrl(Referer r)
  379. {
  380. string empty = string.Empty;
  381. switch (r)
  382. {
  383. default:
  384. return MoreGamesURL;
  385. case Referer.Promo:
  386. return PromoURL;
  387. case Referer.OtherGames:
  388. return OtherGamesURL;
  389. }
  390. }
  391. private bool IsGameInstalled(SparkSocGame game)
  392. {
  393. return isAppInstalledMethod(game.AppId);
  394. }
  395. }
  396. }